HolonSwitch.java 5.8 KB

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