HolonSwitch.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. if (this.manualActive == true) {
  86. setImage("/Images/switch-off.png");
  87. } else {
  88. setImage("/Images/switch-on.png");
  89. }
  90. this.manualActive = !manualActive;
  91. }
  92. }
  93. /**
  94. * Getter for the status of the Switch at a given timestep.
  95. *
  96. * @param timeStep
  97. * int
  98. * @return state value
  99. */
  100. public boolean getState(int timeStep) {
  101. if (manualMode) {
  102. return this.manualActive;
  103. } else {
  104. return getActiveAt()[timeStep];
  105. }
  106. }
  107. /**
  108. * Overall status of the switch (manual or automatic mode).
  109. *
  110. * @return boolean the State
  111. */
  112. public boolean getState() {
  113. if (manualMode) {
  114. return this.manualActive;
  115. } else {
  116. return this.autoActive;
  117. }
  118. }
  119. /**
  120. * Change the state of the Switch to manual.
  121. *
  122. * @param state
  123. * the State
  124. */
  125. public void setManualState(boolean state) {
  126. this.manualActive = state;
  127. setImage();
  128. }
  129. /**
  130. * Set the state of the Switch to automatic.
  131. *
  132. * @param state
  133. * the State
  134. */
  135. public void setAutoState(boolean state) {
  136. this.autoActive = state;
  137. setImage();
  138. }
  139. /**
  140. * Set Image of the Switch.
  141. */
  142. private void setImage() {
  143. if (manualMode) {
  144. if (this.manualActive == false) {
  145. setImage("/Images/switch-off.png");
  146. } else {
  147. setImage("/Images/switch-on.png");
  148. }
  149. } else {
  150. if (this.autoActive == false) {
  151. setImage("/Images/switch-off.png");
  152. } else {
  153. setImage("/Images/switch-on.png");
  154. }
  155. }
  156. }
  157. /**
  158. * For automatic use only (throught the graph).
  159. *
  160. * @return the Graph Points
  161. */
  162. public LinkedList<Point> getGraphPoints() {
  163. return graphPoints;
  164. }
  165. /**
  166. * Set the values of the switch in the graph (auto. mode only).
  167. *
  168. * @param points
  169. * the Graph points
  170. */
  171. public void setGraphPoints(LinkedList<Point> points) {
  172. this.graphPoints = points;
  173. }
  174. /**
  175. * All values of the graph for the selected Switch (only auto-Mode) get the
  176. * activeAt Array.
  177. *
  178. * @return boolean[] the States of each Iteration
  179. */
  180. public boolean[] getActiveAt() {
  181. return activeAt;
  182. }
  183. /**
  184. * Returns the ManualState.
  185. *
  186. * @return boolean Manual State
  187. */
  188. public boolean getActiveManual() {
  189. return this.manualActive;
  190. }
  191. /**
  192. * Set the value of the Switch.
  193. *
  194. * @param active
  195. * the default value
  196. */
  197. public void setActiveAt(boolean active) {
  198. activeAt = new boolean[100];
  199. for (int i = 0; i < activeAt.length; i++) {
  200. this.activeAt[i] = active;
  201. }
  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. }