HolonSwitch.java 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. package model;
  2. import java.util.LinkedList;
  3. import java.util.ListIterator;
  4. import com.google.gson.annotations.Expose;
  5. import interfaces.LocalMode;
  6. import interfaces.TimelineDependent;
  7. import ui.controller.IndexTranslator;
  8. import 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 obj the Object to copy
  71. */
  72. public HolonSwitch(AbstractCanvasObject obj) {
  73. super(obj);
  74. HolonSwitch copyObj = (HolonSwitch) obj;
  75. setLocalPeriod(copyObj.getLocalPeriod());
  76. setUseLocalPeriod(copyObj.isUsingLocalPeriod());
  77. activeAt = new boolean[LocalMode.STANDARD_GRAPH_ACCURACY];
  78. super.setName(obj.getName());
  79. setManualState(copyObj.getManualState());
  80. setAutoState(true);
  81. setGraphPoints(new LinkedList<Vector2Float>());
  82. for (Vector2Float p : copyObj.getGraphPoints()) {
  83. this.graphPoints.add(new Vector2Float(p.getX(), p.getY()));
  84. }
  85. sampleGraph();
  86. setManualMode(copyObj.getManualMode());
  87. }
  88. @Override
  89. public void initForReflection() {
  90. super.initForReflection();
  91. this.graphPoints = new LinkedList<>();
  92. this.reset();
  93. }
  94. /**
  95. * Calculates the state of the Switch.
  96. */
  97. public void switchState() {
  98. if (!manualMode) {
  99. setManualMode(true);
  100. }
  101. if (this.manualActive == true) {
  102. setImage("/Images/switch-off.png");
  103. } else {
  104. setImage("/Images/switch-on.png");
  105. }
  106. this.manualActive = !manualActive;
  107. }
  108. public static String getSwitchClosedImage() {
  109. return "/Images/switch-on.png";
  110. }
  111. public static String getSwitchOpenImage() {
  112. return "/Images/switch-off.png";
  113. }
  114. /**
  115. * Getter for the status of the Switch at a given timestep.
  116. *
  117. * @param timeStep state at given iteration.
  118. * @return state value (true if closed/active, false if open)
  119. */
  120. public boolean getState(int timeStep) {
  121. if (manualMode) {
  122. return this.manualActive;
  123. } else {
  124. return activeAt[IndexTranslator.getEffectiveIndex(this, timeStep)];
  125. }
  126. }
  127. /**
  128. * Change the state of the Switch to manual.
  129. *
  130. * @param state the State
  131. */
  132. public void setManualState(boolean state) {
  133. this.manualActive = state;
  134. setImage();
  135. }
  136. /**
  137. * Set the state of the Switch to automatic.
  138. *
  139. * @param state the State
  140. */
  141. public void setAutoState(boolean state) {
  142. this.autoActive = state;
  143. setImage();
  144. }
  145. /**
  146. * Set Image of the Switch.
  147. */
  148. private void setImage() {
  149. if (manualMode) {
  150. if (!this.manualActive) {
  151. setImage("/Images/switch-off.png");
  152. } else {
  153. setImage("/Images/switch-on.png");
  154. }
  155. } else {
  156. if (!this.autoActive) {
  157. setImage("/Images/switch-off.png");
  158. } else {
  159. setImage("/Images/switch-on.png");
  160. }
  161. }
  162. }
  163. /**
  164. * For automatic use only (through the graph).
  165. *
  166. * @return the Graph Points
  167. */
  168. public LinkedList<Vector2Float> getGraphPoints() {
  169. return graphPoints;
  170. }
  171. /**
  172. * Set the values of the switch in the graph (auto. mode only).
  173. *
  174. * @param linkedList the Graph points
  175. */
  176. public void setGraphPoints(LinkedList<Vector2Float> linkedList) {
  177. this.graphPoints = linkedList;
  178. }
  179. /**
  180. * Initialize the Graph as a closed Switch.
  181. */
  182. private void initGraphPoints() {
  183. graphPoints.clear();
  184. graphPoints.add(new Vector2Float(0, 1));
  185. graphPoints.add(new Vector2Float(1, 1));
  186. }
  187. /**
  188. * Returns the ManualState.
  189. *
  190. * @return boolean Manual State
  191. */
  192. public boolean getManualState() {
  193. return this.manualActive;
  194. }
  195. /**
  196. * Returns the autoActive.
  197. *
  198. * @return boolean autoActive
  199. */
  200. public boolean getAutoActive() {
  201. return autoActive;
  202. }
  203. /**
  204. * Set the overall value of the Switch (manual mode).
  205. *
  206. * @param mode the mode (boolean)
  207. */
  208. public void setManualMode(boolean mode) {
  209. manualMode = mode;
  210. }
  211. /**
  212. * Get manualmode state.
  213. *
  214. * @return boolean manual mode state
  215. */
  216. public boolean getManualMode() {
  217. return manualMode;
  218. }
  219. // interfaces.GraphEditable
  220. @Override
  221. public GraphType getGraphType() {
  222. return GraphType.boolGraph;
  223. }
  224. @Override
  225. public LinkedList<Vector2Float> getStateGraph() {
  226. return graphPoints;
  227. }
  228. @Override
  229. public void reset() {
  230. initGraphPoints();
  231. sampleGraph();
  232. }
  233. @Override
  234. public void sampleGraph() {
  235. activeAt = sampleGraph(100);
  236. }
  237. /**
  238. * Generate out of the Graph Points a array of boolean that represent the
  239. * Curve("on or off"-Graph) at each sample position. The Values are in the Range
  240. * [0,1].
  241. *
  242. * @param sampleLength amount of samplePositions. The positions are equidistant
  243. * on the Range[0,1].
  244. * @return the boolean array of samplepoints.
  245. */
  246. private boolean[] sampleGraph(int sampleLength) {
  247. ListIterator<Vector2Float> iter = this.graphPoints.listIterator();
  248. Vector2Float before = iter.next();
  249. Vector2Float after = iter.next();
  250. boolean[] activeTriggerPos = new boolean[sampleLength];
  251. for (int i = 0; i < sampleLength; i++) {
  252. double graphX = (double) i / (double) (sampleLength - 1); // from 0.0 to 1.0
  253. if (graphX > after.x) {
  254. before = after;
  255. after = iter.next();
  256. }
  257. activeTriggerPos[i] = (before.getY() >= 0.5);
  258. }
  259. return activeTriggerPos;
  260. }
  261. // interfaces.LocalMode
  262. @Override
  263. public void setLocalPeriod(int period) {
  264. localPeriod = period;
  265. }
  266. @Override
  267. public int getLocalPeriod() {
  268. return localPeriod;
  269. }
  270. @Override
  271. public boolean isUsingLocalPeriod() {
  272. return localPeriodActive;
  273. }
  274. @Override
  275. public void setUseLocalPeriod(boolean state) {
  276. this.localPeriodActive = state;
  277. }
  278. public String toString() {
  279. return name + "[ID:" + getId() + "]";
  280. }
  281. }