Search in sources :

Example 1 with Fragment

use of ai.grakn.graql.internal.gremlin.fragment.Fragment in project grakn by graknlabs.

the class GraqlTraversalPlanner method planFromTraversal.

/**
 * @param atoms list of current atoms of interest
 * @param queryPattern corresponding pattern
 * @return an optimally ordered list of provided atoms
 */
static ImmutableList<Atom> planFromTraversal(List<Atom> atoms, PatternAdmin queryPattern, EmbeddedGraknTx<?> tx) {
    Multimap<VarProperty, Atom> propertyMap = HashMultimap.create();
    atoms.stream().filter(at -> !(at instanceof OntologicalAtom)).forEach(at -> at.getVarProperties().forEach(p -> propertyMap.put(p, at)));
    Set<VarProperty> properties = propertyMap.keySet();
    GraqlTraversal graqlTraversal = GreedyTraversalPlan.createTraversal(queryPattern, tx);
    ImmutableList<Fragment> fragments = graqlTraversal.fragments().iterator().next();
    ImmutableList.Builder<Atom> builder = ImmutableList.builder();
    builder.addAll(atoms.stream().filter(at -> at instanceof OntologicalAtom).iterator());
    builder.addAll(fragments.stream().map(Fragment::varProperty).filter(Objects::nonNull).filter(properties::contains).distinct().flatMap(p -> propertyMap.get(p).stream()).distinct().iterator());
    return builder.build();
}
Also used : PatternAdmin(ai.grakn.graql.admin.PatternAdmin) Atom(ai.grakn.graql.internal.reasoner.atom.Atom) ReasonerQueryImpl(ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl) GraqlTraversal(ai.grakn.graql.internal.gremlin.GraqlTraversal) Multimap(com.google.common.collect.Multimap) ArrayList(java.util.ArrayList) GreedyTraversalPlan(ai.grakn.graql.internal.gremlin.GreedyTraversalPlan) OntologicalAtom(ai.grakn.graql.internal.reasoner.atom.binary.OntologicalAtom) HashMultimap(com.google.common.collect.HashMultimap) ImmutableList(com.google.common.collect.ImmutableList) ConceptId(ai.grakn.concept.ConceptId) Fragment(ai.grakn.graql.internal.gremlin.fragment.Fragment) Nullable(javax.annotation.Nullable) Patterns(ai.grakn.graql.internal.pattern.Patterns) Conjunction(ai.grakn.graql.admin.Conjunction) Set(java.util.Set) IdPredicate(ai.grakn.graql.internal.reasoner.atom.predicate.IdPredicate) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) VarProperty(ai.grakn.graql.admin.VarProperty) Objects(java.util.Objects) Atomic(ai.grakn.graql.admin.Atomic) List(java.util.List) Stream(java.util.stream.Stream) EmbeddedGraknTx(ai.grakn.kb.internal.EmbeddedGraknTx) Comparator(java.util.Comparator) VarProperty(ai.grakn.graql.admin.VarProperty) ImmutableList(com.google.common.collect.ImmutableList) Objects(java.util.Objects) GraqlTraversal(ai.grakn.graql.internal.gremlin.GraqlTraversal) Fragment(ai.grakn.graql.internal.gremlin.fragment.Fragment) Atom(ai.grakn.graql.internal.reasoner.atom.Atom) OntologicalAtom(ai.grakn.graql.internal.reasoner.atom.binary.OntologicalAtom) OntologicalAtom(ai.grakn.graql.internal.reasoner.atom.binary.OntologicalAtom)

Example 2 with Fragment

use of ai.grakn.graql.internal.gremlin.fragment.Fragment 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 3 with Fragment

use of ai.grakn.graql.internal.gremlin.fragment.Fragment in project grakn by graknlabs.

the class GreedyTraversalPlan method greedyTraversal.

