HolonSwitch.java 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. setLocalPeriod(((HolonSwitch)obj).getLocalPeriod());
  80. setStretching(((HolonSwitch)obj).isStretching());
  81. activeAt=new boolean[UnitGraph.STANDARD_GRAPH_ACCURACY];
  82. super.setName(obj.getName());
  83. setManualState(((HolonSwitch) obj).getActiveManual());
  84. setAutoState(true);
  85. setLocalPeriod(((IGraphedElement)obj).getLocalPeriod());
  86. setActiveAt(true);
  87. for (int i = 0; i < activeAt.length; i++) {
  88. activeAt[i] = ((HolonSwitch) obj).getState(i);
  89. }
  90. setGraphPoints(new LinkedList<Point>());
  91. for (Point p : ((HolonSwitch) obj).getGraphPoints()) {
  92. this.graphPoints.add(new Point((int) p.getX(), (int) p.getY()));
  93. }
  94. setManualMode(((HolonSwitch) obj).getManualMode());
  95. }
  96. /**
  97. * Calculates the state of the Switch.
  98. */
  99. public void switchState() {
  100. if (!manualMode) {
  101. setManualMode(true);
  102. }
  103. if (this.manualActive == true) {
  104. setImage("/Images/switch-off.png");
  105. } else {
  106. setImage("/Images/switch-on.png");
  107. }
  108. this.manualActive = !manualActive;
  109. }
  110. /**
  111. * Getter for the status of the Switch at a given timestep.
  112. *
  113. * @param timeStep
  114. * int
  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
  140. * the State
  141. */
  142. public void setManualState(boolean state) {
  143. this.manualActive = state;
  144. setImage();
  145. }
  146. /**
  147. * Set the state of the Switch to automatic.
  148. *
  149. * @param state
  150. * the State
  151. */
  152. public void setAutoState(boolean state) {//TODO: This should probably not be public
  153. this.autoActive = state;
  154. setImage();
  155. }
  156. /**
  157. * Set Image of the Switch.
  158. */
  159. private void setImage() {
  160. if (manualMode) {
  161. if (this.manualActive == false) {
  162. setImage("/Images/switch-off.png");
  163. } else {
  164. setImage("/Images/switch-on.png");
  165. }
  166. } else {
  167. if (this.autoActive == false) {
  168. setImage("/Images/switch-off.png");
  169. } else {
  170. setImage("/Images/switch-on.png");
  171. }
  172. }
  173. }
  174. /**
  175. * For automatic use only (throught the graph).
  176. *
  177. * @return the Graph Points
  178. */
  179. public LinkedList<Point> getGraphPoints() {
  180. return graphPoints;
  181. }
  182. /**
  183. * Set the values of the switch in the graph (auto. mode only).
  184. *
  185. * @param points
  186. * the Graph points
  187. */
  188. public void setGraphPoints(LinkedList<Point> points) {
  189. this.graphPoints = points;
  190. }
  191. public boolean[] getValueArray() {//TODO: Only used in SubNet line 97. I am not entirely sure how important it is there.
  192. return activeAt;
  193. }
  194. /**
  195. * Returns the ManualState.
  196. *
  197. * @return boolean Manual State
  198. */
  199. public boolean getActiveManual() {
  200. return this.manualActive;
  201. }
  202. /**
  203. * Set the value of the Switch.
  204. *
  205. * @param active
  206. * the default value
  207. */
  208. public void setActiveAt(boolean active) {
  209. activeAt = new boolean[100];//TODO This is necessary because of thisgson rubbish.
  210. for (int i = 0; i < activeAt.length; i++) {
  211. this.activeAt[i] = active;
  212. }
  213. }
  214. public void setActiveAt(int pos, boolean active) {
  215. //activeAt = new boolean[100];
  216. this.activeAt[pos] = active;
  217. }
  218. /**
  219. * Set the overall value of the Switch (manual mode).
  220. *
  221. * @param mode
  222. * the mode (boolean)
  223. */
  224. public void setManualMode(boolean mode) {
  225. manualMode = mode;
  226. }
  227. /**
  228. * Get manualmode state.
  229. *
  230. * @return boolean manual mode state
  231. */
  232. public boolean getManualMode() {
  233. return manualMode;
  234. }
  235. @Override
  236. public void setLocalPeriod(int period) {
  237. localPeriod=period;
  238. }
  239. @Override
  240. public int getLocalPeriod() {
  241. return localPeriod;
  242. }
  243. @Override
  244. public boolean isStretching() {
  245. return stretch;
  246. }
  247. @Override
  248. public void setStretching(boolean stretch) {
  249. this.stretch=stretch;
  250. }
  251. public HolonSwitch makeCopy(){
  252. return new HolonSwitch(this);
  253. }
  254. }