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