HolonSwitch.java 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 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 : SwitchState.Closed;
  81. case Manual:
  82. default:
  83. state = manualState;
  84. }
  85. }
  86. /*
  87. * STATE
  88. */
  89. public SwitchState getState() {
  90. return state;
  91. }
  92. /**
  93. * For automatic use only (through the graph).
  94. *
  95. * @return the Graph Points
  96. */
  97. public LinkedList<Vec2f> getGraphPoints() {
  98. return graphPoints;
  99. }
  100. /**
  101. * Set the values of the switch in the graph (auto. mode only).
  102. *
  103. * @param linkedList the Graph points
  104. */
  105. public void setGraphPoints(LinkedList<Vec2f> linkedList) {
  106. this.graphPoints = linkedList;
  107. }
  108. /**
  109. * Initialize the Graph as a closed Switch.
  110. */
  111. private void initGraphPoints() {
  112. graphPoints.clear();
  113. graphPoints.add(new Vec2f(0, 1));
  114. graphPoints.add(new Vec2f(1, 1));
  115. }
  116. public SwitchMode getMode() {
  117. return mode;
  118. }
  119. public void setMode(SwitchMode mode) {
  120. this.mode = mode;
  121. }
  122. public SwitchState getManualState() {
  123. return manualState;
  124. }
  125. public void setManualState(SwitchState manuelState) {
  126. this.manualState = manuelState;
  127. }
  128. // interfaces.GraphEditable
  129. @Override
  130. public GraphType getGraphType() {
  131. return GraphType.boolGraph;
  132. }
  133. @Override
  134. public LinkedList<Vec2f> getStateGraph() {
  135. return graphPoints;
  136. }
  137. @Override
  138. public void reset() {
  139. initGraphPoints();
  140. sampleGraph();
  141. }
  142. @Override
  143. public void sampleGraph() {
  144. activeAt = sampleGraph(100);
  145. }
  146. /**
  147. * Generate out of the Graph Points a array of boolean that represent the
  148. * Curve("on or off"-Graph) at each sample position. The Values are in the Range
  149. * [0,1].
  150. *
  151. * @param sampleLength amount of samplePositions. The positions are equidistant
  152. * on the 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 enum SwitchState {
  186. Open, Closed;
  187. public static SwitchState opposite(SwitchState state) {
  188. return switch (state) {
  189. case Closed -> Open;
  190. case Open -> Closed;
  191. };
  192. }
  193. public boolean isOpen() {
  194. return this == Open;
  195. }
  196. public boolean isClosed() {
  197. return this == Closed;
  198. }
  199. }
  200. public enum SwitchMode {
  201. Manual, Auto
  202. }
  203. }