Search in sources :

Example 1 with DataType

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

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

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