HolonSwitch.java 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. setActiveAt(true);
  72. setManualMode(false);
  73. initGraphPoints();
  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[IGraphedElement.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<Point2D.Double>());
  96. for (Point2D.Double p : copyObj.getGraphPoints()) {
  97. this.graphPoints.add(new Point2D.Double(p.getX(),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<Point2D.Double> getGraphPoints() {
  182. return graphPoints;
  183. }
  184. /**
  185. * Set the values of the switch in the graph (auto. mode only).
  186. *
  187. * @param linkedList the Graph points
  188. */
  189. public void setGraphPoints(LinkedList<Double> linkedList) {
  190. this.graphPoints = linkedList;
  191. }
  192. //TODO: javadoc
  193. private void initGraphPoints()
  194. {
  195. graphPoints = new LinkedList<Point2D.Double>();
  196. graphPoints.add(new Point2D.Double(0.0, 1.0));
  197. graphPoints.add(new Point2D.Double(1.0, 1.0));
  198. }
  199. /**
  200. * Returns the ManualState.
  201. *
  202. * @return boolean Manual State
  203. */
  204. public boolean getActiveManual() {
  205. return this.manualActive;
  206. }
  207. /**
  208. * Set the value of the Switch.
  209. *
  210. * @param active
  211. * the default value
  212. */
  213. public void setActiveAt(boolean active) {
  214. activeAt = new boolean[100];//TODO This is necessary because of thisgson rubbish.
  215. for (int i = 0; i < activeAt.length; i++) {
  216. this.activeAt[i] = active;
  217. }
  218. }
  219. public void setActiveAt(int pos, boolean active) {
  220. //activeAt = new boolean[100];
  221. this.activeAt[pos] = active;
  222. }
  223. /**
  224. * Set the overall value of the Switch (manual mode).
  225. *
  226. * @param mode
  227. * the mode (boolean)
  228. */
  229. public void setManualMode(boolean mode) {
  230. manualMode = mode;
  231. }
  232. /**
  233. * Get manualmode state.
  234. *
  235. * @return boolean manual mode state
  236. */
  237. public boolean getManualMode() {
  238. return manualMode;
  239. }
  240. @Override
  241. public void setLocalPeriod(int period) {
  242. localPeriod=period;
  243. }
  244. @Override
  245. public int getLocalPeriod() {
  246. return localPeriod;
  247. }
  248. @Override
  249. public boolean isStretching() {
  250. return stretch;
  251. }
  252. @Override
  253. public void setStretching(boolean stretch) {
  254. this.stretch=stretch;
  255. }
  256. @Override
  257. public Graphtype getGraphType() {
  258. return Graphtype.boolGraph;
  259. }
  260. @Override
  261. public LinkedList<Double> getStateGraph() {
  262. for(Double p: graphPoints)
  263. {
  264. System.out.println(p);
  265. }
  266. return graphPoints;
  267. }
  268. private boolean[] sampleGraph(int sampleLength)
  269. {
  270. ListIterator<Point2D.Double> iter = this.graphPoints.listIterator();
  271. Point.Double before = iter.next();
  272. Point.Double after = iter.next();
  273. boolean [] activeTriggerPos = new boolean[sampleLength];
  274. for(int i = 0; i<sampleLength ; i++)
  275. {
  276. double graphX = (double)i / (double) (sampleLength - 1); //from 0.0 to 1.0
  277. if(graphX > after.x)
  278. {
  279. before = after;
  280. after = iter.next();
  281. }
  282. activeTriggerPos[i] = (before.getY() >= 0.5);
  283. }
  284. return activeTriggerPos;
  285. }
  286. @Override
  287. public void sampleGraph() {
  288. activeAt = sampleGraph(100);
  289. }
  290. }