Search in sources :

Example 1 with KeyType

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

the class GraphMLReader method addProperties.

private static void addProperties(GraphMLElement graphElement, String elementId, Map<String, KeyType> keyIdToTypeMapping, List<DataType> elementData) throws InvalidGraphException {
    // add defined properties
    for (DataType eachDataElement : elementData) {
        KeyType keyType = keyIdToTypeMapping.get(eachDataElement.getKey());
        if (keyType == null) {
            throw new InvalidGraphException("Accessing not existing attribute with key " + eachDataElement.getKey());
        }
        if (keyType.getAttrType() == null) {
            throw new InvalidGraphException("Key with id='" + keyType.getId() + "' and " + "attribute name '" + keyType.getAttrName() + "' is null. " + "This is usually caused by an invalid attribute type value. " + "The following values are supported: " + Arrays.stream(KeyTypeType.values()).map(k -> k.value()).collect(Collectors.joining(", ")));
        }
        Object value = typeCastValue(eachDataElement.getContent(), keyType.getAttrType());
        graphElement.setProperty(keyType.getAttrName(), value);
    }
    // add default values if not already defined
    keyIdToTypeMapping.values().stream().filter(keyType -> keyType.getDefault() != null).filter(keyType -> !Strings.isNullOrEmpty(keyType.getDefault().getContent())).filter(keyType -> graphElement.accept(new GraphMLElement.GraphMLElementVisitor<Boolean>() {

        @Override
        public Boolean visit(GraphMLGraph graph) {
            return Lists.newArrayList(KeyForType.ALL, KeyForType.GRAPH, KeyForType.GRAPHML).contains(keyType.getFor());
        }

        @Override
        public Boolean visit(GraphMLNode node) {
            return Lists.newArrayList(KeyForType.ALL, KeyForType.GRAPH, KeyForType.GRAPHML, KeyForType.NODE).contains(keyType.getFor());
        }

        @Override
        public Boolean visit(GraphMLEdge edge) {
            return Lists.newArrayList(KeyForType.ALL, KeyForType.GRAPH, KeyForType.GRAPHML, KeyForType.EDGE).contains(keyType.getFor());
        }

        @Override
        public Boolean visit(GraphML graphML) {
            return Lists.newArrayList(KeyForType.ALL, KeyForType.GRAPHML).contains(keyType.getFor());
        }
    })).forEach(keyType -> {
        if (graphElement.getProperty(keyType.getAttrName()) == null) {
            graphElement.setProperty(keyType.getAttrName(), typeCastValue(keyType.getDefault().getContent(), keyType.getAttrType()));
        }
    });
    // add id as property
    if (elementId != null) {
        graphElement.setProperty(GraphMLElement.ID, elementId);
    }
}
Also used : Arrays(java.util.Arrays) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) PortType(org.graphdrawing.graphml.PortType) KeyTypeType(org.graphdrawing.graphml.KeyTypeType) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Strings(com.google.common.base.Strings) Lists(com.google.common.collect.Lists) Map(java.util.Map) GraphType(org.graphdrawing.graphml.GraphType) JAXB(javax.xml.bind.JAXB) Logger(org.slf4j.Logger) Set(java.util.Set) HyperedgeType(org.graphdrawing.graphml.HyperedgeType) Collectors(java.util.stream.Collectors) DataType(org.graphdrawing.graphml.DataType) List(java.util.List) NodeType(org.graphdrawing.graphml.NodeType) KeyForType(org.graphdrawing.graphml.KeyForType) GraphmlType(org.graphdrawing.graphml.GraphmlType) KeyType(org.graphdrawing.graphml.KeyType) EdgeType(org.graphdrawing.graphml.EdgeType) InputStream(java.io.InputStream) KeyType(org.graphdrawing.graphml.KeyType) DataType(org.graphdrawing.graphml.DataType)

Example 2 with KeyType

use of org.graphdrawing.graphml.KeyType 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)

Example 3 with KeyType

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

the class GraphMLReader method getKeyIdToTypeMapping.

