SimulationManager.java 15 KB

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