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"); }
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)); }
/** * Checks that existing instances match the provided regex. * * @throws GraknTxOperationException when an instance does not match the provided regex * @param regex The regex to check against */ private void checkInstancesMatchRegex(@Nullable String regex){ if(regex != null) { Pattern pattern = Pattern.compile(regex); instances().forEach(resource -> { String value = (String) resource.value(); Matcher matcher = pattern.matcher(value); if(!matcher.matches()){ throw GraknTxOperationException.regexFailure(this, value, regex); } }); } }
@Override public Set<Keyspace> keyspaces() { try (GraknTx graph = systemKeyspaceSession.transaction(GraknTxType.WRITE)) { AttributeType<String> keyspaceName = graph.getSchemaConcept(KEYSPACE_RESOURCE); return graph.<EntityType>getSchemaConcept(KEYSPACE_ENTITY).instances() .flatMap(keyspace -> keyspace.attributes(keyspaceName)) .map(name -> (String) name.value()) .map(Keyspace::of) .collect(Collectors.toSet()); } }
@Override public Object transform(MatchableConcept item) { return item.get().asAttribute().value(); } };
@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(); }
/** * Add the resources of an entity * @param var var representing the entity * @param thing thing containing resource information * @return var pattern with resources */ private static VarPattern hasResources(VarPattern var, Thing thing){ for(Attribute attribute : thing.attributes().collect(Collectors.toSet())){ var = var.has(attribute.type().label(), var().val(attribute.value())); } return var; }
/** * Map a {@link Attribute} to a var IF it is not attached in a has relation to another instance * @param attribute {@link Attribute} to be mapped * @return var patterns representing the given instance */ private static VarPattern map(Attribute attribute){ if(isHasResourceResource(attribute)){ return var(); } VarPattern var = base(attribute); var = var.val(attribute.value()); return var; }
output.append(colorKeyword("val ")).append(StringUtil.valueToString(concept.asAttribute().value())); } else if (concept.isSchemaConcept()) { SchemaConcept ontoConcept = concept.asSchemaConcept(); concept.asThing().attributes(attributeTypes).forEach(resource -> { String resourceType = colorType(resource.type()); String value = StringUtil.valueToString(resource.value()); output.append(colorKeyword(" has ")).append(resourceType).append(" ").append(value); });
@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); } }
public Pattern pattern(Attribute concept) { Var owner = Graql.var().asUserDefined(); VarPattern resourceVar = Graql.var().asUserDefined().val(concept.value()); return owner .has(concept.type().label(),resourceVar) .id(concept.owner().id()); } }
@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; }