HolonSwitch.java 4.7 KB

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