Model.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. package holeg.model;
  2. import java.util.HashSet;
  3. import java.util.List;
  4. import java.util.Set;
  5. import java.util.logging.Logger;
  6. import java.util.stream.Collectors;
  7. /**
  8. * The Class Model is the class where everything is saved. All changes made to
  9. * the Data is managed via a controller.
  10. *
  11. * @author Gruppe14
  12. */
  13. public class Model {
  14. private static final Logger log = Logger.getLogger(Model.class.getName());
  15. private GroupNode canvas = new GroupNode("Canvas");
  16. public transient Set<Holon> holons = new HashSet<>();
  17. /** the amount of iterations */
  18. private int currentIteration = 0;
  19. private int maxIterations = 100;
  20. /**
  21. * the Fairness model in use
  22. */
  23. private FairnessModel fairnessModel = FairnessModel.MininumDemandFirst;
  24. /*
  25. * Array of all CpsObjects in our canvas. It is set by default as an empty
  26. * list.
  27. */
  28. private Set<Edge> edgesOnCanvas = new HashSet<>();
  29. public Model() {
  30. log.fine("Init Model");
  31. }
  32. public GroupNode getCanvas() {
  33. return canvas;
  34. }
  35. /**
  36. * Get all Edges on the Canvas.
  37. *
  38. * @return the edgesOnCanvas
  39. */
  40. public Set<Edge> getEdgesOnCanvas() {
  41. return edgesOnCanvas;
  42. }
  43. /**
  44. * Adds an Edge to The Canvas.
  45. *
  46. * @param edge the edgesOnCanvas to add
  47. */
  48. public void addEdgeOnCanvas(Edge edge) {
  49. this.edgesOnCanvas.add(edge);
  50. }
  51. /**
  52. * Remove an edge from the Canvas.
  53. *
  54. * @param edge the edge to remove
  55. */
  56. public void removeEdgesOnCanvas(Edge edge) {
  57. this.edgesOnCanvas.remove(edge);
  58. }
  59. /**
  60. * Returns the maximum iterations.
  61. *
  62. * @return iterations
  63. */
  64. public int getMaxIterations() {
  65. return maxIterations;
  66. }
  67. /**
  68. * Returns the current iteration.
  69. *
  70. * @return current iteration
  71. */
  72. public int getCurrentIteration() {
  73. return currentIteration;
  74. }
  75. /**
  76. * sets the current Iteration.
  77. *
  78. * @param value the current Iteration
  79. */
  80. public void setCurrentIteration(int value) {
  81. this.currentIteration = value;
  82. }
  83. public List<HolonElement> getAllHolonElements() {
  84. return canvas.getAllHolonObjectsRecursive().flatMap(HolonObject::elementsStream).collect(Collectors.toList());
  85. }
  86. public List<Flexibility> getAllFlexibilities() {
  87. return canvas.getAllHolonObjectsRecursive().flatMap(hO -> hO.elementsStream().flatMap(ele -> ele.flexList.stream())).collect(Collectors.toList());
  88. }
  89. public void reset() {
  90. resetFlexibilities();
  91. resetEdges();
  92. }
  93. private void resetFlexibilities() {
  94. getAllFlexibilities().forEach(Flexibility::reset);
  95. }
  96. private void resetEdges() {
  97. this.getEdgesOnCanvas().forEach(Edge::reset);
  98. }
  99. /**
  100. * @param iterations the number of steps for this simulation
  101. */
  102. public void setIterations(int iterations) {
  103. this.maxIterations = iterations;
  104. }
  105. /**
  106. * @return the fairnessModel
  107. */
  108. public FairnessModel getFairnessModel() {
  109. return fairnessModel;
  110. }
  111. /**
  112. * @param fairnessModel the fairnessModel to set
  113. */
  114. public void setFairnessModel(FairnessModel fairnessModel) {
  115. this.fairnessModel = fairnessModel;
  116. }
  117. public void clear() {
  118. this.edgesOnCanvas.clear();
  119. canvas.clear();
  120. }
  121. public void setCanvas(GroupNode canvas) {
  122. this.canvas = canvas;
  123. }
  124. public void setEdgesOnCanvas(Set<Edge> edgesOnCanvas) {
  125. this.edgesOnCanvas = edgesOnCanvas;
  126. }
  127. /**
  128. * All implemented FairnessModels:<br>
  129. * {@link FairnessModel#MininumDemandFirst}<br>
  130. * {@link FairnessModel#AllEqual}
  131. */
  132. public enum FairnessModel {
  133. /**
  134. * One Element of each HolonObject will be powered first, starting with the
  135. * smallest Demand. If ale HolonObjects have an active Element, the
  136. * simulation will try to fully supply as many HolonObjects as possible.
  137. */
  138. MininumDemandFirst,
  139. /**
  140. * All HolonObjects will receive the same amount of energy.
  141. */
  142. AllEqual
  143. }
  144. }