Quellcode durchsuchen

BugFixes for SubnetConsumptionBug

tolatesry vor 7 Jahren
Ursprung
Commit
ac6fee0ce3

+ 12 - 5
src/classes/HolonObject.java

@@ -321,7 +321,17 @@ public class HolonObject extends AbstractCpsObject {
         if (getElements().size() == 0) {
             return false;
         }
-        float minConsum = getElements().get(0).getOverallEnergyAtTimeStep(x);
+        float minConsum = 0;
+        // Search for a activ element
+		for (HolonElement e : getElements()) {
+			if (e.isActive()) {
+				float overallEnergy = e.getOverallEnergyAtTimeStep(x);
+				if (overallEnergy < 0) {
+					// Is a consumer
+					minConsum = overallEnergy;
+				}
+			}
+		}
         float prod = 0;
         float cons = 0;
         for (HolonElement e : getElements()) {
@@ -333,10 +343,7 @@ public class HolonObject extends AbstractCpsObject {
                 	cons += overallEnergy;
                 }
                 if (minConsum < 0 && (overallEnergy > minConsum && overallEnergy < 0)) {
-                    minConsum = overallEnergy;
-                    
-                } else if (minConsum >= 0 && overallEnergy < minConsum) {
-                    minConsum = overallEnergy;
+                    minConsum = overallEnergy;   
                 }
             }
         }

+ 26 - 23
src/ui/controller/SimulationManager.java