private static void greedyTraversal(List<Fragment> plan, Arborescence<Node> arborescence, Map<NodeId, Node> nodes, Map<Node, Map<Node, Fragment>> edgeFragmentChildToParent) {
    Map<Node, Set<Node>> edgesParentToChild = new HashMap<>();
    arborescence.getParents().forEach((child, parent) -> {
        if (!edgesParentToChild.containsKey(parent)) {
            edgesParentToChild.put(parent, new HashSet<>());
        }
        edgesParentToChild.get(parent).add(child);
    });
    Node root = arborescence.getRoot();
    Set<Node> reachableNodes = Sets.newHashSet(root);
    while (!reachableNodes.isEmpty()) {
        Optional<Node> optionalNodeWithMinCost = reachableNodes.stream().min(Comparator.comparingDouble(node -> branchWeight(node, arborescence, edgesParentToChild, edgeFragmentChildToParent)));
        assert optionalNodeWithMinCost.isPresent() : "reachableNodes is never empty, so there is always a minimum";
        Node nodeWithMinCost = optionalNodeWithMinCost.get();
        // add edge fragment first, then node fragments
        getEdgeFragment(nodeWithMinCost, arborescence, edgeFragmentChildToParent).ifPresent(plan::add);
        addNodeFragmentToPlan(nodeWithMinCost, plan, nodes, true);
        reachableNodes.remove(nodeWithMinCost);
        if (edgesParentToChild.containsKey(nodeWithMinCost)) {
            reachableNodes.addAll(edgesParentToChild.get(nodeWithMinCost));
        }
    }
}
Also used : PatternAdmin(ai.grakn.graql.admin.PatternAdmin) LabelProperty(ai.grakn.graql.internal.pattern.property.LabelProperty) LoggerFactory(org.slf4j.LoggerFactory) Type(ai.grakn.concept.Type) ChuLiuEdmonds(ai.grakn.graql.internal.gremlin.spanningtree.ChuLiuEdmonds) InSubFragment(ai.grakn.graql.internal.gremlin.fragment.InSubFragment) HashMultimap(com.google.common.collect.HashMultimap) Label(ai.grakn.concept.Label) RelationshipType(ai.grakn.concept.RelationshipType) Map(java.util.Map) CommonUtil.toImmutableSet(ai.grakn.util.CommonUtil.toImmutableSet) Graql.var(ai.grakn.graql.Graql.var) ImmutableSet(com.google.common.collect.ImmutableSet) Conjunction(ai.grakn.graql.admin.Conjunction) Collection(java.util.Collection) Set(java.util.Set) Weighted(ai.grakn.graql.internal.gremlin.spanningtree.util.Weighted) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) Node(ai.grakn.graql.internal.gremlin.spanningtree.graph.Node) InIsaFragment(ai.grakn.graql.internal.gremlin.fragment.InIsaFragment) List(java.util.List) EquivalentFragmentSets(ai.grakn.graql.internal.gremlin.sets.EquivalentFragmentSets) Var(ai.grakn.graql.Var) Optional(java.util.Optional) OutRolePlayerFragment(ai.grakn.graql.internal.gremlin.fragment.OutRolePlayerFragment) ValueProperty(ai.grakn.graql.internal.pattern.property.ValueProperty) Fragments(ai.grakn.graql.internal.gremlin.fragment.Fragments) NodeId(ai.grakn.graql.internal.gremlin.spanningtree.graph.NodeId) Role(ai.grakn.concept.Role) SchemaConcept(ai.grakn.concept.SchemaConcept) HashMap(java.util.HashMap) Multimap(com.google.common.collect.Multimap) Iterators(com.google.common.collect.Iterators) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Arborescence(ai.grakn.graql.internal.gremlin.spanningtree.Arborescence) SparseWeightedGraph(ai.grakn.graql.internal.gremlin.spanningtree.graph.SparseWeightedGraph) Fragment(ai.grakn.graql.internal.gremlin.fragment.Fragment) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) DirectedEdge(ai.grakn.graql.internal.gremlin.spanningtree.graph.DirectedEdge) LabelFragment(ai.grakn.graql.internal.gremlin.fragment.LabelFragment) IsaProperty(ai.grakn.graql.internal.pattern.property.IsaProperty) EmbeddedGraknTx(ai.grakn.kb.internal.EmbeddedGraknTx) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) Comparator(java.util.Comparator) Collections(java.util.Collections) SHARD_LOAD_FACTOR(ai.grakn.graql.internal.gremlin.fragment.Fragment.SHARD_LOAD_FACTOR) CommonUtil.toImmutableSet(ai.grakn.util.CommonUtil.toImmutableSet) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) Node(ai.grakn.graql.internal.gremlin.spanningtree.graph.Node)

Example 4 with Fragment

use of ai.grakn.graql.internal.gremlin.fragment.Fragment in project grakn by graknlabs.

the class GreedyTraversalPlan method addNodeFragmentToPlan.

