Search in sources :

Example 1 with ConnectedComponentTraversalEvent

use of org.jgrapht.event.ConnectedComponentTraversalEvent in project alliance by codice.

the class ResultDAGConverter method getAttributeMap.

public static Map<String, String> getAttributeMap(DAG dag) {
    Map<String, String> attributes = new HashMap<>();
    Map<Integer, Node> nodeMap = createNodeMap(dag.nodes);
    DirectedAcyclicGraph<Node, Edge> graph = getNodeEdgeDirectedAcyclicGraph(dag, nodeMap);
    DepthFirstIterator<Node, Edge> graphIT = new DepthFirstIterator<>(graph, nodeMap.get(0));
    List<String> nodeStack = new ArrayList<>();
    graphIT.addTraversalListener(new TraversalListener<Node, Edge>() {

        @Override
        public void connectedComponentFinished(ConnectedComponentTraversalEvent connectedComponentTraversalEvent) {
        // This method is not expected to be called
        }

        @Override
        public void connectedComponentStarted(ConnectedComponentTraversalEvent connectedComponentTraversalEvent) {
        // This method is not expected to be called
        }

        @Override
        public void edgeTraversed(EdgeTraversalEvent<Node, Edge> edgeTraversalEvent) {
        // This method is not expected to be called
        }

        @Override
        public void vertexTraversed(VertexTraversalEvent<Node> vertexTraversalEvent) {
            Node node = vertexTraversalEvent.getVertex();
            if (node.node_type != NodeType.ATTRIBUTE_NODE) {
                nodeStack.add(node.attribute_name);
            }
        }

        @Override
        public void vertexFinished(VertexTraversalEvent<Node> vertexTraversalEvent) {
            Node node = vertexTraversalEvent.getVertex();
            if (node.node_type == NodeType.ATTRIBUTE_NODE) {
                StringBuilder attribute = new StringBuilder();
                int currEntry = 0;
                int size = nodeStack.size();
                for (String nodeEntry : nodeStack) {
                    attribute.append(nodeEntry);
                    if (currEntry < (size - 1)) {
                        attribute.append(":");
                    } else {
                        attribute.append(".");
                    }
                    currEntry++;
                }
                attribute.append(node.attribute_name);
                attributes.put(attribute.toString(), CorbaUtils.getNodeValue(node.value));
            } else {
                int lastIdx = nodeStack.size() - 1;
                nodeStack.remove(lastIdx);
            }
        }
    });
    Node rootNode = null;
    while (graphIT.hasNext()) {
        graphIT.setCrossComponentTraversal(false);
        Node node = graphIT.next();
        if (rootNode == null) {
            rootNode = node;
        }
    }
    return attributes;
}
Also used : DepthFirstIterator(org.jgrapht.traverse.DepthFirstIterator) HashMap(java.util.HashMap) Node(org.codice.alliance.nsili.common.UCO.Node) ArrayList(java.util.ArrayList) ConnectedComponentTraversalEvent(org.jgrapht.event.ConnectedComponentTraversalEvent) Edge(org.codice.alliance.nsili.common.UCO.Edge)

Aggregations

ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Edge (org.codice.alliance.nsili.common.UCO.Edge)1 Node (org.codice.alliance.nsili.common.UCO.Node)1 ConnectedComponentTraversalEvent (org.jgrapht.event.ConnectedComponentTraversalEvent)1 DepthFirstIterator (org.jgrapht.traverse.DepthFirstIterator)1