要生成这样的 PNG:
或这样的 XML ( GraphML ):
<?xml version="1.0" encoding="UTF-8"?>
<graphml xmlns="http://graphml.graphdrawing.org/xmlns"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd">
<graph id="G" edgedefault="directed">
<node id="Off" />
<node id="Standby" />
<node id="Fail" />
<node id="Oper" />
<node id="Recovery" />
<node id="Shutdown" />
<edge id="1" source="Off" target="Standby" />
<hyperedge>
<endpoint node=Standby" type="in" />
<endpoint node=Fail" type="out" />
<endpoint node=Oper" type="out" />
<endpoint node=Shutdown" type="out" />
</hyperedge>
<hyperedge>
<endpoint node=Fail" type="in" />
<endpoint node=Shutdown" type="out" />
<endpoint node=Recovery" type="out" />
</hyperedge>
<hyperedge>
<endpoint node=Oper" type="in" />
<endpoint node=Standby" type="out" />
<endpoint node=Fail" type="out" />
<endpoint node=Shutdown" type="out" />
</hyperedge>
<edge id="2" source="Shutdown" target="Off" />
<hyperedge>
<endpoint node=Recovery" type="in" />
<endpoint node=Oper" type="out" />
<endpoint node=Shutdown" type="out" />
</hyperedge>
</graph>
</graphml>
你也可以自己做:
public abstract class Edge {
protected final Node _endPoint1;
public Edge( Node endPoint ) {
_endPoint1 = endPoint;
}
public Node getEndPoint1() {
return _endPoint1;
}
}
类 DirectedEdge:
public final class DirectedEdge extends Edge {
private final Node[] _to;
public DirectedEdge( Node from, Node ... to ) {
super( from );
_to = to;
}
public Node getFrom() {
return _endPoint1;
}
public Node[] getTo() {
return _to;
}
}
类图:
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
public final class Graph {
private /* */ String _name = "G";
private final Map< String, Node > _nodes = new LinkedHashMap<>();
private final Set< DirectedEdge > _edges = new LinkedHashSet<>();
public boolean addNode( Node node ) {
return _nodes.put( node._label, node ) == null;
}
public void addEdge( DirectedEdge edge ) {
_edges.add( edge );
}
public String getName() {
return _name;
}
public void setName( String name ) {
_name = name;
}
public final Map<String, Node> getNodes() {
return _nodes;
}
public final Set<DirectedEdge> getEdges() {
return _edges;
}
}
类 Main,使用示例:
import java.io.File;
public class Main {
private static Graph getGraph() {
Graph graph = new Graph();
Node off = new Node( "Off" );
Node standby = new Node( "Standby" );
Node fail = new Node( "Fail" );
Node oper = new Node( "Oper" );
Node recovery = new Node( "Recovery" );
Node shutdown = new Node( "Shutdown" );
graph.addNode( off );
graph.addNode( standby );
graph.addNode( fail );
graph.addNode( oper );
graph.addNode( recovery );
graph.addNode( shutdown );
graph.addEdge( new DirectedEdge( off , standby ));
graph.addEdge( new DirectedEdge( standby , fail, oper, shutdown ));
graph.addEdge( new DirectedEdge( fail , shutdown, recovery ));
graph.addEdge( new DirectedEdge( oper , standby, fail, shutdown ));
graph.addEdge( new DirectedEdge( shutdown, off ));
graph.addEdge( new DirectedEdge( recovery, oper, shutdown ));
return graph;
}
public static void main( String[] args ) throws Exception {
Graph graph = getGraph();
new DotFileGenerator().save( new File( "States.png" ), graph );
new GraphMLGenerator().save( new File( "States.graphml" ), graph );
}
}