Jelajahi Sumber

Rudimentary Mapping Functions

MW 8 tahun lalu
induk
melakukan
f9f6053ab0

+ 11 - 0
scopviz/src/main/java/de/tu_darmstadt/informatik/tk/scopviz/debug/Debug.java

@@ -31,6 +31,17 @@ public class Debug {
 		return fileName;
 	}
 
+	/**
+	 * Returns the Location of the File for the testing Graph.
+	 * 
+	 * @return a sample graph for the Program
+	 */
+	public static String getDefaultGraph2() {
+		String fileName = null;
+		fileName = "/Example2.graphml";
+		return fileName;
+	}
+
 	/**
 	 * Short form for System.out.println().
 	 * 

+ 6 - 7
scopviz/src/main/java/de/tu_darmstadt/informatik/tk/scopviz/io/GraphMLImporter.java

@@ -4,11 +4,10 @@ import java.io.IOException;
 import java.net.URL;
 import java.util.LinkedList;
 
-import org.graphstream.graph.Graph;
-import org.graphstream.graph.implementations.DefaultGraph;
 import org.graphstream.graph.implementations.SingleGraph;
 
 import de.tu_darmstadt.informatik.tk.scopviz.main.Main;
+import de.tu_darmstadt.informatik.tk.scopviz.main.MyGraph;
 import javafx.stage.FileChooser;
 import javafx.stage.Stage;
 
@@ -31,8 +30,8 @@ public class GraphMLImporter {
 	 *            path to the file on disk
 	 * @return the imported Graphstream-Graph
 	 */
