HolonSwitch.java 5.4 KB

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