SimulationManager.java 14 KB

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