HolonSwitch.java 4.7 KB

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