SimulationManager.java 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  1. package ui.controller;
  2. import classes.*;
  3. import classes.comparator.EnergyMinToMaxComparator;
  4. import classes.comparator.MinEnergyComparator;
  5. import classes.comparator.TotalEnergyComparator;
  6. import ui.model.Model;
  7. import ui.view.FlexiblePane;
  8. import ui.view.MyCanvas;
  9. import java.util.ArrayList;
  10. import java.util.HashMap;
  11. /**
  12. * Controller for Simulation.
  13. *
  14. * @author Gruppe14
  15. */
  16. public class SimulationManager {
  17. int global = 0;
  18. private Model model;
  19. private ArrayList<AbstractCpsObject> objectsToHandle;
  20. // private ArrayList<CpsEdge> allConnections;
  21. private ArrayList<SubNet> subNets;
  22. private ArrayList<CpsEdge> brokenEdges;
  23. private MyCanvas canvas;
  24. private int timeStep;
  25. private HashMap<Integer, Float> tagTable = new HashMap<>();
  26. private FlexiblePane flexPane;
  27. private HashMap<HolonElement, Float> flexDevicesTurnedOnThisTurn = new HashMap<>();
  28. /**
  29. * Constructor.
  30. *
  31. * @param m Model
  32. */
  33. SimulationManager(Model m) {
  34. canvas = null;
  35. model = m;
  36. subNets = new ArrayList<>();
  37. brokenEdges = new ArrayList<>();
  38. }
  39. /**
  40. * calculates the flow of the edges and the supply for objects.
  41. *
  42. * @param x current Iteration
  43. */
  44. void calculateStateForTimeStep(int x) {
  45. reset();
  46. timeStep = x;
  47. searchForSubNets();
  48. for (SubNet singleSubNet : subNets) {
  49. resetConnections(singleSubNet.getObjects().get(0), new ArrayList<>(), new ArrayList<>());
  50. }
  51. for (SubNet singleSubNet : subNets) {
  52. float production = calculateEnergyWithoutFlexDevices("prod", singleSubNet, timeStep);
  53. float consumption = calculateEnergyWithoutFlexDevices("cons", singleSubNet, timeStep);
  54. // surplus of energy is computed by sum, since consumption is a negative value
  55. float energySurplus = production + consumption;
  56. float minConsumption = calculateMinimumEnergy(singleSubNet, timeStep);
  57. // --------------- use flexible devices ---------------
  58. if (energySurplus != 0 && model.useFlexibleDevices()) {
  59. turnOnFlexibleDevices(singleSubNet, energySurplus, x);
  60. // if (!flexDevicesTurnedOnThisTurn.isEmpty()) {
  61. // System.out.println("The following devices were turned on in this turn: ");
  62. // System.out.println(flexDevicesTurnedOnThisTurn.toString());
  63. // }
  64. // recompute after having examined/turned on all flexible devices
  65. production = calculateEnergyWithFlexDevices("prod", singleSubNet, timeStep);
  66. consumption = calculateEnergyWithFlexDevices("cons", singleSubNet, timeStep);
  67. energySurplus = production + consumption;
  68. }
  69. // --------------- set flow simulation ---------------
  70. setFlowSimulation(singleSubNet);
  71. // --------------- visualise graph ---------------
  72. /**
  73. * production of subnets, that might be partially turned on/off
  74. */
  75. float currentProduction = production;
  76. /**
  77. * HolonObjects that can be partially Supplied but might be fully Supplied
  78. */
  79. ArrayList<HolonObject> partiallySuppliedList = new ArrayList<HolonObject>();
  80. /**
  81. * supply Buildings with minimal Energy first, if conflicts happen
  82. */
  83. singleSubNet.getObjects().sort(new MinEnergyComparator(x));
  84. for (HolonObject hl : singleSubNet.getObjects()) {
  85. if (hl.getState() != HolonObject.NO_ENERGY
  86. && hl.getState() != HolonObject.PRODUCER) {
  87. for (int i = 0; i < hl.getConnections().size(); i++) {
  88. CpsEdge edge = hl.getConnectedTo().get(i);
  89. if (edge.isWorking() && edge.getFlow() > 0
  90. || edge.getCapacity() == CpsEdge.CAPACITY_INFINITE) {
  91. if ((production + consumption) >= 0) {
  92. if (energySurplus > 0) {
  93. hl.setState(HolonObject.OVER_SUPPLIED);
  94. } else {
  95. hl.setState(HolonObject.SUPPLIED);
  96. }
  97. } else {
  98. if ((production + minConsumption) >= 0) {
  99. hl.setState(HolonObject.PARTIALLY_SUPPLIED);
  100. } else if (hl.checkIfPartiallySupplied(timeStep)) {
  101. hl.setState(HolonObject.PARTIALLY_SUPPLIED);
  102. } else {
  103. /**
  104. * Case that only some HolonObjects can be supplied
  105. */
  106. if(-hl.getCurrentEnergyAtTimeStep(x)<=currentProduction){
  107. hl.setState(HolonObject.PARTIALLY_SUPPLIED);
  108. currentProduction += hl.getMinEnergy(x);
  109. partiallySuppliedList.add(hl);
  110. }else if(-hl.getMinEnergy(x)<=currentProduction){
  111. hl.setState(HolonObject.PARTIALLY_SUPPLIED);
  112. currentProduction += hl.getMinEnergy(x);
  113. partiallySuppliedList.add(hl);
  114. }else{
  115. hl.setState(HolonObject.NOT_SUPPLIED);
  116. //currentProduction += hl.getCurrentEnergyAtTimeStep(x);
  117. }
  118. }
  119. }
  120. break;
  121. }
  122. }
  123. /**
  124. * check if some object cn self supply itself
  125. */
  126. if (hl.checkIfPartiallySupplied(timeStep)
  127. && hl.getState() != HolonObject.SUPPLIED
  128. && hl.getState() != HolonObject.OVER_SUPPLIED) {
  129. hl.setState(HolonObject.PARTIALLY_SUPPLIED);
  130. }
  131. }
  132. }
  133. /**
  134. * check if some partially supplied building might be fully supplied.
  135. */
  136. partiallySuppliedList.sort(new EnergyMinToMaxComparator(x));
  137. for(HolonObject part: partiallySuppliedList){
  138. currentProduction -= part.getMinEnergy(x);
  139. /*
  140. * if possible, supply fully
  141. */
  142. if(-part.getCurrentEnergyAtTimeStep(x)<=currentProduction){
  143. part.setState(HolonObject.SUPPLIED);
  144. currentProduction += part.getCurrentEnergyAtTimeStep(x);
  145. }else{
  146. currentProduction += part.getMinEnergy(x);
  147. }
  148. }
  149. }
  150. canvas.repaint();
  151. flexPane.recalculate();
  152. }
  153. /**
  154. * search for all flexible devices in the network and turn them on, until energy surplus = 0
  155. * or all devices have been examined.
  156. *
  157. * This code could be compressed (cases inside over- and underproduction are the same), but we decided that it
  158. * is better readable this way
  159. *
  160. * @param subNet the subnet
  161. * @param energySurplus the current surplus of energy
  162. */
  163. private void turnOnFlexibleDevices(SubNet subNet, float energySurplus, int timestep) {
  164. for (HolonObject holonOb : subNet.getObjects()) {
  165. for (HolonElement holonEl : holonOb.getElements()) {
  166. // if this element is flexible and active (can be considered for calculations)
  167. if (holonEl.isFlexible() && holonEl.isActive()) {
  168. float energyAvailableSingle = holonEl.getAvailableEnergyAt(timestep);
  169. float energyAvailableMultiple = energyAvailableSingle * holonEl.getAmount();
  170. // ------------- flexible consumer / OVERPRODUCTION -------------
  171. if (energyAvailableMultiple < 0 && energySurplus > 0) {
  172. // if there is more wasted energy than energy that this device can give, give all energy available
  173. if (Math.abs(energyAvailableMultiple) <= Math.abs(energySurplus)) {
  174. energySurplus += energyAvailableMultiple;
  175. // set the new energy consumption to the maximum
  176. holonEl.setEnergyPerElement(energyAvailableSingle);
  177. flexDevicesTurnedOnThisTurn.put(holonEl, energyAvailableMultiple);
  178. }
  179. // else: we just need to turn on part of the flexible energy available
  180. else {
  181. float energyNeeded = -energySurplus;
  182. energySurplus += energyNeeded; // should give 0, but was kept this was for consistency
  183. // the energy needed divided through the amount of elements
  184. holonEl.setEnergyPerElement(energyNeeded / holonEl.getAmount());
  185. flexDevicesTurnedOnThisTurn.put(holonEl, energyNeeded);
  186. }
  187. }
  188. // ------------- flexible producer / UNDERPRODUCTION -------------
  189. else if (energyAvailableMultiple > 0 && energySurplus < 0) {
  190. // if there is more energy needed than this device can give, give all the energy available
  191. if (Math.abs(energyAvailableMultiple) <= Math.abs(energySurplus)) {
  192. energySurplus += energyAvailableMultiple;
  193. // set the new energy consumption to the maximum
  194. holonEl.setEnergyPerElement(energyAvailableSingle);
  195. flexDevicesTurnedOnThisTurn.put(holonEl, energyAvailableMultiple);
  196. }
  197. // else: we just need to turn on part of the flexible energy available
  198. else {
  199. float energyNeeded = -energySurplus;
  200. int i = 0;
  201. energySurplus += energyNeeded; // should give 0, but was kept this was for consistency
  202. // the energy needed divided through the amount of elements
  203. holonEl.setEnergyPerElement(energyNeeded / holonEl.getAmount());
  204. flexDevicesTurnedOnThisTurn.put(holonEl, energyNeeded);
  205. }
  206. }
  207. }
  208. if (energySurplus == 0) {
  209. break;
  210. }
  211. }
  212. if (energySurplus == 0) {
  213. break;
  214. }
  215. }
  216. }
  217. /**
  218. * Set Flow Simulation.
  219. *
  220. * @param sN Subnet
  221. */
  222. private void setFlowSimulation(SubNet sN) {
  223. ArrayList<AbstractCpsObject> producers = new ArrayList<>();
  224. AbstractCpsObject tmp = null;
  225. tagTable = new HashMap<>();
  226. // traverse all objects in this subnet
  227. for (HolonObject hl : sN.getObjects()) {
  228. float energy = hl.getCurrentEnergyAtTimeStep(timeStep);
  229. // if their production is higher than their consumption
  230. if (energy > 0) {
  231. tagTable.put(hl.getId(), energy);
  232. hl.addTag(hl.getId());
  233. for (CpsEdge edge : hl.getConnections()) {
  234. if (edge.isWorking()) {
  235. // set other end of edge as tmp-object
  236. // and add this end to the other end's tag-list
  237. AbstractCpsObject a = edge.getA();
  238. AbstractCpsObject b = edge.getB();
  239. if (a.getId() == hl.getId()) {
  240. b.addTag(hl.getId());
  241. tmp = b;
  242. }
  243. if (b.getId() == hl.getId()) {
  244. a.addTag(hl.getId());
  245. tmp = a;
  246. }
  247. edge.setFlow(edge.getFlow() + energy);
  248. edge.calculateState();
  249. edge.addTag(hl.getId());
  250. if (edge.isWorking() && !producers.contains(tmp)) {
  251. if (tmp instanceof HolonSwitch) {
  252. if (((HolonSwitch) tmp).getState(timeStep)) {
  253. producers.add(tmp);
  254. }
  255. } else if (!(tmp instanceof CpsUpperNode)) {
  256. producers.add(tmp);
  257. }
  258. }
  259. }
  260. }
  261. }
  262. }
  263. setFlowSimRec(producers, 0);
  264. }
  265. /**
  266. * Set Flow Simulation Rec.
  267. *
  268. * @param nodes the nodes
  269. * @param iter the Iteration
  270. */
  271. private void setFlowSimRec(ArrayList<AbstractCpsObject> nodes, int iter) {
  272. ArrayList<AbstractCpsObject> newNodes = new ArrayList<>();
  273. ArrayList<CpsEdge> changedEdges = new ArrayList<>();
  274. AbstractCpsObject tmp;
  275. if (nodes.size() != 0) {
  276. for (AbstractCpsObject cps : nodes) {
  277. // check whether the cps is in a legit state if it is a switch
  278. if (legitState(cps)) {
  279. for (CpsEdge edge : cps.getConnections()) {
  280. // is edge working
  281. // and does the edge's tag-list not (yet) contain all tags of the cps
  282. if (edge.isWorking()
  283. && (!(edge.containsTags(edge.getTags(), cps.getTag())))) {
  284. if (edge.getA().getId() == cps.getId()) {
  285. tmp = edge.getB();
  286. } else {
  287. tmp = edge.getA();
  288. }
  289. for (Integer tag : cps.getTag()) {
  290. if (!(edge.getTags().contains(tag))
  291. && !(edge.getPseudoTags().contains(tag))) {
  292. edge.setFlow(edge.getFlow() + tagTable.get(tag));
  293. edge.addTag(tag);
  294. }
  295. }
  296. // uppernodes do not spread energy
  297. if (!(tmp instanceof CpsUpperNode)) {
  298. for (Integer tag : tmp.getTag()) {
  299. if (!(edge.getTags().contains(tag))
  300. && tagTable.get(tag) != null
  301. && !(edge.getPseudoTags().contains(tag))) {
  302. edge.setFlow(edge.getFlow() + tagTable.get(tag));
  303. edge.addPseudoTag(tag);
  304. changedEdges.add(edge);
  305. }
  306. }
  307. }
  308. edge.calculateState();
  309. if (edge.isWorking()
  310. && !(tmp instanceof CpsUpperNode)) {
  311. tmp.addAllPseudoTags(cps.getTag());
  312. if (!newNodes.contains(tmp)) {
  313. newNodes.add(tmp);
  314. }
  315. }
  316. }
  317. }
  318. }
  319. }
  320. setPseudoTags(newNodes, changedEdges);
  321. setFlowSimRec(newNodes, iter + 1);
  322. }
  323. }
  324. /**
  325. * Set the Pseudo Tags.
  326. *
  327. * @param nodes Array of AbstractCpsObjects
  328. */
  329. private void setPseudoTags(ArrayList<AbstractCpsObject> nodes, ArrayList<CpsEdge> edges) {
  330. for (AbstractCpsObject node : nodes) {
  331. node.recalculateTags();
  332. node.setPseudoTags(new ArrayList<>());
  333. }
  334. for (CpsEdge edge : edges) {
  335. edge.recalculateTags();
  336. edge.setPseudoTag(new ArrayList<>());
  337. }
  338. }
  339. /**
  340. * Reset the Connection.
  341. *
  342. * @param cps CpsObject
  343. * @param visitedObj the visited Objects
  344. * @param visitedEdges the visited Edges
  345. */
  346. private void resetConnections(AbstractCpsObject cps, ArrayList<Integer> visitedObj,
  347. ArrayList<CpsEdge> visitedEdges) {
  348. visitedObj.add(cps.getId());
  349. cps.resetTags();
  350. for (CpsEdge e : cps.getConnections()) {
  351. if (!(visitedEdges.contains(e))) {
  352. e.setFlow(0);
  353. e.calculateState();
  354. e.setTags(new ArrayList<>());
  355. visitedEdges.add(e);
  356. if (!(visitedObj.contains(e.getA().getId()))) {
  357. resetConnections(e.getA(), visitedObj, visitedEdges);
  358. e.getA().resetTags();
  359. }
  360. if (!(visitedObj.contains(e.getB().getId()))) {
  361. resetConnections(e.getB(), visitedObj, visitedEdges);
  362. e.getB().resetTags();
  363. }
  364. }
  365. }
  366. }
  367. /**
  368. * calculates the energy of either all producers or consumers.
  369. * Flexible devices are filtered out
  370. *
  371. * @param type Type
  372. * @param sN Subnet
  373. * @param x Integer
  374. * @return The Energy
  375. */
  376. private float calculateEnergyWithoutFlexDevices(String type, SubNet sN, int x) {
  377. float energy = 0;
  378. for (HolonObject hl : sN.getObjects()) {
  379. float currentEnergyWithoutFlexibles = hl.getCurrentEnergyAtTimeStepWithoutFlexiblesAndResetFlexibles(x);
  380. if (type.equals("prod")) {
  381. if (currentEnergyWithoutFlexibles > 0) {
  382. energy += currentEnergyWithoutFlexibles;
  383. hl.setState(HolonObject.PRODUCER);
  384. }
  385. }
  386. if (type.equals("cons")) {
  387. if (currentEnergyWithoutFlexibles < 0) {
  388. energy = energy + currentEnergyWithoutFlexibles;
  389. hl.setState(HolonObject.NOT_SUPPLIED);
  390. }
  391. }
  392. if (currentEnergyWithoutFlexibles == 0) {
  393. hl.setState(HolonObject.NO_ENERGY);
  394. }
  395. }
  396. return energy;
  397. }
  398. /**
  399. * calculates the energy of either all producers or consumers.
  400. * Flexible devices are filtered out
  401. *
  402. * @param type Type
  403. * @param sN Subnet
  404. * @param x Integer
  405. * @return The Energy
  406. */
  407. private float calculateEnergyWithFlexDevices(String type, SubNet sN, int x) {
  408. float energy = 0;
  409. for (HolonObject hl : sN.getObjects()) {
  410. float currentEnergy = hl.getCurrentEnergyAtTimeStep(x);
  411. if (type.equals("prod")) {
  412. if (currentEnergy > 0) {
  413. energy += currentEnergy;
  414. hl.setState(HolonObject.PRODUCER);
  415. }
  416. }
  417. if (type.equals("cons")) {
  418. if (currentEnergy < 0) {
  419. energy = energy + currentEnergy;
  420. hl.setState(HolonObject.NOT_SUPPLIED);
  421. }
  422. }
  423. if (currentEnergy == 0) {
  424. hl.setState(HolonObject.NO_ENERGY);
  425. }
  426. }
  427. return energy;
  428. }
  429. /**
  430. * Calculate the Minimum Energy.
  431. *
  432. * @param sN Subnet
  433. * @param x Integer
  434. * @return the Calculated minimum Energy
  435. */
  436. private float calculateMinimumEnergy(SubNet sN, int x) {
  437. float min = 0;
  438. float minElement = 0;
  439. for (HolonObject hl : sN.getObjects()) {
  440. if (hl.getElements().size() > 0 && hl.getElements().get(0).getOverallEnergyAtTimeStep(x) < 0) {
  441. minElement = hl.getElements().get(0).getOverallEnergyAtTimeStep(x);
  442. }
  443. for (HolonElement he : hl.getElements()) {
  444. float overallEnergy = he.getOverallEnergyAtTimeStep(x);
  445. if (minElement < overallEnergy && overallEnergy < 0) {
  446. minElement = overallEnergy;
  447. }
  448. }
  449. min = min + minElement;
  450. }
  451. return min;
  452. }
  453. /**
  454. * generates all subNets from all objectsToHandle.
  455. */
  456. private void searchForSubNets() {
  457. subNets = new ArrayList<>();
  458. brokenEdges.clear();
  459. boolean end = false;
  460. int i = 0;
  461. AbstractCpsObject cps;
  462. if (objectsToHandle.size() > 0) {
  463. while (!end) {
  464. cps = objectsToHandle.get(i);
  465. SubNet singleSubNet = new SubNet(new ArrayList<>(), new ArrayList<>(),
  466. new ArrayList<>());
  467. singleSubNet = buildSubNet(cps, new ArrayList<>(), singleSubNet);
  468. if (singleSubNet.getObjects().size() != 0) {
  469. subNets.add(singleSubNet);
  470. }
  471. if (0 == objectsToHandle.size()) {
  472. end = true;
  473. }
  474. }
  475. }
  476. }
  477. /**
  478. * recursivly generates a subnet of all objects, that one specific object is
  479. * connected to.
  480. *
  481. * @param cps AbstractCpsObject
  482. * @param visited visited Array of Integer
  483. * @param sN Subnets
  484. * @return Subnet
  485. */
  486. private SubNet buildSubNet(AbstractCpsObject cps, ArrayList<Integer> visited, SubNet sN) {
  487. visited.add(cps.getId());
  488. if (cps instanceof HolonObject) {
  489. sN.getObjects().add((HolonObject) cps);
  490. }
  491. if (cps instanceof HolonSwitch) {
  492. sN.getSwitches().add((HolonSwitch) cps);
  493. }
  494. removeFromToHandle(cps.getId());
  495. AbstractCpsObject a;
  496. AbstractCpsObject b;
  497. for (CpsEdge edge : cps.getConnections()) {
  498. if (edge.isWorking()) {
  499. a = edge.getA();
  500. b = edge.getB();
  501. if (!(cps instanceof HolonSwitch)) {
  502. if (!(sN.getEdges().contains(edge))) {
  503. sN.getEdges().add(edge);
  504. }
  505. }
  506. if (cps instanceof HolonSwitch && ((HolonSwitch) cps).getState(timeStep)) {
  507. if (!(sN.getEdges().contains(edge))) {
  508. sN.getEdges().add(edge);
  509. }
  510. }
  511. if (!visited.contains(a.getId()) && legitState(cps) && !(a instanceof CpsUpperNode)) {
  512. sN = buildSubNet(a, visited, sN);
  513. }
  514. if (!visited.contains(b.getId()) && legitState(cps) && !(b instanceof CpsUpperNode)) {
  515. sN = buildSubNet(b, visited, sN);
  516. }
  517. if (a instanceof CpsUpperNode && a.getId() != cps.getId()) {
  518. edge.setConnected(CpsEdge.CON_UPPER_NODE_NOT_INSIDE);
  519. checkForConnectedStates(b, (CpsUpperNode) a, edge);
  520. }
  521. if (b instanceof CpsUpperNode && b.getId() != cps.getId()) {
  522. edge.setConnected(CpsEdge.CON_UPPER_NODE_NOT_INSIDE);
  523. checkForConnectedStates(a, (CpsUpperNode) b, edge);
  524. }
  525. } else {
  526. brokenEdges.add(edge);
  527. }
  528. }
  529. return sN;
  530. }
  531. /**
  532. * is the Switch in a legitimate State.
  533. *
  534. * @param current AbstractCpsObject
  535. * @return boolean
  536. */
  537. private boolean legitState(AbstractCpsObject current) {
  538. return !(current instanceof HolonSwitch)
  539. || ((HolonSwitch) current).getState(timeStep);
  540. }
  541. // /**
  542. // * ensures that objectsToHandle only contains HolonObjects.
  543. // */
  544. // public void cleanObjectsToHandle() {
  545. // for (int i = 0; i < objectsToHandle.size(); i++) {
  546. // if (!(objectsToHandle.get(i) instanceof HolonObject)) {
  547. // objectsToHandle.remove(i);
  548. // }
  549. // }
  550. // }
  551. /**
  552. * removes an Object that already has been handled.
  553. *
  554. * @param id the Object ID
  555. */
  556. private void removeFromToHandle(int id) {
  557. for (int i = 0; i < objectsToHandle.size(); i++) {
  558. if (objectsToHandle.get(i).getId() == id) {
  559. objectsToHandle.remove(i);
  560. }
  561. }
  562. }
  563. /**
  564. * copies the data of an array of Objects.
  565. *
  566. * @param toCopy the ArrayList of CpsObjects co Copy
  567. */
  568. private void copyObjects(ArrayList<AbstractCpsObject> toCopy) {
  569. for (AbstractCpsObject cps : toCopy) {
  570. if (cps instanceof CpsUpperNode) {
  571. copyObjects(((CpsUpperNode) cps).getNodes());
  572. } else {
  573. objectsToHandle.add(cps);
  574. }
  575. }
  576. }
  577. /**
  578. * Prints the Components auf all subnets.
  579. */
  580. private void printNetsToConsole() {
  581. for (int i = 0; i < subNets.size(); i++) {
  582. SubNet subNet = subNets.get(i);
  583. System.out.println("SUBNET NR:" + i);
  584. subNet.toString(timeStep);
  585. }
  586. }
  587. /**
  588. * Set the Canvas.
  589. *
  590. * @param can the Canvas
  591. */
  592. public void setCanvas(MyCanvas can) {
  593. canvas = can;
  594. }
  595. /**
  596. * Reset all Data to the current state of the Model.
  597. */
  598. public void reset() {
  599. objectsToHandle = new ArrayList<>();
  600. copyObjects(model.getObjectsOnCanvas());
  601. flexDevicesTurnedOnThisTurn = new HashMap<>();
  602. }
  603. /**
  604. * Resets the State of all Edges
  605. */
  606. private void resetEdges() {
  607. for (CpsEdge e : brokenEdges) {
  608. e.setWorkingState(true);
  609. }
  610. }
  611. /**
  612. * Resets the State for the whole Simulation Model
  613. */
  614. void resetSimulation() {
  615. reset();
  616. resetEdges();
  617. }
  618. /**
  619. * Get all Subnets.
  620. *
  621. * @return all Subnets
  622. */
  623. public ArrayList<SubNet> getSubNets() {
  624. return subNets;
  625. }
  626. /**
  627. * Get broken Edges
  628. */
  629. // public ArrayList<CpsEdge> getBrokenEdges() {
  630. // return brokenEdges;
  631. // }
  632. /**
  633. * checks whether a given object is connected to an object inside the upperNode.
  634. * if yes, the state for the edge is changed in "connected" or "not connected"
  635. */
  636. private void checkForConnectedStates(AbstractCpsObject cps, CpsUpperNode cUNode, CpsEdge theEdge) {
  637. AbstractCpsObject tmp;
  638. for (CpsEdge edge : cps.getConnections()) {
  639. if (edge.getA().getId() == cps.getId()) {
  640. tmp = edge.getB();
  641. } else {
  642. tmp = edge.getA();
  643. }
  644. if (cUNode.getNodes().contains(tmp)) {
  645. if (tmp instanceof CpsUpperNode) {
  646. checkForConnectedStates(cps, (CpsUpperNode) tmp, theEdge);
  647. } else {
  648. theEdge.setConnected(CpsEdge.CON_UPPER_NODE_AND_INSIDE);
  649. break;
  650. }
  651. }
  652. }
  653. }
  654. public FlexiblePane getFlexiblePane() {
  655. return flexPane;
  656. }
  657. void setFlexiblePane(FlexiblePane fp) {
  658. flexPane = fp;
  659. }
  660. }