Browse Source

Abspeichern

Tom 5 years ago
parent
commit
4723bfa34b

+ 2 - 1
.gitignore

@@ -12,6 +12,7 @@ build/
 .metadata
 bin/
 tmp/
+*.class
 *.tmp
 *.bak
 *.swp
@@ -134,4 +135,4 @@ nbdist/
 ### added manually ###
 .idea/
 praktikum-holons.iml
-/gb_singlerun.txt
+/gb_singlerun.txt

+ 0 - 1
0

@@ -1 +0,0 @@
-{"CVSE5":["CVS",1,"Original",1,100.0,1],"CVSE6":["CVS",2,"Power",1,100.0,1],"CVSE7":["CVS",2,"Zero",10,100.0,1],"CVSE8":["CVS",2,"Rehab",10,100.0,1],"CVSE9":["CVS",2,"The Doctor",40,100.0,1],"CVSE10":["CVS",2,"Original",1,100.0,1],"CVSE1":["CVS",1,"Power",1,100.0,1],"CVSE2":["CVS",1,"Zero",10,100.0,1],"CVSE3":["CVS",1,"Rehab",10,100.0,1],"CVSE4":["CVS",1,"The Doctor",40,100.0,1],"CVSO1":["HolonObject","Power Plant","Power Plant",1,"\/Images\/power-plant.png",169,130],"ID":4,"CVSO2":["HolonObject","Power Plant","Power Plant",2,"\/Images\/power-plant.png",158,242],"CVSO3":["CpsNode","Node","Node",3,"\/Images\/node.png",311,187],"CG":["Energy","Building","Component"],"CGE5":["Energy","Power Plant","Original",1,100.0],"CGE4":["Energy","Power Plant","The Doctor",40,100.0],"CGE3":["Energy","Power Plant","Rehab",10,100.0],"CGE2":["Energy","Power Plant","Zero",10,100.0],"CGE1":["Energy","Power Plant","Power",1,100.0],"CGO4":["HolonSwitch","Component","Switch","\/Images\/switch-on.png"],"CGO3":["HolonTransformer","Component","Transformer","\/Images\/transformer-1.png"],"CGO2":["HolonObject","Building","House","\/Images\/home-2.png"],"EDGE1":[1,2,100.0,12400.0],"CGO1":["HolonObject","Energy","Power Plant","\/Images\/power-plant.png"],"EDGE2":[3,2,100.0,0.0]}

+ 52 - 0
src/classes/Constrain.java

@@ -0,0 +1,52 @@
+package classes;
+
+import java.util.function.Predicate;
+
+import com.google.gson.annotations.Expose;
+
+public class Constrain {
+	private Predicate<Flexibility> constrainFunction;
+	@Expose
+	private String name;
+	
+	public Constrain(Predicate<Flexibility> constrainFunction,String name) {
+		this.constrainFunction = constrainFunction;
+		this.name = name;
+	}
+	
+	public Predicate<Flexibility> getConstrainFunction() {
+		return constrainFunction;
+	}
+	public String getName() {
+		return name;
+	}
+	
+	
+	//Example Constrains:
+	/** Flexibility should be offered when Element is active.*/
+	public static Predicate<Flexibility> onConstrain = f 	-> 	f.getElement().isActive();
+	/** Flexibility should be offered when Element is inactive.*/
+	public static Predicate<Flexibility> offConstrain = f	-> 	!f.getElement().isActive();
+	
+	public static Constrain createOnConstrain() {
+		return new Constrain( onConstrain, "onConstrain");
+	}
+	public static Constrain createOffConstrain() {
+		return new Constrain( offConstrain, "offConstrain");
+	}
+	/**
+	 * Delete me ....
+	 * @return
+	 */
+	public boolean fixJson() {
+		if(name.compareTo("onConstrain") == 0) {
+			constrainFunction = onConstrain;
+			return false;
+		}else if(name.compareTo("offConstrain") == 0){
+			constrainFunction = offConstrain;
+			return false;
+		}else {
+			return false;
+		}
+	}
+}

+ 24 - 11
src/classes/Flexibility.java

