TaskFulfillmentMetric.java 3.6 KB

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