HolonSwitch.java 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. package classes;
  2. import java.awt.Point;
  3. import java.awt.geom.Point2D.Double;
  4. import java.util.LinkedList;
  5. import com.google.gson.annotations.Expose;
  6. import interfaces.GraphEditable;
  7. import interfaces.IGraphedElement;
  8. import ui.controller.SingletonControl;
  9. import ui.view.IndexTranslator;
  10. import ui.view.UnitGraph;
  11. /**
  12. * The class HolonSwitch represents a Switch, which can be turned on and off.
  13. *
  14. * @author Gruppe14
  15. *
  16. */
  17. public class HolonSwitch extends AbstractCpsObject implements IGraphedElement, GraphEditable {
  18. /**
  19. * The class HolonSwitch represents an Object in the system, that has the
  20. * capacity of manipulate the electricity flow. The switch can be manage
  21. * automatically through a graph or direct manually.
  22. *
  23. * @author Gruppe14
  24. *
  25. */
  26. /*
  27. * manual state True, if this wire is working (capable of carrying
  28. * electricity), else false
  29. */
  30. @Expose
  31. boolean manualActive;
  32. /*
  33. * active state True, if this wire is working (capable of carrying
  34. * electricity), else false
  35. */
  36. @Expose
  37. private boolean autoActive;
  38. /*
  39. * true if switch has to be used manually
  40. */
  41. @Expose
  42. boolean manualMode;
  43. @Expose
  44. int localPeriod;
  45. @Expose
  46. boolean stretch;
  47. /*
  48. * Energy at each point of the graph with 50 predefined points. At the
  49. * beginning, it starts with all values at energy
  50. */
  51. boolean[] activeAt;
  52. // Points on the UnitGraph
  53. LinkedList<Point> graphPoints = new LinkedList<>();
  54. /**
  55. * Create a new HolonSwitch with the default name ("Switch"), a default
  56. * value of automatic handle and active status.
  57. *
  58. * @param objName
  59. * String
  60. */
  61. public HolonSwitch(String objName) {
  62. super(objName);
  63. setStretching(IGraphedElement.STRETCH_BY_DEFAULT);
  64. activeAt=new boolean[UnitGraph.STANDARD_GRAPH_ACCURACY];
  65. setLocalPeriod(SingletonControl.getInstance().getControl()==null?
  66. UnitGraph.STANDARD_GRAPH_ACCURACY:
  67. SingletonControl.getInstance().getControl().getModel().getGraphIterations()
  68. );
  69. setManualState(true);
  70. setAutoState(true);
  71. setActiveAt(true);
  72. setManualMode(false);
  73. setGraphPoints(new LinkedList<Point>());
  74. }
  75. /**
  76. * Create a copy of an existing HolonSwitch.
  77. *
  78. * @param obj
  79. * the Object to copy
  80. */
  81. public HolonSwitch(AbstractCpsObject obj) {
  82. super(obj);
  83. HolonSwitch copyObj = (HolonSwitch)obj;
  84. setLocalPeriod(copyObj.getLocalPeriod());
  85. setStretching(copyObj.isStretching());
  86. activeAt=new boolean[UnitGraph.STANDARD_GRAPH_ACCURACY];
  87. super.setName(obj.getName());
  88. setManualState(copyObj.getActiveManual());
  89. setAutoState(true);
  90. setLocalPeriod(((IGraphedElement)obj).getLocalPeriod());
  91. setActiveAt(true);
  92. for (int i = 0; i < activeAt.length; i++) {
  93. activeAt[i] = copyObj.getState(i);
  94. }
  95. setGraphPoints(new LinkedList<Point>());
  96. for (Point p : copyObj.getGraphPoints()) {
  97. this.graphPoints.add(new Point((int) p.getX(), (int) p.getY()));
  98. }
  99. setManualMode(copyObj.getManualMode());
  100. }
  101. /**
  102. * Calculates the state of the Switch.
  103. */
  104. public void switchState() {
  105. if (!manualMode) {
  106. setManualMode(true);
  107. }
  108. if (this.manualActive == true) {
  109. setImage("/Images/switch-off.png");
  110. } else {
  111. setImage("/Images/switch-on.png");
  112. }
  113. this.manualActive = !manualActive;
  114. }
  115. /**
  116. * Getter for the status of the Switch at a given timestep.
  117. *
  118. * @param timeStep state at given iteration.
  119. * @return state value
  120. */
  121. public boolean getState(int timeStep) {
  122. if (manualMode) {
  123. return this.manualActive;
  124. } else {
  125. return activeAt[IndexTranslator.getEffectiveIndex(this, timeStep)];
  126. }
  127. }
  128. /**
  129. * Overall status of the switch (manual or automatic mode).
  130. *
  131. * @return boolean the State
  132. */
  133. public boolean getState() {//TODO: not really necessary
  134. if (manualMode) {
  135. return this.manualActive;
  136. } else {
  137. return this.autoActive;
  138. }
  139. }
  140. /**
  141. * Change the state of the Switch to manual.
  142. *
  143. * @param state the State
  144. */
  145. public void setManualState(boolean state) {
  146. this.manualActive = state;
  147. setImage();
  148. }
  149. /**
  150. * Set the state of the Switch to automatic.
  151. *
  152. * @param state the State
  153. */
  154. public void setAutoState(boolean state) {//TODO: This should probably not be public
  155. this.autoActive = state;
  156. setImage();
  157. }
  158. /**
  159. * Set Image of the Switch.
  160. */
  161. private void setImage() {
  162. if (manualMode) {
  163. if (!this.manualActive) {
  164. setImage("/Images/switch-off.png");
  165. } else {
  166. setImage("/Images/switch-on.png");
  167. }
  168. } else {
  169. if (!this.autoActive) {
  170. setImage("/Images/switch-off.png");
  171. } else {
  172. setImage("/Images/switch-on.png");
  173. }
  174. }
  175. }
  176. /**
  177. * For automatic use only (through the graph).
  178. *
  179. * @return the Graph Points
  180. */
  181. public LinkedList<Point> getGraphPoints() {
  182. return graphPoints;
  183. }
  184. /**
  185. * Set the values of the switch in the graph (auto. mode only).
  186. *
  187. * @param points the Graph points
  188. */
  189. public void setGraphPoints(LinkedList<Point> points) {
  190. this.graphPoints = points;
  191. }
  192. /**
  193. * Returns the ManualState.
  194. *
  195. * @return boolean Manual State
  196. */
  197. public boolean getActiveManual() {
  198. return this.manualActive;
  199. }
  200. /**
  201. * Set the value of the Switch.
  202. *
  203. * @param active
  204. * the default value
  205. */
  206. public void setActiveAt(boolean active) {
  207. activeAt = new boolean[100];//TODO This is necessary because of thisgson rubbish.
  208. for (int i = 0; i < activeAt.length; i++) {
  209. this.activeAt[i] = active;
  210. }
  211. }
  212. public void setActiveAt(int pos, boolean active) {
  213. //activeAt = new boolean[100];
  214. this.activeAt[pos] = active;
  215. }
  216. /**
  217. * Set the overall value of the Switch (manual mode).
  218. *
  219. * @param mode
  220. * the mode (boolean)
  221. */
  222. public void setManualMode(boolean mode) {
  223. manualMode = mode;
  224. }
  225. /**
  226. * Get manualmode state.
  227. *
  228. * @return boolean manual mode state
  229. */
  230. public boolean getManualMode() {
  231. return manualMode;
  232. }
  233. @Override
  234. public void setLocalPeriod(int period) {
  235. localPeriod=period;
  236. }
  237. @Override
  238. public int getLocalPeriod() {
  239. return localPeriod;
  240. }
  241. @Override
  242. public boolean isStretching() {
  243. return stretch;
  244. }
  245. @Override
  246. public void setStretching(boolean stretch) {
  247. this.stretch=stretch;
  248. }
  249. @Override
  250. public Graphtype getGraphType() {
  251. return Graphtype.boolGraph;
  252. }
  253. @Override
  254. public LinkedList<Double> getStateGraph() {
  255. // TODO no Double List
  256. return null;
  257. }
  258. }