HolonSwitch.java 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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.Vector2Float;
  9. /**
  10. * The class HolonSwitch represents a Switch, which can be turned on and off.
  11. *
  12. * @author Gruppe14
  13. *
  14. */
  15. public class HolonSwitch extends AbstractCanvasObject implements TimelineDependent {
  16. /**
  17. * The class HolonSwitch represents an Object in the system, that has the
  18. * capacity of manipulate the electricity flow. The switch can be manage
  19. * automatically through a graph or direct manually.
  20. *
  21. */
  22. /*
  23. * manual state True, if this wire is working (capable of carrying electricity),
  24. * else false
  25. */
  26. @Expose
  27. boolean manualActive;
  28. /*
  29. * active state True, if this wire is working (capable of carrying electricity),
  30. * else false
  31. */
  32. @Expose
  33. private boolean autoActive;
  34. /*
  35. * true if switch has to be used manually
  36. */
  37. @Expose
  38. boolean manualMode;
  39. @Expose
  40. int localPeriod;
  41. @Expose
  42. boolean localPeriodActive;
  43. /*
  44. * Energy at each point of the graph with 50 predefined points. At the
  45. * beginning, it starts with all values at energy
  46. */
  47. boolean[] activeAt;
  48. // Points on the UnitGraph
  49. LinkedList<Vector2Float> graphPoints = new LinkedList<>();
  50. /**
  51. * Create a new HolonSwitch with the default name ("Switch"), a default value of
  52. * automatic handle and active status.
  53. *
  54. * @param objName String
  55. */
  56. public HolonSwitch(String objName) {
  57. super(objName);
  58. setUseLocalPeriod(false);
  59. activeAt = new boolean[LocalMode.STANDARD_GRAPH_ACCURACY];
  60. setManualState(true);
  61. setAutoState(true);
  62. setManualMode(false);
  63. setGraphPoints(new LinkedList<Vector2Float>());
  64. initGraphPoints();
  65. sampleGraph();
  66. }
  67. /**
  68. * Create a copy of an existing HolonSwitch.
  69. *
  70. * @param other the Object to copy
  71. */
  72. public HolonSwitch(HolonSwitch other) {
  73. super(other);
  74. setLocalPeriod(other.getLocalPeriod());
  75. setUseLocalPeriod(other.isUsingLocalPeriod());
  76. activeAt = new boolean[LocalMode.STANDARD_GRAPH_ACCURACY];
  77. super.setName(other.getName());
  78. setManualState(other.getManualState());
  79. setAutoState(true);
  80. setGraphPoints(new LinkedList<Vector2Float>());
  81. for (Vector2Float p : other.getGraphPoints()) {
  82. this.graphPoints.add(new Vector2Float(p.getX(), p.getY()));
  83. }
  84. sampleGraph();
  85. setManualMode(other.getManualMode());
  86. }
  87. @Override
  88. public void initForReflection() {
  89. super.initForReflection();
  90. this.graphPoints = new LinkedList<>();
  91. this.reset();
  92. }
  93. /**
  94. * Calculates the state of the Switch.
  95. */
  96. public void switchState() {
  97. if (!manualMode) {
  98. setManualMode(true);
  99. }
  100. if (this.manualActive == true) {
  101. setImage("/Images/switch-off.png");
  102. } else {
  103. setImage("/Images/switch-on.png");
  104. }
  105. this.manualActive = !manualActive;
  106. }
  107. public static String getSwitchClosedImage() {
  108. return "/Images/switch-on.png";
  109. }
  110. public static String getSwitchOpenImage() {
  111. return "/Images/switch-off.png";
  112. }
  113. /**
  114. * Getter for the status of the Switch at a given timestep.
  115. *
  116. * @param timeStep state at given iteration.
  117. * @return state value (true if closed/active, false if open)
  118. */
  119. public boolean getState(int timeStep) {
  120. if (manualMode) {
  121. return this.manualActive;
  122. } else {
  123. return activeAt[IndexTranslator.getEffectiveIndex(this, timeStep)];
  124. }
  125. }
  126. /**
  127. * Change the state of the Switch to manual.
  128. *
  129. * @param state the State
  130. */
  131. public void setManualState(boolean state) {
  132. this.manualActive = state;
  133. setImage();
  134. }
  135. /**
  136. * Set the state of the Switch to automatic.
  137. *
  138. * @param state the State
  139. */
  140. public void setAutoState(boolean state) {
  141. this.autoActive = state;
  142. setImage();
  143. }
  144. /**
  145. * Set Image of the Switch.
  146. */
  147. private void setImage() {
  148. if (manualMode) {
  149. if (!this.manualActive) {
  150. setImage("/Images/switch-off.png");
  151. } else {
  152. setImage("/Images/switch-on.png");
  153. }
  154. } else {
  155. if (!this.autoActive) {
  156. setImage("/Images/switch-off.png");
  157. } else {
  158. setImage("/Images/switch-on.png");
  159. }
  160. }
  161. }
  162. /**
  163. * For automatic use only (through the graph).
  164. *
  165. * @return the Graph Points
  166. */
  167. public LinkedList<Vector2Float> getGraphPoints() {
  168. return graphPoints;
  169. }
  170. /**
  171. * Set the values of the switch in the graph (auto. mode only).
  172. *
  173. * @param linkedList the Graph points
  174. */
  175. public void setGraphPoints(LinkedList<Vector2Float> linkedList) {
  176. this.graphPoints = linkedList;
  177. }
  178. /**
  179. * Initialize the Graph as a closed Switch.
  180. */
  181. private void initGraphPoints() {
  182. graphPoints.clear();
  183. graphPoints.add(new Vector2Float(0, 1));
  184. graphPoints.add(new Vector2Float(1, 1));
  185. }
  186. /**
  187. * Returns the ManualState.
  188. *
  189. * @return boolean Manual State
  190. */
  191. public boolean getManualState() {
  192. return this.manualActive;
  193. }
  194. /**
  195. * Returns the autoActive.
  196. *
  197. * @return boolean autoActive
  198. */
  199. public boolean getAutoActive() {
  200. return autoActive;
  201. }
  202. /**
  203. * Set the overall value of the Switch (manual mode).
  204. *
  205. * @param mode the mode (boolean)
  206. */
  207. public void setManualMode(boolean mode) {
  208. manualMode = mode;
  209. }
  210. /**
  211. * Get manualmode state.
  212. *
  213. * @return boolean manual mode state
  214. */
  215. public boolean getManualMode() {
  216. return manualMode;
  217. }
  218. // interfaces.GraphEditable
  219. @Override
  220. public GraphType getGraphType() {
  221. return GraphType.boolGraph;
  222. }
  223. @Override
  224. public LinkedList<Vector2Float> getStateGraph() {
  225. return graphPoints;
  226. }
  227. @Override
  228. public void reset() {
  229. initGraphPoints();
  230. sampleGraph();
  231. }
  232. @Override
  233. public void sampleGraph() {
  234. activeAt = sampleGraph(100);
  235. }
  236. /**
  237. * Generate out of the Graph Points a array of boolean that represent the
  238. * Curve("on or off"-Graph) at each sample position. The Values are in the Range
  239. * [0,1].
  240. *
  241. * @param sampleLength amount of samplePositions. The positions are equidistant
  242. * on the Range[0,1].
  243. * @return the boolean array of samplepoints.
  244. */
  245. private boolean[] sampleGraph(int sampleLength) {
  246. ListIterator<Vector2Float> iter = this.graphPoints.listIterator();
  247. Vector2Float before = iter.next();
  248. Vector2Float after = iter.next();
  249. boolean[] activeTriggerPos = new boolean[sampleLength];
  250. for (int i = 0; i < sampleLength; i++) {
  251. double graphX = (double) i / (double) (sampleLength - 1); // from 0.0 to 1.0
  252. if (graphX > after.x) {
  253. before = after;
  254. after = iter.next();
  255. }
  256. activeTriggerPos[i] = (before.getY() >= 0.5);
  257. }
  258. return activeTriggerPos;
  259. }
  260. // interfaces.LocalMode
  261. @Override
  262. public void setLocalPeriod(int period) {
  263. localPeriod = period;
  264. }
  265. @Override
  266. public int getLocalPeriod() {
  267. return localPeriod;
  268. }
  269. @Override
  270. public boolean isUsingLocalPeriod() {
  271. return localPeriodActive;
  272. }
  273. @Override
  274. public void setUseLocalPeriod(boolean state) {
  275. this.localPeriodActive = state;
  276. }
  277. public String toString() {
  278. return name + "[ID:" + getId() + "]";
  279. }
  280. }