Browse Source

Flow for timeStep

dominik.rieder 7 years ago
parent
commit
21d11aebc6

+ 0 - 1
src/classes/CpsEdge.java

@@ -11,7 +11,6 @@ public class CpsEdge {
 	public CpsEdge(CpsObject A, CpsObject B){
 		setA(A);
 		setB(B);
-		this.flow = 50;
 		this.A.AddConnection(this);
 		this.B.AddConnection(this);
 		this.maxCapacity = 100;

+ 5 - 0
src/classes/HolonElement.java

@@ -157,6 +157,11 @@ public class HolonElement {
 		totalEnergy = ((float) amount) * energy;
 		return totalEnergy;
 	}
+	
+	public float getTotalEnergyAtTimeStep(int x){
+		float result = ((float) amount) * energyAt[x];
+		return result;
+	}
 
 	/**
 	 * @return the sign

+ 11 - 4
src/classes/HolonObject.java

@@ -79,6 +79,17 @@ public class HolonObject extends CpsObject {
 		currentEnergy = temp;
 		return currentEnergy;
 	}
+	
+	public float getCurrentEnergyAtTimeStep(int x) {
+		float temp = 0;
+		for (HolonElement e : getElements()) {
+			if (e.getActive()) {
+				temp = temp + e.getTotalEnergyAtTimeStep(x);
+			}
+		}
+		currentEnergy = temp;
+		return currentEnergy;
+	}
 
 	/**
 	 * @param currentEnergy
@@ -97,10 +108,6 @@ public class HolonObject extends CpsObject {
 		elements.remove(idx);
 	}
 
-	public float calculateCurrentEnergy() {
-		return currentEnergy;
-	}
-
 	/**
 	 * String of all consumers in this HolonObject
 	 * 

+ 7 - 0
src/ui/controller/Control.java

@@ -25,6 +25,7 @@ public class Control {
 	private final GlobalController globalController;
 	private final StoreController storeController;
 	private final LoadController loadController;
+	private SimulationManager simulationManager;
 
 	public Control(Model model) {
 		this.MODEL = model;
@@ -37,6 +38,7 @@ public class Control {
 		this.storeController = new StoreController(MODEL);
 		this.loadController = new LoadController(MODEL, categoryController, canvasController, objectController,
 				multiPurposeController);
+		this.simulationManager = new SimulationManager(MODEL);
 
 	}
 
@@ -152,6 +154,11 @@ public class Control {
 	public void initListener(CategoryListener catLis) {
 		categoryController.addCategoryListener(catLis);
 	}
+	
+	public void calculateStateForTimeStep(int x){
+		simulationManager.reset();
+		simulationManager.calculateStateForTimeStep(x);
+	}
 
 	/**
 	 * Getter for Model

+ 43 - 9
src/ui/controller/SimulationManager.java

@@ -16,38 +16,47 @@ public class SimulationManager {
 	
 	public SimulationManager(Model m){
 		model = m;
-		copyObjects(m.getObjectsOnCanvas());
 		subNets = new ArrayList<subNet>();
 	}
 	
-	public void calculateStateForTimeStep(){
+	
+	public void calculateStateForTimeStep(int x){
 		searchForSubNets();
 		for(subNet singleSubNet: subNets){
-			float production = calculateEnergy("prod", singleSubNet);
-			float consumption = calculateEnergy("cons", singleSubNet);
+			float production = calculateEnergy("prod", singleSubNet, x);
+			float consumption = calculateEnergy("cons", singleSubNet, x);
 			for(CpsEdge e: singleSubNet.getEdges()){
 				e.setFlow(production);
 			}
 		}
 	}
 	
-	public float calculateEnergy(String type, subNet sN){
+	/**
+	 * 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.getCurrentEnergy() > 0){
-					energy = energy + hl.getCurrentEnergy();
+				if(hl.getCurrentEnergyAtTimeStep(x) > 0){
+					energy = energy + hl.getCurrentEnergyAtTimeStep(x);
 				}
 			}
 			if(type.equals("cons")){
-				if(hl.getCurrentEnergy() < 0){
-					energy = energy + hl.getCurrentEnergy();
+				if(hl.getCurrentEnergyAtTimeStep(x) < 0){
+					energy = energy + hl.getCurrentEnergyAtTimeStep(x);
 				}
 			}
 		}
 		return energy;
 	}
 	
+	/**
+	 * generates all subNets from all objectsToHandle
+	 */
 	public void searchForSubNets(){
 		subNets = new ArrayList<subNet>();
 		boolean end = false;
@@ -69,6 +78,13 @@ public class SimulationManager {
 		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<Integer> visited, subNet sN){
 		visited.add(cps.getID());
 		if(cps instanceof HolonObject){
@@ -89,6 +105,10 @@ public class SimulationManager {
 		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){
@@ -97,6 +117,9 @@ public class SimulationManager {
 		}
 	}
 	
+	/**
+	 * ensures that objectsToHandle only contains HolonObjects
+	 */
 	public void cleanObjectsToHandle(){
 		for(int i = 0; i < objectsToHandle.size(); i++){
 			if(!(objectsToHandle.get(i) instanceof HolonObject)){
@@ -105,6 +128,10 @@ public class SimulationManager {
 		}
 	}
 	
+	/**
+	 * copies the data of an array of Objects
+	 * @param toCopy
+	 */
 	public void copyObjects(ArrayList<CpsObject> toCopy){
 		objectsToHandle = new ArrayList<CpsObject>();
 		for(CpsObject cps: toCopy){
@@ -112,6 +139,9 @@ public class SimulationManager {
 		}
 	}
 	
+	/**
+	 * Prints the Components auf all subnets
+	 */
 	public void printNet(){
 		for(int i = 0; i < subNets.size(); i++){
 			System.out.println("SUBNET NR:" + i);
@@ -129,5 +159,9 @@ public class SimulationManager {
 		}
 	}
 	
+	public void reset(){
+		copyObjects(model.getObjectsOnCanvas());
+	}
+	
 	
 }

+ 4 - 1
src/ui/view/GUI.java

@@ -939,6 +939,9 @@ public class GUI<E> implements CategoryListener {
 
 			@Override
 			public void stateChanged(ChangeEvent e) {
+				int i = model.getCurIteration();
+				controller.calculateStateForTimeStep(i);
+				canvas.repaint();
 				unitGraph.repaint();
 			}
 		});
@@ -969,7 +972,7 @@ public class GUI<E> implements CategoryListener {
 		btnTest.addActionListener(new ActionListener() {
 			public void actionPerformed(ActionEvent arg0) {
 				SimulationManager sm = new SimulationManager(model);
-				sm.calculateStateForTimeStep();
+				sm.calculateStateForTimeStep(1);
 			}
 		});