DecoratedGroupNode.java 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. package ui.model;
  2. import java.util.ArrayList;
  3. import java.util.stream.Stream;
  4. import classes.Node;
  5. import jdk.jfr.Unsigned;
  6. import classes.GroupNode;
  7. import classes.HolonElement;
  8. import classes.Flexibility;
  9. import ui.model.DecoratedHolonObject.HolonObjectState;
  10. /**
  11. * For the @VisualRepresentationalState only.
  12. *
  13. * @author Tom
  14. *
  15. */
  16. public class DecoratedGroupNode {
  17. private GroupNode model;
  18. private ArrayList<Supplier> supplierList;
  19. private ArrayList<Passiv> passivList;
  20. private ArrayList<Consumer> consumerList;
  21. private ArrayList<Node> nodeList;
  22. /**
  23. * Cables that only exist on that group node. From a object in that group node
  24. * to a object in that group Node. Not exit the group node (a layer down).
  25. */
  26. private ArrayList<DecoratedCable> internCableList;
  27. /**
  28. * Cables that exit this group node (a Layer Up). From a object in this group
  29. * node to a object in a upper layer.
  30. */
  31. private ArrayList<ExitCable> exitCableList;
  32. private ArrayList<DecoratedSwitch> switchList;
  33. private ArrayList<DecoratedGroupNode> groupNodeList;
  34. public DecoratedGroupNode(GroupNode model) {
  35. this.model = model;
  36. this.supplierList = new ArrayList<Supplier>();
  37. this.passivList = new ArrayList<Passiv>();
  38. this.consumerList = new ArrayList<Consumer>();
  39. this.nodeList = new ArrayList<Node>();
  40. this.internCableList = new ArrayList<DecoratedCable>();
  41. this.exitCableList = new ArrayList<ExitCable>();
  42. this.switchList = new ArrayList<DecoratedSwitch>();
  43. this.groupNodeList = new ArrayList<DecoratedGroupNode>();
  44. }
  45. public GroupNode getModel() {
  46. return model;
  47. }
  48. public ArrayList<Supplier> getSupplierList() {
  49. return supplierList;
  50. }
  51. public ArrayList<Passiv> getPassivList() {
  52. return passivList;
  53. }
  54. public ArrayList<Consumer> getConsumerList() {
  55. return consumerList;
  56. }
  57. public ArrayList<Node> getNodeList() {
  58. return nodeList;
  59. }
  60. public ArrayList<DecoratedCable> getInternCableList() {
  61. return internCableList;
  62. }
  63. public ArrayList<ExitCable> getExitCableList() {
  64. return exitCableList;
  65. }
  66. public ArrayList<DecoratedSwitch> getSwitchList() {
  67. return switchList;
  68. }
  69. public ArrayList<DecoratedGroupNode> getGroupNodeList() {
  70. return groupNodeList;
  71. }
  72. // Returns the amount of holons and count himself
  73. public int getAmountOfGroupNodes() {
  74. return 1 + groupNodeList.stream().map(groupNode -> groupNode.getAmountOfGroupNodes()).reduce(0, Integer::sum);
  75. }
  76. public Stream<Flexibility> getFlexibilitiesStream() {
  77. Stream<Flexibility> flexInChildGorupNode = this.groupNodeList.stream()
  78. .flatMap(groupNode -> groupNode.getFlexibilitiesStream());
  79. Stream<Flexibility> flexInThisGorupNode = objectStream()
  80. .flatMap(object -> object.getModel().getElements().stream().flatMap(ele -> ele.flexList.stream()));
  81. return Stream.concat(flexInChildGorupNode, flexInThisGorupNode);
  82. }
  83. public Stream<DecoratedHolonObject> objectStream() {
  84. return Stream.concat(Stream.concat(this.consumerList.stream(), this.supplierList.stream()),
  85. this.passivList.stream());
  86. }
  87. // Gather Informations:
  88. public int getAmountOfSupplier() {
  89. return supplierList.size()
  90. + groupNodeList.stream().map(groupNode -> groupNode.getAmountOfSupplier()).reduce(0, Integer::sum);
  91. }
  92. public int getAmountOfConsumer() {
  93. return consumerList.size()
  94. + groupNodeList.stream().map(groupNode -> groupNode.getAmountOfConsumer()).reduce(0, Integer::sum);
  95. }
  96. public int getAmountOfPassiv() {
  97. return passivList.size()
  98. + groupNodeList.stream().map(groupNode -> groupNode.getAmountOfPassiv()).reduce(0, Integer::sum);
  99. }
  100. public int getAmountOfConsumerWithState(HolonObjectState state) {
  101. return ((int) consumerList.stream().map(con -> con.getState()).filter(rightState -> (rightState == state))
  102. .count())
  103. + groupNodeList.stream().map(groupNode -> groupNode.getAmountOfConsumerWithState(state)).reduce(0,
  104. Integer::sum);
  105. }
  106. public int getAmountOfElemntsFromHolonObjects() {
  107. return objectStream().map(object -> object.getModel().getElements().size()).reduce(0, Integer::sum)
  108. + groupNodeList.stream().map(groupNode -> groupNode.getAmountOfElemntsFromHolonObjects()).reduce(0,
  109. Integer::sum);
  110. }
  111. public PriorityCounts getPriorityCounts() {
  112. PriorityCounts priority = new PriorityCounts();
  113. objectStream().forEach(object -> object.getModel().getElements().stream().forEach(ele -> priority.Count(ele)));
  114. groupNodeList.stream().forEach(groupNode -> priority.Add(groupNode.getPriorityCounts()));
  115. return priority;
  116. }
  117. public class PriorityCounts {
  118. @Unsigned
  119. public int low, medium, high, essential;
  120. public void Add(PriorityCounts other) {
  121. low += other.low;
  122. medium += other.medium;
  123. high += other.high;
  124. essential += other.essential;
  125. }
  126. public void Count(HolonElement element) {
  127. switch (element.getPriority()) {
  128. case Essential:
  129. essential++;
  130. break;
  131. case High:
  132. high++;
  133. break;
  134. case Medium:
  135. medium++;
  136. break;
  137. case Low:
  138. low++;
  139. break;
  140. default:
  141. break;
  142. }
  143. }
  144. }
  145. public int getAmountOfAktiveElemntsFromHolonObjects() {
  146. return objectStream().map(object -> object.getModel().getNumberOfActiveElements()).reduce(0,
  147. Integer::sum)
  148. + groupNodeList.stream().map(groupNode -> groupNode.getAmountOfAktiveElemntsFromHolonObjects())
  149. .reduce(0, Integer::sum);
  150. }
  151. public float getConsumptionFromConsumer() {
  152. return consumerList.stream().map(con -> con.getEnergyNeededFromNetwork()).reduce(0.f, Float::sum)
  153. + groupNodeList.stream().map(groupNode -> groupNode.getConsumptionFromConsumer()).reduce(0.f,
  154. Float::sum);
  155. }
  156. public float getProductionFromSupplier() {
  157. return supplierList.stream().map(sup -> sup.getEnergyToSupplyNetwork()).reduce(0.f, Float::sum) + groupNodeList
  158. .stream().map(groupNode -> groupNode.getProductionFromSupplier()).reduce(0.f, Float::sum);
  159. }
  160. public float getAverageConsumption() {
  161. return getConsumptionFromConsumer() / (float) getAmountOfGroupNodes();
  162. }
  163. public float getAverageProduction() {
  164. return getProductionFromSupplier() / (float) getAmountOfGroupNodes();
  165. }
  166. public String toString() {
  167. return "GroupNode" + model.getId() + " with [Supplier:" + getAmountOfSupplier() + ", NotSupplied:"
  168. + getAmountOfConsumerWithState(HolonObjectState.NOT_SUPPLIED) + ", PartiallySupplied:"
  169. + getAmountOfConsumerWithState(HolonObjectState.PARTIALLY_SUPPLIED) + ", Supplied:"
  170. + getAmountOfConsumerWithState(HolonObjectState.SUPPLIED) + ", OverSupplied:"
  171. + getAmountOfConsumerWithState(HolonObjectState.OVER_SUPPLIED) + ", Passiv:" + getAmountOfPassiv()
  172. + "]";
  173. }
  174. }