org.jgrapht.nio.graphml.GraphMLImporter Maven / Gradle / Ivy
/*
* (C) Copyright 2016-2023, by Dimitrios Michail and Contributors.
*
* JGraphT : a free Java graph-theory library
*
* See the CONTRIBUTORS.md file distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the
* GNU Lesser General Public License v2.1 or later
* which is available at
* http://www.gnu.org/licenses/old-licenses/lgpl-2.1-standalone.html.
*
* SPDX-License-Identifier: EPL-2.0 OR LGPL-2.1-or-later
*/
package org.jgrapht.nio.graphml;
import org.jgrapht.*;
import org.jgrapht.alg.util.*;
import org.jgrapht.nio.*;
import java.io.*;
import java.util.*;
import java.util.function.*;
/**
* Imports a graph from a GraphML data source.
*
*
* For a description of the format see
* http://en.wikipedia.org/wiki/ GraphML or the
* GraphML Primer.
*
*
*
* Below is small example of a graph in GraphML format.
*
*
* {@code
*
*
*
* yellow
*
*
*
*
* green
*
*
*
* blue
*
*
* red
*
*
*
* turquoise
*
*
* 1.0
*
*
* 1.0
*
*
* 2.0
*
*
*
*
*
* 1.1
*
*
*
* }
*
*
*
* The importer reads the input into a graph which is provided by the user. In case the graph is
* weighted and the corresponding edge key with attr.name="weight" is defined, the importer also
* reads edge weights. Otherwise edge weights are ignored. To test whether the graph is weighted,
* method {@link Graph#getType()} can be used.
*
*
* GraphML-Attributes Values are read as string key-value pairs and passed on to the vertex or edge
* attribute consumers respectively.
*
*
* The provided graph object, where the imported graph will be stored, must be able to support the
* features of the graph that is read. For example if the GraphML file contains self-loops then the
* graph provided must also support self-loops. The same for multiple edges. Moreover, the parser
* completely ignores the attribute "edgedefault" which denotes whether an edge is directed or not.
* Whether edges are directed or not depends on the underlying implementation of the user provided
* graph object.
*
*
* The importer by default validates the input using the 1.0
* GraphML Schema. The user can
* (not recommended) disable the validation by calling {@link #setSchemaValidation(boolean)}.
*
*
* The graph vertices and edges are build using the corresponding graph suppliers. The id of the
* vertices in the original dot file are reported as a vertex attribute named "ID". Thus, in case
* vertices in the dot file also contain an "ID" attribute, such an attribute will be reported
* multiple times.
*
*
* The default behavior of the importer is to use the graph vertex supplier in order to create
* vertices. The user can also bypass vertex creation by providing a custom vertex factory method
* using {@link #setVertexFactory(Function)}. The factory method is responsible to create a new
* graph vertex given the vertex identifier read from file.
*
* @param the graph vertex type
* @param the graph edge type
*
* @author Dimitrios Michail
*/
public class GraphMLImporter
extends BaseEventDrivenImporter
implements GraphImporter
{
/**
* Default key used for vertex ID.
*/
public static final String DEFAULT_VERTEX_ID_KEY = "ID";
// special attributes
private static final String EDGE_WEIGHT_DEFAULT_ATTRIBUTE_NAME = "weight";
private String edgeWeightAttributeName = EDGE_WEIGHT_DEFAULT_ATTRIBUTE_NAME;
private boolean schemaValidation;
private Function vertexFactory;
/**
* Constructs a new importer.
*/
public GraphMLImporter()
{
this.schemaValidation = true;
}
/**
* Get the attribute name for edge weights
*
* @return the attribute name
*/
public String getEdgeWeightAttributeName()
{
return edgeWeightAttributeName;
}
/**
* Set the attribute name to use for edge weights.
*
* @param edgeWeightAttributeName the attribute name
*/
public void setEdgeWeightAttributeName(String edgeWeightAttributeName)
{
if (edgeWeightAttributeName == null) {
throw new IllegalArgumentException("Edge weight attribute name cannot be null");
}
this.edgeWeightAttributeName = edgeWeightAttributeName;
}
/**
* Whether the importer validates the input
*
* @return true if the importer validates the input
*/
public boolean isSchemaValidation()
{
return schemaValidation;
}
/**
* Set whether the importer should validate the input
*
* @param schemaValidation value for schema validation
*/
public void setSchemaValidation(boolean schemaValidation)
{
this.schemaValidation = schemaValidation;
}
/**
* Get the user custom vertex factory. This is null by default and the graph supplier is used
* instead.
*
* @return the user custom vertex factory
*/
public Function getVertexFactory()
{
return vertexFactory;
}
/**
* Set the user custom vertex factory. The default behavior is being null in which case the
* graph vertex supplier is used.
*
* If supplied the vertex factory is called every time a new vertex is encountered in the file.
* The method is called with parameter the vertex identifier from the file and should return the
* actual graph vertex to add to the graph.
*
* @param vertexFactory a vertex factory
*/
public void setVertexFactory(Function vertexFactory)
{
this.vertexFactory = vertexFactory;
}
/**
* Import a graph.
*
*
* The provided graph must be able to support the features of the graph that is read. For
* example if the GraphML file contains self-loops then the graph provided must also support
* self-loops. The same for multiple edges.
*
*
* If the provided graph is a weighted graph, the importer also reads edge weights.
*
*
* GraphML-Attributes Values are read as string key-value pairs and propagated to the user as
* events.
*
* @param graph the output graph
* @param input the input reader
* @throws ImportException in case an error occurs, such as I/O or parse error
*/
@Override
public void importGraph(Graph graph, Reader input)
{
GraphMLEventDrivenImporter genericImporter = new GraphMLEventDrivenImporter();
genericImporter.setEdgeWeightAttributeName(edgeWeightAttributeName);
genericImporter.setSchemaValidation(schemaValidation);
Consumers globalConsumer = new Consumers(graph);
genericImporter.addGraphAttributeConsumer(globalConsumer.graphAttributeConsumer);
genericImporter.addVertexAttributeConsumer(globalConsumer.vertexAttributeConsumer);
genericImporter.addEdgeAttributeConsumer(globalConsumer.edgeAttributeConsumer);
genericImporter.addVertexConsumer(globalConsumer.vertexConsumer);
genericImporter.addEdgeConsumer(globalConsumer.edgeConsumer);
genericImporter.importInput(input);
}
private class Consumers
{
private Graph graph;
private Map nodesMap;
private E lastEdge;
private Triple lastTriple;
public Consumers(Graph graph)
{
this.graph = graph;
this.nodesMap = new HashMap<>();
this.lastEdge = null;
this.lastTriple = null;
}
public final BiConsumer graphAttributeConsumer = (key, a) -> {
notifyGraphAttribute(key, a);
};
public final BiConsumer, Attribute> vertexAttributeConsumer =
(vertexAndKey, a) -> {
notifyVertexAttribute(
mapNode(vertexAndKey.getFirst()), vertexAndKey.getSecond(), a);
};
public final BiConsumer, String>,
Attribute> edgeAttributeConsumer = (edgeAndKey, a) -> {
Triple qe = edgeAndKey.getFirst();
if (qe == lastTriple) {
if (qe.getThird() != null
&& edgeWeightAttributeName.equals(edgeAndKey.getSecond())
&& graph.getType().isWeighted())
{
graph.setEdgeWeight(lastEdge, qe.getThird());
}
notifyEdgeAttribute(lastEdge, edgeAndKey.getSecond(), a);
}
};
public final Consumer vertexConsumer = (vId) -> {
V v = mapNode(vId);
notifyVertex(v);
notifyVertexAttribute(v, DEFAULT_VERTEX_ID_KEY, DefaultAttribute.createAttribute(vId));
};
public final Consumer> edgeConsumer = (qe) -> {
if (lastTriple != qe) {
String source = qe.getFirst();
String target = qe.getSecond();
Double weight = qe.getThird();
E e = graph.addEdge(mapNode(source), mapNode(target));
if (weight != null && graph.getType().isWeighted()) {
graph.setEdgeWeight(e, weight);
}
lastEdge = e;
lastTriple = qe;
notifyEdge(lastEdge);
}
};
private V mapNode(String vId)
{
V vertex = nodesMap.get(vId);
if (vertex == null) {
if (vertexFactory != null) {
vertex = vertexFactory.apply(vId);
graph.addVertex(vertex);
} else {
vertex = graph.addVertex();
}
nodesMap.put(vId, vertex);
}
return vertex;
}
}
}