HolonSwitch.java 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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.view.IndexTranslator;
  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 AbstractCanvasObject implements LocalMode, 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. */
  24. /*
  25. * manual state True, if this wire is working (capable of carrying
  26. * electricity), else false
  27. */
  28. @Expose
  29. boolean manualActive;
  30. /*
  31. * active state True, if this wire is working (capable of carrying
  32. * electricity), else false
  33. */
  34. @Expose
  35. private boolean autoActive;
  36. /*
  37. * true if switch has to be used manually
  38. */
  39. @Expose
  40. boolean manualMode;
  41. @Expose
  42. int localPeriod;
  43. @Expose
  44. boolean localPeriodActive;
  45. /*
  46. * Energy at each point of the graph with 50 predefined points. At the
  47. * beginning, it starts with all values at energy
  48. */
  49. boolean[] activeAt;
  50. // Points on the UnitGraph
  51. LinkedList<Point2D.Double> graphPoints = new LinkedList<>();
  52. /**
  53. * Create a new HolonSwitch with the default name ("Switch"), a default
  54. * value of automatic handle and active status.
  55. *
  56. * @param objName
  57. * String
  58. */
  59. public HolonSwitch(String objName) {
  60. super(objName);
  61. setUseLocalPeriod(false);
  62. activeAt=new boolean[LocalMode.STANDARD_GRAPH_ACCURACY];
  63. setManualState(true);
  64. setAutoState(true);
  65. setManualMode(false);
  66. setGraphPoints(new LinkedList<Point2D.Double>());
  67. initGraphPoints();
  68. sampleGraph();
  69. }
  70. /**
  71. * Create a copy of an existing HolonSwitch.
  72. *
  73. * @param obj
  74. * the Object to copy
  75. */
  76. public HolonSwitch(AbstractCanvasObject obj) {
  77. super(obj);
  78. System.out.println("SwitchCopy?");
  79. HolonSwitch copyObj = (HolonSwitch)obj;
  80. setLocalPeriod(copyObj.getLocalPeriod());
  81. setUseLocalPeriod(copyObj.isUsingLocalPeriod());
  82. activeAt=new boolean[LocalMode.STANDARD_GRAPH_ACCURACY];
  83. super.setName(obj.getName());
  84. setManualState(copyObj.getManualState());
  85. setAutoState(true);
  86. setGraphPoints(new LinkedList<Point2D.Double>());
  87. for (Point2D.Double p : copyObj.getGraphPoints()) {
  88. this.graphPoints.add(new Point2D.Double(p.getX(),p.getY()));
  89. }
  90. sampleGraph();
  91. setManualMode(copyObj.getManualMode());
  92. }
  93. /**
  94. * Calculates the state of the Switch.
  95. */
  96. public void switchState() {
  97. if (!manualMode) {
  98. setManualMode(true);
  99. }
  100. if (this.manualActive == true) {
  101. setImage("/Images/switch-off.png");
  102. } else {
  103. setImage("/Images/switch-on.png");
  104. }
  105. this.manualActive = !manualActive;
  106. }
  107. public static String getSwitchClosedImage(){
  108. return "/Images/switch-on.png";
  109. }
  110. public static String getSwitchOpenImage(){
  111. return "/Images/switch-off.png";
  112. }
  113. /**
  114. * Getter for the status of the Switch at a given timestep.
  115. *
  116. * @param timeStep state at given iteration.
  117. * @return state value (true if closed/active, false if open)
  118. */
  119. public boolean getState(int timeStep) {
  120. if (manualMode) {
  121. return this.manualActive;
  122. } else {
  123. return activeAt[IndexTranslator.getEffectiveIndex(this, timeStep)];
  124. }
  125. }
  126. /**
  127. * Change the state of the Switch to manual.
  128. *
  129. * @param state the State
  130. */
  131. public void setManualState(boolean state) {
  132. this.manualActive = state;
  133. setImage();
  134. }
  135. /**
  136. * Set the state of the Switch to automatic.
  137. *
  138. * @param state the State
  139. */
  140. public void setAutoState(boolean state) {
  141. this.autoActive = state;
  142. setImage();
  143. }
  144. /**
  145. * Set Image of the Switch.
  146. */
  147. private void setImage() {
  148. if (manualMode) {
  149. if (!this.manualActive) {
  150. setImage("/Images/switch-off.png");
  151. } else {
  152. setImage("/Images/switch-on.png");
  153. }
  154. } else {
  155. if (!this.autoActive) {
  156. setImage("/Images/switch-off.png");
  157. } else {
  158. setImage("/Images/switch-on.png");
  159. }
  160. }
  161. }
  162. /**
  163. * For automatic use only (through the graph).
  164. *
  165. * @return the Graph Points
  166. */
  167. public LinkedList<Point2D.Double> getGraphPoints() {
  168. return graphPoints;
  169. }
  170. /**
  171. * Set the values of the switch in the graph (auto. mode only).
  172. *
  173. * @param linkedList the Graph points
  174. */
  175. public void setGraphPoints(LinkedList<Double> linkedList) {
  176. this.graphPoints = linkedList;
  177. }
  178. /**
  179. * Initialize the Graph as a closed Switch.
  180. */
  181. private void initGraphPoints()
  182. {
  183. graphPoints.clear();
  184. graphPoints.add(new Point2D.Double(0.0, 1.0));
  185. graphPoints.add(new Point2D.Double(1.0, 1.0));
  186. }
  187. /**
  188. * Returns the ManualState.
  189. *
  190. * @return boolean Manual State
  191. */
  192. public boolean getManualState() {
  193. return this.manualActive;
  194. }
  195. /**
  196. * Returns the autoActive.
  197. *
  198. * @return boolean autoActive
  199. */
  200. public boolean getAutoActive() {
  201. return autoActive;
  202. }
  203. /**
  204. * Set the overall value of the Switch (manual mode).
  205. *
  206. * @param mode
  207. * the mode (boolean)
  208. */
  209. public void setManualMode(boolean mode) {
  210. manualMode = mode;
  211. }
  212. /**
  213. * Get manualmode state.
  214. *
  215. * @return boolean manual mode state
  216. */
  217. public boolean getManualMode() {
  218. return manualMode;
  219. }
  220. //interfaces.GraphEditable
  221. @Override
  222. public Graphtype getGraphType() {
  223. return Graphtype.boolGraph;
  224. }
  225. @Override
  226. public LinkedList<Double> getStateGraph() {
  227. return graphPoints;
  228. }
  229. @Override
  230. public void reset() {
  231. initGraphPoints();
  232. sampleGraph();
  233. }
  234. @Override
  235. public void sampleGraph() {
  236. activeAt = sampleGraph(100);
  237. }
  238. /**
  239. * 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].
  240. * @param sampleLength amount of samplePositions. The positions are equidistant on the Range[0,1].
  241. * @return the boolean array of samplepoints.
  242. */
  243. private boolean[] sampleGraph(int sampleLength)
  244. {
  245. ListIterator<Point2D.Double> iter = this.graphPoints.listIterator();
  246. Point.Double before = iter.next();
  247. Point.Double after = iter.next();
  248. boolean [] activeTriggerPos = new boolean[sampleLength];
  249. for(int i = 0; i<sampleLength ; i++)
  250. {
  251. double graphX = (double)i / (double) (sampleLength - 1); //from 0.0 to 1.0
  252. if(graphX > after.x)
  253. {
  254. before = after;
  255. after = iter.next();
  256. }
  257. activeTriggerPos[i] = (before.getY() >= 0.5);
  258. }
  259. return activeTriggerPos;
  260. }
  261. //interfaces.LocalMode
  262. @Override
  263. public void setLocalPeriod(int period) {
  264. localPeriod=period;
  265. }
  266. @Override
  267. public int getLocalPeriod() {
  268. return localPeriod;
  269. }
  270. @Override
  271. public boolean isUsingLocalPeriod() {
  272. return localPeriodActive;
  273. }
  274. @Override
  275. public void setUseLocalPeriod(boolean state) {
  276. this.localPeriodActive=state;
  277. }
  278. @Override
  279. public HolonSwitch makeCopy(){
  280. return new HolonSwitch(this);
  281. }
  282. public String toString() {
  283. return name + "[ID:" + id + "]";
  284. }
  285. }