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));
}
}
}
Aggregations