TaskFulfillmentMetric.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package de.tu_darmstadt.informatik.tk.scopviz.metrics;
  2. import java.util.LinkedList;
  3. import java.util.stream.Collectors;
  4. import de.tu_darmstadt.informatik.tk.scopviz.graphs.MappingGraphManager;
  5. import de.tu_darmstadt.informatik.tk.scopviz.graphs.MyEdge;
  6. import de.tu_darmstadt.informatik.tk.scopviz.graphs.MyGraph;
  7. import de.tu_darmstadt.informatik.tk.scopviz.graphs.MyNode;
  8. import de.tu_darmstadt.informatik.tk.scopviz.main.Layer;
  9. import de.tu_darmstadt.informatik.tk.scopviz.metrics.interfaces.ScopvizGraphMetric;
  10. import javafx.util.Pair;
  11. /**
  12. * Class to compute the Task Fulfillment Metric. Task Fulfillment is defined by
  13. * the priorities of te fully placed operator Graphs and the percentage,
  14. * weighted by priorites, of placed Operator Graphs to all Operator Graphs.
  15. *
  16. * @author Jan Enders
  17. * @version 1.0
  18. *
  19. */
  20. public class TaskFulfillmentMetric implements ScopvizGraphMetric {
  21. /** The text to display in case of an error during computation. */
  22. private static final Pair<String, String> ERROR_MESSAGE = new Pair<String, String>("Error",
  23. "Operator Graph without priority found");
  24. /** Flag for when an operator graph without a priority is found */
  25. private boolean error = false;
  26. @Override
  27. public boolean isSetupRequired() {
  28. return false;
  29. }
  30. @Override
  31. public String getName() {
  32. return "Task Fulfillment";
  33. }
  34. @Override
  35. public void setup() {
  36. // No Setup required.
  37. }
  38. @Override
  39. public LinkedList<Pair<String, String>> calculate(MyGraph g) {
  40. LinkedList<Pair<String, String>> results = new LinkedList<Pair<String, String>>();
  41. // This corresponds to the Function F
  42. double placedSum = 0;
  43. // This corresponds to Fmax
  44. double prioritySum = 0;
  45. if (g.isComposite()) {
  46. LinkedList<MyGraph> graphs = g.getAllSubGraphs();
  47. for (MyGraph current : graphs) {
  48. if (current.getAttribute("layer") == Layer.OPERATOR && !current.isComposite()) {
  49. Double priority = Double.valueOf(current.getAttribute("priority"));
  50. if (priority == null) {
  51. error = true;
  52. } else {
  53. boolean placed = fullyPlaced(current, g);
  54. if (placed) {
  55. placedSum += priority;
  56. }
  57. prioritySum += priority;
  58. }
  59. }
  60. }
  61. }
  62. // This corresponds to F'
  63. double percentagePlaced = (placedSum / prioritySum) * 100;
  64. if (error) {
  65. error = false;
  66. results.add(ERROR_MESSAGE);
  67. }
  68. results.add(new Pair<String, String>("Task Placement", "" + placedSum));
  69. results.add(new Pair<String, String>("Placement Percentage", percentagePlaced + "%"));
  70. return results;
  71. }
  72. /**
  73. * Checks whether an operator Graph is fully mapped onto the underlay.
  74. *
  75. * @param operator
  76. * the operator Graph
  77. * @param mapping
  78. * the mapping Graph
  79. * @return true if all Nodes of the operator Graph have a valid mapping
  80. */
  81. private boolean fullyPlaced(MyGraph operator, MyGraph mapping) {
  82. boolean result = true;
  83. LinkedList<MyEdge> mappingEdges = new LinkedList<MyEdge>(mapping.<MyEdge>getEdgeSet().stream()
  84. .filter(e -> (((Boolean) e.getAttribute(MappingGraphManager.ATTRIBUTE_KEY_MAPPING)) == true))
  85. .collect(Collectors.toList()));
  86. // build list of the operator nodes within the mapping graph
  87. LinkedList<MyNode> operatorNodes = new LinkedList<MyNode>();
  88. for (MyNode n : mapping.<MyNode>getNodeSet()) {
  89. String originalGraph = n.getAttribute("originalGraph");
  90. if ((originalGraph != null && originalGraph.equals(operator.getId()))
  91. || n.getAttribute(MappingGraphManager.ATTRIBUTE_KEY_MAPPING_PARENT_ID).equals(operator.getId())) {
  92. operatorNodes.add(n);
  93. }
  94. }
  95. // check if they have a mapping
  96. for (MyNode n : operatorNodes) {
  97. boolean isMapped = false;
  98. for (MyEdge e : mappingEdges) {
  99. if (e.getNode0().getId().equals(n.getId())) {
  100. isMapped = true;
  101. }
  102. }
  103. if (!isMapped) {
  104. result = false;
  105. }
  106. }
  107. return result;
  108. }
  109. }