SimulationManager.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. package ui.controller;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import classes.CpsEdge;
  5. import classes.CpsNode;
  6. import classes.CpsUpperNode;
  7. import classes.AbstractCpsObject;
  8. import classes.HolonElement;
  9. import classes.HolonObject;
  10. import classes.HolonSwitch;
  11. import classes.SubNet;
  12. import ui.model.Model;
  13. import ui.view.MyCanvas;
  14. /**
  15. * Controller for Simulation.
  16. *
  17. * @author Gruppe14
  18. */
  19. public class SimulationManager {
  20. private Model model;
  21. private ArrayList<AbstractCpsObject> objectsToHandle;
  22. private ArrayList<CpsEdge> allConnections;
  23. private ArrayList<SubNet> subNets;
  24. private MyCanvas canvas;
  25. private int timeStep;
  26. private boolean simMode;
  27. private HashMap<Integer, Float> tagTable = new HashMap<Integer, Float>();
  28. /**
  29. * Constructor.
  30. *
  31. * @param m
  32. * Model
  33. */
  34. public SimulationManager(Model m) {
  35. canvas = null;
  36. model = m;
  37. subNets = new ArrayList<SubNet>();
  38. simMode = model.getIsSimulation();
  39. }
  40. /**
  41. * calculates the flow of the edges and the supply for objects.
  42. *
  43. * @param x
  44. * current Iteration
  45. */
  46. public void calculateStateForTimeStep(int x) {
  47. simMode = model.getIsSimulation();
  48. timeStep = x;
  49. searchForSubNets();
  50. for (SubNet singleSubNet : subNets) {
  51. resetConnections(singleSubNet.getObjects().get(0), new ArrayList<Integer>(), new ArrayList<CpsEdge>());
  52. }
  53. for (SubNet singleSubNet : subNets) {
  54. float production = calculateEnergy("prod", singleSubNet, timeStep);
  55. float consumption = calculateEnergy("cons", singleSubNet, timeStep);
  56. float minConsumption = calculateMinimumEnergy(singleSubNet, timeStep);
  57. setFlow(singleSubNet, simMode);
  58. for (HolonObject hl : singleSubNet.getObjects()) {
  59. if (!(hl.getState() == 0) && !(hl.getState() == 3)) {
  60. for (int i = 0; i < hl.getConnections().size(); i++) {
  61. CpsEdge edge = hl.getConnectedTo().get(i);
  62. if (edge.getState() && edge.getFlow() > 0 || edge.getCapacity() == -1) {
  63. // 0 = no energy, 1 = not supplied, 2 = supplied, 3
  64. // = producer, 4 = partially supplied
  65. if ((production + consumption) >= 0) {
  66. hl.setState(2);
  67. }
  68. if ((production + consumption) < 0) {
  69. if ((production + minConsumption) >= 0) {
  70. hl.setState(4);
  71. } else if (hl.checkIfPartiallySupplied(timeStep)) {
  72. hl.setState(4);
  73. } else {
  74. hl.setState(1);
  75. }
  76. }
  77. break;
  78. }
  79. hl.setState(1);
  80. }
  81. if (hl.checkIfPartiallySupplied(timeStep) && !(hl.getState() == 2)) {
  82. hl.setState(4);
  83. }
  84. }
  85. }
  86. }
  87. canvas.repaint();
  88. }
  89. /**
  90. * Sets the Flow.
  91. *
  92. * @param sN
  93. * Subnet
  94. * @param simulation
  95. * boolean if Simulation is on
  96. */
  97. public void setFlow(SubNet sN, boolean simulation) {
  98. if (simulation) {
  99. setFlowSimulation(sN);
  100. } else {
  101. setFlowModelation(sN);
  102. }
  103. }
  104. /**
  105. * set the Flow Modelation.
  106. *
  107. * @param sN
  108. * Subnet
  109. */
  110. public void setFlowModelation(SubNet sN) {
  111. for (HolonObject hl : sN.getObjects()) {
  112. float energy = hl.getCurrentEnergyAtTimeStep(timeStep);
  113. if (energy > 0) {
  114. for (CpsEdge e : sN.getEdges()) {
  115. e.setFlow(e.getFlow() + energy);
  116. e.calculateState(simMode);
  117. }
  118. }
  119. }
  120. }
  121. /**
  122. * Set Flow Simulation.
  123. *
  124. * @param sN
  125. * Subnet
  126. */
  127. public void setFlowSimulation(SubNet sN) {
  128. ArrayList<AbstractCpsObject> producers = new ArrayList<AbstractCpsObject>();
  129. AbstractCpsObject tmp = null;
  130. tagTable = new HashMap<Integer, Float>();
  131. for (HolonObject hl : sN.getObjects()) {
  132. float energy = hl.getCurrentEnergyAtTimeStep(timeStep);
  133. if (energy > 0) {
  134. tagTable.put(hl.getID(), energy);
  135. hl.addTag(hl.getID());
  136. for (CpsEdge edge : hl.getConnections()) {
  137. if (edge.getState()) {
  138. if (edge.getA().getID() == hl.getID()) {
  139. edge.getB().addTag(hl.getID());
  140. tmp = edge.getB();
  141. }
  142. if (edge.getB().getID() == hl.getID()) {
  143. edge.getA().addTag(hl.getID());
  144. tmp = edge.getA();
  145. }
  146. edge.setFlow(edge.getFlow() + energy);
  147. edge.calculateState(true);
  148. edge.addTag(hl.getID());
  149. if (edge.getState() && !producers.contains(tmp)) {
  150. if(tmp instanceof HolonSwitch){
  151. if(((HolonSwitch)tmp).getState(timeStep)){
  152. producers.add(tmp);
  153. }
  154. }else{
  155. producers.add(tmp);
  156. }
  157. }
  158. }
  159. }
  160. }
  161. }
  162. setFlowSimRec(producers, 0);
  163. }
  164. /**
  165. * Set Flow Rimulation Rec.
  166. *
  167. * @param nodes
  168. * the nodes
  169. * @param iter
  170. * the Iteration
  171. */
  172. public void setFlowSimRec(ArrayList<AbstractCpsObject> nodes, int iter) {
  173. ArrayList<AbstractCpsObject> newNodes = new ArrayList<AbstractCpsObject>();
  174. ArrayList<CpsEdge> changedEdges = new ArrayList<CpsEdge>();
  175. AbstractCpsObject tmp;
  176. if(nodes.size() != 0){
  177. for(AbstractCpsObject cps: nodes){
  178. if(legitState(cps)){
  179. for(CpsEdge edge: cps.getConnections()){
  180. if(edge.getState() && (!(edge.containsTags(edge.getTags(), cps.getTag())))){
  181. if(edge.getA().getID() == cps.getID()){
  182. tmp = edge.getB();
  183. }else{
  184. tmp = edge.getA();
  185. }
  186. for(Integer tag: cps.getTag()){
  187. if(!(edge.getTags().contains(tag)) && !(edge.getPseudoTags().contains(tag))){
  188. edge.setFlow(edge.getFlow() + tagTable.get(tag));
  189. edge.addTag(tag);
  190. }
  191. }
  192. for(Integer tag: tmp.getTag()){
  193. if(!(edge.getTags().contains(tag)) && tagTable.get(tag) != null && !(edge.getPseudoTags().contains(tag))){
  194. edge.setFlow(edge.getFlow() + tagTable.get(tag));
  195. edge.addPseudoTag(tag);
  196. changedEdges.add(edge);
  197. }
  198. }
  199. edge.calculateState(true);
  200. if(edge.getState()){
  201. tmp.addAllPseudoTags(cps.getTag());
  202. if(!newNodes.contains(tmp)){
  203. newNodes.add(tmp);
  204. }
  205. }
  206. }
  207. }
  208. }
  209. }
  210. setPseudoTags(newNodes, changedEdges);
  211. setFlowSimRec(newNodes, iter+1);
  212. }
  213. }
  214. /**
  215. * Set the Pseudo Tags.
  216. *
  217. * @param nodes
  218. * Array of AbstractCpsObjects
  219. */
  220. public void setPseudoTags(ArrayList<AbstractCpsObject> nodes, ArrayList<CpsEdge> edges) {
  221. for (AbstractCpsObject node : nodes) {
  222. node.recalculateTags();
  223. node.setPseudoTags(new ArrayList<Integer>());
  224. }
  225. for(CpsEdge edge : edges){
  226. edge.recalculateTags();
  227. edge.setPseudoTag(new ArrayList<Integer>());
  228. }
  229. }
  230. /**
  231. * Print the nodes.
  232. *
  233. * @param nodes
  234. * Array of AbstractCpsObject
  235. */
  236. public void printNodes(ArrayList<AbstractCpsObject> nodes) {
  237. System.out.println("new Nodes:");
  238. for (AbstractCpsObject node : nodes) {
  239. System.out.println(node.getID());
  240. }
  241. }
  242. /**
  243. * Get String.
  244. *
  245. * @param tags
  246. * the tags
  247. * @return the String
  248. */
  249. public String getString(ArrayList<Integer> tags) {
  250. String result = "";
  251. for (Integer i : tags) {
  252. result = result + ", " + i;
  253. }
  254. return result;
  255. }
  256. /**
  257. * Merge the Lists.
  258. *
  259. * @param a
  260. * first liust
  261. * @param b
  262. * second list
  263. * @return the Result
  264. */
  265. public ArrayList<Integer> mergeLists(ArrayList<Integer> a, ArrayList<Integer> b) {
  266. ArrayList<Integer> result = new ArrayList<Integer>();
  267. for (Integer i : a) {
  268. if (!(result.contains(i))) {
  269. result.add(i);
  270. }
  271. }
  272. for (Integer j : b) {
  273. if (!(result.contains(j))) {
  274. result.add(j);
  275. }
  276. }
  277. return result;
  278. }
  279. /**
  280. * Reset the Connection.
  281. *
  282. * @param cps
  283. * CpsObject
  284. * @param visitedObj
  285. * the visited Objects
  286. * @param visitedEdges
  287. * the visited Edges
  288. */
  289. public void resetConnections(AbstractCpsObject cps, ArrayList<Integer> visitedObj,
  290. ArrayList<CpsEdge> visitedEdges) {
  291. visitedObj.add(cps.getID());
  292. cps.resetTags();
  293. for (CpsEdge e : cps.getConnections()) {
  294. if (!(visitedEdges.contains(e))) {
  295. e.setFlow(0);
  296. e.calculateState(simMode);
  297. e.setTags(new ArrayList<Integer>());
  298. visitedEdges.add(e);
  299. if (!(visitedObj.contains(e.getA().getID()))) {
  300. resetConnections(e.getA(), visitedObj, visitedEdges);
  301. e.getA().resetTags();
  302. }
  303. if (!(visitedObj.contains(e.getB().getID()))) {
  304. resetConnections(e.getB(), visitedObj, visitedEdges);
  305. e.getB().resetTags();
  306. }
  307. }
  308. }
  309. }
  310. /**
  311. * calculates the energy of either all producers or consumers.
  312. *
  313. * @param type
  314. * Type
  315. * @param sN
  316. * Subnet
  317. * @param x
  318. * Integer
  319. *
  320. * @return The Energy
  321. */
  322. public float calculateEnergy(String type, SubNet sN, int x) {
  323. float energy = 0;
  324. for (HolonObject hl : sN.getObjects()) {
  325. if (type.equals("prod")) {
  326. if (hl.getCurrentEnergyAtTimeStep(x) > 0) {
  327. energy = energy + hl.getCurrentEnergyAtTimeStep(x);
  328. hl.setState(3);
  329. }
  330. }
  331. if (type.equals("cons")) {
  332. if (hl.getCurrentEnergyAtTimeStep(x) < 0) {
  333. energy = energy + hl.getCurrentEnergyAtTimeStep(x);
  334. hl.setState(1);
  335. }
  336. }
  337. if (hl.getCurrentEnergyAtTimeStep(x) == 0) {
  338. hl.setState(0);
  339. }
  340. }
  341. return energy;
  342. }
  343. /**
  344. * Calculate the Minimum Energy.
  345. *
  346. * @param sN
  347. * Subnet
  348. * @param x
  349. * Integer
  350. * @return the Calculated minimum Energy
  351. */
  352. public float calculateMinimumEnergy(SubNet sN, int x) {
  353. float min = 0;
  354. float minElement = 0;
  355. for (HolonObject hl : sN.getObjects()) {
  356. if (hl.getElements().size() > 0 && hl.getElements().get(0).getTotalEnergyAtTimeStep(x) < 0) {
  357. minElement = hl.getElements().get(0).getTotalEnergyAtTimeStep(x);
  358. }
  359. for (HolonElement he : hl.getElements()) {
  360. if (minElement < he.getTotalEnergyAtTimeStep(x) && he.getTotalEnergyAtTimeStep(x) < 0) {
  361. minElement = he.getTotalEnergyAtTimeStep(x);
  362. }
  363. }
  364. min = min + minElement;
  365. }
  366. return min;
  367. }
  368. /**
  369. * generates all subNets from all objectsToHandle.
  370. */
  371. public void searchForSubNets() {
  372. subNets = new ArrayList<SubNet>();
  373. boolean end = false;
  374. int i = 0;
  375. AbstractCpsObject cps;
  376. if (objectsToHandle.size() > 0) {
  377. while (!end) {
  378. cps = objectsToHandle.get(i);
  379. SubNet singleSubNet = new SubNet(new ArrayList<HolonObject>(), new ArrayList<CpsEdge>(),
  380. new ArrayList<HolonSwitch>());
  381. singleSubNet = buildSubNet(cps, new ArrayList<Integer>(), singleSubNet);
  382. if (singleSubNet.getObjects().size() != 0) {
  383. subNets.add(singleSubNet);
  384. }
  385. if (0 == objectsToHandle.size()) {
  386. end = true;
  387. }
  388. }
  389. }
  390. }
  391. /**
  392. * recursivly generates a subnet of all objects, that one specific object is
  393. * connected to.
  394. *
  395. * @param cps
  396. * AbstractCpsObject
  397. * @param visited
  398. * visited Array of Integer
  399. * @param sN
  400. * Subnets
  401. * @return Subnet
  402. */
  403. public SubNet buildSubNet(AbstractCpsObject cps, ArrayList<Integer> visited, SubNet sN) {
  404. visited.add(cps.getID());
  405. if (cps instanceof HolonObject) {
  406. sN.getObjects().add((HolonObject) cps);
  407. }
  408. if (cps instanceof HolonSwitch) {
  409. sN.getSwitches().add((HolonSwitch) cps);
  410. }
  411. removeFromToHandle(cps.getID());
  412. AbstractCpsObject a;
  413. AbstractCpsObject b;
  414. for (CpsEdge edge : cps.getConnections()) {
  415. if(!(simMode && !edge.getState())){
  416. a = edge.getA();
  417. b = edge.getB();
  418. if (!(cps instanceof HolonSwitch)) {
  419. if (!(sN.getEdges().contains(edge))) {
  420. sN.getEdges().add(edge);
  421. }
  422. }
  423. if(cps instanceof HolonSwitch && ((HolonSwitch)cps).getState(timeStep)){
  424. if (!(sN.getEdges().contains(edge))) {
  425. sN.getEdges().add(edge);
  426. }
  427. }
  428. if (!visited.contains(a.getID()) && legitState(cps)) {
  429. sN = buildSubNet(a, visited, sN);
  430. }
  431. if (!visited.contains(b.getID()) && legitState(cps)) {
  432. sN = buildSubNet(b, visited, sN);
  433. }
  434. if(a instanceof CpsUpperNode && a.getID() != cps.getID()){
  435. edge.setConnected(false);
  436. checkForConnectedStates(b, (CpsUpperNode)a, edge);
  437. }
  438. if(b instanceof CpsUpperNode && b.getID() != cps.getID()){
  439. edge.setConnected(false);
  440. checkForConnectedStates(a, (CpsUpperNode)b, edge);
  441. }
  442. }
  443. }
  444. return sN;
  445. }
  446. /**
  447. * is the Switch in a legitimate State.
  448. *
  449. * @param neighbor AbstractCpsObject
  450. * @param current AbstractCpsObject
  451. * @return boolean
  452. */
  453. public boolean legitState(AbstractCpsObject current) {
  454. if (current instanceof HolonSwitch) {
  455. if (((HolonSwitch) current).getState(timeStep)) {
  456. return true;
  457. }else{
  458. return false;
  459. }
  460. }
  461. return true;
  462. }
  463. /**
  464. * removes an Object that already has been handled with.
  465. *
  466. * @param id the Object ID
  467. */
  468. public void removeFromToHandle(int id) {
  469. for (int i = 0; i < objectsToHandle.size(); i++) {
  470. if (objectsToHandle.get(i).getID() == id) {
  471. objectsToHandle.remove(i);
  472. }
  473. }
  474. }
  475. /**
  476. * ensures that objectsToHandle only contains HolonObjects.
  477. */
  478. public void cleanObjectsToHandle() {
  479. for (int i = 0; i < objectsToHandle.size(); i++) {
  480. if (!(objectsToHandle.get(i) instanceof HolonObject)) {
  481. objectsToHandle.remove(i);
  482. }
  483. }
  484. }
  485. /**
  486. * copies the data of an array of Objects.
  487. *
  488. * @param toCopy the ArrayList of CpsObjects co Copy
  489. */
  490. public void copyObjects(ArrayList<AbstractCpsObject> toCopy) {
  491. for (AbstractCpsObject cps : toCopy) {
  492. objectsToHandle.add(cps);
  493. if(cps instanceof CpsUpperNode){
  494. copyObjects(((CpsUpperNode)cps).getNodes());
  495. }
  496. }
  497. }
  498. /**
  499. * Prints the Components auf all subnets.
  500. */
  501. public void printNet() {
  502. for (int i = 0; i < subNets.size(); i++) {
  503. System.out.println("SUBNET NR:" + i);
  504. System.out.println(" Objects:");
  505. for (int j = 0; j < subNets.get(i).getObjects().size(); j++) {
  506. HolonObject hl = subNets.get(i).getObjects().get(j);
  507. System.out.println(" " + hl.getName() + " " + hl.getID());
  508. }
  509. System.out.println(" Edges:");
  510. for (int j = 0; j < subNets.get(i).getEdges().size(); j++) {
  511. CpsEdge edge = subNets.get(i).getEdges().get(j);
  512. System.out.println(" " + edge.getA().getName() + " connected To " + edge.getB().getName());
  513. }
  514. System.out.println(" Switches:");
  515. for (int j = 0; j < subNets.get(i).getSwitches().size(); j++) {
  516. HolonSwitch sw = subNets.get(i).getSwitches().get(j);
  517. System.out.println(" " + sw.getName() + " " + sw.getID() + " State:" + sw.getActiveAt()[timeStep]);
  518. }
  519. }
  520. }
  521. /**
  522. * Set the Canvas.
  523. *
  524. * @param can
  525. * the Canvas
  526. */
  527. public void setCanvas(MyCanvas can) {
  528. canvas = can;
  529. }
  530. /**
  531. * Reset all Data to the current state of the Model.
  532. */
  533. public void reset() {
  534. objectsToHandle = new ArrayList<AbstractCpsObject>();
  535. copyObjects(model.getObjectsOnCanvas());
  536. }
  537. /**
  538. * Get all Subnets.
  539. *
  540. * @return all Subnets
  541. */
  542. public ArrayList<SubNet> getSubNets() {
  543. return subNets;
  544. }
  545. /**
  546. * checks wether a given object is connected to an object inside the upperNode.
  547. * if yes, the state for the edge is changed in "connected" or "not connected"
  548. * @param cps
  549. * @param cUNode
  550. */
  551. public void checkForConnectedStates(AbstractCpsObject cps, CpsUpperNode cUNode, CpsEdge theEdge){
  552. AbstractCpsObject tmp;
  553. for(CpsEdge edge: cps.getConnections()){
  554. if(edge.getA().getID() == cps.getID()){
  555. tmp = edge.getB();
  556. } else{
  557. tmp = edge.getA();
  558. }
  559. if(cUNode.getNodes().contains(tmp)){
  560. if(tmp instanceof CpsUpperNode){
  561. checkForConnectedStates(cps, (CpsUpperNode)tmp, theEdge);
  562. }else{
  563. theEdge.setConnected(true);
  564. break;
  565. }
  566. }
  567. }
  568. }
  569. }