Bladeren bron

Merge remote-tracking branch 'origin/Matthias'

Jan Enders 8 jaren geleden
bovenliggende
commit
beb3f95115

+ 131 - 15
scopviz/src/main/java/de/tu_darmstadt/informatik/tk/scopviz/main/MappingGraphManager.java

@@ -31,13 +31,18 @@ public class MappingGraphManager extends GraphManager implements EdgeCreatedList
 	private static final double UNDERLAYER_MOVE_Y = 0;
 	private static final double OPERATOR_MOVE_Y = 1.5;
 
-	// Variables to keep track of new Nodes in the parent graphs
+	/** Variables to keep track of new Nodes in the underlay graph */
 	private boolean underlayNodesChanged = false;
+
+	/** Variables to keep track of new Nodes in the operator graph */
 	private boolean operatorNodesChanged = false;
 
-	// References to the parent graphs
-	GraphManager underlay, operator;
-	HashMap<String, String> parentsID;;
+	/** References to the underlay graph */
+	GraphManager underlay;
+	/** References to the operator graph */
+	GraphManager operator;
+	/** Map to store the id of the underlay and operator graphs IDs */
+	HashMap<String, String> parentsID;
 
 	/**
 	 * Creates a new manager for an empty graph. there is no need to check for
@@ -71,6 +76,27 @@ public class MappingGraphManager extends GraphManager implements EdgeCreatedList
 		view.getCamera().resetView();
 	}
 
+	/**
+	 * 
+	 * @param g
+	 */
+	public void loadGraph(MyGraph g) {
+
+		// reset used capacity to 0 for every procEnabled node
+		for (Node n : this.g.getNodeSet()) {
+			if (hasClass(n, UI_CLASS_PROCESSING_ENABLED)) {
+				resetCapacity(n);
+			}
+		}
+
+		// recreates every mapping edge to properly calculate capacities
+		for (Edge e : g.getEdgeSet()) {
+			if ((boolean) e.getAttribute(ATTRIBUTE_KEY_MAPPING)) {
+				createEdge(e.getSourceNode().getId(), e.getTargetNode().getId());
+			}
+		}
+	}
+
 	/**
 	 * Adds all nodes and edges of the given graph, adds a prefix to the ID of
 	 * every node and edge and offsets the normalized coordinates in y
@@ -86,13 +112,13 @@ public class MappingGraphManager extends GraphManager implements EdgeCreatedList
 	private void mergeGraph(GraphManager gm, String idPrefix, double moveY) {
 		mergeNodes(gm, idPrefix, moveY);
 
-		// TODO: Debug only
+		// Debug only
 		int i = 0;
 
 		for (Edge edge : gm.getGraph().getEdgeSet()) {
 			addEdge(edge, idPrefix);
 
-			// TODO: Debug only
+			// Debug only
 			i++;
 		}
 
@@ -123,7 +149,7 @@ public class MappingGraphManager extends GraphManager implements EdgeCreatedList
 		double scaleY = 1 / (maxY - minY);
 		double addY = -minY * scaleY + moveY;
 
-		// TODO: Debug only
+		// Debug only
 		int i = 0;
 
 		// loops through all nodes, adds them if they don't exist already and
@@ -135,7 +161,7 @@ public class MappingGraphManager extends GraphManager implements EdgeCreatedList
 				addNode(node, idPrefix);
 				newNode = getGraph().getNode(idPrefix + node.getId());
 
-				// TODO: Debug only
+				// Debug only
 				i++;
 			}
 
@@ -317,7 +343,7 @@ public class MappingGraphManager extends GraphManager implements EdgeCreatedList
 			return false;
 
 		// check and update capacity
-		if (!updatedCapacity(underlayNode, operatorNode))
+		if (!addMapping(underlayNode, operatorNode))
 			return false;
 
 		e = getGraph().addEdge(newID, operatorNode, underlayNode, true);
@@ -330,6 +356,14 @@ public class MappingGraphManager extends GraphManager implements EdgeCreatedList
 		return true;
 	}
 
+	/**
+	 * Initialize the pie chart for the given node. It uses the process_use and
+	 * process_max values of the given node. If process_max is null or 0 it
+	 * won't do anything. If process_use is null it will be initialized to 0.
+	 * 
+	 * @param underlayNode
+	 *            The Node for which the pie chart should be initialized
+	 */
 	private void initCapacity(Node underlayNode) {
 		Double used = underlayNode.getAttribute(ATTRIBUTE_KEY_PROCESS_USE);
 		Double max = underlayNode.getAttribute(ATTRIBUTE_KEY_PROCESS_MAX);
@@ -342,18 +376,77 @@ public class MappingGraphManager extends GraphManager implements EdgeCreatedList
 		underlayNode.setAttribute(ATTRIBUTE_KEY_PROCESS_USE, used);
 	}
 
