SimulationManager.java 7.4 KB

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