Search in sources :

Example 1 with Fragments

use of ai.grakn.graql.internal.gremlin.fragment.Fragments 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)

Aggregations

Label (ai.grakn.concept.Label)1 RelationshipType (ai.grakn.concept.RelationshipType)1 Role (ai.grakn.concept.Role)1 SchemaConcept (ai.grakn.concept.SchemaConcept)1 Type (ai.grakn.concept.Type)1 Graql.var (ai.grakn.graql.Graql.var)1 Var (ai.grakn.graql.Var)1 Conjunction (ai.grakn.graql.admin.Conjunction)1 PatternAdmin (ai.grakn.graql.admin.PatternAdmin)1 VarPatternAdmin (ai.grakn.graql.admin.VarPatternAdmin)1 Fragment (ai.grakn.graql.internal.gremlin.fragment.Fragment)1 SHARD_LOAD_FACTOR (ai.grakn.graql.internal.gremlin.fragment.Fragment.SHARD_LOAD_FACTOR)1 Fragments (ai.grakn.graql.internal.gremlin.fragment.Fragments)1 InIsaFragment (ai.grakn.graql.internal.gremlin.fragment.InIsaFragment)1 InSubFragment (ai.grakn.graql.internal.gremlin.fragment.InSubFragment)1 LabelFragment (ai.grakn.graql.internal.gremlin.fragment.LabelFragment)1 OutRolePlayerFragment (ai.grakn.graql.internal.gremlin.fragment.OutRolePlayerFragment)1 EquivalentFragmentSets (ai.grakn.graql.internal.gremlin.sets.EquivalentFragmentSets)1 Arborescence (ai.grakn.graql.internal.gremlin.spanningtree.Arborescence)1 ChuLiuEdmonds (ai.grakn.graql.internal.gremlin.spanningtree.ChuLiuEdmonds)1