SimulationManager.java 15 KB

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