Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 package utils;
00025
00026 import java.util.*;
00027
00037 public class GMLGraph {
00038
00039 protected static String GRAPH_HEADER = "graph [";
00040 protected static String GRAPH_FOOTER = "]";
00041 protected static String NODE_HEADER = "node [";
00042 protected static String NODE_FOOTER = "]";
00043 protected static String EDGE_HEADER = "edge [";
00044 protected static String EDGE_FOOTER = "]";
00045
00046 protected static String KEY_ID = "id";
00047 protected static String KEY_LABEL = "label";
00048 protected static String KEY_SOURCE = "source";
00049 protected static String KEY_TARGET = "target";
00050
00051 protected Properties headerProps = new Properties();
00052
00054 protected ArrayList nodeLines;
00055
00057 protected ArrayList edgeLines;
00058
00060 protected Collection additionalNodeLines;
00061
00062 protected Collection additionalEdgeLines;
00063
00064
00070 public void addHeaderProperty(String key, String value) {
00071 headerProps.setProperty(key, value);
00072 }
00073
00077 public void defineAdditionalNodeLines(Collection additionalNodeLines) {
00078 this.additionalNodeLines = additionalNodeLines;
00079 }
00080
00084 public void defineAdditionalEdgeLines(Collection additionalEdgeLines) {
00085 this.additionalEdgeLines = additionalEdgeLines;
00086 }
00087
00091 public void generateFrom(DoubleLinkedDAG source) {
00092
00093 nodeLines = new ArrayList();
00094 edgeLines = new ArrayList();
00095 Properties nodeProps = new Properties();
00096 Properties edgeProps = new Properties();
00097
00098 Collection nodes = source.getNodes();
00099 Iterator itNodes = nodes.iterator();
00100
00101 while (itNodes.hasNext()) {
00102
00103
00104
00105 DoubleLinkedDAGNode node = (DoubleLinkedDAGNode)itNodes.next();
00106 assert(node instanceof GMLNode);
00107 if (((GMLNode)node).includeInGMLCode()) {
00108
00109 generateNodeProps(node, nodeProps);
00110
00111 nodeLines.add(NODE_HEADER);
00112 addProperties(nodeProps, nodeLines);
00113 nodeProps.clear();
00114 if (additionalNodeLines != null) nodeLines.addAll(additionalNodeLines);
00115 nodeLines.add(NODE_FOOTER);
00116
00117
00118
00119 Iterator itChildren = node.getChildrenIterator();
00120 while (itChildren.hasNext()) {
00121 DoubleLinkedDAGNode child = (DoubleLinkedDAGNode)itChildren.next();
00122 assert(child instanceof GMLNode);
00123 if (((GMLNode)child).includeInGMLCode()) {
00124
00125 generateEdgeProps(node, child, edgeProps);
00126
00127 edgeLines.add(EDGE_HEADER);
00128 addProperties(edgeProps, edgeLines);
00129 edgeProps.clear();
00130 if (additionalEdgeLines != null) edgeLines.addAll(additionalEdgeLines);
00131 edgeLines.add(EDGE_FOOTER);
00132 }
00133 }
00134 }
00135 }
00136 }
00137
00144 public Collection getGMLCode() {
00145 assert((nodeLines != null) && (edgeLines != null));
00146
00147 ArrayList result = new ArrayList();
00148 result.add(GRAPH_HEADER);
00149 addProperties(headerProps, result);
00150 result.addAll(nodeLines);
00151 result.addAll(edgeLines);
00152 result.add(GRAPH_FOOTER);
00153
00154 return result;
00155 }
00156
00157 protected void addProperties(Properties props, Collection lines) {
00158 Iterator itProps = props.entrySet().iterator();
00159 while (itProps.hasNext()) {
00160 Map.Entry entry = (Map.Entry)itProps.next();
00161 assert((entry.getKey() instanceof String) && (entry.getValue() instanceof String));
00162 lines.add(entry.getKey().toString() + " " + entry.getValue().toString());
00163 }
00164 }
00165
00166 protected void generateNodeProps(DoubleLinkedDAGNode node, Properties props) {
00167 assert(node instanceof GMLNode);
00168
00169 props.setProperty(KEY_ID, (new Integer(node.getID())).toString());
00170 props.setProperty(KEY_LABEL, "\"" + ((GMLNode)node).getLabel() + "\"");
00171
00172 }
00173
00174 protected void generateEdgeProps(DoubleLinkedDAGNode from, DoubleLinkedDAGNode to,
00175 Properties props) {
00176
00177 assert((from instanceof GMLNode) && (to instanceof GMLNode));
00178
00179 props.setProperty(KEY_SOURCE, (new Integer(from.getID())).toString());
00180 props.setProperty(KEY_TARGET, (new Integer(to.getID())).toString());
00181 }
00182
00183 }