HolonSwitch.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. package classes;
  2. import java.awt.Point;
  3. import java.util.LinkedList;
  4. import com.google.gson.annotations.Expose;
  5. /**
  6. * The class HolonSwitch represents a Switch, which can be turned on and off.
  7. *
  8. * @author Gruppe14
  9. *
  10. */
  11. public class HolonSwitch extends AbstractCpsObject implements IGraphedElement {
  12. /**
  13. * The class HolonSwitch represents an Object in the system, that has the
  14. * capacity of manipulate the electricity flow. The switch can be manage
  15. * automatically through a graph or direct manually.
  16. *
  17. * @author Gruppe14
  18. *
  19. */
  20. /*
  21. * manual state True, if this wire is working (capable of carrying
  22. * electricity), else false
  23. */
  24. @Expose
  25. boolean manualActive;
  26. /*
  27. * active state True, if this wire is working (capable of carrying
  28. * electricity), else false
  29. */
  30. @Expose
  31. boolean autoActive;
  32. /*
  33. * true if switch has to be used manually
  34. */
  35. @Expose
  36. boolean manualMode;
  37. @Expose
  38. int localPeriod;
  39. /*
  40. * Energy at each point of the graph with 50 predefined points. At the
  41. * beginning, it starts with all values at energy
  42. */
  43. boolean[] activeAt;
  44. // Points on the UnitGraph
  45. LinkedList<Point> graphPoints = new LinkedList<>();
  46. /**
  47. * Create a new HolonSwitch with the default name ("Switch"), a default
  48. * value of automatic handle and active status.
  49. *
  50. * @param objName
  51. * String
  52. */
  53. public HolonSwitch(String objName) {
  54. super(objName);
  55. activeAt=new boolean[100];
  56. setManualState(true);
  57. setAutoState(true);
  58. setActiveAt(true);
  59. setManualMode(false);
  60. setGraphPoints(new LinkedList<Point>());
  61. }
  62. /**
  63. * Create a copy of an existing HolonSwitch.
  64. *
  65. * @param obj
  66. * the Object to copy
  67. */
  68. public HolonSwitch(AbstractCpsObject obj) {
  69. super(obj);
  70. super.setName(obj.getName());
  71. setManualState(((HolonSwitch) obj).getActiveManual());
  72. setAutoState(true);
  73. setLocalPeriod(((IGraphedElement)obj).getLocalPeriod());
  74. setActiveAt(true);
  75. for (int i = 0; i < activeAt.length; i++) {
  76. activeAt[i] = ((HolonSwitch) obj).getActiveAt()[i];
  77. }
  78. setGraphPoints(new LinkedList<Point>());
  79. for (Point p : ((HolonSwitch) obj).getGraphPoints()) {
  80. this.graphPoints.add(new Point((int) p.getX(), (int) p.getY()));
  81. }
  82. setManualMode(((HolonSwitch) obj).getManualMode());
  83. }
  84. /**
  85. * Calculates the state of the Switch.
  86. */
  87. public void switchState() {
  88. if (!manualMode) {
  89. setManualMode(true);
  90. }
  91. if (this.manualActive == true) {
  92. setImage("/Images/switch-off.png");
  93. } else {
  94. setImage("/Images/switch-on.png");
  95. }
  96. this.manualActive = !manualActive;
  97. }
  98. /**
  99. * Getter for the status of the Switch at a given timestep.
  100. *
  101. * @param timeStep
  102. * int
  103. * @return state value
  104. */
  105. public boolean getState(int timeStep) {
  106. if (manualMode) {
  107. return this.manualActive;
  108. } else {
  109. return getActiveAt()[timeStep];
  110. }
  111. }
  112. /**
  113. * Overall status of the switch (manual or automatic mode).
  114. *
  115. * @return boolean the State
  116. */
  117. public boolean getState() {
  118. if (manualMode) {
  119. return this.manualActive;
  120. } else {
  121. return this.autoActive;
  122. }
  123. }
  124. /**
  125. * Change the state of the Switch to manual.
  126. *
  127. * @param state
  128. * the State
  129. */
  130. public void setManualState(boolean state) {
  131. this.manualActive = state;
  132. setImage();
  133. }
  134. /**
  135. * Set the state of the Switch to automatic.
  136. *
  137. * @param state
  138. * 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 == false) {
  150. setImage("/Images/switch-off.png");
  151. } else {
  152. setImage("/Images/switch-on.png");
  153. }
  154. } else {
  155. if (this.autoActive == false) {
  156. setImage("/Images/switch-off.png");
  157. } else {
  158. setImage("/Images/switch-on.png");
  159. }
  160. }
  161. }
  162. /**
  163. * For automatic use only (throught the graph).
  164. *
  165. * @return the Graph Points
  166. */
  167. public LinkedList<Point> getGraphPoints() {
  168. return graphPoints;
  169. }
  170. /**
  171. * Set the values of the switch in the graph (auto. mode only).
  172. *
  173. * @param points
  174. * the Graph points
  175. */
  176. public void setGraphPoints(LinkedList<Point> points) {
  177. this.graphPoints = points;
  178. }
  179. /**
  180. * All values of the graph for the selected Switch (only auto-Mode) get the
  181. * activeAt Array.
  182. *
  183. * @return boolean[] the States of each Iteration
  184. */
  185. public boolean[] getActiveAt() {
  186. return activeAt;
  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];
  204. for (int i = 0; i < activeAt.length; i++) {
  205. this.activeAt[i] = active;
  206. }
  207. }
  208. /**
  209. * Set the overall value of the Switch (manual mode).
  210. *
  211. * @param mode
  212. * the mode (boolean)
  213. */
  214. public void setManualMode(boolean mode) {
  215. manualMode = mode;
  216. }
  217. /**
  218. * Get manualmode state.
  219. *
  220. * @return boolean manual mode state
  221. */
  222. public boolean getManualMode() {
  223. return manualMode;
  224. }
  225. @Override
  226. public void setLocalPeriod(int period) {
  227. localPeriod=period;
  228. activeAt=new boolean[localPeriod];
  229. }
  230. @Override
  231. public int getLocalPeriod() {
  232. return localPeriod;
  233. }
  234. }