Tom Troppmann 6 лет назад
Родитель
Сommit
854039294e
2 измененных файлов с 22 добавлено и 166 удалено
  1. 22 17
      src/classes/HolonObject.java
  2. 0 149
      src/ui/controller/SimulationManager.java

+ 22 - 17
src/classes/HolonObject.java

@@ -20,6 +20,10 @@ public class HolonObject extends AbstractCpsObject {
      * (whether it needs no energy, whether it is not supplied, fully supplied,
      * a producer, partially supplied or over-supplied)
      */
+	//TODO: enum
+	enum State{
+		NO_ENERGY, NOT_SUPPLIED, SUPPLIED, PRODUCER, PARTIALLY_SUPPLIED, OVER_SUPPLIED
+	}
     public final static int NO_ENERGY = 0;
     public final static int NOT_SUPPLIED = 1;
     public final static int SUPPLIED = 2;
@@ -27,13 +31,12 @@ public class HolonObject extends AbstractCpsObject {
     public final static int PARTIALLY_SUPPLIED = 4;
     public final static int OVER_SUPPLIED = 5;
     
-    private static final int DEFAULT_GRAPH_LENGTH = 100;//TODO
     /*
      * Color of the actual state (red = no supplied, yellow = partially supplied
      * and green = supplied)
      */
     @Expose
-    private Color stateColor;
+    private Color stateColor; //TODO: to draw function 
     /* Array of all consumers */
     private ArrayList<HolonElement> elements;
     /* Total of consumption */
@@ -52,7 +55,7 @@ public class HolonObject extends AbstractCpsObject {
      */
     private float currentSupply;
     
-    /**
+    /** TODO: outsourcing 
      * Percentage of supplied energy of the energy level needed to supply all elements
      */
     private float suppliedPercentage;
@@ -133,6 +136,14 @@ public class HolonObject extends AbstractCpsObject {
         elements.add(element);
     }
 
+    /**
+     * deletes Element at a given index.
+     *
+     * @param idx index
+     */
+    public void deleteElement(int idx) {
+    	elements.remove(idx);
+    }
     /**
      * Calculate the maximum energy a holonObject can consume from all object that are aktive this moment;
      *
@@ -168,7 +179,6 @@ public class HolonObject extends AbstractCpsObject {
      * @return corresponding energy
      */
     public float getCurrentEnergyAtTimeStep(int x) {
-        float temp = 0;
         float cons = 0;
         float prod = currentSupply;
         float t;
@@ -180,10 +190,9 @@ public class HolonObject extends AbstractCpsObject {
                 }else{
                 	prod+=t;
                 }
-                temp += t;
             }
         }
-        currentEnergy = temp;
+        currentEnergy = prod + cons;
         suppliedPercentage = prod / -cons;
         return currentEnergy;
     }
@@ -207,14 +216,6 @@ public class HolonObject extends AbstractCpsObject {
         return currentEnergy;
     }
 
-    /**
-     * deletes Element at a given index.
-     *
-     * @param idx index
-     */
-    public void deleteElement(int idx) {
-        elements.remove(idx);
-    }
 
     /**
      * String of all consumers in this HolonObject.
@@ -294,7 +295,7 @@ public class HolonObject extends AbstractCpsObject {
     }
 
     /**
-     * Search for the element with the name.
+     * Search for the first element with the name.
      *
      * @param name name of the object to be searched
      * @return the searched HolonElement
@@ -304,6 +305,7 @@ public class HolonObject extends AbstractCpsObject {
         for (HolonElement e : getElements()) {
             if (e.getEleName().equals(name)) {
                 ele = e;
+                break;
             }
         }
         return ele;
@@ -320,6 +322,7 @@ public class HolonObject extends AbstractCpsObject {
         for (HolonElement e : getElements()) {
             if (e.getId() == id) {
                 ele = e;
+                break;
             }
         }
         return ele;
@@ -371,8 +374,10 @@ public class HolonObject extends AbstractCpsObject {
         }
     }
     
-    /**@param x TimeStep
-     * @return 
+    /**
+     * 
+     * @param x
+     * @return
      */
     public float getSelfMadeEnergy(int x)
     {

+ 0 - 149
src/ui/controller/SimulationManager.java

@@ -338,155 +338,6 @@ public class SimulationManager {
 		flexPane.recalculate();
 	}
 
-	private void calculation(int x, SubNet singleSubNet, float production, float consumption, float energySurplus,
-			float currentProduction, ArrayList<HolonObject> partiallySuppliedList,
-			ArrayList<HolonObject> notSuppliedList, float energyPerHolonObject) {
-		long numberOfConsumers;
-		if (model.getFairnessModel() == fairnessMininumDemandFirst) {
-			/**
-			 * supply Buildings with minimal Energy first, if conflicts
-			 * happen
-			 */
-			singleSubNet.getObjects().sort(new MinEnergyComparator(x));
-		} else if (model.getFairnessModel() == fairnessAllEqual) {
-			/*
-			 * give every building (just consumer) the same energy
-			 */
-			numberOfConsumers = singleSubNet
-					.getObjects()
-					.stream()
-					.filter(hl -> (hl.getState() != HolonObject.NO_ENERGY
-							&& hl.getState() != HolonObject.PRODUCER && hl
-							.getConnectedTo().stream()
-							.filter(e -> (e.getFlow() > 0)).count() > 0))
-					.count();
-			if (numberOfConsumers != 0)
-				energyPerHolonObject = currentProduction / numberOfConsumers;
-		}
-		for (HolonObject hl : singleSubNet.getObjects()) {
-			hl.setCurrentSupply(0);
-			if (hl.getState() != HolonObject.NO_ENERGY
-					&& hl.getState() != HolonObject.PRODUCER) {
-				for (int i = 0; i < hl.getConnections().size(); i++) {
-					CpsEdge edge = hl.getConnectedTo().get(i);
-					if (edge.isWorking()
-							&& edge.getFlow() > 0
-							|| edge.getCapacity() == CpsEdge.CAPACITY_INFINITE) {
-
-						if (model.getFairnessModel() == fairnessAllEqual) {
-							/* fairness: everyone receives the Same Energy */
-							float neededEnergy = hl
-									.getCurrentEnergyAtTimeStep(x);
-							if (neededEnergy > 0) {
-								hl.setState(HolonObject.PRODUCER);
-							} else if (energyPerHolonObject > -neededEnergy) {
-								hl.setState(HolonObject.OVER_SUPPLIED);
-							} else if (energyPerHolonObject == -neededEnergy) {
-								hl.setState(HolonObject.SUPPLIED);
-							} else if (energyPerHolonObject >= -hl
-									.getMinEnergy(x)) {
-								hl.setState(HolonObject.PARTIALLY_SUPPLIED);
-							} else {
-								hl.setState(HolonObject.NOT_SUPPLIED);
-							}
-							hl.setCurrentSupply(energyPerHolonObject);
-						} else if (model.getFairnessModel() == fairnessMininumDemandFirst) {
-							/* fairness: minimum demand gets Energy first */
-							if ((production + consumption) >= 0) {
-								if (energySurplus > 0) {
-									hl.setState(HolonObject.OVER_SUPPLIED);
-									hl.setCurrentSupply((-energySurplus
-											/ consumption + 1)
-											* hl.getCurrentEnergyAtTimeStep(x));
-								} else {
-									hl.setState(HolonObject.SUPPLIED);
-									hl.setCurrentSupply(-hl
-											.getCurrentEnergyAtTimeStep(x));
-								}
-							} else {
-								float minEnergy = hl.getMinEnergy(x);									
-								if (hl.checkIfPartiallySupplied(timeStep)) {
-									hl.setState(HolonObject.PARTIALLY_SUPPLIED);
-									currentProduction += minEnergy;
-									hl.setCurrentSupply(-minEnergy);
-									partiallySuppliedList.add(hl);
-								} else {
-									/**
-									 * Case that only some HolonObjects can
-									 * be supplied
-									 */
-									if (-hl.getCurrentEnergyAtTimeStep(x) <= currentProduction) {
-										hl.setState(HolonObject.PARTIALLY_SUPPLIED);
-										currentProduction += minEnergy;
-										hl.setCurrentSupply(-minEnergy);
-										partiallySuppliedList.add(hl);
-									} else if (-hl.getMinEnergy(x) <= currentProduction) {
-										hl.setState(HolonObject.PARTIALLY_SUPPLIED);
-										currentProduction += minEnergy;
-										hl.setCurrentSupply(-minEnergy);
-										partiallySuppliedList.add(hl);
-									} else {
-										hl.setState(HolonObject.NOT_SUPPLIED);
-										hl.setCurrentSupply(0.0f);
-										notSuppliedList.add(hl);
-										// currentProduction +=
-										// hl.getCurrentEnergyAtTimeStep(x);
-
-									}
-								}
-							}
-						}
-						hl.getCurrentEnergyAtTimeStep(x);
-						break;
-					}
-				}
-
-				/**
-				 * check if some object cn self supply itself
-				 */
-				if (hl.checkIfPartiallySupplied(timeStep)
-						&& hl.getState() != HolonObject.SUPPLIED
-						&& hl.getState() != HolonObject.OVER_SUPPLIED) {
-					hl.setState(HolonObject.PARTIALLY_SUPPLIED);
-				}
-			}
-		}
-
-		if (model.getFairnessModel() == fairnessMininumDemandFirst) {
-			/**
-			 * check if some partially supplied building might be fully
-			 * supplied.
-			 */
-			partiallySuppliedList.sort(new EnergyMinToMaxComparator(x));
-			for (HolonObject hl : partiallySuppliedList) {
-				float minEnergy = hl.getMinEnergy(x);
-				currentProduction -= minEnergy;
-				/*
-				 * if possible, supply fully
-				 */
-				float currentEnergyAtTimeStep = hl
-						.getCurrentEnergyAtTimeStep(x);
-				if (-currentEnergyAtTimeStep <= currentProduction) {
-					hl.setState(HolonObject.SUPPLIED);
-					currentProduction += currentEnergyAtTimeStep;
-					hl.setCurrentSupply(-currentEnergyAtTimeStep);
-					hl.getCurrentEnergyAtTimeStep(x);
-				} else {
-					currentProduction += minEnergy;
-					notSuppliedList.add(hl);
-				}
-			}
-			if (!notSuppliedList.isEmpty() && currentProduction > 0) {
-				float energyPerHolon = currentProduction
-						/ notSuppliedList.size();
-				for (HolonObject hl : notSuppliedList) {
-					hl.setCurrentSupply(hl.getCurrentSupply()
-							+ energyPerHolon);
-					hl.getCurrentEnergyAtTimeStep(x);
-				}
-			}
-		}
-	}
 	/**
 	 * add all battries.getOut() from a list of battries and return them
 	 * @param aL a List of HolonBattries likely from subnet.getBatteries()