package ui.controller; import java.util.ArrayList; import classes.CpsEdge; import classes.CpsNode; import classes.CpsObject; import classes.HolonObject; import classes.subNet; import ui.model.Model; import ui.view.MyCanvas; public class SimulationManager { private Model model; private ArrayList objectsToHandle; private ArrayList subNets; private MyCanvas canvas; public SimulationManager(Model m){ canvas = null; model = m; subNets = new ArrayList(); } /** * calculates the flow of the edges and the supply for objects * @param x */ public void calculateStateForTimeStep(int x){ searchForSubNets(); for(subNet singleSubNet: subNets){ float production = calculateEnergy("prod", singleSubNet, x); float consumption = calculateEnergy("cons", singleSubNet, x); for(CpsEdge e: singleSubNet.getEdges()){ e.setFlow(production); } for(HolonObject hl: singleSubNet.getObjects()){ for(int i = 0; i < hl.getConnections().size(); i++){ CpsEdge edge = hl.getConnectedTo().get(i); if(edge.getState()){ if((production + consumption) >= 0 ){ hl.setSupplied(true); }else{ hl.setSupplied(false); } break; } hl.setSupplied(false); } } } canvas.repaint(); } /** * calculates the energy of either all producers or consumers * @param type * @param sN * @return */ public float calculateEnergy(String type, subNet sN, int x){ float energy = 0; for(HolonObject hl: sN.getObjects()){ if(type.equals("prod")){ if(hl.getCurrentEnergyAtTimeStep(x) > 0){ energy = energy + hl.getCurrentEnergyAtTimeStep(x); } } if(type.equals("cons")){ if(hl.getCurrentEnergyAtTimeStep(x) < 0){ energy = energy + hl.getCurrentEnergyAtTimeStep(x); } } } return energy; } /** * generates all subNets from all objectsToHandle */ public void searchForSubNets(){ subNets = new ArrayList(); boolean end = false; int i = 0; CpsObject cps; if(objectsToHandle.size() > 0){ while(!end){ cps = objectsToHandle.get(i); subNet singleSubNet = new subNet(new ArrayList(), new ArrayList()); singleSubNet = buildSubNet(cps, new ArrayList(), singleSubNet); if(singleSubNet.getObjects().size() != 0){ subNets.add(singleSubNet); } if(0 == objectsToHandle.size()){ end = true; } } } //printNet(); } /** * recursivly generates a subnet of all objects, that one specific object is connected to * @param cps * @param visited * @param sN * @return */ public subNet buildSubNet(CpsObject cps, ArrayList visited, subNet sN){ visited.add(cps.getID()); if(cps instanceof HolonObject){ sN.getObjects().add((HolonObject) cps); } removeFromToHandle(cps.getID()); for(CpsEdge edge: cps.getConnections()){ if(!(sN.getEdges().contains(edge))){ sN.getEdges().add(edge); } if(!visited.contains(edge.getA().getID())){ sN = buildSubNet(edge.getA(), visited, sN); } if(!visited.contains(edge.getB().getID())){ sN = buildSubNet(edge.getB(), visited, sN); } } return sN; } /** * removes an Object that already has been handled with * @param id */ public void removeFromToHandle(int id){ for(int i = 0; i < objectsToHandle.size(); i++){ if(objectsToHandle.get(i).getID() == id){ objectsToHandle.remove(i); } } } /** * ensures that objectsToHandle only contains HolonObjects */ public void cleanObjectsToHandle(){ for(int i = 0; i < objectsToHandle.size(); i++){ if(!(objectsToHandle.get(i) instanceof HolonObject)){ objectsToHandle.remove(i); } } } /** * copies the data of an array of Objects * @param toCopy */ public void copyObjects(ArrayList toCopy){ objectsToHandle = new ArrayList(); for(CpsObject cps: toCopy){ objectsToHandle.add(cps); } } /** * Prints the Components auf all subnets */ public void printNet(){ for(int i = 0; i < subNets.size(); i++){ System.out.println("SUBNET NR:" + i); System.out.println(" Objects:"); for(int j = 0; j < subNets.get(i).getObjects().size(); j++){ HolonObject hl = subNets.get(i).getObjects().get(j); System.out.println(" " + hl.getName() + " " + hl.getID()); } System.out.println(" Edges:"); for(int j = 0; j < subNets.get(i).getEdges().size(); j++){ CpsEdge edge = subNets.get(i).getEdges().get(j); System.out.println(" " + edge.getA().getName() + " connected To " + edge.getB().getName()); } } } public void setCanvas(MyCanvas can){ canvas = can; } public void reset(){ copyObjects(model.getObjectsOnCanvas()); } }