HolonSwitch.java 5.2 KB

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