HolonSwitch.java 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. package holeg.model;
  2. import holeg.interfaces.LocalMode;
  3. import holeg.interfaces.TimelineDependent;
  4. import holeg.preferences.ImagePreference;
  5. import holeg.ui.controller.IndexTranslator;
  6. import holeg.utility.math.vector.Vec2f;
  7. import java.util.LinkedList;
  8. import java.util.ListIterator;
  9. /**
  10. * The class HolonSwitch represents an Object in the system, that has the
  11. * capacity of manipulate the electricity flow. The switch can be manage
  12. * automatically through a graph or direct manually.
  13. *
  14. * @author Gruppe14
  15. */
  16. public class HolonSwitch extends AbstractCanvasObject implements TimelineDependent {
  17. /**
  18. * Energy at each point of the graph with 50 predefined points. At the
  19. * beginning, it starts with all values at energy
  20. */
  21. boolean[] activeAt = new boolean[LocalMode.STANDARD_GRAPH_ACCURACY];
  22. /**
  23. * Points on the UnitGraph
  24. */
  25. LinkedList<Vec2f> graphPoints = new LinkedList<>();
  26. private SwitchMode mode = SwitchMode.Auto;
  27. private SwitchState manualState = SwitchState.Closed;
  28. private SwitchState state = SwitchState.Closed;
  29. private Period period = new Period();
  30. /**
  31. * Create a new HolonSwitch with a custom name, a default value of automatic
  32. * handle and active status.
  33. *
  34. * @param name String
  35. */
  36. public HolonSwitch(String name) {
  37. super(name);
  38. initGraphPoints();
  39. sampleGraph();
  40. }
  41. /**
  42. * Create a copy of an existing HolonSwitch.
  43. *
  44. * @param other the Object to copy
  45. */
  46. public HolonSwitch(HolonSwitch other) {
  47. super(other);
  48. mode = other.mode;
  49. manualState = other.manualState;
  50. state = other.state;
  51. setPeriod(other.getPeriod());
  52. activeAt = new boolean[LocalMode.STANDARD_GRAPH_ACCURACY];
  53. setName(other.getName());
  54. for (Vec2f p : other.getGraphPoints()) {
  55. this.graphPoints.add(new Vec2f(p.getX(), p.getY()));
  56. }
  57. sampleGraph();
  58. }
  59. @Override
  60. public String getImagePath() {
  61. return switch (state) {
  62. case Open -> ImagePreference.Canvas.Switch.Open;
  63. case Closed -> ImagePreference.Canvas.Switch.Closed;
  64. };
  65. }
  66. /**
  67. * Calculates the state of the Switch.
  68. */
  69. public void flipManualState() {
  70. manualState = SwitchState.opposite(manualState);
  71. }
  72. public void calculateState(int timestep) {
  73. switch (mode) {
  74. case Auto:
  75. state = activeAt[IndexTranslator.getEffectiveIndex(this, timestep)] ? SwitchState.Open : SwitchState.Closed;
  76. case Manual:
  77. default:
  78. state = manualState;
  79. }
  80. }
  81. /*
  82. * STATE
  83. */
  84. public SwitchState getState() {
  85. return state;
  86. }
  87. /**
  88. * For automatic use only (through the graph).
  89. *
  90. * @return the Graph Points
  91. */
  92. public LinkedList<Vec2f> getGraphPoints() {
  93. return graphPoints;
  94. }
  95. /**
  96. * Set the values of the switch in the graph (auto. mode only).
  97. *
  98. * @param linkedList the Graph points
  99. */
  100. public void setGraphPoints(LinkedList<Vec2f> linkedList) {
  101. this.graphPoints = linkedList;
  102. }
  103. /**
  104. * Initialize the Graph as a closed Switch.
  105. */
  106. private void initGraphPoints() {
  107. graphPoints.clear();
  108. graphPoints.add(new Vec2f(0, 1));
  109. graphPoints.add(new Vec2f(1, 1));
  110. }
  111. public SwitchMode getMode() {
  112. return mode;
  113. }
  114. public void setMode(SwitchMode mode) {
  115. this.mode = mode;
  116. }
  117. public SwitchState getManualState() {
  118. return manualState;
  119. }
  120. public void setManualState(SwitchState manuelState) {
  121. this.manualState = manuelState;
  122. }
  123. // interfaces.GraphEditable
  124. @Override
  125. public GraphType getGraphType() {
  126. return GraphType.boolGraph;
  127. }
  128. @Override
  129. public LinkedList<Vec2f> getStateGraph() {
  130. return graphPoints;
  131. }
  132. @Override
  133. public void reset() {
  134. initGraphPoints();
  135. sampleGraph();
  136. }
  137. @Override
  138. public void sampleGraph() {
  139. activeAt = sampleGraph(100);
  140. }
  141. /**
  142. * Generate out of the Graph Points a array of boolean that represent the
  143. * Curve("on or off"-Graph) at each sample position. The Values are in the Range
  144. * [0,1].
  145. *
  146. * @param sampleLength amount of samplePositions. The positions are equidistant
  147. * on the Range[0,1].
  148. * @return the boolean array of samplepoints.
  149. */
  150. private boolean[] sampleGraph(int sampleLength) {
  151. ListIterator<Vec2f> iter = this.graphPoints.listIterator();
  152. Vec2f before = iter.next();
  153. Vec2f after = iter.next();
  154. boolean[] activeTriggerPos = new boolean[sampleLength];
  155. for (int i = 0; i < sampleLength; i++) {
  156. double graphX = (double) i / (double) (sampleLength - 1); // from 0.0 to 1.0
  157. if (graphX > after.x) {
  158. before = after;
  159. after = iter.next();
  160. }
  161. activeTriggerPos[i] = (before.getY() >= 0.5);
  162. }
  163. return activeTriggerPos;
  164. }
  165. @Override
  166. public Period getPeriod() {
  167. return period;
  168. }
  169. @Override
  170. public void setPeriod(Period period) {
  171. this.period = period;
  172. }
  173. public String toString() {
  174. return name + "[ID:" + getId() + "]";
  175. }
  176. public enum SwitchState {
  177. Open, Closed;
  178. public static SwitchState opposite(SwitchState state) {
  179. return switch (state) {
  180. case Closed -> Open;
  181. case Open -> Closed;
  182. };
  183. }
  184. public boolean isOpen() {
  185. return this == Open;
  186. }
  187. public boolean isClosed() {
  188. return this == Closed;
  189. }
  190. }
  191. public enum SwitchMode {
  192. Manual, Auto
  193. }
  194. }