HolonSwitch.java 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. package classes;
  2. import java.awt.Point;
  3. import java.util.LinkedList;
  4. import com.google.gson.annotations.Expose;
  5. import ui.controller.SingletonControl;
  6. import ui.view.UnitGraph;
  7. /**
  8. * The class HolonSwitch represents a Switch, which can be turned on and off.
  9. *
  10. * @author Gruppe14
  11. *
  12. */
  13. public class HolonSwitch extends AbstractCpsObject implements IGraphedElement {
  14. /**
  15. * The class HolonSwitch represents an Object in the system, that has the
  16. * capacity of manipulate the electricity flow. The switch can be manage
  17. * automatically through a graph or direct manually.
  18. *
  19. * @author Gruppe14
  20. *
  21. */
  22. /*
  23. * manual state True, if this wire is working (capable of carrying
  24. * electricity), else false
  25. */
  26. @Expose
  27. boolean manualActive;
  28. /*
  29. * active state True, if this wire is working (capable of carrying
  30. * electricity), 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 stretch;
  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<Point> graphPoints = new LinkedList<>();
  50. /**
  51. * Create a new HolonSwitch with the default name ("Switch"), a default
  52. * value of automatic handle and active status.
  53. *
  54. * @param objName
  55. * String
  56. */
  57. public HolonSwitch(String objName) {
  58. super(objName);
  59. setStretching(IGraphedElement.STRETCH_BY_DEFAULT);
  60. activeAt=new boolean[UnitGraph.STANDARD_GRAPH_ACCURACY];
  61. setLocalPeriod(SingletonControl.getInstance().getControl()==null?
  62. UnitGraph.STANDARD_GRAPH_ACCURACY:
  63. SingletonControl.getInstance().getControl().getModel().getGraphIterations()
  64. );
  65. setManualState(true);
  66. setAutoState(true);
  67. setActiveAt(true);
  68. setManualMode(false);
  69. setGraphPoints(new LinkedList<Point>());
  70. }
  71. /**
  72. * Create a copy of an existing HolonSwitch.
  73. *
  74. * @param obj
  75. * the Object to copy
  76. */
  77. public HolonSwitch(AbstractCpsObject obj) {
  78. super(obj);
  79. HolonSwitch copyObj = (HolonSwitch)obj;
  80. setLocalPeriod(copyObj.getLocalPeriod());
  81. setStretching(copyObj.isStretching());
  82. activeAt=new boolean[UnitGraph.STANDARD_GRAPH_ACCURACY];
  83. super.setName(obj.getName());
  84. setManualState(copyObj.getActiveManual());
  85. setAutoState(true);
  86. setLocalPeriod(((IGraphedElement)obj).getLocalPeriod());
  87. setActiveAt(true);
  88. for (int i = 0; i < activeAt.length; i++) {
  89. activeAt[i] = copyObj.getState(i);
  90. }
  91. setGraphPoints(new LinkedList<Point>());
  92. for (Point p : copyObj.getGraphPoints()) {
  93. this.graphPoints.add(new Point((int) p.getX(), (int) p.getY()));
  94. }
  95. setManualMode(copyObj.getManualMode());
  96. }
  97. /**
  98. * Calculates the state of the Switch.
  99. */
  100. public void switchState() {
  101. if (!manualMode) {
  102. setManualMode(true);
  103. }
  104. if (this.manualActive == true) {
  105. setImage("/Images/switch-off.png");
  106. } else {
  107. setImage("/Images/switch-on.png");
  108. }
  109. this.manualActive = !manualActive;
  110. }
  111. /**
  112. * Getter for the status of the Switch at a given timestep.
  113. *
  114. * @param timeStep state at given iteration.
  115. * @return state value
  116. */
  117. public boolean getState(int timeStep) {
  118. if (manualMode) {
  119. return this.manualActive;
  120. } else {
  121. return activeAt[UnitGraph.getEffectiveIndex(this, timeStep)];
  122. }
  123. }
  124. /**
  125. * Overall status of the switch (manual or automatic mode).
  126. *
  127. * @return boolean the State
  128. */
  129. public boolean getState() {//TODO: not really necessary
  130. if (manualMode) {
  131. return this.manualActive;
  132. } else {
  133. return this.autoActive;
  134. }
  135. }
  136. /**
  137. * Change the state of the Switch to manual.
  138. *
  139. * @param state the State
  140. */
  141. public void setManualState(boolean state) {
  142. this.manualActive = state;
  143. setImage();
  144. }
  145. /**
  146. * Set the state of the Switch to automatic.
  147. *
  148. * @param state the State
  149. */
  150. public void setAutoState(boolean state) {//TODO: This should probably not be public
  151. this.autoActive = state;
  152. setImage();
  153. }
  154. /**
  155. * Set Image of the Switch.
  156. */
  157. private void setImage() {
  158. if (manualMode) {
  159. if (!this.manualActive) {
  160. setImage("/Images/switch-off.png");
  161. } else {
  162. setImage("/Images/switch-on.png");
  163. }
  164. } else {
  165. if (!this.autoActive) {
  166. setImage("/Images/switch-off.png");
  167. } else {
  168. setImage("/Images/switch-on.png");
  169. }
  170. }
  171. }
  172. /**
  173. * For automatic use only (through the graph).
  174. *
  175. * @return the Graph Points
  176. */
  177. public LinkedList<Point> getGraphPoints() {
  178. return graphPoints;
  179. }
  180. /**
  181. * Set the values of the switch in the graph (auto. mode only).
  182. *
  183. * @param points the Graph points
  184. */
  185. public void setGraphPoints(LinkedList<Point> points) {
  186. this.graphPoints = points;
  187. }
  188. /**
  189. * Returns the ManualState.
  190. *
  191. * @return boolean Manual State
  192. */
  193. public boolean getActiveManual() {
  194. return this.manualActive;
  195. }
  196. /**
  197. * Set the value of the Switch.
  198. *
  199. * @param active
  200. * the default value
  201. */
  202. public void setActiveAt(boolean active) {
  203. activeAt = new boolean[100];//TODO This is necessary because of thisgson rubbish.
  204. for (int i = 0; i < activeAt.length; i++) {
  205. this.activeAt[i] = active;
  206. }
  207. }
  208. public void setActiveAt(int pos, boolean active) {
  209. //activeAt = new boolean[100];
  210. this.activeAt[pos] = active;
  211. }
  212. /**
  213. * Set the overall value of the Switch (manual mode).
  214. *
  215. * @param mode
  216. * the mode (boolean)
  217. */
  218. public void setManualMode(boolean mode) {
  219. manualMode = mode;
  220. }
  221. /**
  222. * Get manualmode state.
  223. *
  224. * @return boolean manual mode state
  225. */
  226. public boolean getManualMode() {
  227. return manualMode;
  228. }
  229. @Override
  230. public void setLocalPeriod(int period) {
  231. localPeriod=period;
  232. }
  233. @Override
  234. public int getLocalPeriod() {
  235. return localPeriod;
  236. }
  237. @Override
  238. public boolean isStretching() {
  239. return stretch;
  240. }
  241. @Override
  242. public void setStretching(boolean stretch) {
  243. this.stretch=stretch;
  244. }
  245. }