DecoratedGroupNode.java 6.4 KB

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