-	private boolean updatedCapacity(Node underlayNode, Node operatorNode) {
-		Double needed = operatorNode.getAttribute(ATTRIBUTE_KEY_PROCESS_NEED);
-		Double used = underlayNode.getAttribute(ATTRIBUTE_KEY_PROCESS_USE);
+	/**
+	 * Resets the pie chart for the given node. If process_max is null or 0 it
+	 * won't display anything. Process_use set to 0.
+	 * 
+	 * @param underlayNode
+	 *            The Node for which the pie chart should be initialized
+	 */
+	private void resetCapacity(Node underlayNode) {
+		Double used = new Double(0);
+		underlayNode.setAttribute(ATTRIBUTE_KEY_PROCESS_USE, used);
 		Double max = underlayNode.getAttribute(ATTRIBUTE_KEY_PROCESS_MAX);
+		if (max == null || max == 0)
+			return;
+		double[] pieValues = { 0, 0, 1 };
+		underlayNode.setAttribute("ui.pie-values", pieValues);
+	}
+
+	/**
+	 * Checks and updates the Capacity for a procEn node. Tries to map the given
+	 * operatorNode to the given underlayNode.
+	 * 
+	 * @param underlayNode
+	 *            The underlayNode the operatorNode gets mapped to
+	 * @param operatorNode
+	 *            The operatorNode which gets mapped
+	 * @return true if the mapping was successful. false otherwise.
+	 */
+	private boolean addMapping(Node underlayNode, Node operatorNode) {
+		Double needed = operatorNode.getAttribute(ATTRIBUTE_KEY_PROCESS_NEED);
+		if (needed == null)
+			return true;
+		return changeCapacity(underlayNode, needed);
+	}
 
+	/**
+	 * Checks and updates the Capacity for a procEn node. Tries to unmap the
+	 * given operatorNode to the given underlayNode.
+	 * 
+	 * @param underlayNode
+	 *            The underlayNode the operatorNode gets mapped to
+	 * @param operatorNode
+	 *            The operatorNode which gets mapped
+	 * @return true if the mapping was successful. false otherwise.
+	 */
+	private boolean removeMapping(Node underlayNode, Node operatorNode) {
+		Double needed = operatorNode.getAttribute(ATTRIBUTE_KEY_PROCESS_NEED);
 		if (needed == null)
 			return true;
+		return changeCapacity(underlayNode, -needed);
+	}
+
+	/**
+	 * Checks and updates the Capacity for a procEn node. Tries to map the
+	 * capacity to the given underlayNode.
+	 * 
+	 * @param underlayNode
+	 *            The underlayNode which capacity gets updated
+	 * @param capacity
+	 *            The capacity. may be positive or negative
+	 * @return true if the capacity change was successful. false otherwise.
+	 */
+	private boolean changeCapacity(Node underlayNode, double capacity) {
+		Double needed = capacity;
+		Double used = underlayNode.getAttribute(ATTRIBUTE_KEY_PROCESS_USE);
+		Double max = underlayNode.getAttribute(ATTRIBUTE_KEY_PROCESS_MAX);
+		if (needed == 0)
+			return true;
+
 		if (max == null || max == 0)
 			if (needed > 0)
 				return false;
-			else
-				return true;
 		if (used == null)
 			used = new Double(0);
 		if (used + needed > max)
@@ -365,6 +458,14 @@ public class MappingGraphManager extends GraphManager implements EdgeCreatedList
 		return true;
 	}
 
+	/**
+	 * Displays the capacity change to the node if the needed Cost is applied.
+	 * 
+	 * @param underlayNode
+	 *            The node which gets updated
+	 * @param need
+	 *            the capacity change
+	 */
 	private void showExpectedCapacity(Node underlayNode, double need) {
 		Double used = underlayNode.getAttribute(ATTRIBUTE_KEY_PROCESS_USE);
 		Double max = underlayNode.getAttribute(ATTRIBUTE_KEY_PROCESS_MAX);
@@ -399,7 +500,22 @@ public class MappingGraphManager extends GraphManager implements EdgeCreatedList
 	}
 
 	public void deleteEdge(final String id) {
-		super.deleteEdge(id);
+		Edge e = g.getEdge(id);
+		if ((boolean) e.getAttribute(ATTRIBUTE_KEY_MAPPING)) {
+			Node operatorNode = e.getSourceNode();
+			Node underlayNode = e.getTargetNode();
+			removeMapping(underlayNode, operatorNode);
+			super.deleteEdge(id);
+		}
+	}
+
+	@Override
+	public void undelete() {
+		super.undelete();
+		for (Edge e : deletedEdges) {
+			if ((boolean) e.getAttribute(ATTRIBUTE_KEY_MAPPING))
+				changeCapacity(e.getTargetNode(), e.getSourceNode().getAttribute(ATTRIBUTE_KEY_PROCESS_NEED));
+		}
 	}
 
 }

