SimulationManager.java 6.7 KB

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