Search in sources :

Example 16 with Var

use of ai.grakn.graql.Var in project grakn by graknlabs.

the class TemplateVisitor method visitVarLiteral.

@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);
}
Also used : Var(ai.grakn.graql.Var)

Example 17 with Var

use of ai.grakn.graql.Var in project grakn by graknlabs.

the class AttributeIndexFragmentSet method optimise.

private static void optimise(Collection<EquivalentFragmentSet> fragmentSets, ValueFragmentSet valueSet, IsaFragmentSet isaSet, Label label) {
    // Remove fragment sets we are going to replace
    fragmentSets.remove(valueSet);
    fragmentSets.remove(isaSet);
    // Add a new fragment set to replace the old ones
    Var attribute = valueSet.var();
    Optional<Object> maybeValue = valueSet.predicate().equalsValue();
    assert maybeValue.isPresent() : "This is filtered to only ones with equalValues in equalValueFragments method";
    Object value = maybeValue.get();
    AttributeIndexFragmentSet indexFragmentSet = AttributeIndexFragmentSet.of(attribute, label, value);
    fragmentSets.add(indexFragmentSet);
}
Also used : Var(ai.grakn.graql.Var)

Example 18 with Var

use of ai.grakn.graql.Var in project grakn by graknlabs.

the class GraqlTraversal method getGraphTraversal.

/**
 * Get the {@code GraphTraversal} that this {@code GraqlTraversal} represents.
 */
// Because 'union' accepts an array, we can't use generics
@SuppressWarnings("unchecked")
public GraphTraversal<Vertex, Map<String, Element>> getGraphTraversal(EmbeddedGraknTx<?> tx, Set<Var> vars) {
    if (fragments().size() == 1) {
        // If there are no disjunctions, we don't need to union them and get a performance boost
        ImmutableList<Fragment> list = Iterables.getOnlyElement(fragments());
        return getConjunctionTraversal(tx, tx.getTinkerTraversal().V(), vars, list);
    } else {
        Traversal[] traversals = fragments().stream().map(list -> getConjunctionTraversal(tx, __.V(), vars, list)).toArray(Traversal[]::new);
        // This is a sneaky trick - we want to do a union but tinkerpop requires all traversals to start from
        // somewhere, so we start from a single arbitrary vertex.
        GraphTraversal traversal = tx.getTinkerTraversal().V().limit(1).union(traversals);
        return selectVars(traversal, vars);
    }
}
Also used : Iterables(com.google.common.collect.Iterables) SelectStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.SelectStep) HashSet(java.util.HashSet) ImmutableList(com.google.common.collect.ImmutableList) Map(java.util.Map) ConceptId(ai.grakn.concept.ConceptId) Fragment(ai.grakn.graql.internal.gremlin.fragment.Fragment) CommonUtil.toImmutableSet(ai.grakn.util.CommonUtil.toImmutableSet) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) Collection(java.util.Collection) Set(java.util.Set) org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) GraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) Collectors.joining(java.util.stream.Collectors.joining) Match(ai.grakn.graql.Match) List(java.util.List) Element(org.apache.tinkerpop.gremlin.structure.Element) Traversal(org.apache.tinkerpop.gremlin.process.traversal.Traversal) EmbeddedGraknTx(ai.grakn.kb.internal.EmbeddedGraknTx) Var(ai.grakn.graql.Var) AutoValue(com.google.auto.value.AutoValue) Schema(ai.grakn.util.Schema) GraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal) GraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal) Traversal(org.apache.tinkerpop.gremlin.process.traversal.Traversal) Fragment(ai.grakn.graql.internal.gremlin.fragment.Fragment)

Example 19 with Var

use of ai.grakn.graql.Var in project grakn by graknlabs.

the class Fragments method traverseSchemaConceptFromEdge.

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())));
}
Also used : Var(ai.grakn.graql.Var)

Example 20 with Var

use of ai.grakn.graql.Var in project grakn by graknlabs.

the class ConjunctionQuery method equivalentFragmentSetsOfVar.

private static Stream<EquivalentFragmentSet> equivalentFragmentSetsOfVar(VarPatternAdmin var) {
    Collection<EquivalentFragmentSet> traversals = new HashSet<>();
    Var start = var.var();
    var.getProperties().forEach(property -> {
        VarPropertyInternal propertyInternal = (VarPropertyInternal) property;
        Collection<EquivalentFragmentSet> newTraversals = propertyInternal.match(start);
        traversals.addAll(newTraversals);
    });
    if (!traversals.isEmpty()) {
        return traversals.stream();
    } else {
        // If this variable has no properties, only confirm that it is not internal and nothing else.
        return Stream.of(EquivalentFragmentSets.notInternalFragmentSet(null, start));
    }
}
Also used : VarPropertyInternal(ai.grakn.graql.internal.pattern.property.VarPropertyInternal) Var(ai.grakn.graql.Var) HashSet(java.util.HashSet)

Aggregations

Var (ai.grakn.graql.Var)100 Test (org.junit.Test)29 Answer (ai.grakn.graql.admin.Answer)28 ConceptId (ai.grakn.concept.ConceptId)26 Concept (ai.grakn.concept.Concept)25 Role (ai.grakn.concept.Role)25 VarPatternAdmin (ai.grakn.graql.admin.VarPatternAdmin)24 HashSet (java.util.HashSet)24 Set (java.util.Set)24 VarPattern (ai.grakn.graql.VarPattern)21 IdPredicate (ai.grakn.graql.internal.reasoner.atom.predicate.IdPredicate)21 Sets (com.google.common.collect.Sets)20 Map (java.util.Map)20 Stream (java.util.stream.Stream)20 GraqlQueryException (ai.grakn.exception.GraqlQueryException)19 ImmutableSet (com.google.common.collect.ImmutableSet)19 HashMap (java.util.HashMap)19 QueryAnswer (ai.grakn.graql.internal.query.QueryAnswer)18 List (java.util.List)18 SchemaConcept (ai.grakn.concept.SchemaConcept)17