+ 90 - 0
scopviz/src/main/java/de/tu_darmstadt/informatik/tk/scopviz/ui/GraphDisplayManager.java

@@ -5,6 +5,8 @@ import java.net.URL;
 import java.util.ArrayList;
 
 import org.apache.commons.math3.exception.NullArgumentException;
+import org.graphstream.graph.Edge;
+import org.graphstream.graph.Node;
 
 import de.tu_darmstadt.informatik.tk.scopviz.debug.Debug;
 import de.tu_darmstadt.informatik.tk.scopviz.io.GraphMLImporter;
@@ -346,4 +348,92 @@ public final class GraphDisplayManager {
 
 	};
 
+	/**
+	 * reads a Mapping Graph and sets the underlay, operator and mapping layers
+	 * accordingly
+	 */
+	public static void readMapping2() {
+		GraphMLImporter reader = new GraphMLImporter();
+		MyGraph g = reader.readGraph(getGraphStringID(count), Main.getInstance().getPrimaryStage());
+		Layer tempLayer = currentLayer;
+
+		// underlay Graph
+		MyGraph tempGraph = new MyGraph(getGraphStringID(count));
+		count++;
+		for (Node n : g.getNodeSet()) {
+			String id = n.getId();
+			if (id.startsWith(MappingGraphManager.UNDERLAY)) {
+				id = id.substring(MappingGraphManager.UNDERLAY.length());
+				Node tempNode = tempGraph.addNode(id);
+				for (String s : n.getAttributeKeySet()) {
+					Debug.out(s + ":" + n.getAttribute(s).toString());
+					tempNode.addAttribute(s, (Object) n.getAttribute(s));
+				}
+			}
+		}
+		for (Edge e : g.getEdgeSet()) {
+			String id = e.getId();
+			if (id.startsWith(MappingGraphManager.UNDERLAY)) {
+				id = id.substring(MappingGraphManager.UNDERLAY.length());
+				Edge tempEdge = tempGraph.addEdge(id,
+						e.getSourceNode().getId().substring(MappingGraphManager.UNDERLAY.length()),
+						e.getTargetNode().getId().substring(MappingGraphManager.UNDERLAY.length()), e.isDirected());
+				for (String s : e.getAttributeKeySet()) {
+					tempEdge.addAttribute(s, (Object) e.getAttribute(s));
+				}
+			}
+		}
+		// TODO get Graphmanager;
+		currentLayer = Layer.UNDERLAY;
+		addGraph(tempGraph, true);
+		GraphManager und = getGraphManager(Layer.UNDERLAY);
+		// operator graph
+		tempGraph = new MyGraph(getGraphStringID(count));
+		count++;
+		for (Node n : g.getNodeSet()) {
+			String id = n.getId();
+			if (id.startsWith(MappingGraphManager.OPERATOR)) {
+				id = id.substring(MappingGraphManager.OPERATOR.length());
+				Node tempNode = tempGraph.addNode(id);
+				for (String s : n.getAttributeKeySet()) {
+					Debug.out(s + ":" + n.getAttribute(s).toString());
+					tempNode.addAttribute(s, (Object) n.getAttribute(s));
+				}
+			}
+		}
+		for (Edge e : g.getEdgeSet()) {
+			String id = e.getId();
+			if (id.startsWith(MappingGraphManager.OPERATOR)) {
+				id = id.substring(MappingGraphManager.OPERATOR.length());
+				Edge tempEdge = tempGraph.addEdge(id,
+						e.getSourceNode().getId().substring(MappingGraphManager.OPERATOR.length()),
+						e.getTargetNode().getId().substring(MappingGraphManager.OPERATOR.length()), e.isDirected());
+				for (String s : e.getAttributeKeySet()) {
+					tempEdge.addAttribute(s, (Object) e.getAttribute(s));
+				}
+			}
+		}
+		currentLayer = Layer.OPERATOR;
+		addGraph(tempGraph, true);
+		GraphManager op = getGraphManager(Layer.OPERATOR);
+		// Mapping graph
+		MyGraph moreTempGraph = new MyGraph(getGraphStringID(count));
+		moreTempGraph.addAttribute("layer", Layer.MAPPING);
+		MappingGraphManager map = new MappingGraphManager(moreTempGraph, und, op);
+		count++;
+		g.addAttribute("layer", Layer.MAPPING);
+		g.addAttribute("ui.antialias");
+		map.setStylesheet(StylesheetManager.DEFAULT_STYLESHEET);
+		currentLayer = Layer.MAPPING;
+		removeAllCurrentGraphs();
+		vList.add(map);
+		und.addEdgeCreatedListener(map);
+		und.addNodeCreatedListener(map);
+		op.addEdgeCreatedListener(map);
+		op.addNodeCreatedListener(map);
+		// TODO wait for implementation
+		map.loadGraph(g);
+		currentLayer = tempLayer;
+	}
+
 }

