Browse Source

Finished TaskFulfillmentMetric, small Bugfixes

Jan Enders 8 years ago
parent
commit
3529c6dd79

+ 55 - 0
scopviz/src/main/java/de/tu_darmstadt/informatik/tk/scopviz/graphs/MappingGraphManager.java

@@ -1,6 +1,8 @@
 package de.tu_darmstadt.informatik.tk.scopviz.graphs;
 
+import java.util.Collection;
 import java.util.HashMap;
+import java.util.LinkedList;
 
 import org.graphstream.algorithm.Toolkit;
 import org.graphstream.graph.Edge;
@@ -77,6 +79,7 @@ public class MappingGraphManager extends GraphManager implements EdgeCreatedList
 		graph.addSubGraph(operator.getGraph());
 		mergeGraph(underlay, UNDERLAY, UNDERLAYER_MOVE_Y);
 		mergeGraph(operator, OPERATOR, OPERATOR_MOVE_Y);
+		autoMapSourcesAndSinks(underlay, operator);
 
 		view.getCamera().resetView();
 	}
@@ -549,4 +552,56 @@ public class MappingGraphManager extends GraphManager implements EdgeCreatedList
 		}
 	}
 
+	private void autoMapSourcesAndSinks(GraphManager underlay, GraphManager operator) {
+		for (Node operatorNode : getOperatorNodeSet()) {
+			if (operatorNode.getAttribute("typeofNode").toString().equals("source")) {
+				for (Node underlayNode : getUnderlayNodeSet()) {
+					if (operatorNode.getAttribute("identifier") != null && operatorNode.getAttribute("identifier")
+							.equals(underlayNode.getAttribute("identifier"))) {
+						String newID = Main.getInstance().getUnusedID(this);
+						Edge e = getGraph().addEdge(newID, operatorNode, underlayNode, true);
+						Debug.out("Created an directed edge with Id " + newID + " from " + operatorNode + " to "
+								+ underlayNode);
+
+						e.addAttribute("ui.class", UI_CLASS_MAPPING);
+						e.addAttribute(ATTRIBUTE_KEY_MAPPING, true);
+					}
+				}
+			} else if (operatorNode.getAttribute("typeofNode").equals("sink")) {
+				for (Node underlayNode : getUnderlayNodeSet()) {
+					String identifier = operatorNode.getAttribute("identifier");
+					if (identifier != null && identifier.equals(underlayNode.getAttribute("identifier"))) {
+						String newID = Main.getInstance().getUnusedID(this);
+						Edge e = getGraph().addEdge(newID, operatorNode, underlayNode, true);
+						Debug.out("Created an directed edge with Id " + newID + " from " + operatorNode + " to "
+								+ underlayNode);
+
+						e.addAttribute("ui.class", UI_CLASS_MAPPING);
+						e.addAttribute(ATTRIBUTE_KEY_MAPPING, true);
+					}
+				}
+			}
+		}
+	}
+
+	private Collection<Node> getOperatorNodeSet() {
+		LinkedList<Node> result = new LinkedList<Node>();
+		for (Node n : getGraph().getNodeSet()) {
+			if (n.getAttribute(ATTRIBUTE_KEY_MAPPING_PARENT) == OPERATOR) {
+				result.add(n);
+			}
+		}
+		return result;
+	}
+
+	private Collection<Node> getUnderlayNodeSet() {
+		LinkedList<Node> result = new LinkedList<Node>();
+		for (Node n : getGraph().getNodeSet()) {
+			if (n.getAttribute(ATTRIBUTE_KEY_MAPPING_PARENT) == UNDERLAY) {
+				result.add(n);
+			}
+		}
+		return result;
+	}
+
 }

+ 4 - 0
scopviz/src/main/java/de/tu_darmstadt/informatik/tk/scopviz/graphs/MyGraph.java

@@ -299,4 +299,8 @@ public class MyGraph extends SingleGraph {
 		}
 		return result;
 	}
+
+	public LinkedList<MyGraph> getChildren() {
+		return children;
+	}
 }

+ 12 - 0
scopviz/src/main/java/de/tu_darmstadt/informatik/tk/scopviz/main/Main.java

@@ -90,6 +90,18 @@ public final class Main {
 		}
 	}
 