private static Map<String, KeyType> getKeyIdToTypeMapping(List<KeyType> key) throws InvalidGraphException {
    // Filter by id
    Map<String, List<KeyType>> keyTypeIdMap = new HashMap<>();
    for (KeyType eachKeyType : key) {
        keyTypeIdMap.putIfAbsent(eachKeyType.getId(), new ArrayList<>());
        keyTypeIdMap.get(eachKeyType.getId()).add(eachKeyType);
    }
    // Verify that all types are the same
    for (List<KeyType> eachList : keyTypeIdMap.values()) {
        if (eachList.stream().map(e -> e.getAttrType()).collect(Collectors.toSet()).size() > 1) {
            throw new InvalidGraphException("Attribute Type of key with id " + eachList.get(0).getId() + " varies.");
        }
    }
    // Unify
    Map<String, KeyType> keyIdToTypeMapping = keyTypeIdMap.values().stream().map(list -> list.get(0)).collect(Collectors.toMap(keyType -> keyType.getId(), Function.identity()));
    if (keyIdToTypeMapping.keySet().stream().filter(keyId -> keyId.equals("id")).findFirst().isPresent()) {
        throw new InvalidGraphException("Property with id cannot be defined");
    }
    return keyIdToTypeMapping;
}
Also used : Arrays(java.util.Arrays) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) PortType(org.graphdrawing.graphml.PortType) KeyTypeType(org.graphdrawing.graphml.KeyTypeType) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Strings(com.google.common.base.Strings) Lists(com.google.common.collect.Lists) Map(java.util.Map) GraphType(org.graphdrawing.graphml.GraphType) JAXB(javax.xml.bind.JAXB) Logger(org.slf4j.Logger) Set(java.util.Set) HyperedgeType(org.graphdrawing.graphml.HyperedgeType) Collectors(java.util.stream.Collectors) DataType(org.graphdrawing.graphml.DataType) List(java.util.List) NodeType(org.graphdrawing.graphml.NodeType) KeyForType(org.graphdrawing.graphml.KeyForType) GraphmlType(org.graphdrawing.graphml.GraphmlType) KeyType(org.graphdrawing.graphml.KeyType) EdgeType(org.graphdrawing.graphml.EdgeType) InputStream(java.io.InputStream) KeyType(org.graphdrawing.graphml.KeyType) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) List(java.util.List)

Example 4 with KeyType

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

the class GraphMLWriter method addProperties.

private static void addProperties(GraphmlType graphmlType, KeyForType keyForType, GraphMLElement element, DataTypeAddCallback callback) throws InvalidGraphException {
    for (Map.Entry<String, Object> eachEntry : element.getProperties().entrySet()) {
        if (eachEntry.getKey().equals(GraphMLElement.ID)) {
            // skip IDs
            continue;
        }
        List<KeyType> definedKeys = graphmlType.getKey().stream().filter(eachKey -> eachKey.getFor() == keyForType).filter(eachKey -> eachKey.getId().equals(eachEntry.getKey())).collect(Collectors.toList());
        if (definedKeys.isEmpty()) {
            KeyType keyType = new KeyType();
            keyType.setFor(keyForType);
            keyType.setId(eachEntry.getKey());
            keyType.setAttrName(eachEntry.getKey());
            keyType.setAttrType(parseType(eachEntry.getValue()));
            graphmlType.getKey().add(keyType);
        }
        if (definedKeys.size() > 1) {
            throw new InvalidGraphException("Duplicate key found for id " + eachEntry.getKey());
        }
        DataType dataType = new DataType();
        dataType.setKey(eachEntry.getKey());
        dataType.setContent(String.valueOf(eachEntry.getValue()));
        callback.addData(dataType);
    }
}
Also used : KeyTypeType(org.graphdrawing.graphml.KeyTypeType) Collectors(java.util.stream.Collectors) DataType(org.graphdrawing.graphml.DataType) File(java.io.File) List(java.util.List) NodeType(org.graphdrawing.graphml.NodeType) KeyForType(org.graphdrawing.graphml.KeyForType) Map(java.util.Map) GraphmlType(org.graphdrawing.graphml.GraphmlType) KeyType(org.graphdrawing.graphml.KeyType) GraphType(org.graphdrawing.graphml.GraphType) JAXB(javax.xml.bind.JAXB) EdgeType(org.graphdrawing.graphml.EdgeType) KeyType(org.graphdrawing.graphml.KeyType) DataType(org.graphdrawing.graphml.DataType) Map(java.util.Map)

Aggregations

DataType (org.graphdrawing.graphml.DataType)4 EdgeType (org.graphdrawing.graphml.EdgeType)4 GraphType (org.graphdrawing.graphml.GraphType)4 KeyType (org.graphdrawing.graphml.KeyType)4 NodeType (org.graphdrawing.graphml.NodeType)4 HashMap (java.util.HashMap)3 List (java.util.List)3 Map (java.util.Map)3 Collectors (java.util.stream.Collectors)3 JAXB (javax.xml.bind.JAXB)3 GraphmlType (org.graphdrawing.graphml.GraphmlType)3 HyperedgeType (org.graphdrawing.graphml.HyperedgeType)3 KeyForType (org.graphdrawing.graphml.KeyForType)3 KeyTypeType (org.graphdrawing.graphml.KeyTypeType)3 PortType (org.graphdrawing.graphml.PortType)3 Strings (com.google.common.base.Strings)2 Lists (com.google.common.collect.Lists)2 InputStream (java.io.InputStream)2 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2