+ 26 - 0
scopviz/src/main/java/de/tu_darmstadt/informatik/tk/scopviz/ui/css/CSSCondition.java

@@ -0,0 +1,26 @@
+package de.tu_darmstadt.informatik.tk.scopviz.ui.css;
+
+import java.util.HashSet;
+import java.util.Iterator;
+
+class CSSCondition {
+	String type;
+	HashSet<String> classes = new HashSet<String>();
+
+	public CSSCondition(String type, HashSet<String> classes) {
+		this.type = type;
+		this.classes = classes;
+	}
+
+	int ConditionsMetBy(CSSable suspect) {
+		if (type != null && !type.equals(suspect.getType()))
+			return 0;
+
+		Iterator<String> i = classes.iterator();
+		while (i.hasNext()) {
+			if (!suspect.getClasses().contains(i.next()))
+				return 0;
+		}
+		return classes.size();
+	}
+}

+ 54 - 0
scopviz/src/main/java/de/tu_darmstadt/informatik/tk/scopviz/ui/css/CSSManager.java

@@ -0,0 +1,54 @@
+package de.tu_darmstadt.informatik.tk.scopviz.ui.css;
+
+import java.util.HashSet;
+
+import de.tu_darmstadt.informatik.tk.scopviz.debug.Debug;
+
+public class CSSManager {
+	/**
+	 * Du zerstörst diesen REGEX und Matthias zerstört dich
+	 */
+	private static final String CSS_MATCH_REGEX = "(\\s*([A-Za-z]+|[A-Za-z]*(\\.[A-Za-z_-]*)+)\\s*\\{\\s*[A-Za-z_-]+\\s*\\:\\s*[A-Za-z\\(\\)_\\#\\'\\\"-]+\\s*\\;?\\s*\\})+\\s*";
+
+	static HashSet<CSSRule> rules = new HashSet<CSSRule>();
+
+	public static void addRule(String rule) {
+		if (!rule.matches(CSS_MATCH_REGEX)) {
+			Debug.out("rule <<" + rule + ">> doesn't match regex");
+			return;
+		}
+
+		HashSet<CSSCondition> conditions = new HashSet<>();
+		String[] ruleSplit = rule.split("{");
+		int i = 0;
+		String front = ruleSplit[0].trim();
+		while (i < ruleSplit.length - 1) {
+			String type = "";
+			HashSet<String> classes = new HashSet<String>();
+			if (front.contains(".")) {
+				String[] dots = front.split(".");
+				int j = 1;
+				if (front.startsWith(".")) {
+					j = 0;
+				} else {
+					type = dots[0];
+				}
+				while (j < dots.length) {
+					classes.add(dots[i]);
+				}
+			} else {
+				type = front;
+			}
+			conditions.add(new CSSCondition(type, classes));
+			i++;
+		}
+		
+		//TODO , split einfügen
+
+		String css = null;
+
+		CSSRule e = new CSSRule(conditions, css);
+
+		rules.add(e);
+	}
+}

