SimulationManager.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. package ui.controller;
  2. import java.util.ArrayList;
  3. import classes.CpsEdge;
  4. import classes.CpsNode;
  5. import classes.CpsObject;
  6. import classes.HolonObject;
  7. import classes.subNet;
  8. import ui.model.Model;
  9. import ui.view.MyCanvas;
  10. public class SimulationManager {
  11. private Model model;
  12. private ArrayList<CpsObject> objectsToHandle;
  13. private ArrayList<subNet> subNets;
  14. private MyCanvas canvas;
  15. public SimulationManager(Model m){
  16. canvas = null;
  17. model = m;
  18. subNets = new ArrayList<subNet>();
  19. }
  20. /**
  21. * calculates the flow of the edges and the supply for objects
  22. * @param x
  23. */
  24. public void calculateStateForTimeStep(int x){
  25. searchForSubNets();
  26. for(subNet singleSubNet: subNets){
  27. float production = calculateEnergy("prod", singleSubNet, x);
  28. float consumption = calculateEnergy("cons", singleSubNet, x);
  29. for(CpsEdge e: singleSubNet.getEdges()){
  30. e.setFlow(production);
  31. }
  32. for(HolonObject hl: singleSubNet.getObjects()){
  33. for(int i = 0; i < hl.getConnections().size(); i++){
  34. CpsEdge edge = hl.getConnectedTo().get(i);
  35. if(edge.getState()){
  36. System.out.println(production + consumption);
  37. if((production + consumption) >= 0 ){
  38. hl.setSupplied(true);
  39. }else{
  40. hl.setSupplied(false);
  41. }
  42. break;
  43. }
  44. hl.setSupplied(false);
  45. }
  46. }
  47. }
  48. canvas.repaint();
  49. }
  50. /**
  51. * calculates the energy of either all producers or consumers
  52. * @param type
  53. * @param sN
  54. * @return
  55. */
  56. public float calculateEnergy(String type, subNet sN, int x){
  57. float energy = 0;
  58. for(HolonObject hl: sN.getObjects()){
  59. if(type.equals("prod")){
  60. if(hl.getCurrentEnergyAtTimeStep(x) > 0){
  61. energy = energy + hl.getCurrentEnergyAtTimeStep(x);
  62. }
  63. }
  64. if(type.equals("cons")){
  65. if(hl.getCurrentEnergyAtTimeStep(x) < 0){
  66. energy = energy + hl.getCurrentEnergyAtTimeStep(x);
  67. }
  68. }
  69. }
  70. return energy;
  71. }
  72. /**
  73. * generates all subNets from all objectsToHandle
  74. */
  75. public void searchForSubNets(){
  76. subNets = new ArrayList<subNet>();
  77. boolean end = false;
  78. int i = 0;
  79. CpsObject cps;
  80. if(objectsToHandle.size() > 0){
  81. while(!end){
  82. cps = objectsToHandle.get(i);
  83. subNet singleSubNet = new subNet(new ArrayList<HolonObject>(), new ArrayList<CpsEdge>());
  84. singleSubNet = buildSubNet(cps, new ArrayList<Integer>(), singleSubNet);
  85. if(singleSubNet.getObjects().size() != 0){
  86. subNets.add(singleSubNet);
  87. }
  88. if(0 == objectsToHandle.size()){
  89. end = true;
  90. }
  91. }
  92. }
  93. //printNet();
  94. }
  95. /**
  96. * recursivly generates a subnet of all objects, that one specific object is connected to
  97. * @param cps
  98. * @param visited
  99. * @param sN
  100. * @return
  101. */
  102. public subNet buildSubNet(CpsObject cps, ArrayList<Integer> visited, subNet sN){
  103. visited.add(cps.getID());
  104. if(cps instanceof HolonObject){
  105. sN.getObjects().add((HolonObject) cps);
  106. }
  107. removeFromToHandle(cps.getID());
  108. for(CpsEdge edge: cps.getConnections()){
  109. if(!(sN.getEdges().contains(edge))){
  110. sN.getEdges().add(edge);
  111. }
  112. if(!visited.contains(edge.getA().getID())){
  113. sN = buildSubNet(edge.getA(), visited, sN);
  114. }
  115. if(!visited.contains(edge.getB().getID())){
  116. sN = buildSubNet(edge.getB(), visited, sN);
  117. }
  118. }
  119. return sN;
  120. }
  121. /**
  122. * removes an Object that already has been handled with
  123. * @param id
  124. */
  125. public void removeFromToHandle(int id){
  126. for(int i = 0; i < objectsToHandle.size(); i++){
  127. if(objectsToHandle.get(i).getID() == id){
  128. objectsToHandle.remove(i);
  129. }
  130. }
  131. }
  132. /**
  133. * ensures that objectsToHandle only contains HolonObjects
  134. */
  135. public void cleanObjectsToHandle(){
  136. for(int i = 0; i < objectsToHandle.size(); i++){
  137. if(!(objectsToHandle.get(i) instanceof HolonObject)){
  138. objectsToHandle.remove(i);
  139. }
  140. }
  141. }
  142. /**
  143. * copies the data of an array of Objects
  144. * @param toCopy
  145. */
  146. public void copyObjects(ArrayList<CpsObject> toCopy){
  147. objectsToHandle = new ArrayList<CpsObject>();
  148. for(CpsObject cps: toCopy){
  149. objectsToHandle.add(cps);
  150. }
  151. }
  152. /**
  153. * Prints the Components auf all subnets
  154. */
  155. public void printNet(){
  156. for(int i = 0; i < subNets.size(); i++){
  157. System.out.println("SUBNET NR:" + i);
  158. System.out.println(" Objects:");
  159. for(int j = 0; j < subNets.get(i).getObjects().size(); j++){
  160. HolonObject hl = subNets.get(i).getObjects().get(j);
  161. System.out.println(" " + hl.getName() + " " + hl.getID());
  162. }
  163. System.out.println(" Edges:");
  164. for(int j = 0; j < subNets.get(i).getEdges().size(); j++){
  165. CpsEdge edge = subNets.get(i).getEdges().get(j);
  166. System.out.println(" " + edge.getA().getName() + " connected To " + edge.getB().getName());
  167. }
  168. }
  169. }
  170. public void setCanvas(MyCanvas can){
  171. canvas = can;
  172. }
  173. public void reset(){
  174. copyObjects(model.getObjectsOnCanvas());
  175. }
  176. }