Model.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 Set<Edge> edgesOnCanvas = new HashSet<>();
  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 Set<Edge> getEdgesOnCanvas() {
  58. return edgesOnCanvas;
  59. }
  60. /**
  61. * Adds an Edge to The Canvas.
  62. *
  63. * @param edge the edgesOnCanvas to add
  64. */
  65. public void addEdgeOnCanvas(Edge edge) {
  66. this.edgesOnCanvas.add(edge);
  67. }
  68. /**
  69. * Remove an edge from the Canvas.
  70. *
  71. * @param edge the edge to remove
  72. */
  73. public void removeEdgesOnCanvas(Edge edge) {
  74. this.edgesOnCanvas.remove(edge);
  75. }
  76. /**
  77. * Returns the maximum iterations.
  78. *
  79. * @return iterations
  80. */
  81. public int getMaxIterations() {
  82. return maxIterations;
  83. }
  84. /**
  85. * Returns the current iteration.
  86. *
  87. * @return current iteration
  88. */
  89. public int getCurrentIteration() {
  90. return currentIteration;
  91. }
  92. /**
  93. * sets the current Iteration.
  94. *
  95. * @param value the current Iteration
  96. */
  97. public void setCurrentIteration(int value) {
  98. this.currentIteration = value;
  99. }
  100. public List<HolonElement> getAllHolonElements() {
  101. return canvas.getAllHolonObjectsRecursive().flatMap(HolonObject::getElements).collect(Collectors.toList());
  102. }
  103. public List<Flexibility> getAllFlexibilities() {
  104. return canvas.getAllHolonObjectsRecursive().flatMap(hO -> hO.getElements().flatMap(ele -> ele.flexList.stream())).collect(Collectors.toList());
  105. }
  106. public void reset() {
  107. resetFlexibilities();
  108. resetEdges();
  109. }
  110. private void resetFlexibilities() {
  111. getAllFlexibilities().forEach(Flexibility::reset);
  112. }
  113. private void resetEdges() {
  114. this.getEdgesOnCanvas().forEach(Edge::reset);
  115. }
  116. /**
  117. * @param iterations the number of steps for this simulation
  118. */
  119. public void setIterations(int iterations){
  120. this.maxIterations=iterations;
  121. }
  122. /**
  123. * @return the fairnessModel
  124. */
  125. public FairnessModel getFairnessModel() {
  126. return fairnessModel;
  127. }
  128. /**
  129. * @param fairnessModel the fairnessModel to set
  130. */
  131. public void setFairnessModel(FairnessModel fairnessModel) {
  132. this.fairnessModel = fairnessModel;
  133. }
  134. public void clear() {
  135. this.edgesOnCanvas.clear();
  136. canvas.clear();
  137. }
  138. }