SimulationManager.java 15 KB

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