-	public Graph readGraph(String id, final String fileName) {
-		Graph g = new DefaultGraph(id);
+	public MyGraph readGraph(String id, final String fileName) {
+		MyGraph g = new MyGraph(id);
 		fs.addSink(g);
 		try {
 			fs.readAll(fileName);
@@ -52,7 +51,7 @@ public class GraphMLImporter {
 	 *            the parent window of the open file window
 	 * @return the imported Graphstream-Graph
 	 */
-	public Graph readGraph(final String id, final Stage stage) {
+	public MyGraph readGraph(final String id, final Stage stage) {
 		FileChooser fileChooser = new FileChooser();
 		fileChooser.setTitle("open graph");
 		try {
@@ -74,8 +73,8 @@ public class GraphMLImporter {
 	 * @return the imported Graphstream-Graph
 	 */
 	// TODO backup reader/Exception handling
-	public Graph readGraph(String id, final URL fileURL) {
-		Graph g = new DefaultGraph(id);
+	public MyGraph readGraph(String id, final URL fileURL) {
+		MyGraph g = new MyGraph(id);
 		fs.addSink(g);
 		try {
 			fs.readAll(fileURL);

+ 1 - 1
scopviz/src/main/java/de/tu_darmstadt/informatik/tk/scopviz/main/EdgeCreatedListener.java

@@ -4,5 +4,5 @@ import org.graphstream.graph.Edge;
 
 public interface EdgeCreatedListener {
 
-	public void edgeCreated(Edge e);
+	public void edgeCreated(Edge e, String graphID);
 }

+ 4 - 13
scopviz/src/main/java/de/tu_darmstadt/informatik/tk/scopviz/main/GraphManager.java

@@ -13,7 +13,6 @@ import org.graphstream.ui.swingViewer.ViewPanel;
 import org.graphstream.ui.view.Viewer;
 import org.graphstream.ui.view.ViewerPipe;
 
-import de.tu_darmstadt.informatik.tk.scopviz.debug.Debug;
 import de.tu_darmstadt.informatik.tk.scopviz.ui.OptionsManager;
 import de.tu_darmstadt.informatik.tk.scopviz.ui.PropertiesManager;
 import de.tu_darmstadt.informatik.tk.scopviz.ui.handlers.MyMouseManager;
@@ -30,7 +29,7 @@ import de.tu_darmstadt.informatik.tk.scopviz.ui.handlers.MyMouseManager;
 public class GraphManager {
 
 	/** The Graph this instance of GraphManager manages. */
-	protected Graph g;
+	protected MyGraph g;
 
 	/**
 	 * The Stylesheet for this Graph, excluding parts that can be set by
@@ -68,7 +67,7 @@ public class GraphManager {
 	 * @param graph
 	 *            the graph this visualizer should handle
 	 */
-	public GraphManager(Graph graph) {
+	public GraphManager(MyGraph graph) {
 		g = graph;
 		/* Viewer */ viewer = new Viewer(g, Viewer.ThreadingModel.GRAPH_IN_ANOTHER_THREAD);
 		view = viewer.addDefaultView(false);
@@ -385,9 +384,7 @@ public class GraphManager {
 			if (n.hasAttribute("x") && currentMin > (Double) n.getAttribute("x")) {
 				currentMin = (Double) n.getAttribute("x");
 			}
-			Debug.out(Double.toString(currentMin));
 		}
-		Debug.out(Double.toString(currentMin));
 		return currentMin;
 	}
 
@@ -397,7 +394,7 @@ public class GraphManager {
 	 * @return the biggest X Coordinate in the Graph
 	 */
 	public double getMaxX() {
-		double currentMax = Double.MAX_VALUE;
+		double currentMax = Double.MIN_VALUE;
 		Node n = null;
 		Iterator<Node> allNodes = g.getNodeIterator();
 
@@ -406,9 +403,7 @@ public class GraphManager {
 			if (n.hasAttribute("x") && currentMax < (Double) n.getAttribute("x")) {
 				currentMax = (Double) n.getAttribute("x");
 			}
-			Debug.out(Double.toString(currentMax));
 		}
-		Debug.out(Double.toString(currentMax));
 		return currentMax;
 	}
 
@@ -427,9 +422,7 @@ public class GraphManager {
 			if (n.hasAttribute("y") && currentMin > (Double) n.getAttribute("y")) {
 				currentMin = (Double) n.getAttribute("y");
 			}
-			Debug.out(Double.toString(currentMin));
 		}
-		Debug.out(Double.toString(currentMin));
 		return currentMin;
 	}
 
@@ -439,7 +432,7 @@ public class GraphManager {
 	 * @return the biggest Y Coordinate in the Graph
 	 */
 	public double getMaxY() {
-		double currentMax = Double.MAX_VALUE;
+		double currentMax = Double.MIN_VALUE;
 		Node n = null;
 		Iterator<Node> allNodes = g.getNodeIterator();
 
@@ -448,9 +441,7 @@ public class GraphManager {
 			if (n.hasAttribute("y") && currentMax < (Double) n.getAttribute("y")) {
 				currentMax = (Double) n.getAttribute("y");
 			}
-			Debug.out(Double.toString(currentMax));
 		}
-		Debug.out(Double.toString(currentMax));
 		return currentMax;
 	}
 

+ 3 - 0
scopviz/src/main/java/de/tu_darmstadt/informatik/tk/scopviz/main/MainApp.java

@@ -66,6 +66,9 @@ public class MainApp extends Application {
 		Main.getInstance().setPrimaryStage(this.primaryStage);
 		initRootLayout();
 		if (Debug.DEBUG_ENABLED) {
+			GraphDisplayManager.setCurrentLayer(Layer.OPERATOR);
+			GraphDisplayManager.addGraph(Debug.getDefaultGraph2(), true);
+			GraphDisplayManager.setCurrentLayer(Layer.UNDERLAY);
 			GraphDisplayManager.addGraph(Debug.getDefaultGraph(), true);
 		}
 	}

+ 78 - 14
scopviz/src/main/java/de/tu_darmstadt/informatik/tk/scopviz/main/MappingGraphManager.java

@@ -2,33 +2,79 @@ package de.tu_darmstadt.informatik.tk.scopviz.main;
 
 import java.util.HashMap;
 
+import org.graphstream.algorithm.Toolkit;
 import org.graphstream.graph.Edge;
-import org.graphstream.graph.Graph;
 import org.graphstream.graph.Node;
 
 import de.tu_darmstadt.informatik.tk.scopviz.debug.Debug;
 
-public class MappingGraphManager extends GraphManager {
+public class MappingGraphManager extends GraphManager implements EdgeCreatedListener, NodeCreatedListener {
 	public static final String UNDERLAYER_PREFIX = "underlay";
 	public static final String OPERATOR_PREFIX = "operator";
+	private static final double UNDERLAYER_MOVE_Y = 0;
+	private static final double OPERATOR_MOVE_Y = 1.5;
+	private boolean underlayNodesChanged = false;
+	private boolean operatorNodesChanged = false;
 
 	GraphManager underlay, operator;
 
-	public MappingGraphManager(Graph graph, GraphManager underlay, GraphManager operator) {
+	public MappingGraphManager(MyGraph graph, GraphManager underlay, GraphManager operator) {
 		super(graph);
 		this.underlay = underlay;
 		this.operator = operator;
+		Debug.out("Created a new Mapping");
+		mergeGraph(underlay, UNDERLAYER_PREFIX, UNDERLAYER_MOVE_Y);
+		mergeGraph(operator, OPERATOR_PREFIX, OPERATOR_MOVE_Y);
+		view.getCamera().resetView();
+	}
+
+	private void mergeGraph(GraphManager gm, String idPrefix, double moveY) {
 
-		mergeGraph(underlay, UNDERLAYER_PREFIX);
-		mergeGraph(operator, OPERATOR_PREFIX);
+		mergeNodes(gm, idPrefix, moveY);
+		int i = 0;
+		for (Edge edge : gm.getGraph().getEdgeSet()) {
+			addEdge(edge, idPrefix);
+			i++;
+		}
+		Debug.out("added " + i + " Edge" + (i == 1 ? "" : "s") + " from \"" + idPrefix + "\"");
 	}
 
-	private void mergeGraph(GraphManager gm, String idPrefix) {
+	private void mergeNodes(GraphManager gm, String idPrefix, double moveY) {
+		int i = 0;
+		double maxX = gm.getMaxX();
+		double minX = gm.getMinX();
+		double scaleX = 1 / (maxX - minX);
+		double addX = -minX * scaleX;
+		double maxY = gm.getMaxY();
+		double minY = gm.getMinY();
+		double scaleY = 1 / (maxY - minY);
+		double addY = -minY * scaleY;
 		for (Node node : gm.getGraph().getNodeSet()) {
-			addNode(node, idPrefix);
+			Node newNode = getGraph().getNode(idPrefix + node.getId());
+			if (newNode == null) {
+				addNode(node, idPrefix);
+				newNode = getGraph().getNode(idPrefix + node.getId());
+				i++;
+			}
+			double[] n = Toolkit.nodePosition(node);
+			double cX = n[0];
+			double x = cX * scaleX + addX;
+			double cY = n[1];
+			double y = cY * scaleY + addY + moveY;
+			newNode.changeAttribute("x", x);
+			newNode.changeAttribute("y", y);
 		}
-		for (Edge edge : gm.getGraph().getEdgeSet()) {
-			addEdge(edge, idPrefix);
+		Debug.out("added " + i + " Node" + (i == 1 ? "" : "s") + " from \"" + idPrefix + "\"");
+	}
+
+	public void activated() {
+		if (underlayNodesChanged) {
+			mergeNodes(underlay, UNDERLAYER_PREFIX, UNDERLAYER_MOVE_Y);
+			underlayNodesChanged = false;
+		}
+		if (operatorNodesChanged) {
+			mergeNodes(operator, OPERATOR_PREFIX, OPERATOR_MOVE_Y);
+			operatorNodesChanged = false;
 		}
 	}
 
@@ -53,12 +99,12 @@ public class MappingGraphManager extends GraphManager {
 			attributes.put(s, e.getAttribute(s));
 		}
 		g.addEdge(idPrefix + e.getId(), idPrefix + e.getSourceNode().getId(), idPrefix + e.getTargetNode().getId());
-		g.getEdge(e.getId()).addAttributes(attributes);
+		g.getEdge(idPrefix + e.getId()).addAttributes(attributes);
 	}
-	
+
 	@Override
-	public void addNode(Node n){
-		//This function mustn't be called.
+	public void addNode(Node n) {
+		// This function mustn't be called.
 		Debug.out("Someone called addNode(Node n) with a MappingGraphManager");
 	}
 
@@ -78,6 +124,24 @@ public class MappingGraphManager extends GraphManager {
 			attributes.put(s, n.getAttribute(s));
 		}
 		g.addNode(idPrefix + n.getId());
-		g.getNode(n.getId()).addAttributes(attributes);
+		g.getNode(idPrefix + n.getId()).addAttributes(attributes);
+	}
+
+	@Override
+	public void nodeCreated(Node n, String graphID) {
+		Debug.out("Node " + n + " was added to Graph " + graphID);
+		if (graphID.equals(underlay.getGraph().getId()))
+			underlayNodesChanged = true;
+		else
+			operatorNodesChanged = true;
+	}
+
+	@Override
+	public void edgeCreated(Edge e, String graphID) {
+		Debug.out("Edge " + e + " was added to Graph " + graphID);
+		if (graphID.equals(underlay.getGraph().getId()))
+			addEdge(e, UNDERLAYER_PREFIX);
+		else
+			addEdge(e, OPERATOR_PREFIX);
 	}
 }

+ 2 - 2
scopviz/src/main/java/de/tu_darmstadt/informatik/tk/scopviz/main/MyGraph.java

@@ -89,7 +89,7 @@ public class MyGraph extends SingleGraph {
 	 */
 	private void edgeCreatedNotify(Edge e) {
 		for (EdgeCreatedListener list : allEdgeListeners) {
-			list.edgeCreated(e);
+			list.edgeCreated(e, id);
 		}
 	}
 
@@ -112,7 +112,7 @@ public class MyGraph extends SingleGraph {
 	 */
 	private void nodeCreatedNotify(Node n) {
 		for (NodeCreatedListener list : allNodeListeners) {
-			list.nodeCreated(n);
+			list.nodeCreated(n, id);
 		}
 	}
 

+ 1 - 1
scopviz/src/main/java/de/tu_darmstadt/informatik/tk/scopviz/main/NodeCreatedListener.java

@@ -4,5 +4,5 @@ import org.graphstream.graph.Node;
 
 public interface NodeCreatedListener {
 
-	public void nodeCreated(Node n);
+	public void nodeCreated(Node n, String graphID);
 }

+ 63 - 16
scopviz/src/main/java/de/tu_darmstadt/informatik/tk/scopviz/ui/GraphDisplayManager.java

@@ -5,14 +5,14 @@ import java.net.URL;
 import java.util.ArrayList;
 
 import org.apache.commons.math3.exception.NullArgumentException;
-import org.graphstream.graph.Graph;
-import org.graphstream.graph.implementations.SingleGraph;
 
+import de.tu_darmstadt.informatik.tk.scopviz.debug.Debug;
 import de.tu_darmstadt.informatik.tk.scopviz.io.GraphMLImporter;
 import de.tu_darmstadt.informatik.tk.scopviz.main.CreationMode;
 import de.tu_darmstadt.informatik.tk.scopviz.main.GraphManager;
 import de.tu_darmstadt.informatik.tk.scopviz.main.Layer;
 import de.tu_darmstadt.informatik.tk.scopviz.main.Main;
+import de.tu_darmstadt.informatik.tk.scopviz.main.MappingGraphManager;
 import de.tu_darmstadt.informatik.tk.scopviz.main.MyGraph;
 import javafx.event.EventHandler;
 import javafx.scene.input.ScrollEvent;
@@ -48,7 +48,7 @@ public final class GraphDisplayManager {
 	 * An empty GraphManager to use with Layers not yet filled with another
 	 * GraphManager.
 	 */
-	private final static GraphManager emptyLayer = new GraphManager(new SingleGraph("g"));
+	private final static GraphManager emptyLayer = new GraphManager(new MyGraph("g"));
 
 	/** Importer for loading Graphs from Disk */
 	private static GraphMLImporter importer = new GraphMLImporter();
@@ -68,15 +68,16 @@ public final class GraphDisplayManager {
 	 */
 	public static void init(GUIController guiController) {
 		GraphDisplayManager.guiController = guiController;
-		
-		addGraph();
-	    currentLayer=Layer.OPERATOR;
+
 		addGraph();
-		currentLayer=Layer.MAPPING;
+		currentLayer = Layer.OPERATOR;
+		/*
+		 * addGraph(); currentLayer = Layer.MAPPING;
+		 */
 		addGraph();
-		currentLayer=Layer.SYMBOL;
+		currentLayer = Layer.SYMBOL;
 		addGraph();
-		currentLayer=Layer.UNDERLAY;
+		currentLayer = Layer.UNDERLAY;
 	}
 
 	/**
@@ -86,7 +87,7 @@ public final class GraphDisplayManager {
 	 */
 	public static int addGraph() {
 		String id = getGraphStringID(count);
-		Graph g = new MyGraph(id);
+		MyGraph g = new MyGraph(id);
 		return addGraph(g, true);
 	}
 
@@ -103,7 +104,7 @@ public final class GraphDisplayManager {
 	 */
 	public static int addGraph(String fileName, boolean replaceCurrent) {
 		String id = getGraphStringID(count);
-		Graph g = importer.readGraph(id, Main.class.getResource(fileName));
+		MyGraph g = importer.readGraph(id, Main.class.getResource(fileName));
 		return addGraph(g, replaceCurrent);
 	}
 
@@ -119,7 +120,7 @@ public final class GraphDisplayManager {
 	 */
 	public static int addGraph(Stage stage, boolean replaceCurrent) {
 		String id = getGraphStringID(count);
-		Graph g = importer.readGraph(id, stage);
+		MyGraph g = importer.readGraph(id, stage);
 		if (g == null) {
 			return currentGraphManager;
 		}
@@ -138,7 +139,7 @@ public final class GraphDisplayManager {
 	 */
 	public static int addGraph(URL fileURL, boolean currentLayer) {
 		String id = getGraphStringID(count);
-		Graph g = importer.readGraph(id, fileURL);
+		MyGraph g = importer.readGraph(id, fileURL);
 		return addGraph(g, currentLayer);
 	}
 
@@ -153,7 +154,7 @@ public final class GraphDisplayManager {
 	 *            the current layer, if false they will be merged.
 	 * @return the id to access the specific graph
 	 */
-	public static int addGraph(Graph g, boolean replaceCurrent) {
+	public static int addGraph(MyGraph g, boolean replaceCurrent) {
 		if (g == null) {
 			throw new NullArgumentException();
 		}
@@ -185,12 +186,15 @@ public final class GraphDisplayManager {
 	 * Removes all GraphManagers from the current Layer.
 	 */
 	private static void removeAllCurrentGraphs() {
-		// TODO weird multithread behavior
+		// TODO weird multithread behavior; Explain to Matthias why we need that
+		// function
 		for (int i = 0; i < vList.size(); i++) {
 			GraphManager man = vList.get(i);
 			if (man.getGraph().getAttribute("layer").equals(currentLayer)) {
 				vList.remove(i);
-				count--;
+				// TODO hat Matthias auskommentiert, da es iding kaputt macht
+				// und keinen Mehrwert bringt, bitte prüfen
+				// count--;
 			}
 		}
 	}
@@ -264,9 +268,52 @@ public final class GraphDisplayManager {
 	 *            the layer to switch to
 	 */
 	public static void setCurrentLayer(Layer currentLayer) {
+		if (currentLayer.equals(Layer.MAPPING)) {
+			initMappingLayer();
+		}
 		GraphDisplayManager.currentLayer = currentLayer;
 	}
 
+	private static void initMappingLayer() {
+		GraphManager underlay = null, operator = null;
+		MappingGraphManager mapping = null;
+		for (GraphManager man : vList) {
+			if (man.getGraph().getAttribute("layer").equals(Layer.UNDERLAY))
+				underlay = man;
+			else if (man.getGraph().getAttribute("layer").equals(Layer.OPERATOR))
+				operator = man;
+			else if (man.getGraph().getAttribute("layer").equals(Layer.MAPPING)
+					&& MappingGraphManager.class.isInstance(man))
+				mapping = (MappingGraphManager) man;
+		}
+		if (underlay == null) {
+			Debug.out("no Underlay found");
+			return;
+		}
+		if (operator == null) {
+			Debug.out("no Operator found");
+			return;
+		}
+		if (mapping == null || !mapping.hasGraphManagerAsParent(underlay)
+				|| !mapping.hasGraphManagerAsParent(operator)) {
+			Debug.out("no Mapping found");
+			MyGraph g;
+			g = new MyGraph(getGraphStringID(count));
+			count++;
+			mapping = new MappingGraphManager(g, underlay, operator);
+			g.addAttribute("layer", Layer.MAPPING);
+			g.addAttribute("ui.antialias");
+			mapping.setStylesheet(OptionsManager.DEFAULT_STYLESHEET);
+			vList.add(mapping);
+
+			underlay.addEdgeCreatedListener(mapping);
+			underlay.addNodeCreatedListener(mapping);
+			operator.addEdgeCreatedListener(mapping);
+			operator.addNodeCreatedListener(mapping);
+		}
+		mapping.activated();
+	}
+
 	/**
 	 * Handler for Scrolling while the Mouse is over the Graph Display Window.
 	 */

TEMPAT SAMPAH
scopviz/src/main/resources/png/Thumbs.db