Model.java 4.2 KB

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