DecoratedGroupNode.java 6.4 KB

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