HolonSwitch.java 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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.LocalMode;
  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 AbstractCanvasObject implements LocalMode, 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 localPeriodActive;
  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. setUseLocalPeriod(false);
  64. activeAt=new boolean[LocalMode.STANDARD_GRAPH_ACCURACY];
  65. setManualState(true);
  66. setAutoState(true);
  67. setManualMode(false);
  68. setGraphPoints(new LinkedList<Point2D.Double>());
  69. initGraphPoints();
  70. sampleGraph();
  71. }
  72. /**
  73. * Create a copy of an existing HolonSwitch.
  74. *
  75. * @param obj
  76. * the Object to copy
  77. */
  78. public HolonSwitch(AbstractCanvasObject obj) {
  79. super(obj);
  80. System.out.println("SwitchCopy?");
  81. HolonSwitch copyObj = (HolonSwitch)obj;
  82. setLocalPeriod(copyObj.getLocalPeriod());
  83. setUseLocalPeriod(copyObj.isUsingLocalPeriod());
  84. activeAt=new boolean[LocalMode.STANDARD_GRAPH_ACCURACY];
  85. super.setName(obj.getName());
  86. setManualState(copyObj.getManualState());
  87. setAutoState(true);
  88. setGraphPoints(new LinkedList<Point2D.Double>());
  89. for (Point2D.Double p : copyObj.getGraphPoints()) {
  90. this.graphPoints.add(new Point2D.Double(p.getX(),p.getY()));
  91. }
  92. sampleGraph();
  93. setManualMode(copyObj.getManualMode());
  94. }
  95. /**
  96. * Calculates the state of the Switch.
  97. */
  98. public void switchState() {
  99. if (!manualMode) {
  100. setManualMode(true);
  101. }
  102. if (this.manualActive == true) {
  103. setImage("/Images/switch-off.png");
  104. } else {
  105. setImage("/Images/switch-on.png");
  106. }
  107. this.manualActive = !manualActive;
  108. }
  109. public static String getSwitchClosedImage(){
  110. return "/Images/switch-on.png";
  111. }
  112. public static String getSwitchOpenImage(){
  113. return "/Images/switch-off.png";
  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 (true if closed/active, false if open)
  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. * Change the state of the Switch to manual.
  130. *
  131. * @param state the State
  132. */
  133. public void setManualState(boolean state) {
  134. this.manualActive = state;
  135. setImage();
  136. }
  137. /**
  138. * Set the state of the Switch to automatic.
  139. *
  140. * @param state the State
  141. */
  142. public void setAutoState(boolean state) {
  143. this.autoActive = state;
  144. setImage();
  145. }
  146. /**
  147. * Set Image of the Switch.
  148. */
  149. private void setImage() {
  150. if (manualMode) {
  151. if (!this.manualActive) {
  152. setImage("/Images/switch-off.png");
  153. } else {
  154. setImage("/Images/switch-on.png");
  155. }
  156. } else {
  157. if (!this.autoActive) {
  158. setImage("/Images/switch-off.png");
  159. } else {
  160. setImage("/Images/switch-on.png");
  161. }
  162. }
  163. }
  164. /**
  165. * For automatic use only (through the graph).
  166. *
  167. * @return the Graph Points
  168. */
  169. public LinkedList<Point2D.Double> getGraphPoints() {
  170. return graphPoints;
  171. }
  172. /**
  173. * Set the values of the switch in the graph (auto. mode only).
  174. *
  175. * @param linkedList the Graph points
  176. */
  177. public void setGraphPoints(LinkedList<Double> linkedList) {
  178. this.graphPoints = linkedList;
  179. }
  180. /**
  181. * Initialize the Graph as a closed Switch.
  182. */
  183. private void initGraphPoints()
  184. {
  185. graphPoints.clear();
  186. graphPoints.add(new Point2D.Double(0.0, 1.0));
  187. graphPoints.add(new Point2D.Double(1.0, 1.0));
  188. }
  189. /**
  190. * Returns the ManualState.
  191. *
  192. * @return boolean Manual State
  193. */
  194. public boolean getManualState() {
  195. return this.manualActive;
  196. }
  197. /**
  198. * Returns the autoActive.
  199. *
  200. * @return boolean autoActive
  201. */
  202. public boolean getAutoActive() {
  203. return autoActive;
  204. }
  205. /**
  206. * Set the overall value of the Switch (manual mode).
  207. *
  208. * @param mode
  209. * the mode (boolean)
  210. */
  211. public void setManualMode(boolean mode) {
  212. manualMode = mode;
  213. }
  214. /**
  215. * Get manualmode state.
  216. *
  217. * @return boolean manual mode state
  218. */
  219. public boolean getManualMode() {
  220. return manualMode;
  221. }
  222. //interfaces.GraphEditable
  223. @Override
  224. public Graphtype getGraphType() {
  225. return Graphtype.boolGraph;
  226. }
  227. @Override
  228. public LinkedList<Double> getStateGraph() {
  229. return graphPoints;
  230. }
  231. @Override
  232. public void reset() {
  233. initGraphPoints();
  234. sampleGraph();
  235. }
  236. @Override
  237. public void sampleGraph() {
  238. activeAt = sampleGraph(100);
  239. }
  240. /**
  241. * Generate out of the Graph Points a array of boolean that represent the Curve("on or off"-Graph) at each sample position. The Values are in the Range [0,1].
  242. * @param sampleLength amount of samplePositions. The positions are equidistant on the Range[0,1].
  243. * @return the boolean array of samplepoints.
  244. */
  245. private boolean[] sampleGraph(int sampleLength)
  246. {
  247. ListIterator<Point2D.Double> iter = this.graphPoints.listIterator();
  248. Point.Double before = iter.next();
  249. Point.Double after = iter.next();
  250. boolean [] activeTriggerPos = new boolean[sampleLength];
  251. for(int i = 0; i<sampleLength ; i++)
  252. {
  253. double graphX = (double)i / (double) (sampleLength - 1); //from 0.0 to 1.0
  254. if(graphX > after.x)
  255. {
  256. before = after;
  257. after = iter.next();
  258. }
  259. activeTriggerPos[i] = (before.getY() >= 0.5);
  260. }
  261. return activeTriggerPos;
  262. }
  263. //interfaces.LocalMode
  264. @Override
  265. public void setLocalPeriod(int period) {
  266. localPeriod=period;
  267. }
  268. @Override
  269. public int getLocalPeriod() {
  270. return localPeriod;
  271. }
  272. @Override
  273. public boolean isUsingLocalPeriod() {
  274. return localPeriodActive;
  275. }
  276. @Override
  277. public void setUseLocalPeriod(boolean state) {
  278. this.localPeriodActive=state;
  279. }
  280. @Override
  281. public HolonSwitch makeCopy(){
  282. return new HolonSwitch(this);
  283. }
  284. public String toString() {
  285. return name + "[ID:" + id + "]";
  286. }
  287. }