+ 32 - 0
scopviz/src/main/java/de/tu_darmstadt/informatik/tk/scopviz/ui/css/CSSRule.java

@@ -0,0 +1,32 @@
+package de.tu_darmstadt.informatik.tk.scopviz.ui.css;
+
+import java.util.HashSet;
+import java.util.Iterator;
+
+class CSSRule {
+	HashSet<CSSCondition> conditions = new HashSet<CSSCondition>();
+	String css;
+
+	public CSSRule(HashSet<CSSCondition> conditions, String css) {
+		super();
+		this.conditions = conditions;
+		this.css = css;
+	}
+
+	int ConditionsMetBy(CSSable suspect) {
+		int result = 0;
+		Iterator<CSSCondition> i = conditions.iterator();
+		while (i.hasNext()) {
+			CSSCondition condition = i.next();
+			int r = condition.ConditionsMetBy(suspect);
+			if (r > result)
+				result = r;
+		}
+
+		return result;
+	}
+
+	String getCSS() {
+		return css;
+	}
+}

+ 46 - 0
scopviz/src/main/java/de/tu_darmstadt/informatik/tk/scopviz/ui/css/CSSable.java

@@ -0,0 +1,46 @@
+package de.tu_darmstadt.informatik.tk.scopviz.ui.css;
+
+import java.util.Set;
+
+public interface CSSable {
+	/**
+	 * Adds a CSS class to the object. classes already added are ignored
+	 * silently. multiple classes can be separated by a '.' or ' '.
+	 * 
+	 * @param c
+	 *            the class to add
+	 */
+	public void addCSSClass(String c);
+
+	/**
+	 * Removes a CSS class from the object. classes not part of the object are
+	 * ignored silently. multiple classes can be separated by a '.' or ' '.
+	 * 
+	 * @param c
+	 *            the class to add
+	 */
+	public void removeCSSClass(String c);
+
+	/**
+	 * 
+	 * @return a Set of Strings containing all the previously added CSS classes
+	 */
+	public Set<String> getClasses();
+
+	/**
+	 * 
+	 * @return the Type of the CSS Object
+	 */
+	public String getType();
+
+	/**
+	 * Updates the stored CSS String
+	 */
+	public void updateCSS();
+
+	/**
+	 * 
+	 * @return the stored CSS String
+	 */
+	public String getCSS();
+}