@@ -78,8 +78,7 @@ public class SimulationManager {
 			// negative value
 			float energySurplus = production + consumption;
 
-			float minConsumption = calculateMinimumEnergy(singleSubNet,
-					timeStep);
+			//float minConsumption = calculateMinimumEnergy(singleSubNet, timeStep);
 
 			// --------------- use flexible devices ---------------
 			if (energySurplus != 0 && model.useFlexibleDevices()) {
@@ -187,14 +186,8 @@ public class SimulationManager {
 												.getCurrentEnergyAtTimeStep(x));
 									}
 								} else {
-									float minEnergy = hl.getMinEnergy(x);
-									if ((production + minConsumption) >= 0) {
-										hl.setState(HolonObject.PARTIALLY_SUPPLIED);
-										currentProduction += minEnergy;
-										hl.setCurrentSupply(-minEnergy);
-										partiallySuppliedList.add(hl);
-									} else if (hl
-											.checkIfPartiallySupplied(timeStep)) {
+									float minEnergy = hl.getMinEnergy(x);									
+									if (hl.checkIfPartiallySupplied(timeStep)) {
 										hl.setState(HolonObject.PARTIALLY_SUPPLIED);
 										currentProduction += minEnergy;
 										hl.setCurrentSupply(-minEnergy);
@@ -622,32 +615,42 @@ public class SimulationManager {
 	}
 
 	/**
-	 * Calculate the Minimum Energy.
+	 * Calculate the Minimum Energy of a Subnet.
 	 *
 	 * @param sN
 	 *            Subnet
 	 * @param x
 	 *            Integer
-	 * @return the Calculated minimum Energy
+	 * @return the Calculated minimum Energy of a Subnet
 	 */
 	private float calculateMinimumEnergy(SubNet sN, int x) {
-		float min = 0;
-		float minElement = 0;
+		float minimummConsumptionSubnet = 0;
 		for (HolonObject hl : sN.getObjects()) {
-			if (hl.getElements().size() > 0
-					&& hl.getElements().get(0).getOverallEnergyAtTimeStep(x) < 0) {
-				minElement = hl.getElements().get(0)
-						.getOverallEnergyAtTimeStep(x);
+			float minElement = 0;
+			// Search for a activ element
+			for (HolonElement he : hl.getElements()) {
+				if (he.isActive()) {
+					float overallEnergy = he.getOverallEnergyAtTimeStep(x);
+					if (overallEnergy < 0) {
+						// Is a consumer
+						minElement = overallEnergy;
+					}
+				}
 			}
 			for (HolonElement he : hl.getElements()) {
-				float overallEnergy = he.getOverallEnergyAtTimeStep(x);
-				if (minElement < overallEnergy && overallEnergy < 0) {
-					minElement = overallEnergy;
+				if (he.isActive()) {
+					float overallEnergy = he.getOverallEnergyAtTimeStep(x);
+
+					if (minElement < overallEnergy && overallEnergy < 0) {
+						// is a smaller consumer
+						minElement = overallEnergy;
+					}
 				}
 			}
-			min = min + minElement;
+			minimummConsumptionSubnet += minElement;
 		}
-		return min;
+		System.out.println("MinimumEnergy = "+ minimummConsumptionSubnet);
+		return minimummConsumptionSubnet;
 	}
 
 	/**

+ 8 - 0
src/ui/view/GUI.java

@@ -468,9 +468,17 @@ public class GUI implements CategoryListener {
 					// or Canvas?
 				} else if (canvasOrUpperNodeCanvas instanceof MyCanvas) {
 					boolean save = false;
+					//Edge Deleting
 					CpsEdge edgeHighlight = model.getSelectedEdge();
 					if (edgeHighlight != null) {
 						controller.removeEdgesOnCanvas(edgeHighlight);
+						//Look for a CPSNode with no Connections and delete them
+						if(edgeHighlight.getA().getClass() == CpsNode.class && edgeHighlight.getA().getConnections().size() == 0){ 
+							controller.delCanvasObject(edgeHighlight.getA(), false);	
+						}
+						if(edgeHighlight.getB().getClass() == CpsNode.class && edgeHighlight.getB().getConnections().size() == 0){ //Look on the other end of the cable
+							controller.delCanvasObject(edgeHighlight.getB(), false);
+						}
 						((MyCanvas) canvasOrUpperNodeCanvas).edgeHighlight = null;
 					}
 					for (int j = 0; j < model.getSelectedCpsObjects().size(); j++) {

+ 8 - 0
src/ui/view/MyCanvas.java

@@ -256,8 +256,16 @@ public class MyCanvas extends AbstractCanvas implements MouseListener,
 
 		itemDelete.addActionListener(actionEvent -> {
 			// Remove the selected Object objects
+				//Edge Deleting
 				if (tempCps == null && edgeHighlight != null) {
 					controller.removeEdgesOnCanvas(edgeHighlight);
+					//Look for a CPSNode with no Connections and delete them
+					if(edgeHighlight.getA().getClass() == CpsNode.class && edgeHighlight.getA().getConnections().size() == 0){ 
+						controller.delCanvasObject(edgeHighlight.getA(), false);	
+					}
+					if(edgeHighlight.getB().getClass() == CpsNode.class && edgeHighlight.getB().getConnections().size() == 0){ //Look on the other end of the cable
+						controller.delCanvasObject(edgeHighlight.getB(), false);
+					}
 					edgeHighlight = null;
 				}
 				boolean save = false;

+ 4 - 4
src/ui/view/StatisticGraph.java

@@ -323,7 +323,7 @@ public class StatisticGraph extends JPanel {
             switch (set.getProperty()) {
                 case TrackedDataSet.CONSUMPTION:
                     for (HolonElement h : ((HolonObject) set.getCpsObject()).getElements()) {
-                        if (h.getOverallEnergyAtTimeStep(model.getCurIteration()) < 0 && h.isActive()) {
+                        if (h.isActive() && h.getOverallEnergyAtTimeStep(model.getCurIteration()) < 0 ) {
                             val += Math.abs(h.getOverallEnergyAtTimeStep(model.getCurIteration()));
                         }
                         set.setValAt(val, model.getCurIteration());
@@ -331,7 +331,7 @@ public class StatisticGraph extends JPanel {
                     break;
                 case TrackedDataSet.PRODUCTION:
                     for (HolonElement h : ((HolonObject) set.getCpsObject()).getElements()) {
-                        if (h.getOverallEnergyAtTimeStep(model.getCurIteration()) > 0 && h.isActive()) {
+                        if (h.isActive()&& h.getOverallEnergyAtTimeStep(model.getCurIteration()) > 0) {
                             val += Math.abs(h.getOverallEnergyAtTimeStep(model.getCurIteration()));
                         }
                         set.setValAt(val, model.getCurIteration());
@@ -673,7 +673,7 @@ public class StatisticGraph extends JPanel {
         for (AbstractCpsObject obj : objects) {
             if (obj instanceof HolonObject) {
                 for (HolonElement ele : ((HolonObject) obj).getElements()) {
-                    if (ele.getOverallEnergyAtTimeStep(tStep) > 0 && ele.isActive()) {
+                    if (ele.isActive() && ele.getOverallEnergyAtTimeStep(tStep) > 0 ) {
                         val += ele.getOverallEnergyAtTimeStep(tStep);
                     }
                 }
@@ -693,7 +693,7 @@ public class StatisticGraph extends JPanel {
         for (AbstractCpsObject obj : objects) {
             if (obj instanceof HolonObject) {
                 for (HolonElement ele : ((HolonObject) obj).getElements()) {
-                    if (ele.getAvailableEnergyPerElementAt()[tStep] < 0 && ele.isActive()) {
+                    if (ele.isActive() && ele.getAvailableEnergyPerElementAt()[tStep] < 0 ) {
                         val += ele.getOverallEnergyAtTimeStep(tStep);
                     }
                 }