+	public String getUnusedID(GraphManager gm) {
+		int i = 0;
+		while (true) {
+			String tempID = i + "";
+			if (gm.getGraph().getNode(tempID) == null && gm.getGraph().getEdge(tempID) == null) {
+				return (tempID);
+			} else {
+				i++;
+			}
+		}
+	}
+
 	/**
 	 * Returns the primary Stage for the Application Window.
 	 * 

+ 8 - 5
scopviz/src/main/java/de/tu_darmstadt/informatik/tk/scopviz/metrics/CommunicationCostMetric.java

@@ -27,10 +27,13 @@ import javafx.util.Pair;
 // TODO: make this work well with multiple operator graphs
 public class CommunicationCostMetric implements ScopvizGraphMetric {
 
-	/** Message to show if not all Operator nodes have been placed onto the underlay */
+	/**
+	 * Message to show if not all Operator nodes have been placed onto the
+	 * underlay
+	 */
 	private static final Pair<String, String> ERROR_MESSAGE = new Pair<String, String>("Warning",
 			"Not all Nodes have a valid Mapping");
-	
+
 	/** Flag for when an error occurs during computation. */
 	// TODO: this is not yet being used for output and never reset
 	private boolean error = false;
@@ -53,8 +56,8 @@ public class CommunicationCostMetric implements ScopvizGraphMetric {
 	@Override
 	public LinkedList<Pair<String, String>> calculate(MyGraph g) {
 		LinkedList<Pair<String, String>> results = new LinkedList<Pair<String, String>>();
-		
-		if(error){
+
+		if (error) {
 			error = false;
 			results.add(ERROR_MESSAGE);
 		}
@@ -79,7 +82,7 @@ public class CommunicationCostMetric implements ScopvizGraphMetric {
 		// want, requires testing
 		double communicationCost = Toolkit.diameter(operator, "cost", true);
 		communicationCost = Math.round(communicationCost * 100) / 100;
-		
+
 		results.add(new Pair<String, String>("Overall Cost", "" + communicationCost));
 
 		return results;

+ 72 - 11
scopviz/src/main/java/de/tu_darmstadt/informatik/tk/scopviz/metrics/TaskFulfillmentMetric.java

@@ -4,6 +4,7 @@ import java.util.LinkedList;
 import java.util.stream.Collectors;
 
 import org.graphstream.graph.Edge;
+import org.graphstream.graph.Node;
 
 import de.tu_darmstadt.informatik.tk.scopviz.graphs.MappingGraphManager;
 import de.tu_darmstadt.informatik.tk.scopviz.graphs.MyGraph;
@@ -11,9 +12,13 @@ import de.tu_darmstadt.informatik.tk.scopviz.main.Layer;
 import de.tu_darmstadt.informatik.tk.scopviz.metrics.interfaces.ScopvizGraphMetric;
 import javafx.util.Pair;
 
-//TODO: TaksFulfillmentMetric not yet implemented due to missing support for graph attributes.
 public class TaskFulfillmentMetric implements ScopvizGraphMetric {
-	
+
+	/** The text to display in case of an error during computation. */
+	private static final Pair<String, String> ERROR_MESSAGE = new Pair<String, String>("Error",
+			"Operator Graph without priority found");
+
+	/** Flag for when an operator graph without a priority is found */
 	private boolean error = false;
 
 	@Override
@@ -34,24 +39,80 @@ public class TaskFulfillmentMetric implements ScopvizGraphMetric {
 	@Override
 	public LinkedList<Pair<String, String>> calculate(MyGraph g) {
 		LinkedList<Pair<String, String>> results = new LinkedList<Pair<String, String>>();
-		
-		
+
+		// This corresponds to the Function F
+		double placedSum = 0;
+		// This corresponds to Fmax
+		double prioritySum = 0;
+
 		if (g.isComposite()) {
 			LinkedList<MyGraph> graphs = g.getAllSubGraphs();
-			for (MyGraph current : graphs) {				
-				if (current.getAttribute("layer") == Layer.OPERATOR){
-					Double priority = current.getAttribute("priority");
-					if (priority == null){
+			for (MyGraph current : graphs) {
+				if (current.getAttribute("layer") == Layer.OPERATOR && !current.isComposite()) {
+					Double priority = Double.valueOf(current.getAttribute("priority"));
+					if (priority == null) {
 						error = true;
+					} else {
+						boolean placed = fullyPlaced(current, g);
+						if (placed) {
+							placedSum += priority;
+						}
+						prioritySum += priority;
 					}
 				}
-//				
 			}
 		}
-		LinkedList<Edge> mappingEdges = new LinkedList<Edge>(g.getEdgeSet().stream()
+		// This corresponds to F'
+		double percentagePlaced = placedSum / prioritySum;
+
+		if (error) {
+			error = false;
+			results.add(ERROR_MESSAGE);
+		}
+
+		results.add(new Pair<String, String>("Task Placement", "" + placedSum));
+		results.add(new Pair<String, String>("Placement Percentage", "" + percentagePlaced));
+
+		return results;
+	}
+
+	/**
+	 * Checks whether an operator Graph is fully mapped onto the underlay.
+	 * 
+	 * @param operator
+	 *            the operator Graph
+	 * @param mapping
+	 *            the mapping Graph
+	 * @return true if all Nodes of the operator Graph have a valid mapping
+	 */
+	private boolean fullyPlaced(MyGraph operator, MyGraph mapping) {
+		boolean result = true;
+		LinkedList<Edge> mappingEdges = new LinkedList<Edge>(mapping.getEdgeSet().stream()
 				.filter(e -> (((Boolean) e.getAttribute(MappingGraphManager.ATTRIBUTE_KEY_MAPPING)) == true))
 				.collect(Collectors.toList()));
-		return results;
+		// build list of the operator nodes within the mapping graph
+		LinkedList<Node> operatorNodes = new LinkedList<Node>();
+		for (Node n : mapping.getNodeSet()) {
+			String originalGraph = n.getAttribute("originalGraph");
+			if ((originalGraph != null && originalGraph.equals(operator.getId()))
+					|| n.getAttribute(MappingGraphManager.ATTRIBUTE_KEY_MAPPING_PARENT_ID).equals(operator.getId())) {
+				operatorNodes.add(n);
+			}
+		}
+		// check if they have a mapping
+		for (Node n : operatorNodes) {
+			boolean isMapped = false;
+			for (Edge e : mappingEdges) {
+				if (e.getNode0().getId().equals(n.getId())) {
+					isMapped = true;
+				}
+			}
+			if (!isMapped) {
+				result = false;
+			}
+		}
+
+		return result;
 	}
 
 }

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

@@ -209,7 +209,9 @@ public final class GraphDisplayManager {
 		} else {
 			v = new GraphManager(GraphHelper.newMerge(false, getGraphManager().getGraph(), g));
 			v.getGraph().addAttribute("layer", currentLayer);
+			g.addAttribute("layer", currentLayer);
 			v.getGraph().addAttribute("ui.antialias");
+			g.addAttribute("ui.antialias");
 			removeAllCurrentGraphs();
 			vList.add(v);
 			ret = count++;

+ 5 - 0
scopviz/src/main/java/de/tu_darmstadt/informatik/tk/scopviz/ui/handlers/MyMouseManager.java

@@ -88,6 +88,7 @@ public class MyMouseManager extends DefaultMouseManager {
 			n = graph.addNode(Main.getInstance().getUnusedID());
 			n.setAttribute("xyz", cursorPos);
 			n.setAttribute("ui.class", "standard");
+			n.setAttribute("typeofNode", "standard");
 			graphManager.selectNode(n.getId());
 			Debug.out("Added Node with ID " + n.getId() + " at Position (" + cursorPos.x + "/" + cursorPos.y + ")");
 
@@ -97,6 +98,7 @@ public class MyMouseManager extends DefaultMouseManager {
 			n = graph.addNode(Main.getInstance().getUnusedID());
 			n.setAttribute("xyz", cursorPos);
 			n.setAttribute("ui.class", "source");
+			n.setAttribute("typeofNode", "source");
 			graphManager.selectNode(n.getId());
 			Debug.out("Added Source Node with ID " + n.getId() + " at Position (" + cursorPos.x + "/" + cursorPos.y
 					+ ")");
@@ -107,6 +109,7 @@ public class MyMouseManager extends DefaultMouseManager {
 			n = graph.addNode(Main.getInstance().getUnusedID());
 			n.setAttribute("xyz", cursorPos);
 			n.setAttribute("ui.class", "sink");
+			n.setAttribute("typeofNode", "sink");
 			graphManager.selectNode(n.getId());
 			Debug.out(
 					"Added Sink Node with ID " + n.getId() + " at Position (" + cursorPos.x + "/" + cursorPos.y + ")");
@@ -117,6 +120,7 @@ public class MyMouseManager extends DefaultMouseManager {
 			n = graph.addNode(Main.getInstance().getUnusedID());
 			n.setAttribute("xyz", cursorPos);
 			n.setAttribute("ui.class", "procEn");
+			n.setAttribute("typeofNode", "procEn");
 			graphManager.selectNode(n.getId());
 			Debug.out("Added ProcEn Node with ID " + n.getId() + " at Position (" + cursorPos.x + "/" + cursorPos.y
 					+ ")");
@@ -127,6 +131,7 @@ public class MyMouseManager extends DefaultMouseManager {
 			n = graph.addNode(Main.getInstance().getUnusedID());
 			n.setAttribute("xyz", cursorPos);
 			n.setAttribute("ui.class", "operator");
+			n.setAttribute("typeofNode", "operator");
 			graphManager.selectNode(n.getId());
 			Debug.out("Added Operator Node with ID " + n.getId() + " at Position (" + cursorPos.x + "/" + cursorPos.y
 					+ ")");