/** * Create an aggregate that will find the median of a variable's values. * @param var the variable to find the median of */ @CheckReturnValue public static Aggregate<Value> median(String var) { return Aggregates.median(Graql.var(var)); }
/** * Create an aggregate that will sum the values of a variable. */ @CheckReturnValue public static Aggregate<Value> sum(String var) { return Aggregates.sum(Graql.var(var)); }
/** * Create an aggregate that will find the unbiased sample standard deviation of a variable's values. * @param var the variable to find the standard deviation of */ @CheckReturnValue public static Aggregate<Value> std(String var) { return Aggregates.std(Graql.var(var)); }
private Var var() { Var var = Graql.var(); if (defineAllVars) { return var.asUserDefined(); } else { return var; } }
/** * Create an aggregate that will group a query by a variable and apply the given aggregate to each group * @param var the variable to group results by * @param aggregate the aggregate to apply to each group * @param <T> the type the aggregate returns */ @CheckReturnValue public static <T extends Answer> Aggregate<AnswerGroup<T>> group(String var, Aggregate<T> aggregate) { return Aggregates.group(Graql.var(var), aggregate); }
@Override public Concept get(String var) { return get(Graql.var(var)); }
/** * @param label the label of a concept * @return a variable pattern that identifies a concept by label */ @CheckReturnValue public static VarPattern label(String label) { return var().label(label); }
/** * Create an aggregate that will find the minimum of a variable's values. * @param var the variable to find the maximum of */ @CheckReturnValue public static Aggregate<Value> min(String var) { return Aggregates.min(Graql.var(var)); }
/** * Create an aggregate that will find the maximum of a variable's values. * @param var the variable to find the maximum of */ @CheckReturnValue public static Aggregate<Value> max(String var) { return Aggregates.max(Graql.var(var)); }
/** * Create an aggregate that will find the mean of a variable's values. * @param var the variable to find the mean of */ @CheckReturnValue public static Aggregate<Value> mean(String var) { return Aggregates.mean(Graql.var(var)); }
static ConceptMap conceptMap(AnswerProto.ConceptMap res, Grakn.Transaction tx) { ImmutableMap.Builder<Var, ai.grakn.concept.Concept> map = ImmutableMap.builder(); res.getMapMap().forEach((resVar, resConcept) -> { map.put(Graql.var(resVar), RemoteConcept.of(resConcept, tx)); }); return new ConceptMapImpl(map.build()); }
private Answer convert(GrpcGrakn.Answer answer) { ImmutableMap.Builder<Var, Concept> map = ImmutableMap.builder(); answer.getAnswerMap().forEach((grpcVar, grpcConcept) -> { map.put(Graql.var(grpcVar), conceptConverter.convert(grpcConcept)); }); return new QueryAnswer(map.build()); }
@Override public String visitVarLiteral(GraqlTemplateParser.VarLiteralContext ctx){ Var var = var(ctx.getText().substring(1)); if(!scope.hasSeen(var)){ scope.markAsSeen(var); iteration.compute(var, (k, v) -> v == null ? 0 : v + 1); } return ctx.getText() + iteration.get(var); }
@Override public Var visitVarResolved(GraqlTemplateParser.VarResolvedContext ctx) { Object value = null; for(GraqlTemplateParser.UntypedExpressionContext c:ctx.untypedExpression()){ value = aggregateResult(value, this.visitUntypedExpression(c, Object.class)); } if(value == null) throw GraqlSyntaxException.parsingTemplateMissingKey(ctx.getText(), originalContext); String valueAsVar = value.toString().replaceAll("[^a-zA-Z0-9_]", "-"); return var(valueAsVar); }
/** * NB: this is somewhat ambiguous cause from {$x has resource $r;} we can extract: * - $x isa ???; * - $r isa resource; * We pick the latter as the type information is available. * * @return corresponding isa atom */ @Override public IsaAtom toIsaAtom(){ return IsaAtom.create(getPredicateVariable(), Graql.var(), getTypeId(), false, getParentQuery()); }
/** * Helper method to check whether the concept types in the scope have any instances * * @return */ private boolean scopeContainsInstance() { if (scopeTypeLabels().isEmpty()) return false; List<Pattern> checkSubtypes = scopeTypeLabels().stream() .map(type -> Graql.var("x").isa(Graql.label(type))).collect(Collectors.toList()); return tx.graql().infer(false).match(Graql.or(checkSubtypes)).iterator().hasNext(); }
/** * Given an thing, return a var with the type. * @param thing thing to map * @return var patterns representing given thing */ private static VarPattern base(Thing thing){ VarPattern var = var(thing.id().getValue()).isa(Graql.label(thing.type().label())); return hasResources(var, thing); }
@Override public Atomic mapToAtom(VarPatternAdmin var, Set<VarPatternAdmin> vars, ReasonerQuery parent) { //NB: HasResourceType is a special case and it doesn't allow variables as resource types Var varName = var.var().asUserDefined(); Label label = this.resourceType().getTypeLabel().orElse(null); Var predicateVar = var().asUserDefined(); SchemaConcept schemaConcept = parent.tx().getSchemaConcept(label); ConceptId predicateId = schemaConcept != null? schemaConcept.id() : null; //isa part VarPatternAdmin resVar = varName.has(Graql.label(label)).admin(); return HasAtom.create(resVar, predicateVar, predicateId, parent); } }
static <S> GraphTraversal<S, Vertex> traverseSchemaConceptFromEdge( GraphTraversal<S, Edge> traversal, Schema.EdgeProperty edgeProperty) { // Access label ID from edge Var labelId = Graql.var(); traversal.values(edgeProperty.name()).as(labelId.name()); // Look up schema concept using ID return traversal.V().has(LABEL_ID.name(), __.where(P.eq(labelId.name()))); }