HolonSwitch.java 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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.ArrayList;
  6. import java.util.LinkedList;
  7. import java.util.ListIterator;
  8. import java.util.UUID;
  9. import com.google.gson.annotations.Expose;
  10. import interfaces.GraphEditable;
  11. import interfaces.LocalMode;
  12. import ui.view.IndexTranslator;
  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. private String uniqueID;
  55. /**
  56. * Create a new HolonSwitch with the default name ("Switch"), a default
  57. * value of automatic handle and active status.
  58. *
  59. * @param objName
  60. * String
  61. */
  62. public HolonSwitch(String objName) {
  63. super(objName);
  64. setUseLocalPeriod(false);
  65. activeAt=new boolean[LocalMode.STANDARD_GRAPH_ACCURACY];
  66. setManualState(true);
  67. setAutoState(true);
  68. setManualMode(false);
  69. setGraphPoints(new LinkedList<Point2D.Double>());
  70. initGraphPoints();
  71. sampleGraph();
  72. this.uniqueID = "Switch "+UUID.randomUUID().toString();
  73. }
  74. /**
  75. * Create a copy of an existing HolonSwitch.
  76. *
  77. * @param obj
  78. * the Object to copy
  79. */
  80. public HolonSwitch(AbstractCanvasObject obj) {
  81. super(obj);
  82. System.out.println("SwitchCopy?");
  83. HolonSwitch copyObj = (HolonSwitch)obj;
  84. setLocalPeriod(copyObj.getLocalPeriod());
  85. setUseLocalPeriod(copyObj.isUsingLocalPeriod());
  86. activeAt=new boolean[LocalMode.STANDARD_GRAPH_ACCURACY];
  87. super.setName(obj.getName());
  88. setManualState(copyObj.getManualState());
  89. setAutoState(true);
  90. setGraphPoints(new LinkedList<Point2D.Double>());
  91. for (Point2D.Double p : copyObj.getGraphPoints()) {
  92. this.graphPoints.add(new Point2D.Double(p.getX(),p.getY()));
  93. }
  94. sampleGraph();
  95. setManualMode(copyObj.getManualMode());
  96. this.uniqueID = "Switch "+UUID.randomUUID().toString();
  97. }
  98. /**
  99. * Calculates the state of the Switch.
  100. */
  101. public void switchState() {
  102. if (!manualMode) {
  103. setManualMode(true);
  104. }
  105. if (this.manualActive == true) {
  106. setImage("/Images/switch-off.png");
  107. } else {
  108. setImage("/Images/switch-on.png");
  109. }
  110. this.manualActive = !manualActive;
  111. }
  112. public static String getSwitchClosedImage(){
  113. return "/Images/switch-on.png";
  114. }
  115. public static String getSwitchOpenImage(){
  116. return "/Images/switch-off.png";
  117. }
  118. /**
  119. * Getter for the status of the Switch at a given timestep.
  120. *
  121. * @param timeStep state at given iteration.
  122. * @return state value (true if closed/active, false if open)
  123. */
  124. public boolean getState(int timeStep) {
  125. if (manualMode) {
  126. return this.manualActive;
  127. } else {
  128. return activeAt[IndexTranslator.getEffectiveIndex(this, timeStep)];
  129. }
  130. }
  131. /**
  132. * Change the state of the Switch to manual.
  133. *
  134. * @param state the State
  135. */
  136. public void setManualState(boolean state) {
  137. this.manualActive = state;
  138. setImage();
  139. }
  140. /**
  141. * Set the state of the Switch to automatic.
  142. *
  143. * @param state the State
  144. */
  145. public void setAutoState(boolean state) {
  146. this.autoActive = state;
  147. setImage();
  148. }
  149. /**
  150. * Set Image of the Switch.
  151. */
  152. private void setImage() {
  153. if (manualMode) {
  154. if (!this.manualActive) {
  155. setImage("/Images/switch-off.png");
  156. } else {
  157. setImage("/Images/switch-on.png");
  158. }
  159. } else {
  160. if (!this.autoActive) {
  161. setImage("/Images/switch-off.png");
  162. } else {
  163. setImage("/Images/switch-on.png");
  164. }
  165. }
  166. }
  167. /**
  168. * For automatic use only (through the graph).
  169. *
  170. * @return the Graph Points
  171. */
  172. public LinkedList<Point2D.Double> getGraphPoints() {
  173. return graphPoints;
  174. }
  175. /**
  176. * Set the values of the switch in the graph (auto. mode only).
  177. *
  178. * @param linkedList the Graph points
  179. */
  180. public void setGraphPoints(LinkedList<Double> linkedList) {
  181. this.graphPoints = linkedList;
  182. }
  183. /**
  184. * Initialize the Graph as a closed Switch.
  185. */
  186. private void initGraphPoints()
  187. {
  188. graphPoints.clear();
  189. graphPoints.add(new Point2D.Double(0.0, 1.0));
  190. graphPoints.add(new Point2D.Double(1.0, 1.0));
  191. }
  192. /**
  193. * Returns the ManualState.
  194. *
  195. * @return boolean Manual State
  196. */
  197. public boolean getManualState() {
  198. return this.manualActive;
  199. }
  200. /**
  201. * Returns the autoActive.
  202. *
  203. * @return boolean autoActive
  204. */
  205. public boolean getAutoActive() {
  206. return autoActive;
  207. }
  208. /**
  209. * Set the overall value of the Switch (manual mode).
  210. *
  211. * @param mode
  212. * the mode (boolean)
  213. */
  214. public void setManualMode(boolean mode) {
  215. manualMode = mode;
  216. }
  217. /**
  218. * Get manualmode state.
  219. *
  220. * @return boolean manual mode state
  221. */
  222. public boolean getManualMode() {
  223. return manualMode;
  224. }
  225. //interfaces.GraphEditable
  226. @Override
  227. public Graphtype getGraphType() {
  228. return Graphtype.boolGraph;
  229. }
  230. @Override
  231. public LinkedList<Double> getStateGraph() {
  232. return graphPoints;
  233. }
  234. @Override
  235. public void reset() {
  236. initGraphPoints();
  237. sampleGraph();
  238. }
  239. @Override
  240. public void sampleGraph() {
  241. activeAt = sampleGraph(100);
  242. }
  243. /**
  244. * 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].
  245. * @param sampleLength amount of samplePositions. The positions are equidistant on the Range[0,1].
  246. * @return the boolean array of samplepoints.
  247. */
  248. private boolean[] sampleGraph(int sampleLength)
  249. {
  250. ListIterator<Point2D.Double> iter = this.graphPoints.listIterator();
  251. Point.Double before = iter.next();
  252. Point.Double after = iter.next();
  253. boolean [] activeTriggerPos = new boolean[sampleLength];
  254. for(int i = 0; i<sampleLength ; i++)
  255. {
  256. double graphX = (double)i / (double) (sampleLength - 1); //from 0.0 to 1.0
  257. if(graphX > after.x)
  258. {
  259. before = after;
  260. after = iter.next();
  261. }
  262. activeTriggerPos[i] = (before.getY() >= 0.5);
  263. }
  264. return activeTriggerPos;
  265. }
  266. //interfaces.LocalMode
  267. @Override
  268. public void setLocalPeriod(int period) {
  269. localPeriod=period;
  270. }
  271. @Override
  272. public int getLocalPeriod() {
  273. return localPeriod;
  274. }
  275. @Override
  276. public boolean isUsingLocalPeriod() {
  277. return localPeriodActive;
  278. }
  279. @Override
  280. public void setUseLocalPeriod(boolean state) {
  281. this.localPeriodActive=state;
  282. }
  283. @Override
  284. public HolonSwitch makeCopy(){
  285. return new HolonSwitch(this);
  286. }
  287. public String toString() {
  288. return name + "[ID:" + id + "]";
  289. }
  290. public ArrayList<String> getAllConnectedHolons() {
  291. ArrayList<String> conns = new ArrayList();
  292. //switch is open therefore it does not connect objects
  293. if(!this.isClosed())
  294. return conns;
  295. for(Edge e : this.connections) {
  296. AbstractCanvasObject a = e.getA();
  297. AbstractCanvasObject b = e.getB();
  298. System.out.println("This: "+this.id+" A: "+e.getA().id+" B: "+e.getB().id);
  299. if(b.equals(this)) {
  300. if(a instanceof HolonObject) {
  301. conns.add(((HolonObject) a).holon.getUniqueID());
  302. } else if(a instanceof HolonSwitch) {
  303. conns.addAll(((HolonSwitch)a).getAllConnectedHolons());
  304. } else {
  305. }
  306. } else {
  307. if(b instanceof HolonObject) {
  308. conns.add(((HolonObject) b).holon.getUniqueID());
  309. } else if(b instanceof HolonSwitch) {
  310. conns.addAll(((HolonSwitch)b).getAllConnectedHolons());
  311. } else {
  312. }
  313. }
  314. }
  315. return conns;
  316. }
  317. public String getUniqueID() {
  318. if(this.uniqueID == null)
  319. this.uniqueID = "Switch "+UUID.randomUUID().toString();
  320. return this.uniqueID;
  321. }
  322. /**
  323. * switch is closed is equivalent to on, therefore energy can flow through the switch
  324. * @return
  325. */
  326. public boolean isClosed() {
  327. if( (this.manualMode && this.manualActive) || (!this.manualMode && this.autoActive) ) {
  328. return true;
  329. }
  330. return false;
  331. }
  332. }