SimulationManager.java 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. package ui.controller;
  2. import java.util.ArrayList;
  3. import classes.CpsEdge;
  4. import classes.CpsNode;
  5. import classes.CpsObject;
  6. import classes.HolonElement;
  7. import classes.HolonObject;
  8. import classes.HolonSwitch;
  9. import classes.subNet;
  10. import ui.model.Model;
  11. import ui.view.MyCanvas;
  12. public class SimulationManager {
  13. private Model model;
  14. private ArrayList<CpsObject> objectsToHandle;
  15. private ArrayList<CpsEdge> allConnections;
  16. private ArrayList<subNet> subNets;
  17. private MyCanvas canvas;
  18. private int timeStep;
  19. public SimulationManager(Model m){
  20. canvas = null;
  21. model = m;
  22. subNets = new ArrayList<subNet>();
  23. }
  24. /**
  25. * calculates the flow of the edges and the supply for objects
  26. * @param x
  27. */
  28. public void calculateStateForTimeStep(int x){
  29. timeStep = x;
  30. searchForSubNets();
  31. for(subNet singleSubNet: subNets){
  32. ResetConnections(singleSubNet.getObjects().get(0), new ArrayList<Integer>(), new ArrayList<CpsEdge>());
  33. }
  34. for(subNet singleSubNet: subNets){
  35. float production = calculateEnergy("prod", singleSubNet, timeStep);
  36. float consumption = calculateEnergy("cons", singleSubNet, timeStep);
  37. float minConsumption = calculateMinimumEnergy( singleSubNet, timeStep);
  38. setFlow(singleSubNet);
  39. for(HolonObject hl: singleSubNet.getObjects()){
  40. if(!(hl.getState() == 0) && !(hl.getState() == 3)){
  41. for(int i = 0; i < hl.getConnections().size(); i++){
  42. CpsEdge edge = hl.getConnectedTo().get(i);
  43. if(edge.getState() && edge.getFlow() > 0 || edge.getCapacity() == -1){
  44. // 0 = no energy, 1 = not supplied, 2 = supplied, 3 = producer, 4 = partially supplied
  45. if((production + consumption) >= 0){
  46. hl.setState(2);
  47. }
  48. if((production + consumption) < 0 ){
  49. if((production + minConsumption) >= 0){
  50. hl.setState(4);
  51. }else if(hl.checkIfPartiallySupplied(timeStep)){
  52. hl.setState(4);
  53. }else {
  54. hl.setState(1);
  55. }
  56. }
  57. break;
  58. }
  59. hl.setState(1);
  60. }
  61. if(hl.checkIfPartiallySupplied(timeStep) && !(hl.getState() == 2)){
  62. hl.setState(4);
  63. }
  64. }
  65. }
  66. }
  67. canvas.repaint();
  68. }
  69. public void setFlow(subNet sN){
  70. for(HolonObject hl: sN.getObjects()){
  71. float energy = hl.getCurrentEnergyAtTimeStep(timeStep);
  72. if(energy > 0){
  73. for(CpsEdge e : sN.getEdges()){
  74. e.setFlow(e.getFlow() + energy);
  75. }
  76. }
  77. }
  78. }
  79. public void ResetConnections(CpsObject cps, ArrayList<Integer> visitedObj, ArrayList<CpsEdge> visitedEdges){
  80. visitedObj.add(cps.getID());
  81. for(CpsEdge e: cps.getConnections()){
  82. if(!(visitedEdges.contains(e))){
  83. e.setFlow(0);
  84. visitedEdges.add(e);
  85. if(!(visitedObj.contains(e.getA().getID()))){
  86. ResetConnections(e.getA(), visitedObj, visitedEdges);
  87. }
  88. if(!(visitedObj.contains(e.getB().getID()))){
  89. ResetConnections(e.getB(), visitedObj, visitedEdges);
  90. }
  91. }
  92. }
  93. }
  94. /**
  95. * calculates the energy of either all producers or consumers
  96. * @param type
  97. * @param sN
  98. * @return
  99. */
  100. public float calculateEnergy(String type, subNet sN, int x){
  101. float energy = 0;
  102. for(HolonObject hl: sN.getObjects()){
  103. if(type.equals("prod")){
  104. if(hl.getCurrentEnergyAtTimeStep(x) > 0){
  105. energy = energy + hl.getCurrentEnergyAtTimeStep(x);
  106. hl.setState(3);
  107. }
  108. }
  109. if(type.equals("cons")){
  110. if(hl.getCurrentEnergyAtTimeStep(x) < 0){
  111. energy = energy + hl.getCurrentEnergyAtTimeStep(x);
  112. hl.setState(1);
  113. }
  114. }
  115. if(hl.getCurrentEnergyAtTimeStep(x) == 0){
  116. hl.setState(0);
  117. }
  118. }
  119. return energy;
  120. }
  121. public float calculateMinimumEnergy(subNet sN, int x){
  122. float min = 0;
  123. float minElement = 0;
  124. for(HolonObject hl: sN.getObjects()){
  125. if(hl.getElements().size() > 0 && hl.getElements().get(0).getTotalEnergyAtTimeStep(x) < 0 ){
  126. minElement = hl.getElements().get(0).getTotalEnergyAtTimeStep(x);
  127. }
  128. for(HolonElement he: hl.getElements()){
  129. if(minElement < he.getTotalEnergyAtTimeStep(x) && he.getTotalEnergyAtTimeStep(x) < 0){
  130. minElement = he.getTotalEnergyAtTimeStep(x);
  131. }
  132. }
  133. min = min + minElement;
  134. }
  135. return min;
  136. }
  137. /**
  138. * generates all subNets from all objectsToHandle
  139. */
  140. public void searchForSubNets(){
  141. subNets = new ArrayList<subNet>();
  142. boolean end = false;
  143. int i = 0;
  144. CpsObject cps;
  145. if(objectsToHandle.size() > 0){
  146. while(!end){
  147. cps = objectsToHandle.get(i);
  148. subNet singleSubNet = new subNet(new ArrayList<HolonObject>(), new ArrayList<CpsEdge>(), new ArrayList<HolonSwitch>());
  149. singleSubNet = buildSubNet(cps, new ArrayList<Integer>(), singleSubNet);
  150. if(singleSubNet.getObjects().size() != 0){
  151. subNets.add(singleSubNet);
  152. }
  153. if(0 == objectsToHandle.size()){
  154. end = true;
  155. }
  156. }
  157. }
  158. }
  159. /**
  160. * recursivly generates a subnet of all objects, that one specific object is connected to
  161. * @param cps
  162. * @param visited
  163. * @param sN
  164. * @return
  165. */
  166. public subNet buildSubNet(CpsObject cps, ArrayList<Integer> visited, subNet sN){
  167. visited.add(cps.getID());
  168. if(cps instanceof HolonObject){
  169. sN.getObjects().add((HolonObject) cps);
  170. }
  171. if(cps instanceof HolonSwitch){
  172. sN.getSwitches().add((HolonSwitch) cps);
  173. }
  174. removeFromToHandle(cps.getID());
  175. CpsObject A;
  176. CpsObject B;
  177. for(CpsEdge edge: cps.getConnections()){
  178. A = edge.getA();
  179. B = edge.getB();
  180. if(!(cps instanceof HolonSwitch)){
  181. if(!(sN.getEdges().contains(edge))){
  182. sN.getEdges().add(edge);
  183. }
  184. }
  185. if(!visited.contains(A.getID()) && legitState(A, cps)){
  186. sN = buildSubNet(A, visited, sN);
  187. }
  188. if(!visited.contains(B.getID()) && legitState(B, cps)){
  189. sN = buildSubNet(B, visited, sN);
  190. }
  191. }
  192. return sN;
  193. }
  194. public boolean legitState(CpsObject neighbor, CpsObject current){
  195. if(current instanceof HolonSwitch){
  196. if(((HolonSwitch) current).getActiveAt()[timeStep]){
  197. if(neighbor instanceof HolonSwitch){
  198. if(((HolonSwitch) neighbor).getActiveAt()[timeStep]){
  199. return true;
  200. }else{
  201. return false;
  202. }
  203. }
  204. }else{
  205. return false;
  206. }
  207. }
  208. return true;
  209. }
  210. /**
  211. * removes an Object that already has been handled with
  212. * @param id
  213. */
  214. public void removeFromToHandle(int id){
  215. for(int i = 0; i < objectsToHandle.size(); i++){
  216. if(objectsToHandle.get(i).getID() == id){
  217. objectsToHandle.remove(i);
  218. }
  219. }
  220. }
  221. /**
  222. * ensures that objectsToHandle only contains HolonObjects
  223. */
  224. public void cleanObjectsToHandle(){
  225. for(int i = 0; i < objectsToHandle.size(); i++){
  226. if(!(objectsToHandle.get(i) instanceof HolonObject)){
  227. objectsToHandle.remove(i);
  228. }
  229. }
  230. }
  231. /**
  232. * copies the data of an array of Objects
  233. * @param toCopy
  234. */
  235. public void copyObjects(ArrayList<CpsObject> toCopy){
  236. objectsToHandle = new ArrayList<CpsObject>();
  237. for(CpsObject cps: toCopy){
  238. objectsToHandle.add(cps);
  239. }
  240. }
  241. /**
  242. * Prints the Components auf all subnets
  243. */
  244. public void printNet(){
  245. for(int i = 0; i < subNets.size(); i++){
  246. System.out.println("SUBNET NR:" + i);
  247. System.out.println(" Objects:");
  248. for(int j = 0; j < subNets.get(i).getObjects().size(); j++){
  249. HolonObject hl = subNets.get(i).getObjects().get(j);
  250. System.out.println(" " + hl.getName() + " " + hl.getID());
  251. }
  252. System.out.println(" Edges:");
  253. for(int j = 0; j < subNets.get(i).getEdges().size(); j++){
  254. CpsEdge edge = subNets.get(i).getEdges().get(j);
  255. System.out.println(" " + edge.getA().getName() + " connected To " + edge.getB().getName());
  256. }
  257. System.out.println(" Switches:");
  258. for(int j = 0; j < subNets.get(i).getSwitches().size(); j++){
  259. HolonSwitch sw = subNets.get(i).getSwitches().get(j);
  260. System.out.println(" " + sw.getName() + " " + sw.getID() + " State:" + sw.getActiveAt()[timeStep]);
  261. }
  262. }
  263. }
  264. public void setCanvas(MyCanvas can){
  265. canvas = can;
  266. }
  267. public void reset(){
  268. copyObjects(model.getObjectsOnCanvas());
  269. }
  270. }