Explorar el Código

beginning multigraph export rewrite

rewrite
	still needs setup
	calling
	and testing

Bug fix
	saving a graph no longer removes some 	attributes
jascha Bohne hace 8 años
padre
commit
cc12681630

+ 2 - 52
scopviz/src/main/java/de/tu_darmstadt/informatik/tk/scopviz/io/GraphMLExporter.java

@@ -36,10 +36,10 @@ public class GraphMLExporter {
 		FileSinkGraphML writer = new MyFileSinkGraphML();
 		if(g.isComposite()){
 			for(int i = 0; i < g.getChildren().toArray().length; i++){
-				writeGraph(g.getChildren().toArray(new MyGraph[0])[i], fileName + "children"+i);
+				writeGraph(g.getChildren().toArray(new MyGraph[0])[i], fileName +  "children"+i);
 			}
+			return;
 		}
-		clearAttributes(g);
 		try {
 			writer.writeAll(g, new FileOutputStream(fileName));
 		} catch (IOException e) {
@@ -68,60 +68,10 @@ public class GraphMLExporter {
 				for(int i = 0; i < g.getChildren().toArray().length; i++){
 					writeGraph(g.getChildren().toArray(new MyGraph[0])[i], fileName + "children"+i);
 				}
-				clearAttributes(g);
 				writeGraph(g, fileName);
 			}
 		} catch (NullPointerException e) {
 
 		}
 	}
-
-	/**
-	 * Cleans up the Attributes of all Nodes and Edges of a given Graph,
-	 * removing the ui.j2dsk and ui.class Attribute. also removes all Attributes
-	 * that are not a String or (a Wrapper of) a primitive type
-	 * 
-	 * @param g
-	 *            the Graph to clean up
-	 */
-	private void clearAttributes(Graph g) {
-		Iterator<? extends Edge> edges = g.getEdgeIterator();
-		while (edges.hasNext()) {
-			Edge e = edges.next();
-			e.removeAttribute("ui.j2dsk");
-			String[] temp = new String[0];
-			temp = e.getAttributeKeySet().toArray(temp);
-			for (String s : temp) {
-				Class<? extends Object> c = e.getAttribute(s).getClass();
-				if (!c.isPrimitive() && !(c == String.class) && !(c == Character.class) && !(c == Boolean.class)
-						&& !(c == Integer.class) && !(c == Long.class) && !(c == Short.class) && !(c == Byte.class)
-						&& !(c == Float.class) && !(c == Double.class)) {
-					Debug.out("Could not parse an Attribute because it is not Primitive or a String \n\t"
-							+ "(Attribute: " + s + ", Value: " + e.getAttribute(s) + ", from Edge: " + e + ", Type: "
-							+ c + ") ");
-					e.removeAttribute(s);
-				}
-			}
-		}
-		Iterator<? extends Node> nodes = g.getNodeIterator();
-		while (nodes.hasNext()) {
-			Node n = nodes.next();
-			n.removeAttribute("ui.j2dsk");
-			n.removeAttribute("ui.class");
-			n.removeAttribute("ui.pie-values");
-			String[] temp = new String[0];
-			temp = n.getAttributeKeySet().toArray(temp);
-			for (String s : temp) {
-				Class<? extends Object> c = n.getAttribute(s).getClass();
-				if (!c.isPrimitive() && !(c == String.class) && !(c == Character.class) && !(c == Boolean.class)
-						&& !(c == Integer.class) && !(c == Long.class) && !(c == Short.class) && !(c == Byte.class)
-						&& !(c == Float.class) && !(c == Double.class)) {
-					Debug.out("Could not parse an Attribute because it is not Primitive or a String \n\t"
-							+ "(Attribute: " + s + ", Value: " + n.getAttribute(s) + ", from Node: " + n + ", Type: "
-							+ c + ") ");
-					n.removeAttribute(s);
-				}
-			}
-		}
-	}
 }

+ 56 - 1
scopviz/src/main/java/de/tu_darmstadt/informatik/tk/scopviz/io/MyFileSinkGraphML.java

@@ -2,6 +2,7 @@ package de.tu_darmstadt.informatik.tk.scopviz.io;
 
 import java.io.IOException;
 import java.util.HashMap;
