Browse Source

Preparation for Flexibility.

Tom 5 years ago
parent
commit
84e3aae7b1

+ 80 - 0
src/classes/Flexibility.java

@@ -0,0 +1,80 @@
+package classes;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.function.Predicate;
+/**
+ * Representative of a flexibility for a HolonElement.
+ *
+ */
+public class Flexibility {
+	/** How fast in TimeSteps the Flexibility can be activated. */
+	public int speed;
+	/** How high the cost for a activation are. */
+	public float cost;
+	/** SHould this Flexibility be Offered when is constrainList is fulfilled the flexibility can be activated.*/
+	public boolean offered;
+	/** The Duration in TimeSteps how long the Flexibility is activated.*/
+	private int duration;
+	/** The Duration after a successful activation between the next possible activation.*/
+	private int cooldown;
+	/** The Element this flexibility is assigned.*/
+	private HolonElement element;
+	/** The List of Constrains the Flexibility */
+	public List<Predicate<Flexibility>> constrainList;
+	
+	
+	public Flexibility(HolonElement element){
+		this(0 , 0.f, 1, 0, false, element);		
+	}
+	public Flexibility(int speed, float cost, int duration, int cooldown, boolean offered, HolonElement element){
+		this.speed = speed;
+		this.cost = cost;
+		setDuration(duration);
+		setCooldown(cooldown);
+		this.offered=offered;
+		this.element = element;
+		constrainList = new ArrayList<Predicate<Flexibility>>();		
+	}
+	
+	
+	/** Checks if all assigned constrains are fulfilled. When no constrains assigned returns true.*/
+	public boolean fulfillsConstrains() {
+		return constrainList.stream().reduce(Predicate::and).orElse(f -> true).test(this);
+	}
+	
+	public HolonElement getElement() {
+		return element;
+	}
+
+	public int getDuration() {
+		return duration;
+	}
+	/** Minimum duration is 1 TimeStep.*/
+	public void setDuration(int duration) {
+		this.duration = Math.max(1, duration);
+	}
+
+	public int getCooldown() {
+		return cooldown;
+	}
+	/** No negative cooldown TimeSteps.*/
+	public void setCooldown(int cooldown) {
+		this.cooldown = Math.max(0, cooldown);
+	}
+
+	/** returns the total energy Amount accumulated over the TimeSteps.*/
+	public float magnitude() {
+		return ((float)duration) *  element.getEnergyPerElement();
+	}
+	
+	
+	//Example Constrains:
+	/** Flexibility should be offered when Element is active.*/
+	public Predicate<Flexibility> onConstrain = f 	-> 	f.getElement().isActive();
+	/** Flexibility should be offered when Element is inactive.*/
+	public Predicate<Flexibility> offConstrain = f	-> 	!f.getElement().isActive();
+	
+	
+	
+}

+ 8 - 63
src/classes/HolonElement.java

