HolonSwitch.java 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. package classes;
  2. import java.awt.Point;
  3. import java.awt.geom.Point2D;
  4. import java.awt.geom.Point2D.Double;
  5. import java.util.LinkedList;
  6. import java.util.ListIterator;
  7. import com.google.gson.annotations.Expose;
  8. import interfaces.GraphEditable;
  9. import interfaces.IGraphedElement;
  10. import ui.controller.SingletonControl;
  11. import ui.view.IndexTranslator;
  12. import ui.view.UnitGraph;
  13. /**
  14. * The class HolonSwitch represents a Switch, which can be turned on and off.
  15. *
  16. * @author Gruppe14
  17. *
  18. */
  19. public class HolonSwitch extends AbstractCpsObject implements IGraphedElement, GraphEditable {
  20. /**
  21. * The class HolonSwitch represents an Object in the system, that has the
  22. * capacity of manipulate the electricity flow. The switch can be manage
  23. * automatically through a graph or direct manually.
  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<Point2D.Double> 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[IGraphedElement.STANDARD_GRAPH_ACCURACY];
  65. setLocalPeriod(SingletonControl.getInstance().getControl()==null?
  66. IGraphedElement.STANDARD_GRAPH_ACCURACY:
  67. SingletonControl.getInstance().getControl().getModel().getGraphIterations()
  68. );
  69. setManualState(true);
  70. setAutoState(true);
  71. setManualMode(false);
  72. setGraphPoints(new LinkedList<Point2D.Double>());
  73. initGraphPoints();
  74. sampleGraph();
  75. }
  76. /**
  77. * Create a copy of an existing HolonSwitch.
  78. *
  79. * @param obj
  80. * the Object to copy
  81. */
  82. public HolonSwitch(AbstractCpsObject obj) {
  83. super(obj);
  84. System.out.println("SwitchCopy?");
  85. HolonSwitch copyObj = (HolonSwitch)obj;
  86. setLocalPeriod(copyObj.getLocalPeriod());
  87. setStretching(copyObj.isStretching());
  88. activeAt=new boolean[IGraphedElement.STANDARD_GRAPH_ACCURACY];
  89. super.setName(obj.getName());
  90. setManualState(copyObj.getActiveManual());
  91. setAutoState(true);
  92. setLocalPeriod(((IGraphedElement)obj).getLocalPeriod());
  93. setGraphPoints(new LinkedList<Point2D.Double>());
  94. for (Point2D.Double p : copyObj.getGraphPoints()) {
  95. this.graphPoints.add(new Point2D.Double(p.getX(),p.getY()));
  96. }
  97. sampleGraph();
  98. setManualMode(copyObj.getManualMode());
  99. }
  100. /**
  101. * Calculates the state of the Switch.
  102. */
  103. public void switchState() {
  104. if (!manualMode) {
  105. setManualMode(true);
  106. }
  107. if (this.manualActive == true) {
  108. setImage("/Images/switch-off.png");
  109. } else {
  110. setImage("/Images/switch-on.png");
  111. }
  112. this.manualActive = !manualActive;
  113. }
  114. /**
  115. * Getter for the status of the Switch at a given timestep.
  116. *
  117. * @param timeStep state at given iteration.
  118. * @return state value
  119. */
  120. public boolean getState(int timeStep) {
  121. if (manualMode) {
  122. return this.manualActive;
  123. } else {
  124. return activeAt[IndexTranslator.getEffectiveIndex(this, timeStep)];
  125. }
  126. }
  127. /**
  128. * Overall status of the switch (manual or automatic mode).
  129. *
  130. * @return boolean the State
  131. */
  132. public boolean getState() {//TODO: not really necessary
  133. if (manualMode) {
  134. return this.manualActive;
  135. } else {
  136. return this.autoActive;
  137. }
  138. }
  139. /**
  140. * Change the state of the Switch to manual.
  141. *
  142. * @param state the State
  143. */
  144. public void setManualState(boolean state) {
  145. this.manualActive = state;
  146. setImage();
  147. }
  148. /**
  149. * Set the state of the Switch to automatic.
  150. *
  151. * @param state the State
  152. */
  153. public void setAutoState(boolean state) {//TODO: This should probably not be public
  154. this.autoActive = state;
  155. setImage();
  156. }
  157. /**
  158. * Set Image of the Switch.
  159. */
  160. private void setImage() {
  161. if (manualMode) {
  162. if (!this.manualActive) {
  163. setImage("/Images/switch-off.png");
  164. } else {
  165. setImage("/Images/switch-on.png");
  166. }
  167. } else {
  168. if (!this.autoActive) {
  169. setImage("/Images/switch-off.png");
  170. } else {
  171. setImage("/Images/switch-on.png");
  172. }
  173. }
  174. }
  175. /**
  176. * For automatic use only (through the graph).
  177. *
  178. * @return the Graph Points
  179. */
  180. public LinkedList<Point2D.Double> getGraphPoints() {
  181. return graphPoints;
  182. }
  183. /**
  184. * Set the values of the switch in the graph (auto. mode only).
  185. *
  186. * @param linkedList the Graph points
  187. */
  188. public void setGraphPoints(LinkedList<Double> linkedList) {
  189. this.graphPoints = linkedList;
  190. }
  191. //TODO: javadoc
  192. private void initGraphPoints()
  193. {
  194. graphPoints.clear();
  195. graphPoints.add(new Point2D.Double(0.0, 1.0));
  196. graphPoints.add(new Point2D.Double(1.0, 1.0));
  197. }
  198. /**
  199. * Returns the ManualState.
  200. *
  201. * @return boolean Manual State
  202. */
  203. public boolean getActiveManual() {
  204. return this.manualActive;
  205. }
  206. /**
  207. * Set the overall value of the Switch (manual mode).
  208. *
  209. * @param mode
  210. * the mode (boolean)
  211. */
  212. public void setManualMode(boolean mode) {
  213. manualMode = mode;
  214. }
  215. /**
  216. * Get manualmode state.
  217. *
  218. * @return boolean manual mode state
  219. */
  220. public boolean getManualMode() {
  221. return manualMode;
  222. }
  223. @Override
  224. public void setLocalPeriod(int period) {
  225. localPeriod=period;
  226. }
  227. @Override
  228. public int getLocalPeriod() {
  229. return localPeriod;
  230. }
  231. @Override
  232. public boolean isStretching() {
  233. return stretch;
  234. }
  235. @Override
  236. public void setStretching(boolean stretch) {
  237. this.stretch=stretch;
  238. }
  239. @Override
  240. public Graphtype getGraphType() {
  241. return Graphtype.boolGraph;
  242. }
  243. @Override
  244. public LinkedList<Double> getStateGraph() {
  245. return graphPoints;
  246. }
  247. private boolean[] sampleGraph(int sampleLength)
  248. {
  249. ListIterator<Point2D.Double> iter = this.graphPoints.listIterator();
  250. Point.Double before = iter.next();
  251. Point.Double after = iter.next();
  252. boolean [] activeTriggerPos = new boolean[sampleLength];
  253. for(int i = 0; i<sampleLength ; i++)
  254. {
  255. double graphX = (double)i / (double) (sampleLength - 1); //from 0.0 to 1.0
  256. if(graphX > after.x)
  257. {
  258. before = after;
  259. after = iter.next();
  260. }
  261. activeTriggerPos[i] = (before.getY() >= 0.5);
  262. }
  263. return activeTriggerPos;
  264. }
  265. @Override
  266. public void sampleGraph() {
  267. activeAt = sampleGraph(100);
  268. }
  269. @Override
  270. public void reset() {
  271. initGraphPoints();
  272. sampleGraph();
  273. }
  274. }