@Override
public Graph<String, String> createGraph() {
DirectedOrderedSparseMultigraph<String, String> treeGraph = new DirectedOrderedSparseMultigraph<String, String>();
Tree root = this.model.getRoot();
treeGraph.addVertex("Root");
vertexMap.put("Root", root);
addTree(treeGraph, root, "Root", new ArrayList<>());
return new DelegateForest<String, String>(treeGraph);
}
private void addTree(DirectedOrderedSparseMultigraph<String, String> treeGraph, Tree node, String parentName,
List<String> currentParentList) {
Iterator<Edge> e = node.childIterator();
double edgeWeightSum = model.getRoot().getSubtreeFrequencySum();
e = node.childIterator();
while (e.hasNext()) {
Edge edge = e.next();
Tree child = edge.getChild();
SplitCondition condition = edge.getCondition();
String childName = vertexFactory.create();
String edgeName = edgeFactory.create();
currentParentList.add(parentName);
currentParentList.add(edgeName);
pathToRootMap.put(edgeName, currentParentList);
pathToRootMap.put(childName, currentParentList);
vertexMap.put(childName, child);
edgeMap.put(edgeName, condition);
edgeStrengthMap.put(edgeName, child.getSubtreeFrequencySum() / edgeWeightSum);
treeGraph.addEdge(edgeName, parentName, childName);
addTree(treeGraph, child, childName, new ArrayList<>(currentParentList));
// siblings would use the same list with wrong edges if we did not copy
currentParentList = new ArrayList<String>(currentParentList);
currentParentList.remove(edgeName);
}
}