HolonSwitch.java 4.4 KB

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