SimulationManager.java 14 KB

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