HolonSwitch.java 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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
  12. * capacity of manipulate the electricity flow. The switch can be manage
  13. * automatically through a graph or direct manually.
  14. *
  15. * @author Gruppe14
  16. */
  17. public class HolonSwitch extends AbstractCanvasObject implements TimelineDependent, PostDeserialize {
  18. /**
  19. * Energy at each point of the graph with 50 predefined points. At the
  20. * beginning, it starts with 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
  33. * handle and active 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 String getImagePath() {
  62. return switch (state) {
  63. case Open -> ImagePreference.Canvas.Switch.Open;
  64. case Closed -> ImagePreference.Canvas.Switch.Closed;
  65. };
  66. }
  67. /**
  68. * Calculates the state of the Switch.
  69. */
  70. public void flipManualState() {
  71. manualState = SwitchState.opposite(manualState);
  72. }
  73. public void calculateState(int timestep) {
  74. switch (mode) {
  75. case Auto:
  76. state = activeAt[IndexTranslator.getEffectiveIndex(this, timestep)] ? SwitchState.Open : SwitchState.Closed;
  77. case Manual:
  78. default:
  79. state = manualState;
  80. }
  81. }
  82. /*
  83. * STATE
  84. */
  85. public SwitchState getState() {
  86. return state;
  87. }
  88. /**
  89. * For automatic use only (through the graph).
  90. *
  91. * @return the Graph Points
  92. */
  93. public LinkedList<Vec2f> getGraphPoints() {
  94. return graphPoints;
  95. }
  96. /**
  97. * Set the values of the switch in the graph (auto. mode only).
  98. *
  99. * @param linkedList the Graph points
  100. */
  101. public void setGraphPoints(LinkedList<Vec2f> linkedList) {
  102. this.graphPoints = linkedList;
  103. }
  104. /**
  105. * Initialize the Graph as a closed Switch.
  106. */
  107. private void initGraphPoints() {
  108. graphPoints.clear();
  109. graphPoints.add(new Vec2f(0, 1));
  110. graphPoints.add(new Vec2f(1, 1));
  111. }
  112. public SwitchMode getMode() {
  113. return mode;
  114. }
  115. public void setMode(SwitchMode mode) {
  116. this.mode = mode;
  117. }
  118. public SwitchState getManualState() {
  119. return manualState;
  120. }
  121. public void setManualState(SwitchState manuelState) {
  122. this.manualState = manuelState;
  123. }
  124. // interfaces.GraphEditable
  125. @Override
  126. public GraphType getGraphType() {
  127. return GraphType.boolGraph;
  128. }
  129. @Override
  130. public LinkedList<Vec2f> getStateGraph() {
  131. return graphPoints;
  132. }
  133. @Override
  134. public void reset() {
  135. initGraphPoints();
  136. sampleGraph();
  137. }
  138. @Override
  139. public void sampleGraph() {
  140. activeAt = sampleGraph(100);
  141. }
  142. /**
  143. * Generate out of the Graph Points a array of boolean that represent the
  144. * Curve("on or off"-Graph) at each sample position. The Values are in the Range
  145. * [0,1].
  146. *
  147. * @param sampleLength amount of samplePositions. The positions are equidistant
  148. * on the Range[0,1].
  149. * @return the boolean array of samplepoints.
  150. */
  151. private boolean[] sampleGraph(int sampleLength) {
  152. ListIterator<Vec2f> iter = this.graphPoints.listIterator();
  153. Vec2f before = iter.next();
  154. Vec2f after = iter.next();
  155. boolean[] activeTriggerPos = new boolean[sampleLength];
  156. for (int i = 0; i < sampleLength; i++) {
  157. double graphX = (double) i / (double) (sampleLength - 1); // from 0.0 to 1.0
  158. if (graphX > after.x) {
  159. before = after;
  160. after = iter.next();
  161. }
  162. activeTriggerPos[i] = (before.getY() >= 0.5);
  163. }
  164. return activeTriggerPos;
  165. }
  166. @Override
  167. public Period getPeriod() {
  168. return period;
  169. }
  170. @Override
  171. public void setPeriod(Period period) {
  172. this.period = period;
  173. }
  174. public String toString() {
  175. return name + "[ID:" + getId() + "]";
  176. }
  177. @Override
  178. public void postDeserialize() {
  179. sampleGraph();
  180. }
  181. public enum SwitchState {
  182. Open, Closed;
  183. public static SwitchState opposite(SwitchState state) {
  184. return switch (state) {
  185. case Closed -> Open;
  186. case Open -> Closed;
  187. };
  188. }
  189. public boolean isOpen() {
  190. return this == Open;
  191. }
  192. public boolean isClosed() {
  193. return this == Closed;
  194. }
  195. }
  196. public enum SwitchMode {
  197. Manual, Auto
  198. }
  199. }