@Override public Object generate() { String className; if (dataType == null) { className = random.choose(AttributeType.DataType.SUPPORTED_TYPES.keySet()); } else { className = dataType.getName(); } Class<?> clazz; try { clazz = Class.forName(className); } catch (ClassNotFoundException e) { throw new RuntimeException("Unrecognised class " + className); } if(clazz.equals(LocalDateTime.class)){ return LocalDateTime.ofInstant(Instant.ofEpochMilli(random.nextLong()), ZoneId.systemDefault()); } return gen(clazz); }
/** * This is to handle casting longs and doubles when the type allows for the data type to be a number * @param value The value of the resource * @return The value casted to the correct type */ private static Object castValue(AttributeType.DataType dataType, Object value){ try { if (dataType.equals(AttributeType.DataType.DOUBLE)) { return ((Number) value).doubleValue(); } else if (dataType.equals(AttributeType.DataType.LONG)) { if (value instanceof Double) { throw new ClassCastException(); } return ((Number) value).longValue(); } else if (dataType.equals(AttributeType.DataType.DATE) && (value instanceof Long)){ return value; } else { return dataType.getPersistenceValue(value); } } catch (ClassCastException e) { throw GraknTxOperationException.invalidAttributeValue(value, dataType); } }
@Override public final <S, E> GraphTraversal<S, E> applyPredicate(GraphTraversal<S, E> traversal) { var.ifPresent(theVar -> { // Compare to another variable String thisVar = UUID.randomUUID().toString(); Var otherVar = theVar.var(); String otherValue = UUID.randomUUID().toString(); Traversal[] traversals = Stream.of(VALUE_PROPERTIES) .map(prop -> __.values(prop).as(otherValue).select(thisVar).values(prop).where(gremlinPredicate(otherValue))) .toArray(Traversal[]::new); traversal.as(thisVar).select(otherVar.name()).or(traversals).select(thisVar); }); persistedValue().ifPresent(theValue -> { // Compare to a given value DataType<?> dataType = DataType.SUPPORTED_TYPES.get(value().get().getClass().getTypeName()); Schema.VertexProperty property = dataType.getVertexProperty(); traversal.has(property.name(), gremlinPredicate(theValue)); }); return traversal; }
@Override public <V> Collection<Attribute<V>> getAttributesByValue(V value) { if (value == null) return Collections.emptySet(); //Make sure you trying to retrieve supported data type if (!AttributeType.DataType.SUPPORTED_TYPES.containsKey(value.getClass().getName())) { throw GraknTxOperationException.unsupportedDataType(value); } HashSet<Attribute<V>> attributes = new HashSet<>(); AttributeType.DataType dataType = AttributeType.DataType.SUPPORTED_TYPES.get(value.getClass().getTypeName()); //noinspection unchecked getConcepts(dataType.getVertexProperty(), dataType.getPersistenceValue(value)).forEach(concept -> { if (concept != null && concept.isAttribute()) { //noinspection unchecked attributes.add(concept.asAttribute()); } }); return attributes; }
public static GraqlQueryException attributeMustBeANumber(AttributeType.DataType dataType, Label attributeType) { return new GraqlQueryException(attributeType + " must have data type of `long` or `double`, but was " + dataType.getName()); }
@Override public String name() { return "[datatype:" + dataType().getName() + "]"; }
GraknMapReduce(Set<LabelId> selectedTypes, AttributeType.DataType resourceDataType) { this(selectedTypes); persistentProperties.put(RESOURCE_DATA_TYPE_KEY, resourceDataType.getName()); }
@Override public String toString(){ return getName(); }
/** * * @param value The value to store on the resource */ private void setValue(Object value) { Schema.VertexProperty property = dataType().getVertexProperty(); //noinspection unchecked vertex().propertyImmutable(property, value, vertex().property(property)); }
final Optional<Object> persistedValue() { return value().map(value -> { // Convert values to how they are stored in the graph DataType dataType = DataType.SUPPORTED_TYPES.get(value.getClass().getName()); if (dataType == null) { throw GraqlQueryException.invalidValueClass(value); } // We can trust the `SUPPORTED_TYPES` map to store things with the right type //noinspection unchecked return dataType.getPersistenceValue(value); }); }
final boolean usingLong() { return persistentProperties.get(RESOURCE_DATA_TYPE_KEY).equals(AttributeType.DataType.LONG.getName()); } }
@Override public GraphTraversal<Vertex, ? extends Element> applyTraversalInner( GraphTraversal<Vertex, ? extends Element> traversal, EmbeddedGraknTx<?> tx, Collection<Var> vars) { return traversal.has(DATA_TYPE.name(), dataType().getName()); }
/** * Thrown when creating a {@link Attribute} whose value {@link Object} does not match it's {@link AttributeType}'s * {@link ai.grakn.concept.AttributeType.DataType} */ public static GraknTxOperationException invalidAttributeValue(Object object, AttributeType.DataType dataType){ return create(ErrorMessage.INVALID_DATATYPE.getMessage(object, dataType.getName())); }
/** * * @return The value casted to the correct type */ @Override public D value(){ return dataType().getValue(vertex().property(dataType().getVertexProperty())); }