+import java.util.LinkedList;
 
 import org.graphstream.graph.Edge;
 import org.graphstream.graph.Graph;
@@ -9,9 +10,11 @@ import org.graphstream.graph.Node;
 import org.graphstream.stream.file.FileSinkGraphML;
 
 import de.tu_darmstadt.informatik.tk.scopviz.debug.Debug;
+import de.tu_darmstadt.informatik.tk.scopviz.graphs.MyGraph;
 
 
 public class MyFileSinkGraphML extends FileSinkGraphML{
+	public boolean isWritingMultigraph = false;
 
 	private void print(String format, Object... args) throws IOException {
 		output.write(String.format(format, args));
@@ -57,6 +60,19 @@ public class MyFileSinkGraphML extends FileSinkGraphML{
 
 			for (Node n : g.getEachNode()) {
 				for (String k : n.getAttributeKeySet()) {
+					//AttributeFiltering
+					if(k.equals("ui.j2dsk") || k.equals("ui.class") || k.equals("ui.pie-values")){
+						continue;
+						}
+					Class<? extends Object> c = n.getAttribute(k).getClass();
+					if (!c.isPrimitive() && !(c == String.class) && !(c == Character.class) && !(c == Boolean.class)
+							&& !(c == Integer.class) && !(c == Long.class) && !(c == Short.class) && !(c == Byte.class)
+							&& !(c == Float.class) && !(c == Double.class)) {
+						Debug.out("Could not parse an Attribute because it is not Primitive or a String \n\t"
+								+ "(Attribute: " + k + ", Value: " + n.getAttribute(k) + ", from Node: " + n + ", Type: "
+								+ c + ") ", 2);
+					}
+					
 					if (!nodeAttributes.containsKey(k)) {
 						Object value = n.getAttribute(k);
 						String type;
@@ -89,6 +105,19 @@ public class MyFileSinkGraphML extends FileSinkGraphML{
 
 			for (Edge n : g.getEachEdge()) {
 				for (String k : n.getAttributeKeySet()) {
+					//AttributeFiltering
+					if(k.equals("ui.j2dsk")){
+						continue;
+					}
+					Class<? extends Object> c = n.getAttribute(k).getClass();
+					if (!c.isPrimitive() && !(c == String.class) && !(c == Character.class) && !(c == Boolean.class)
+							&& !(c == Integer.class) && !(c == Long.class) && !(c == Short.class) && !(c == Byte.class)
+							&& !(c == Float.class) && !(c == Double.class)) {
+						Debug.out("Could not parse an Attribute because it is not Primitive or a String \n\t"
+								+ "(Attribute: " + k + ", Value: " + n.getAttribute(k) + ", from Edge: " + n + ", Type: "
+								+ c + ") ", 2);
+					}	
+					
 					if (!edgeAttributes.containsKey(k)) {
 						Object value = n.getAttribute(k);
 						String type;
@@ -141,7 +170,9 @@ public class MyFileSinkGraphML extends FileSinkGraphML{
 					print("\t\t\t<data key=\"%s\">%s</data>\n", edgeAttributes
 							.get(k), escapeXmlString(e.getAttribute(k).toString()));
 				}
-				print("\t\t</edge>\n");
+				if(!isWritingMultigraph){
+					print("\t\t</edge>\n");
+				}
 			}
 			print("\t</graph>\n");
 		} catch (IOException e) {
@@ -158,4 +189,28 @@ public class MyFileSinkGraphML extends FileSinkGraphML{
 				.replace("\"", "&quot;")
 				.replace("'", "&apos;");
 	}
+	
+	/**
+	 * 
+	 * @param graphs
+	 */
+	public void exportGraphs(LinkedList<MyGraph> graphs){
+		for(MyGraph g : graphs){
+			if(g.isComposite()){
+				graphs.remove(g);
+			}
+		}
+		//TODO setup
+		isWritingMultigraph = true;
+		for(MyGraph g : graphs){
+			exportGraph(g);
+		}
+		isWritingMultigraph = false;
+		try {
+		print("\t</graph>\n");
+		} catch(IOException e){
+			e.printStackTrace();
+		}
+	}
+	
 }