SimulationManager.java 4.6 KB

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