Search in sources :

Example 1 with GraphType

use of org.graphdrawing.graphml.GraphType in project opennms by OpenNMS.

the class GraphMLWriter method convert.

public static GraphmlType convert(GraphML graphML) throws InvalidGraphException {
    GraphmlType graphmlType = new GraphmlType();
    addProperties(graphmlType, KeyForType.GRAPHML, graphML, dataType -> graphmlType.getGraphOrData().add(dataType));
    for (GraphMLGraph eachGraph : graphML.getGraphs()) {
        GraphType graphType = new GraphType();
        graphType.setId(eachGraph.getId());
        addProperties(graphmlType, KeyForType.GRAPH, eachGraph, dataType -> graphType.getDataOrNodeOrEdge().add(dataType));
        for (GraphMLNode eachNode : eachGraph.getNodes()) {
            NodeType nodeType = new NodeType();
            nodeType.setId(eachNode.getId());
            graphType.getDataOrNodeOrEdge().add(nodeType);
            addProperties(graphmlType, KeyForType.NODE, eachNode, dataType -> nodeType.getDataOrPort().add(dataType));
        }
        for (GraphMLEdge eachEdge : eachGraph.getEdges()) {
            EdgeType edgeType = new EdgeType();
            edgeType.setId(eachEdge.getId());
            edgeType.setSource(eachEdge.getSource().getId());
            edgeType.setTarget(eachEdge.getTarget().getId());
            graphType.getDataOrNodeOrEdge().add(edgeType);
            addProperties(graphmlType, KeyForType.EDGE, eachEdge, dataType -> edgeType.getData().add(dataType));
        }
        graphmlType.getGraphOrData().add(graphType);
    }
    return graphmlType;
}
Also used : GraphmlType(org.graphdrawing.graphml.GraphmlType) GraphType(org.graphdrawing.graphml.GraphType) NodeType(org.graphdrawing.graphml.NodeType) EdgeType(org.graphdrawing.graphml.EdgeType)

Example 2 with GraphType

use of org.graphdrawing.graphml.GraphType in project opennms by OpenNMS.

the class GraphMLReader method convert.

public static GraphML convert(GraphmlType input) throws InvalidGraphException {
    final Map<String, GraphMLGraph> nodeIdToGraphMapping = new HashMap<>();
    final Map<String, KeyType> keyIdToTypeMapping = getKeyIdToTypeMapping(input.getKey());
    final List<GraphType> graphs = filter(input.getGraphOrData(), GraphType.class);
    final GraphML convertedGraphML = new GraphML();
    addProperties(convertedGraphML, null, keyIdToTypeMapping, filter(input.getGraphOrData(), DataType.class));
    for (GraphType eachGraph : graphs) {
        GraphMLGraph convertedGraph = new GraphMLGraph();
        addProperties(convertedGraph, eachGraph.getId(), keyIdToTypeMapping, filter(eachGraph.getDataOrNodeOrEdge(), DataType.class));
        // Nodes
        List<NodeType> nodes = filter(eachGraph.getDataOrNodeOrEdge(), NodeType.class);
        for (NodeType eachNode : nodes) {
            GraphMLNode convertedNode = new GraphMLNode();
            nodeIdToGraphMapping.put(eachNode.getId(), convertedGraph);
            addProperties(convertedNode, eachNode.getId(), keyIdToTypeMapping, filter(eachNode.getDataOrPort(), DataType.class));
            List<PortType> ports = filter(eachNode.getDataOrPort(), PortType.class);
            if (!ports.isEmpty()) {
                LOG.warn("Ports are defined for node with id {} but ports are not supported. Ignoring {} defined ports", eachNode.getId(), ports.size());
            }
            convertedGraph.addNode(convertedNode);
        }
        convertedGraphML.addGraph(convertedGraph);
    }
    // Add Edges Last, as they may connect between graphs, and we just now know all graphs
    for (GraphType eachGraphType : graphs) {
        // Edges
        List<EdgeType> edges = filter(eachGraphType.getDataOrNodeOrEdge(), EdgeType.class);
        for (EdgeType eachEdge : edges) {
            GraphMLEdge convertedEdge = new GraphMLEdge();
            GraphMLGraph sourceGraph = nodeIdToGraphMapping.get(eachEdge.getSource());
            GraphMLGraph targetGraph = nodeIdToGraphMapping.get(eachEdge.getTarget());
            if (sourceGraph == null) {
                throw new InvalidGraphException("No graph found for edge with id " + eachEdge.getSource());
            }
            if (targetGraph == null) {
                throw new InvalidGraphException("No graph found for edge with id " + eachEdge.getTarget());
            }
            GraphMLNode sourceNode = sourceGraph.getNodeById(eachEdge.getSource());
            GraphMLNode targetNode = targetGraph.getNodeById(eachEdge.getTarget());
            convertedEdge.setSource(sourceNode);
            convertedEdge.setTarget(targetNode);
            addProperties(convertedEdge, eachEdge.getId(), keyIdToTypeMapping, filter(eachEdge.getData(), DataType.class));
            sourceGraph.addEdge(convertedEdge);
        }
        // Hyper Edges
        List<HyperedgeType> hyperEdges = filter(eachGraphType.getDataOrNodeOrEdge(), HyperedgeType.class);
        if (!hyperEdges.isEmpty()) {
            LOG.warn("Hyper Edges are defined for graph with id {} but are not supported. Ignoring {} defined hyper edges", eachGraphType.getId(), hyperEdges.size());
        }
    }
    validate(convertedGraphML);
    return convertedGraphML;
}
Also used : KeyType(org.graphdrawing.graphml.KeyType) HashMap(java.util.HashMap) EdgeType(org.graphdrawing.graphml.EdgeType) HyperedgeType(org.graphdrawing.graphml.HyperedgeType) GraphType(org.graphdrawing.graphml.GraphType) NodeType(org.graphdrawing.graphml.NodeType) DataType(org.graphdrawing.graphml.DataType) PortType(org.graphdrawing.graphml.PortType)

Aggregations

EdgeType (org.graphdrawing.graphml.EdgeType)2 GraphType (org.graphdrawing.graphml.GraphType)2 NodeType (org.graphdrawing.graphml.NodeType)2 HashMap (java.util.HashMap)1 DataType (org.graphdrawing.graphml.DataType)1 GraphmlType (org.graphdrawing.graphml.GraphmlType)1 HyperedgeType (org.graphdrawing.graphml.HyperedgeType)1 KeyType (org.graphdrawing.graphml.KeyType)1 PortType (org.graphdrawing.graphml.PortType)1