private static void addNodeFragmentToPlan(Node node, List<Fragment> plan, Map<NodeId, Node> nodes, boolean visited) {
    if (!visited) {
        node.getFragmentsWithoutDependency().stream().min(Comparator.comparingDouble(Fragment::fragmentCost)).ifPresent(firstNodeFragment -> {
            plan.add(firstNodeFragment);
            node.getFragmentsWithoutDependency().remove(firstNodeFragment);
        });
    }
    node.getFragmentsWithoutDependency().addAll(node.getFragmentsWithDependencyVisited());
    plan.addAll(node.getFragmentsWithoutDependency().stream().sorted(Comparator.comparingDouble(Fragment::fragmentCost)).collect(Collectors.toList()));
    node.getFragmentsWithoutDependency().clear();
    node.getFragmentsWithDependencyVisited().clear();
    node.getDependants().forEach(fragment -> {
        Node otherNode = nodes.get(new NodeId(NodeId.NodeType.VAR, fragment.start()));
        if (node.equals(otherNode)) {
            otherNode = nodes.get(new NodeId(NodeId.NodeType.VAR, fragment.dependencies().iterator().next()));
        }
        otherNode.getDependants().remove(fragment.getInverse());
        otherNode.getFragmentsWithDependencyVisited().add(fragment);
    });
    node.getDependants().clear();
}
Also used : Node(ai.grakn.graql.internal.gremlin.spanningtree.graph.Node) NodeId(ai.grakn.graql.internal.gremlin.spanningtree.graph.NodeId) InSubFragment(ai.grakn.graql.internal.gremlin.fragment.InSubFragment) InIsaFragment(ai.grakn.graql.internal.gremlin.fragment.InIsaFragment) OutRolePlayerFragment(ai.grakn.graql.internal.gremlin.fragment.OutRolePlayerFragment) Fragment(ai.grakn.graql.internal.gremlin.fragment.Fragment) LabelFragment(ai.grakn.graql.internal.gremlin.fragment.LabelFragment)

Example 5 with Fragment

use of ai.grakn.graql.internal.gremlin.fragment.Fragment in project grakn by graknlabs.

the class GraqlTraversalTest method testAllTraversalsSimpleQuery.

@Test
public void testAllTraversalsSimpleQuery() {
    IdProperty titanicId = IdProperty.of(ConceptId.of("Titanic"));
    IdProperty movieId = IdProperty.of(ConceptId.of("movie"));
    SubProperty subProperty = SubProperty.of(Patterns.varPattern(y, ImmutableSet.of(movieId)));
    VarPattern pattern = Patterns.varPattern(x, ImmutableSet.of(titanicId, subProperty));
    Set<GraqlTraversal> traversals = allGraqlTraversals(pattern).collect(toSet());
    assertEquals(12, traversals.size());
    Fragment xId = id(titanicId, x, ConceptId.of("Titanic"));
    Fragment yId = id(movieId, y, ConceptId.of("movie"));
    Fragment xSubY = outSub(subProperty, x, y);
    Fragment ySubX = inSub(subProperty, y, x);
    Set<GraqlTraversal> expected = ImmutableSet.of(traversal(xId, xSubY, yId), traversal(xId, ySubX, yId), traversal(xId, yId, xSubY), traversal(xId, yId, ySubX), traversal(xSubY, xId, yId), traversal(xSubY, yId, xId), traversal(ySubX, xId, yId), traversal(ySubX, yId, xId), traversal(yId, xId, xSubY), traversal(yId, xId, ySubX), traversal(yId, xSubY, xId), traversal(yId, ySubX, xId));
    assertEquals(expected, traversals);
}
Also used : IdProperty(ai.grakn.graql.internal.pattern.property.IdProperty) SubProperty(ai.grakn.graql.internal.pattern.property.SubProperty) VarPattern(ai.grakn.graql.VarPattern) Fragment(ai.grakn.graql.internal.gremlin.fragment.Fragment) Test(org.junit.Test)

Aggregations

Fragment (ai.grakn.graql.internal.gremlin.fragment.Fragment)17 InIsaFragment (ai.grakn.graql.internal.gremlin.fragment.InIsaFragment)12 LabelFragment (ai.grakn.graql.internal.gremlin.fragment.LabelFragment)12 Test (org.junit.Test)10 Pattern (ai.grakn.graql.Pattern)9 NeqFragment (ai.grakn.graql.internal.gremlin.fragment.NeqFragment)9 OutIsaFragment (ai.grakn.graql.internal.gremlin.fragment.OutIsaFragment)9 Var (ai.grakn.graql.Var)5 HashSet (java.util.HashSet)4 InSubFragment (ai.grakn.graql.internal.gremlin.fragment.InSubFragment)3 OutRolePlayerFragment (ai.grakn.graql.internal.gremlin.fragment.OutRolePlayerFragment)3 EmbeddedGraknTx (ai.grakn.kb.internal.EmbeddedGraknTx)3 Sets (com.google.common.collect.Sets)3 List (java.util.List)3 Set (java.util.Set)3 Collectors (java.util.stream.Collectors)3 ConceptId (ai.grakn.concept.ConceptId)2 Label (ai.grakn.concept.Label)2 RelationshipType (ai.grakn.concept.RelationshipType)2 Type (ai.grakn.concept.Type)2