HolonSwitch.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 {
  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. /*
  38. * Energy at each point of the graph with 50 predefined points. At the
  39. * beginning, it starts with all values at energy
  40. */
  41. boolean[] activeAt = new boolean[100];
  42. // Points on the UnitGraph
  43. LinkedList<Point> graphPoints = new LinkedList<>();
  44. /**
  45. * Create a new HolonSwitch with the default name ("Switch"), a default
  46. * value of automatic handle and active status.
  47. *
  48. * @param objName
  49. * String
  50. */
  51. public HolonSwitch(String objName) {
  52. super(objName);
  53. setManualState(true);
  54. setAutoState(true);
  55. setActiveAt(true);
  56. setManualMode(false);
  57. setGraphPoints(new LinkedList<Point>());
  58. }
  59. /**
  60. * Create a copy of an existing HolonSwitch.
  61. *
  62. * @param obj
  63. * the Object to copy
  64. */
  65. public HolonSwitch(AbstractCpsObject obj) {
  66. super(obj);
  67. super.setName(obj.getName());
  68. setManualState(((HolonSwitch) obj).getActiveManual());
  69. setAutoState(true);
  70. setActiveAt(true);
  71. for (int i = 0; i < activeAt.length; i++) {
  72. activeAt[i] = ((HolonSwitch) obj).getActiveAt()[i];
  73. }
  74. setGraphPoints(new LinkedList<Point>());
  75. for (Point p : ((HolonSwitch) obj).getGraphPoints()) {
  76. this.graphPoints.add(new Point((int) p.getX(), (int) p.getY()));
  77. }
  78. setManualMode(((HolonSwitch) obj).getManualMode());
  79. }
  80. /**
  81. * Calculates the state of the Switch.
  82. */
  83. public void switchState() {
  84. if (!manualMode) {
  85. setManualMode(true);
  86. }
  87. if (this.manualActive == true) {
  88. setImage("/Images/switch-off.png");
  89. } else {
  90. setImage("/Images/switch-on.png");
  91. }
  92. this.manualActive = !manualActive;
  93. }
  94. /**
  95. * Getter for the status of the Switch at a given timestep.
  96. *
  97. * @param timeStep
  98. * int
  99. * @return state value
  100. */
  101. public boolean getState(int timeStep) {
  102. if (manualMode) {
  103. return this.manualActive;
  104. } else {
  105. return getActiveAt()[timeStep];
  106. }
  107. }
  108. /**
  109. * Overall status of the switch (manual or automatic mode).
  110. *
  111. * @return boolean the State
  112. */
  113. public boolean getState() {
  114. if (manualMode) {
  115. return this.manualActive;
  116. } else {
  117. return this.autoActive;
  118. }
  119. }
  120. /**
  121. * Change the state of the Switch to manual.
  122. *
  123. * @param state
  124. * the State
  125. */
  126. public void setManualState(boolean state) {
  127. this.manualActive = state;
  128. setImage();
  129. }
  130. /**
  131. * Set the state of the Switch to automatic.
  132. *
  133. * @param state
  134. * the State
  135. */
  136. public void setAutoState(boolean state) {
  137. this.autoActive = state;
  138. setImage();
  139. }
  140. /**
  141. * Set Image of the Switch.
  142. */
  143. private void setImage() {
  144. if (manualMode) {
  145. if (this.manualActive == false) {
  146. setImage("/Images/switch-off.png");
  147. } else {
  148. setImage("/Images/switch-on.png");
  149. }
  150. } else {
  151. if (this.autoActive == false) {
  152. setImage("/Images/switch-off.png");
  153. } else {
  154. setImage("/Images/switch-on.png");
  155. }
  156. }
  157. }
  158. /**
  159. * For automatic use only (throught the graph).
  160. *
  161. * @return the Graph Points
  162. */
  163. public LinkedList<Point> getGraphPoints() {
  164. return graphPoints;
  165. }
  166. /**
  167. * Set the values of the switch in the graph (auto. mode only).
  168. *
  169. * @param points
  170. * the Graph points
  171. */
  172. public void setGraphPoints(LinkedList<Point> points) {
  173. this.graphPoints = points;
  174. }
  175. /**
  176. * All values of the graph for the selected Switch (only auto-Mode) get the
  177. * activeAt Array.
  178. *
  179. * @return boolean[] the States of each Iteration
  180. */
  181. public boolean[] getActiveAt() {
  182. return activeAt;
  183. }
  184. /**
  185. * Returns the ManualState.
  186. *
  187. * @return boolean Manual State
  188. */
  189. public boolean getActiveManual() {
  190. return this.manualActive;
  191. }
  192. /**
  193. * Set the value of the Switch.
  194. *
  195. * @param active
  196. * the default value
  197. */
  198. public void setActiveAt(boolean active) {
  199. activeAt = new boolean[100];
  200. for (int i = 0; i < activeAt.length; i++) {
  201. this.activeAt[i] = active;
  202. }
  203. }
  204. /**
  205. * Set the overall value of the Switch (manual mode).
  206. *
  207. * @param mode
  208. * the mode (boolean)
  209. */
  210. public void setManualMode(boolean mode) {
  211. manualMode = mode;
  212. }
  213. /**
  214. * Get manualmode state.
  215. *
  216. * @return boolean manual mode state
  217. */
  218. public boolean getManualMode() {
  219. return manualMode;
  220. }
  221. }