@@ -3,27 +3,41 @@ package classes;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.function.Predicate;
+
+import com.google.gson.annotations.Expose;
 /**
  * Representative of a flexibility for a HolonElement.
  *
  */
 public class Flexibility {
 	/** The Name of a Flexibility.*/
+	@Expose
 	public String name;
 	/** How fast in TimeSteps the Flexibility can be activated. */
+	@Expose
 	public int speed;
 	/** How high the cost for a activation are. */
+	@Expose
 	public float cost;
 	/** SHould this Flexibility be Offered when is constrainList is fulfilled the flexibility can be activated.*/
+	@Expose
 	public boolean offered;
 	/** The Duration in TimeSteps how long the Flexibility is activated.*/
+	@Expose
 	private int duration;
 	/** The Duration after a successful activation between the next possible activation.*/
+	@Expose
 	private int cooldown;
 	/** The Element this flexibility is assigned.*/
 	private HolonElement element;
+
+
+
+
+
 	/** The List of Constrains the Flexibility */
-	public List<Predicate<Flexibility>> constrainList;
+	@Expose
+	public List<Constrain> constrainList;
 	
 	
 	public Flexibility(HolonElement element){
@@ -36,18 +50,22 @@ public class Flexibility {
 		setCooldown(cooldown);
 		this.offered=offered;
 		this.element = element;
-		constrainList = new ArrayList<Predicate<Flexibility>>();		
+		constrainList = new ArrayList<Constrain>();		
 	}
 	
 	
 	/** 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);
+		//System.out.println("Fix me when other is fixed");
+		return constrainList.stream().map(constrain -> constrain.getConstrainFunction()).reduce(Predicate::and).orElse(f -> true).test(this);
 	}
 	
 	public HolonElement getElement() {
 		return element;
 	}
+	public void setElement(HolonElement element) {
+		this.element = element;
+	}
 
 	public int getDuration() {
 		return duration;
@@ -79,15 +97,10 @@ public class Flexibility {
 		return "Flexibility: " + name + " from [HolonElement: " + element.getEleName() + " ID:" + element.getId()+"]";
 	}
 
+	
+	
 
-
-
-
-	//Example Constrains:
-	/** Flexibility should be offered when Element is active.*/
-	public static Predicate<Flexibility> onConstrain = f 	-> 	f.getElement().isActive();
-	/** Flexibility should be offered when Element is inactive.*/
-	public static Predicate<Flexibility> offConstrain = f	-> 	!f.getElement().isActive();
+	
 	
 	
 	

+ 1 - 1
src/ui/controller/FlexManager.java

@@ -56,7 +56,7 @@ public class FlexManager {
 		List<HolonObject> list = new ArrayList<HolonObject>();
 		for(AbstractCpsObject aCps :  objectsOnCanvas) {
 			if(aCps instanceof HolonObject) list.add((HolonObject) aCps);
-			else list.addAll(createListOfHolonObjects(((CpsUpperNode)aCps).getNodes()));
+			else if(aCps instanceof CpsUpperNode)list.addAll(createListOfHolonObjects(((CpsUpperNode)aCps).getNodes()));
 		}
 		return list;
 	}

+ 13 - 3
src/ui/controller/LoadController.java

@@ -5,6 +5,7 @@ import classes.*;
 import com.google.gson.JsonElement;
 import com.google.gson.JsonObject;
 import com.google.gson.JsonParser;
+import com.google.gson.reflect.TypeToken;
 
 import org.apache.commons.compress.archivers.ArchiveEntry;
 import org.apache.commons.compress.archivers.ArchiveException;
@@ -298,9 +299,18 @@ public class LoadController {
                                     HashMap<Integer, HolonElement> eleDispatch) {
 
         JsonObject object = jsonElement.getAsJsonObject();
-        System.out.println("loadCanvasElements");
         HolonElement temp = model.getGson().fromJson(object.get("properties"), HolonElement.class);
         initElements(temp);
+        temp.flexList = model.getGson().fromJson(object.get("FlexList"), new TypeToken<List<Flexibility>>() {}.getType());
+        //object.get("FlexList").getAsJsonArray().forEach(jo -> System.out.println("Hallo"));
+        //object.get("FlexList").getAsJsonArray().forEach(flexJson -> flexJson.getAsJsonObject().get("constrainList").getAsJsonArray().forEach(constrainJson -> System.out.println("Constrain:" + constrainJson.getAsJsonObject().get("name").getAsString())));
+        temp.flexList.stream().forEach(flex -> {
+        	flex.setElement(temp);
+        	flex.constrainList.forEach(con -> con.fixJson());
+        	
+        });
+        
+        
         // id which Object it was stored before
         int stored = object.get("ID").getAsInt();
         // lookup that object
@@ -372,7 +382,7 @@ public class LoadController {
      */
     private void loadUnitGraph(GRAPHTYPE type, JsonElement jsonElement, HashMap<Integer, AbstractCpsObject> objDispatch,
                                HashMap<Integer, HolonElement> eleDispatch) {
-    	//TODO Make UnitGraph unterscheidung hinfällig!!!!
+    	//TODO Make UnitGraph unterscheidung hinf�llig!!!!
         JsonObject object = jsonElement.getAsJsonObject();
         List<String> keys = getKeys(object);
         String p;
@@ -526,7 +536,7 @@ public class LoadController {
      * @param ele the element to be initialized
      */
     void initElements(HolonElement ele) {
-        
+        ele.flexList = new ArrayList<Flexibility>();
         ele.setGraphPoints(new LinkedList<>());
         ele.reset();
     }

+ 0 - 2
src/ui/controller/ObjectController.java

@@ -133,8 +133,6 @@ public class ObjectController {
      */
     public void addSelectedObject(AbstractCpsObject obj) {
         model.getSelectedCpsObjects().add(obj);
-        
-        System.out.println("Add Object");
     }
 
     /**

+ 4 - 0
src/ui/controller/SaveController.java

@@ -3,6 +3,8 @@ package ui.controller;
 import classes.*;
 import com.google.gson.JsonObject;
 import com.google.gson.JsonPrimitive;
+import com.google.gson.reflect.TypeToken;
+
 import org.apache.commons.compress.archivers.ArchiveException;
 import org.apache.commons.compress.archivers.ArchiveOutputStream;
 import org.apache.commons.compress.archivers.ArchiveStreamFactory;
@@ -254,6 +256,8 @@ public class SaveController {
         for (HolonElement ele : ((HolonObject) obj).getElements()) {
             temp.add("properties", model.getGson().toJsonTree(ele));
             temp.add("ID", new JsonPrimitive(obj.getId()));
+            temp.add("FlexList", model.getGson().toJsonTree(ele.flexList,  new TypeToken<List<Flexibility>>() {
+            }.getType()));
             // switch for deciding the key
             switch (type) {
                 case CANVAS:

+ 3 - 2
src/ui/controller/UpdateController.java

@@ -81,7 +81,7 @@ public class UpdateController {
 			}
 			for (HolonElement e1 : elemList) {
 				Object[] temp2 = { e1.getObjName(), e1.getId(), e1.getEleName(), e1.getEnergyPerElement(),
-						controller.getSimManager().getActualFlexManager().isAFlexInUseOfHolonElement(e1), e1.getAmount(), e1.isActive(), false };
+						controller.getSimManager().getActualFlexManager().isAFlexInUseOfHolonElement(e1), e1.getAmount(), e1.isActive()};
 				table.addRow(temp2);
 			}
 
@@ -95,7 +95,7 @@ public class UpdateController {
 				
 				for (HolonElement e1 : elemList) {
 					Object[] temp2 = { e1.getId(), e1.getEleName(), e1.getEnergyPerElement(),
-							controller.getSimManager().getActualFlexManager().isAFlexInUseOfHolonElement(e1), e1.getAmount(), e1.isActive(), false };
+							controller.getSimManager().getActualFlexManager().isAFlexInUseOfHolonElement(e1), e1.getAmount(), e1.isActive()};
 					table.addRow(temp2);
 				}
 			}
@@ -146,6 +146,7 @@ public class UpdateController {
 	public void refreshTableHolonElement(PropertyTable multiTable, PropertyTable singleTable) {
 		// Update of the Information about the HolonElements - only for
 		// HolonObjects
+		if(multiTable == null || singleTable == null ) return;
 		if (model.getSelectedCpsObjects().size() > 1) {
 			deleteRows(multiTable);
 			fillElementTable(model.getSelectedCpsObjects(), multiTable);

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

@@ -44,6 +44,7 @@ import javax.swing.tree.DefaultMutableTreeNode;
 import javax.swing.tree.DefaultTreeModel;
 
 import classes.AbstractCpsObject;
+import classes.Constrain;
 import classes.CpsUpperNode;
 import classes.Flexibility;
 import classes.HolonElement;
@@ -311,7 +312,6 @@ public class FlexWindow extends JFrame {
 			if(aCps instanceof HolonObject) expandTreeHolonObject((HolonObject)aCps, newObjectChild);
 			if(aCps instanceof CpsUpperNode)expandTreeUpperNode((CpsUpperNode)aCps, newObjectChild);
 			listOfAllSelectedHolonObjects.add(newObjectChild);
-			System.out.println("Added: " + aCps.getName());
 		}
 		treeModel.nodeStructureChanged(listOfAllSelectedHolonObjects);
 		stateTree.revalidate();
@@ -391,7 +391,7 @@ public class FlexWindow extends JFrame {
 		List<HolonObject> list = new ArrayList<HolonObject>();
 		for(AbstractCpsObject aCps :  objectsOnCanvas) {
 			if(aCps instanceof HolonObject) list.add((HolonObject) aCps);
-			else list.addAll(createListOfHolonObjects(((CpsUpperNode)aCps).getNodes()));
+			else if (aCps instanceof CpsUpperNode) list.addAll(createListOfHolonObjects(((CpsUpperNode)aCps).getNodes()));
 		}
 		return list;
 	}
@@ -573,8 +573,8 @@ public class FlexWindow extends JFrame {
 			toCreateFlex.cost = intermediateFlex.cost;
 			toCreateFlex.setCooldown(intermediateFlex.getCooldown());
 			toCreateFlex.offered=offeredCheckBox.isSelected();
-			if(onConstrainCheckBox.isSelected())toCreateFlex.constrainList.add(Flexibility.onConstrain);
-			if(offConstrainCheckBox.isSelected())toCreateFlex.constrainList.add(Flexibility.offConstrain);
+			if(onConstrainCheckBox.isSelected())toCreateFlex.constrainList.add(Constrain.createOnConstrain());
+			if(offConstrainCheckBox.isSelected())toCreateFlex.constrainList.add(Constrain.createOffConstrain());
 			
 			ele.flexList.add(toCreateFlex);
 			//save checkboxes

+ 3 - 2
src/ui/view/GUI.java

@@ -3047,8 +3047,8 @@ public class GUI implements CategoryListener {
 	 */
 	private void updateElementTableAfterChange(HolonElement eleBTemp,
 			int selectedValueBY) {
-		model.getSingleTable().setValueAt(false,
-				selectedValueBY, 6);
+//		model.getSingleTable().setValueAt(false,
+//				selectedValueBY, 6);
 		model.getSingleTable().setValueAt(eleBTemp.isActive(), selectedValueBY,
 				5);
 		model.getSingleTable().setValueAt(eleBTemp.getAmount(),
@@ -3061,6 +3061,7 @@ public class GUI implements CategoryListener {
 	}
 
 	public void triggerUpdateController(AbstractCpsObject temp) {
+		if(model != null) { return;}
 		if (temp != null) {
 			updCon.paintProperties(temp);
 		}

+ 12 - 20
src/ui/view/PropertyTable.java

@@ -9,38 +9,30 @@ import javax.swing.table.DefaultTableModel;
  */
 public class PropertyTable extends DefaultTableModel {
 
+	private final int maxColumns = 7;
+	
 	@Override
 	public Class<?> getColumnClass(int columnIndex) {
-		Class clazz = String.class;
-		if(columnIndex == 3) {
-			clazz = Boolean.class;
-			return clazz;
-		}
-		
-		
-		if (getColumnCount() == 8) {
+		//System.out.println("getColumnClass(" + "index:" + columnIndex+ ", ColumnCount:" + getColumnCount() + ")");
+		if (getColumnCount() == maxColumns) {
 			switch (columnIndex) {
+			case 4:
 			case 6:
-				clazz = Boolean.class;
-			case 7:
-				clazz = Boolean.class;
-				break;
+				return Boolean.class;
 			}
-		} else if (getColumnCount() == 7) {
+		} else if (getColumnCount() == maxColumns - 1) {
 			switch (columnIndex) {
+			case 3:
 			case 5:
-				clazz = Boolean.class;
-			case 6:
-				clazz = Boolean.class;
-				break;
+				return Boolean.class;
 			}
 		}
-		return clazz;
+		return String.class;
 	}
 	
 	@Override
 	public boolean isCellEditable(int row, int column) {
-		return getColumnCount() == 8 && column > 1
-				|| getColumnCount() == 7 && column > 0;
+		return getColumnCount() == maxColumns && column > 1
+				|| getColumnCount() == maxColumns -1  && column > 0;
 	}
 }