@@ -43,11 +43,7 @@ public class HolonElement implements LocalMode, GraphEditable{
     @Expose
     private boolean active;
     /** Gives us whether this element is flexible and can flexibly use any part of the energy in flexibleEnergyAvailable */
-    @Expose
-    private boolean flexible;
-    /** Flexibility (meaning the actual */
-    @Expose
-    private float flexibleEnergyAvailable;
+
 
     
     /** Place where the Object is Stored */
@@ -57,8 +53,8 @@ public class HolonElement implements LocalMode, GraphEditable{
     @Expose
     private int id;
     
-    //private static final int DEFAULT_GRAPH_LENGTH=100;
-    //private int graphLength=DEFAULT_GRAPH_LENGTH; Unimplementable due to former developer's dark magic.
+    
+    public java.util.List<Flexibility> flexList;
     
     /*
      * Energy at each point of the graph with 100 predefined points. At the
@@ -100,8 +96,6 @@ public class HolonElement implements LocalMode, GraphEditable{
         initGraphPoints();
         sampleGraph();
         setId(id);
-        setFlexibleEnergyAvailable(0);
-        setFlexible(false);
     }
 
     /**
@@ -124,8 +118,7 @@ public class HolonElement implements LocalMode, GraphEditable{
         sampleGraph();
         setSaving(null);
         setId(IdCounterElem.nextId());
-        setFlexibleEnergyAvailable(0);
-        setFlexible(false);
+
     }
 
 	public String getObjName() {
@@ -246,11 +239,7 @@ public class HolonElement implements LocalMode, GraphEditable{
      * @return energyPerElement value
      */
     public float getOverallEnergyAtTimeStep(int timestep) {
-    	if (flexible) {
-            return ((float) amount) * energyPerElement;
-        } else {
-            return ((float) amount) * energyPerElement * curveSample[IndexTranslator.getEffectiveIndex(this, timestep)];
-        }
+       return ((float) amount) * energyPerElement * curveSample[IndexTranslator.getEffectiveIndex(this, timestep)];
     }
     /**
      * Get the energyPerElement currently(at given time step) available
@@ -259,48 +248,6 @@ public class HolonElement implements LocalMode, GraphEditable{
     	return amount * energyPerElement * this.curveSample[IndexTranslator.getEffectiveIndex(this, timestep)];
     }
 
-    /**
-     * Get the flexibleEnergyAvailable of an element
-     */
-    public float getFlexibleEnergyAvailablePerElement() {
-        return this.flexibleEnergyAvailable;
-    }
-
-    /**
-     * Set the flexibleEnergyAvailable of an element
-     */
-    public void setFlexibleEnergyAvailable(float energy) {
-        this.flexibleEnergyAvailable = energy;
-    }
-
-    /**
-     * Get the flexibleEnergyAvailable of an element
-     */
-    public boolean isFlexible() {
-        return this.flexible;
-    }
-
-    /**
-     * Set the flexibleEnergyAvailable of an element, ~switches energyPerElement and flexible energyPerElement
-     */
-    public void setFlexible(boolean b) {
-        // if flexibleEnergyAvailable was set to true
-        if (b && !this.flexible) {
-            this.flexible = b;
-            // move energyPerElement to flexibleEnergyAvailable (becomes the possible-to-use energyPerElement)
-            if (getEnergyPerElement() != 0) {
-                setFlexibleEnergyAvailable(getEnergyPerElement());
-                setEnergyPerElement(0);
-            }
-        } else if (!b && this.flexible) {
-            this.flexible = b;
-            // move the energyPerElement to actually used energyPerElement and set flexible amount to 0
-            if (getFlexibleEnergyAvailablePerElement() != 0) {
-                setEnergyPerElement(getFlexibleEnergyAvailablePerElement());
-            }
-            setFlexibleEnergyAvailable(0);
-        }
-    }
 
     /**
      * Get the Id of the selected HolonElement.
@@ -342,9 +289,7 @@ public class HolonElement implements LocalMode, GraphEditable{
                 .append(", eleName=").append(eleName)
                 .append(", amount=").append(amount)
                 .append(", active=").append(active)
-                .append(", flexible=").append(flexible)
-                .append(", energyPerElement used=").append(energyPerElement)
-                .append(", flexible energyPerElement available=").append(flexibleEnergyAvailable);
+                .append(", energyPerElement used=").append(energyPerElement);
         sb.append("]");
 
         return sb.toString();
@@ -433,7 +378,7 @@ public class HolonElement implements LocalMode, GraphEditable{
 	/**
 	 * Helper method for {@link HolonElement#sampleGraph(int)}.
 	 * <p>
-	 * Its get the start and Endposition and calculate the Points in between for the Beziér Curve.
+	 * Its get the start and Endposition and calculate the Points in between for the Bezi�r Curve.
 	 * Then its get the Y Value a.k.a. the percentage from the curve at the X value t.  
 	 * @param t is in Range [0,1] and represent how much the X value is traverse along the Curve between the two Points.
 	 * @param start is the start Point of the Curve.
@@ -459,7 +404,7 @@ public class HolonElement implements LocalMode, GraphEditable{
 	 */
 	private Point.Double getBezierPoint(double t, Point.Double p0, Point.Double p1,Point.Double p2,Point.Double p3) {
 		/*
-		 * Calculate Beziér:
+		 * Calculate Bezi�r:
 		 * B(t) = (1-t)^3 * P0 + 3*(1-t)^2 * t * P1 + 3*(1-t)*t^2 * P2 + t^3 * P3 , 0 < t < 1
 		 * 
 		 * Source: //http://www.theappguruz.com/blog/bezier-curve-in-games

+ 0 - 36
src/classes/HolonObject.java

@@ -284,44 +284,8 @@ public class HolonObject extends AbstractCpsObject {
         this.totalFlex = totalFlex;
     }
 
-    /**
-     * Update the totalFlex
-     */
-    public void updateTotalFlex() {
-        float tempFlex = 0;
-        for (HolonElement e : getElements()) {
-            if (e.isFlexible()) {
-                tempFlex += e.getFlexibleEnergyAvailablePerElement() * e.getAmount();
-            }
-        }
-        this.totalFlex = tempFlex;
-    }
 
-    /**
-     * calculates total flexible Production
-     */
-    public float getFlexProd() {
-        float tempFlex = 0;
-        for (HolonElement e : getElements()) {
-            if (e.getFlexibleEnergyAvailablePerElement() > 0) {
-                tempFlex += e.getFlexibleEnergyAvailablePerElement() * e.getAmount();
-            }
-        }
-        return tempFlex;
-    }
 
-    /**
-     * calculates total flexible Concumption
-     */
-    public float getFlexCons() {
-        float tempFlex = 0;
-        for (HolonElement e : getElements()) {
-            if (e.getFlexibleEnergyAvailablePerElement() < 0) {
-                tempFlex += e.getFlexibleEnergyAvailablePerElement() * e.getAmount();
-            }
-        }
-        return tempFlex;
-    }
 
     /**
      * If the user track any HolonObject the tracking information will be

+ 0 - 19
src/classes/comparator/elementComparator/ElemCompOnFlexible.java

@@ -1,19 +0,0 @@
-package classes.comparator.elementComparator;
-
-import classes.HolonElement;
-
-public class ElemCompOnFlexible extends ElementComparator {
-	
-	@Override
-	public int compare(HolonElement a, HolonElement b) {
-
-		float flexA = a.getFlexibleEnergyAvailablePerElement();
-		float flexB = b.getFlexibleEnergyAvailablePerElement();
-		if (flexA < flexB)
-			return -1;
-		if (flexA > flexB)
-			return 1;
-		return 0;
-
-	}
-}

+ 0 - 18
src/classes/comparator/elementComparator/ElemCompOnIsFlexible.java

@@ -1,18 +0,0 @@
-package classes.comparator.elementComparator;
-
-import classes.HolonElement;
-
-public class ElemCompOnIsFlexible extends ElementComparator{
-
-	@Override
-	public int compare(HolonElement a, HolonElement b) {
-		boolean ifA = a.isFlexible();
-		boolean ifB = b.isFlexible();
-		if (ifA&&!ifB)
-			return -1;
-		if (!ifA && ifB)
-			return 1;
-		return 0;
-	}
-
-}

+ 4 - 4
src/connection/ConnectHandheld.java

@@ -1,4 +1,4 @@
-package Connection;
+package connection;
 
 import java.awt.BorderLayout;
 import java.awt.Dimension;
@@ -26,9 +26,9 @@ import api.Algorithm;
 import classes.AbstractCpsObject;
 import classes.CpsUpperNode;
 import classes.HolonObject;
-import Connection.ConnectPhysical.HolonObjectStatus;
-import Connection.ConnectPhysical.PhysicalLinkWrapper;
-import Connection.socket.Server;
+import connection.ConnectPhysical.HolonObjectStatus;
+import connection.ConnectPhysical.PhysicalLinkWrapper;
+import connection.socket.Server;
 import ui.controller.Control;
 import ui.view.Console;
 

+ 1 - 1
src/connection/ConnectPhysical.java

@@ -1,4 +1,4 @@
-package Connection;
+package connection;
 import java.awt.BorderLayout;
 import java.awt.Component;
 import java.awt.Dimension;

+ 1 - 1
src/connection/socket/Server.java

@@ -1,4 +1,4 @@
-package Connection.socket;
+package connection.socket;
 import java.net.*;
 import java.util.ArrayList;
 import java.util.List;

+ 4 - 7
src/ui/controller/UpdateController.java

@@ -3,10 +3,8 @@ package ui.controller;
 import classes.*;
 import classes.comparator.elementComparator.ElemCompOnEleName;
 import classes.comparator.elementComparator.ElemCompOnEnergy;
-import classes.comparator.elementComparator.ElemCompOnFlexible;
 import classes.comparator.elementComparator.ElemCompOnId;
 import classes.comparator.elementComparator.ElemCompOnIsActivated;
-import classes.comparator.elementComparator.ElemCompOnIsFlexible;
 import classes.comparator.elementComparator.ElemCompOnObj;
 import classes.comparator.elementComparator.ElemCompOnQuantity;
 import classes.comparator.elementComparator.ElementComparator;
@@ -52,7 +50,6 @@ public class UpdateController {
 					table.removeRow(2);
 					Object[] tempEnergy = { Languages.getLanguage()[73], ((HolonObject) tempCps).getEnergyNeededFromConsumingElements(model.getCurIteration()) };
 					table.insertRow(2, tempEnergy);
-					((HolonObject) tempCps).updateTotalFlex();
 					table.removeRow(3);
 					Object[] tempFlex = { "Flexibility", ((HolonObject) tempCps).getTotalFlex() };
 					table.insertRow(3, tempFlex);
@@ -84,7 +81,7 @@ public class UpdateController {
 			}
 			for (HolonElement e1 : elemList) {
 				Object[] temp2 = { e1.getObjName(), e1.getId(), e1.getEleName(), e1.getEnergyPerElement(),
-						e1.getFlexibleEnergyAvailablePerElement(), e1.getAmount(), e1.isActive(), e1.isFlexible() };
+						666.f, e1.getAmount(), e1.isActive(), false };
 				table.addRow(temp2);
 			}
 
@@ -98,7 +95,7 @@ public class UpdateController {
 				
 				for (HolonElement e1 : elemList) {
 					Object[] temp2 = { e1.getId(), e1.getEleName(), e1.getEnergyPerElement(),
-							e1.getFlexibleEnergyAvailablePerElement(), e1.getAmount(), e1.isActive(), e1.isFlexible() };
+							666.f, e1.getAmount(), e1.isActive(), false };
 					table.addRow(temp2);
 				}
 			}
@@ -124,7 +121,7 @@ public class UpdateController {
 			elemList.sort(new ElemCompOnEnergy());
 			break;
 		case 4://"Flexible Energy Available": 
-			elemList.sort(new ElemCompOnFlexible());
+			elemList.sort(null);
 			break;
 		case 5://"Quantity": 
 			elemList.sort(new ElemCompOnQuantity());
@@ -133,7 +130,7 @@ public class UpdateController {
 			elemList.sort(new ElemCompOnIsActivated());
 			break;
 		case 7://"Flexible": 
-			elemList.sort(new ElemCompOnIsFlexible());
+			elemList.sort(null);
 			break;
 		default: 
 			elemList.sort(new ElemCompOnId());

+ 0 - 4
src/ui/view/AddElementPopUp.java

@@ -175,8 +175,6 @@ public class AddElementPopUp extends JDialog {
 		hl = holonElement;
 		elementName.setText(hl.getEleName());
 		amount.setText(""+hl.getAmount());
-		flexibleEnergy.setText(""+hl.getFlexibleEnergyAvailablePerElement());
-		checkboxFlexible.setSelected(hl.isFlexible());
 		providedEnergy.setText(""+hl.getEnergyPerElement());
 		checkBoxActive.setSelected(hl.isActive());
 		
@@ -205,8 +203,6 @@ public class AddElementPopUp extends JDialog {
 					hl.setEnergyPerElement(energy);
 				}
 				hl.setActive(checkBoxActive.isSelected());
-				hl.setFlexible(checkboxFlexible.isSelected());
-				hl.setFlexibleEnergyAvailable(Float.parseFloat(flexibleEnergy.getText()));
 				dispose();
 			} catch (NumberFormatException e) {
 				JOptionPane.showMessageDialog(new JFrame(), Languages.getLanguage()[68]);

+ 4 - 5
src/ui/view/FlexiblePane.java

@@ -59,10 +59,9 @@ public class FlexiblePane extends JScrollPane implements ObjectListener {
 			float subCons = 0;
 			Color subColor = netColors.get(colorCounter);
 			for(HolonObject hl: sn.getObjects()){
-				subProd += hl.getFlexProd();
-				subCons += hl.getFlexCons();
+				
 				FlexibleData tmp = new FlexibleData(hl.getName()+" "+hl.getId(),
-						hl.getFlexProd(), hl.getFlexCons(), gridProd, gridCons);
+						666.f, 666.f, gridProd, gridCons);
 				tmp.getColorPanel().setBackground(subColor);
 				objects.add(tmp);
 			}
@@ -92,8 +91,8 @@ public class FlexiblePane extends JScrollPane implements ObjectListener {
 		if(sn != null){
 			for(SubNet s: sn){
 				for(HolonObject h: s.getObjects()){
-					prod += h.getFlexProd();
-					cons += h.getFlexCons();
+					prod += 666.f;
+					cons += 666.f;
 				}
 			}
 			result.add(prod);

+ 6 - 9
src/ui/view/GUI.java

@@ -1399,7 +1399,7 @@ public class GUI implements CategoryListener {
 													selectedValueBX).toString();
 									Boolean bTemp = Boolean
 											.parseBoolean(newBStuff);
-									eleBTemp.setFlexible(bTemp);
+									//TODO: delete here more
 								} else {
 									// Update of HolonElement
 									HolonElement eleTemp = updCon
@@ -1423,7 +1423,7 @@ public class GUI implements CategoryListener {
 									else if (selectedValueX == 4) {
 										Float ftemp = Float
 												.parseFloat(newStuff);
-										eleTemp.setFlexibleEnergyAvailable(ftemp);
+										//TODO and here
 									}
 									// Amount of Elements update
 									else if (selectedValueX == 5) {
@@ -1473,7 +1473,7 @@ public class GUI implements CategoryListener {
 													selectedValueBX).toString();
 									Boolean bTemp = Boolean
 											.parseBoolean(newBStuff);
-									eleTemp.setFlexible(bTemp);
+									//TODO: delete Boolean bTemp = Boolean.parseBoolean(newBStuff);
 
 								} else {
 									// Update of HolonElement
@@ -1496,16 +1496,13 @@ public class GUI implements CategoryListener {
 									else if (selectedValueX == 3) {
 										Float ftemp = Float
 												.parseFloat(newStuff);
-										eleTemp.setFlexibleEnergyAvailable(ftemp);
+										//TODO ---
 
 										// if this is a flexible device and the
 										// flexibly available energy was
 										// changed,
 										// set used energy to 0, so that it can
 										// be computed anew
-										if (eleTemp.isFlexible()) {
-											eleTemp.setEnergyPerElement(0);
-										}
 									}
 									// Amount of Elements update
 									else if (selectedValueX == 4) {
@@ -3025,14 +3022,14 @@ public class GUI implements CategoryListener {
 	 */
 	private void updateElementTableAfterChange(HolonElement eleBTemp,
 			int selectedValueBY) {
-		model.getSingleTable().setValueAt(eleBTemp.isFlexible(),
+		model.getSingleTable().setValueAt(false,
 				selectedValueBY, 6);
 		model.getSingleTable().setValueAt(eleBTemp.isActive(), selectedValueBY,
 				5);
 		model.getSingleTable().setValueAt(eleBTemp.getAmount(),
 				selectedValueBY, 4);
 		model.getSingleTable().setValueAt(
-				eleBTemp.getFlexibleEnergyAvailablePerElement(),
+				666.f,
 				selectedValueBY, 3);
 		model.getSingleTable().setValueAt(eleBTemp.getEnergyPerElement(),
 				selectedValueBY, 2);

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

@@ -182,7 +182,7 @@ public class StatisticGraph extends JPanel {
                 case TrackedDataSet.CONSUMPTION:
                     for (HolonElement h : ((HolonObject) set.getCpsObject()).getElements()) {
                         if (h.getEnergyPerElement() < 0) {
-                            val += (h.getEnergyPerElement() + h.getFlexibleEnergyAvailablePerElement()) * h.getAmount();
+                            val += (h.getEnergyPerElement() ) * h.getAmount();
                         }
                     }
                     val *= -1;
@@ -190,7 +190,7 @@ public class StatisticGraph extends JPanel {
                 case TrackedDataSet.PRODUCTION:
                     for (HolonElement h : ((HolonObject) set.getCpsObject()).getElements()) {
                         if (h.getEnergyPerElement() > 0) {
-                            val += (h.getEnergyPerElement() + h.getFlexibleEnergyAvailablePerElement()) * h.getAmount();
+                            val += (h.getEnergyPerElement() ) * h.getAmount();
                         }
                     }
                     break;
@@ -601,7 +601,7 @@ public class StatisticGraph extends JPanel {
             if (obj instanceof HolonObject) {
                 for (HolonElement ele : ((HolonObject) obj).getElements()) {
                     if (ele.getEnergyPerElement() > 0) {
-                        val += (ele.getEnergyPerElement() + ele.getFlexibleEnergyAvailablePerElement()) * ele.getAmount();
+                        val += (ele.getEnergyPerElement() ) * ele.getAmount();
                     }
                 }
             } else if (obj instanceof CpsUpperNode) {
@@ -621,7 +621,7 @@ public class StatisticGraph extends JPanel {
             if (obj instanceof HolonObject) {
                 for (HolonElement ele : ((HolonObject) obj).getElements()) {
                     if (ele.getEnergyPerElement() < 0) {
-                        val += (ele.getEnergyPerElement() + ele.getFlexibleEnergyAvailablePerElement()) * ele.getAmount();
+                        val += (ele.getEnergyPerElement() ) * ele.getAmount();
                     }
                 }
             } else if (obj instanceof CpsUpperNode) {
@@ -642,7 +642,7 @@ public class StatisticGraph extends JPanel {
         for (AbstractCpsObject obj : objects) {
             if (obj instanceof HolonObject) {
                 for (HolonElement ele : ((HolonObject) obj).getElements()) {
-                    val += (ele.getEnergyPerElement() + ele.getFlexibleEnergyAvailablePerElement()) * ele.getAmount();
+                    val += (ele.getEnergyPerElement() ) * ele.getAmount();
                 }
             } else if (obj instanceof CpsUpperNode) {
                 val += getMaxWastedEnergy(((CpsUpperNode) obj).getNodes());