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);
}
}
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;
}
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;
}
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);
}
}
Aggregations