@Override protected final Attribute<D> asInstance(Concept concept) { return concept.asAttribute(); } }
@Override final Attribute<D> asCurrentBaseType(Concept other) { return other.asAttribute(); } }
@Override final Attribute<D> asSelf(Concept concept) { return concept.asAttribute(); } }
@Override protected final Attribute<D> asInstance(Concept concept) { return concept.asAttribute(); } }
private Number getValue(ConceptMap result) { Object value = result.get(varName).asAttribute().value(); if (value instanceof Number) return (Number) value; else throw new RuntimeException("Invalid attempt to compare non-Numbers in Min Aggregate function"); }
@Override public List<Value> apply(Stream<? extends ConceptMap> stream) { // initial value is set to null so that we can return null if there is no Answers to consume Number number = stream.map(result -> (Number) result.get(varName).asAttribute().value()).reduce(null, this::add); if (number == null) return Collections.emptyList(); else return Collections.singletonList(new Value(number)); }
private Number getValue(ConceptMap result) { Object value = result.get(varName).asAttribute().value(); if (value instanceof Number) return (Number) value; else throw new RuntimeException("Invalid attempt to compare non-Numbers in Max Aggregate function"); }
@Override public Object transform(MatchableConcept item) { return item.get().asAttribute().value(); } };
@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; }
@Override public List<Value> apply(Stream<? extends ConceptMap> stream) { List<Number> results = stream .map(result -> ((Number) result.get(varName).asAttribute().value())) .sorted() .collect(toList()); int size = results.size(); int halveFloor = Math.floorDiv(size - 1, 2); int halveCeiling = (int) Math.ceil((size - 1) / 2.0); if (size == 0) { return Collections.emptyList(); } else if (size % 2 == 1) { // Take exact middle result return Collections.singletonList(new Value(results.get(halveFloor))); } else { // Take average of middle results Number result = (results.get(halveFloor).doubleValue() + results.get(halveCeiling).doubleValue()) / 2; return Collections.singletonList(new Value(result)); } }
@Override public List<Value> apply(Stream<? extends ConceptMap> stream) { Stream<Double> numStream = stream.map(result -> result.get(varName).<Number>asAttribute().value().doubleValue()); Iterable<Double> data = numStream::iterator; // Online algorithm to calculate unbiased sample standard deviation // https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Online_algorithm long n = 0; double mean = 0d; double M2 = 0d; for (double x : data) { n += 1; double delta = x - mean; mean += delta / (double) n; double delta2 = x - mean; M2 += delta*delta2; } if (n < 2) { return Collections.emptyList(); } else { return Collections.singletonList(new Value(sqrt(M2 / (double) (n - 1)))); } }
@SuppressWarnings("unchecked") private Comparable<? super Comparable> getOrderValue(ConceptMap result) { return (Comparable<? super Comparable>) result.get(var()).asAttribute().value(); }
/** * * @param concept The concept to no longer track */ @SuppressWarnings("SuspiciousMethodCalls") public void remove(Concept concept){ modifiedThings.remove(concept); modifiedRoles.remove(concept); modifiedRelationshipTypes.remove(concept); modifiedRules.remove(concept); if(concept.isAttribute()) { newAttributes.removeAll(AttributeImpl.from(concept.asAttribute()).getIndex()); } if(concept.isRelationship()){ newRelationships.remove(concept.asRelationship()); } conceptCache.remove(concept.id()); if (concept.isSchemaConcept()) { Label label = concept.asSchemaConcept().label(); schemaConceptCache.remove(label); labelCache.remove(label); } }
@Override public String toString() { if (concept.isAttribute()) { return "hasValue(" + valueToString(concept.asAttribute().value()) + ")"; } else if (concept.isThing()) { Thing thing = concept.asThing(); Stream<Attribute<?>> resources = thing.attributes(); Optional<?> value = resources .filter(resource -> NAME_TYPES.contains(resource.type().label())) .map(Attribute::value).findFirst(); return "instance(" + value.map(StringUtil::valueToString).orElse("") + ") isa " + thing.type().label(); } else if (concept.isType()) { return "type(" + concept.asType().label() + ")"; } else if (concept.isRole()) { return "role(" + concept.asRole().label() + ")"; } else if (concept.isRule()) { return "rule(" + concept.asRule().label() + ")"; } else { throw CommonUtil.unreachableStatement("Unrecognised concept " + concept); } }
@Override public Stream<ConceptMap> materialise(){ ConceptMap substitution = getParentQuery().getSubstitution(); AttributeTypeImpl attributeType = AttributeTypeImpl.from(getSchemaConcept().asAttributeType()); Concept owner = substitution.get(getVarName()); Var resourceVariable = getPredicateVariable(); //if the attribute already exists, only attach a new link to the owner, otherwise create a new attribute Attribute attribute; if(this.isSpecific()){ Object value = Iterables.getOnlyElement(getMultiPredicate()).getPredicate().equalsValue().orElse(null); Attribute existingAttribute = attributeType.attribute(value); attribute = existingAttribute == null? attributeType.putAttributeInferred(value) : existingAttribute; } else { attribute = substitution.containsVar(resourceVariable)? substitution.get(resourceVariable).asAttribute() : null; } attachAttribute(owner, attribute); return Stream.of(substitution.merge(new ConceptMapImpl(ImmutableMap.of(resourceVariable, attribute)))); }
@Nullable @Override public final Attribute<D> attribute(D value) { ConceptProto.Method.Req method = ConceptProto.Method.Req.newBuilder() .setAttributeTypeAttributeReq(ConceptProto.AttributeType.Attribute.Req.newBuilder() .setValue(RequestBuilder.Concept.attributeValue(value))).build(); ConceptProto.AttributeType.Attribute.Res response = runMethod(method).getAttributeTypeAttributeRes(); switch (response.getResCase()) { case NULL: return null; case ATTRIBUTE: return RemoteConcept.of(response.getAttribute(), tx()).asAttribute(); default: throw CommonUtil.unreachableStatement("Unexpected response " + response); } }
private void attachAttribute(Concept owner, Attribute attribute){ if (owner.isEntity()){ EntityImpl.from(owner.asEntity()).attributeInferred(attribute); } else if (owner.isRelationship()){ RelationshipImpl.from(owner.asRelationship()).attributeInferred(attribute); } else if (owner.isAttribute()){ AttributeImpl.from(owner.asAttribute()).attributeInferred(attribute); } }
@Override public Collection<PropertyExecutor> insert(Var var) throws GraqlQueryException { PropertyExecutor.Method method = executor -> { Attribute attributeConcept = executor.get(attribute().var()).asAttribute(); Thing thing = executor.get(var).asThing(); ConceptId relationshipId = thing.relhas(attributeConcept).id(); executor.builder(relationship().var()).id(relationshipId); }; PropertyExecutor executor = PropertyExecutor.builder(method) .produces(relationship().var()) .requires(var, attribute().var()) .build(); return ImmutableSet.of(executor); }
static Pattern toPattern(Concept concept) { if (!concept.isThing()){ throw GraqlQueryException.conceptNotAThing(concept); } if (concept.isEntity()) { return new EntityConverter().pattern(concept.asEntity()); } else if (concept.isRelationship()) { return new RelationshipConverter().pattern(concept.asRelationship()); } else if (concept.isAttribute()) { return new AttributeConverter().pattern(concept.asAttribute()); } else { throw CommonUtil.unreachableStatement("Unrecognised concept " + concept); } }
@Override protected Json concept(Concept concept) { Json json = Json.object("id", concept.id().getValue()); if (concept.isSchemaConcept()) { json.set("name", concept.asSchemaConcept().label().getValue()); SchemaConcept superConcept = concept.asSchemaConcept().sup(); if (superConcept != null) json.set("sub", superConcept.label().getValue()); } else if (concept.isThing()) { json.set("isa", concept.asThing().type().label().getValue()); } else { throw CommonUtil.unreachableStatement("Unrecognised concept " + concept); } if (concept.isAttribute()) { json.set("value", concept.asAttribute().value()); } if (concept.isRule()) { Pattern when = concept.asRule().when(); if (when != null) { json.set("when", when.toString()); } Pattern then = concept.asRule().then(); if (then != null) { json.set("then", then.toString()); } } return json; }