123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261 |
- package ui.controller;
- import java.util.ArrayList;
- import classes.CpsEdge;
- import classes.CpsNode;
- import classes.CpsObject;
- import classes.HolonElement;
- import classes.HolonObject;
- import classes.HolonSwitch;
- import classes.subNet;
- import ui.model.Model;
- import ui.view.MyCanvas;
- public class SimulationManager {
- private Model model;
- private ArrayList<CpsObject> objectsToHandle;
- private ArrayList<subNet> subNets;
- private MyCanvas canvas;
- private int timeStep;
-
- public SimulationManager(Model m){
- canvas = null;
- model = m;
- subNets = new ArrayList<subNet>();
- }
-
-
- /**
- * calculates the flow of the edges and the supply for objects
- * @param x
- */
- public void calculateStateForTimeStep(int x){
- timeStep = x;
- searchForSubNets();
- for(subNet singleSubNet: subNets){
- float production = calculateEnergy("prod", singleSubNet, x);
- float consumption = calculateEnergy("cons", singleSubNet, x);
- float minConsumption = calculateMinimumEnergy( singleSubNet, x);
- for(CpsEdge e: singleSubNet.getEdges()){
- e.setFlow(production);
- }
- for(HolonObject hl: singleSubNet.getObjects()){
- if(!(hl.getState() == 0) && !(hl.getState() == 3)){
- for(int i = 0; i < hl.getConnections().size(); i++){
- CpsEdge edge = hl.getConnectedTo().get(i);
- if(edge.getState()){
- // 0 = no energy, 1 = not supplied, 2 = supplied
- if((production + consumption) >= 0){
- hl.setState(2);
- }
- if((production + consumption) < 0){
- if((production + minConsumption) >= 0){
- hl.setState(4);
- System.out.println("yellow");
- }else{
- hl.setState(1);
- System.out.println("orange");
- }
- }
- break;
- }
- hl.setState(1);
- }
- }
- }
- }
- //printNet();
- 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);
- hl.setState(3);
- }
- }
- if(type.equals("cons")){
- if(hl.getCurrentEnergyAtTimeStep(x) < 0){
- energy = energy + hl.getCurrentEnergyAtTimeStep(x);
- hl.setState(1);
- }
- }
- if(hl.getCurrentEnergyAtTimeStep(x) == 0){
- hl.setState(0);
- }
- }
- return energy;
- }
-
- public float calculateMinimumEnergy(subNet sN, int x){
- float min = 0;
- float minElement = 0;
- for(HolonObject hl: sN.getObjects()){
- if(hl.getElements().size() > 0 && hl.getElements().get(0).getTotalEnergyAtTimeStep(x) < 0 ){
- minElement = hl.getElements().get(0).getTotalEnergyAtTimeStep(x);
- }
- for(HolonElement he: hl.getElements()){
- if(minElement < he.getTotalEnergyAtTimeStep(x) && he.getTotalEnergyAtTimeStep(x) < 0){
- minElement = he.getTotalEnergyAtTimeStep(x);
- }
- }
- System.out.println(minElement);
- min = min + minElement;
- }
- return min;
- }
-
- /**
- * generates all subNets from all objectsToHandle
- */
- public void searchForSubNets(){
- subNets = new ArrayList<subNet>();
- boolean end = false;
- int i = 0;
- CpsObject cps;
- if(objectsToHandle.size() > 0){
- while(!end){
- cps = objectsToHandle.get(i);
- subNet singleSubNet = new subNet(new ArrayList<HolonObject>(), new ArrayList<CpsEdge>(), new ArrayList<HolonSwitch>());
- singleSubNet = buildSubNet(cps, new ArrayList<Integer>(), singleSubNet);
- if(singleSubNet.getObjects().size() != 0){
- subNets.add(singleSubNet);
- }
- if(0 == objectsToHandle.size()){
- end = true;
- }
- }
- }
- }
-
- /**
- * 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<Integer> visited, subNet sN){
- visited.add(cps.getID());
- if(cps instanceof HolonObject){
- sN.getObjects().add((HolonObject) cps);
- }
- if(cps instanceof HolonSwitch){
- sN.getSwitches().add((HolonSwitch) cps);
- }
- removeFromToHandle(cps.getID());
- CpsObject A;
- CpsObject B;
- for(CpsEdge edge: cps.getConnections()){
- A = edge.getA();
- B = edge.getB();
- if(!(cps instanceof HolonSwitch)){
- if(!(sN.getEdges().contains(edge))){
- sN.getEdges().add(edge);
- }
- }
- if(!visited.contains(A.getID()) && legitState(A, cps)){
- sN = buildSubNet(A, visited, sN);
- }
- if(!visited.contains(B.getID()) && legitState(B, cps)){
- sN = buildSubNet(B, visited, sN);
- }
- }
- return sN;
- }
-
- public boolean legitState(CpsObject neighbor, CpsObject current){
- if(current instanceof HolonSwitch){
- if(((HolonSwitch) current).getActiveAt()[timeStep]){
- if(neighbor instanceof HolonSwitch){
- if(((HolonSwitch) neighbor).getActiveAt()[timeStep]){
- return true;
- }else{
- return false;
- }
- }
- }else{
- return false;
- }
- }
- return true;
- }
-
- /**
- * 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<CpsObject> toCopy){
- objectsToHandle = new ArrayList<CpsObject>();
- 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());
- }
- System.out.println(" Switches:");
- for(int j = 0; j < subNets.get(i).getSwitches().size(); j++){
- HolonSwitch sw = subNets.get(i).getSwitches().get(j);
- System.out.println(" " + sw.getName() + " " + sw.getID() + " State:" + sw.getActiveAt()[timeStep]);
- }
- }
- }
-
- public void setCanvas(MyCanvas can){
- canvas = can;
- }
-
- public void reset(){
- copyObjects(model.getObjectsOnCanvas());
- }
-
-
- }
|