private static FieldValue wrapValue(DataType type, Object val) { if (val == null) { return null; } if (val instanceof FieldValue) { return (FieldValue)val; } FieldValue ret = type.createFieldValue(); ret.assign(val); return ret; }
private boolean compatibleTypes(DataType summaryType, DataType existingType) { if (summaryType instanceof TensorDataType && existingType instanceof TensorDataType) { return summaryType.isAssignableFrom(existingType); // TODO: Just do this for all types } return summaryType.equals(existingType); }
public final boolean isAssignableFrom(DataType dataType) { // TODO: Reverse this so that isValueCompatible() uses this instead. return isValueCompatible(dataType.createFieldValue()); }
@Override protected void doVerify(VerificationContext context) { DataType input = context.getValue(); if (input instanceof ArrayDataType || input instanceof WeightedSetDataType) { context.setValue(((CollectionDataType)input).getNestedType()).execute(exp); if (input instanceof ArrayDataType) { context.setValue(DataType.getArray(context.getValue())); } else { WeightedSetDataType wset = (WeightedSetDataType)input; context.setValue(DataType.getWeightedSet(context.getValue(), wset.createIfNonExistent(), wset.removeIfZero())); } } else if (input instanceof StructDataType) { for (Field field : ((StructDataType)input).getFields()) { DataType fieldType = field.getDataType(); DataType valueType = context.setValue(fieldType).execute(exp).getValue(); if (!fieldType.isAssignableFrom(valueType)) { throw new VerificationException(this, "Expected " + fieldType.getName() + " output, got " + valueType.getName() + "."); } } context.setValue(input); } else { throw new VerificationException(this, "Expected Array, Struct or WeightedSet input, got " + input.getName() + "."); } }
private static boolean compatibleTypes(DataType seenType, DataType fieldType) { // legacy tag field type compatibility; probably not needed any more (Oct 2016) if ("tag".equals(seenType.getName())) { return "tag".equals(fieldType.getName()) || "WeightedSet<string>".equals(fieldType.getName()); } if ("tag".equals(fieldType.getName())) { return "tag".equals(seenType.getName()) || "WeightedSet<string>".equals(seenType.getName()); } if (seenType instanceof TensorDataType && fieldType instanceof TensorDataType) { return fieldType.isAssignableFrom(seenType); // TODO: Just do this for all types } return seenType.equals(fieldType); }
private boolean isTypeOrNested(ImmutableSDField field, DataType type) { return field.getDataType().equals(type) || field.getDataType().equals(DataType.getArray(type)) || field.getDataType().equals(DataType.getWeightedSet(type)); }
private static DataType resolveInputType(Collection<? extends StatementExpression> list) { DataType prev = null; for (Expression exp : list) { DataType next = exp.requiredInputType(); if (prev == null) { prev = next; } else if (next != null && !prev.isAssignableFrom(next)) { throw new VerificationException(ScriptExpression.class, "Statements require conflicting input types, " + prev.getName() + " vs " + next.getName() + "."); } } return prev; }
DataType input = context.getValue(); if (input == null) { throw new VerificationException(this, "Expected " + inputType.getName() + " input, got null."); if (input.getPrimitiveType() == UnresolvedDataType.INSTANCE) { throw new VerificationException(this, "Failed to resolve input type."); if (!inputType.isAssignableFrom(input)) { throw new VerificationException(this, "Expected " + inputType.getName() + " input, got " + input.getName() + "."); DataType output = context.getValue(); if (output == null) { throw new VerificationException(this, "Expected " + outputType.getName() + " output, got null."); if (output.getPrimitiveType() == UnresolvedDataType.INSTANCE) { throw new VerificationException(this, "Failed to resolve output type."); if (!outputType.isAssignableFrom(output)) { throw new VerificationException(this, "Expected " + outputType.getName() + " output, got " + output.getName() + ".");
@SuppressWarnings({ "unchecked", "rawtypes" }) private FieldValue convertWset(WeightedSet val) { Map<FieldValue, Integer> next = new LinkedHashMap<FieldValue, Integer>(); DataType nextType = null; for (Iterator<FieldValue> it = val.fieldValueIterator(); it.hasNext();) { FieldValue prevKey = it.next(); Integer prevVal = val.get(prevKey); FieldValue nextKey = convert(prevKey); if (nextKey == null) { continue; } if (nextType == null) { nextType = nextKey.getDataType(); } else if (!nextType.isValueCompatible(nextKey)) { throw new IllegalArgumentException("Expected " + nextType.getName() + ", got " + nextKey.getDataType().getName() + "."); } next.put(nextKey, prevVal); } if (nextType == null) { return null; } WeightedSet ret = DataType.getWeightedSet(nextType, val.getDataType().createIfNonExistent(), val.getDataType().removeIfZero()).createFieldValue(); for (Map.Entry<FieldValue, Integer> entry : next.entrySet()) { ret.put(entry.getKey(), entry.getValue()); } return ret; }
public static FieldValue readAtomic(String field, DataType expectedType) { if (expectedType.equals(DataType.RAW)) { return expectedType.createFieldValue(new Base64().decode(field)); } else if (expectedType.equals(PositionDataType.INSTANCE)) { return PositionDataType.fromString(field); } else if (expectedType instanceof ReferenceDataType) { return readReferenceFieldValue(field, expectedType); } else { return expectedType.createFieldValue(field); } }
@SuppressWarnings("deprecation") static List<Field> flattenField(Field field) { DataType fieldType = field.getDataType(); if (fieldType.getPrimitiveType() != null){ return Collections.singletonList(field); } if (fieldType instanceof ArrayDataType) { boolean header = field.isHeader(); List<Field> ret = new LinkedList<>(); Field innerField = new Field(field.getName(), ((ArrayDataType)fieldType).getNestedType(), header); for (Field flatField : flattenField(innerField)) { ret.add(new Field(flatField.getName(), DataType.getArray(flatField.getDataType()), header)); } return ret; } if (fieldType instanceof StructuredDataType) { List<Field> ret = new LinkedList<>(); String fieldName = field.getName(); for (Field childField : ((StructuredDataType)fieldType).getFields()) { for (Field flatField : flattenField(childField)) { ret.add(new Field(fieldName + "." + flatField.getName(), flatField)); } } return ret; } throw new UnsupportedOperationException(fieldType.getName()); }
@SuppressWarnings({ "deprecation" }) private void validate() { if (attribute || !indexed) { return; } DataType fieldType = getDataType(); DataType primiType = fieldType.getPrimitiveType(); if (DataType.STRING.equals(primiType) || DataType.URI.equals(primiType)) { return; } throw new IllegalStateException("Expected type " + DataType.STRING.getName() + " for indexed field '" + getName() + "', got " + fieldType.getName() + "."); }
@SuppressWarnings({ "unchecked", "rawtypes" }) private FieldValue convertArray(Array val) { List<FieldValue> next = new LinkedList<FieldValue>(); DataType nextType = null; for (Iterator<FieldValue> it = val.fieldValueIterator(); it.hasNext();) { FieldValue prevVal = it.next(); FieldValue nextVal = convert(prevVal); if (nextVal == null) { continue; } if (nextType == null) { nextType = nextVal.getDataType(); } else if (!nextType.isValueCompatible(nextVal)) { throw new IllegalArgumentException("Expected " + nextType.getName() + ", got " + nextVal.getDataType().getName() + "."); } next.add(nextVal); } if (nextType == null) { return null; } Array ret = DataType.getArray(nextType).createFieldValue(); for (FieldValue nextVal : next) { ret.add(nextVal); } return ret; }
} else if (!nextKeyType.isValueCompatible(nextKey)) { throw new IllegalArgumentException("Expected " + nextKeyType.getName() + ", got " + nextKey.getDataType().getName() + "."); } else if (!nextValType.isValueCompatible(nextVal)) { throw new IllegalArgumentException("Expected " + nextValType.getName() + ", got " + nextVal.getDataType().getName() + "."); return null; MapFieldValue ret = DataType.getMap(nextKeyType, nextValType).createFieldValue(); for (Map.Entry<FieldValue, FieldValue> entry : next.entrySet()) { ret.put(entry.getKey(), entry.getValue());
private static DataType createCompatType(DataType origType) { if (origType instanceof ArrayDataType) { return DataType.getArray(createCompatType(((ArrayDataType)origType).getNestedType())); } else if (origType instanceof MapDataType) { MapDataType mapType = (MapDataType)origType; return DataType.getMap(createCompatType(mapType.getKeyType()), createCompatType(mapType.getValueType())); } else if (origType instanceof WeightedSetDataType) { return DataType.getWeightedSet(createCompatType(((WeightedSetDataType)origType).getNestedType())); } else if (origType == PositionDataType.INSTANCE) { return DataType.LONG; } else { return origType; } } }
public DataTypeRepo add(DataType type) { if (typeByName.containsKey(type.getName()) || typeById.containsKey(type.getId())) { throw new IllegalStateException("Data type '" + type.getName() + "', id '" + type.getId() + "' is already registered."); } typeByName.put(type.getName(), type); typeById.put(type.getId(), type); return this; }