Procházet zdrojové kódy

Fixes Flex Manager

TomTroppmann před 3 roky
rodič
revize
0516d4d773

+ 12 - 26
src/classes/HolonObject.java

@@ -4,6 +4,7 @@ package classes;
 import ui.controller.FlexManager;
 
 import java.util.ArrayList;
+import java.util.List;
 
 
 
@@ -16,7 +17,7 @@ import java.util.ArrayList;
  */
 public class HolonObject extends AbstractCanvasObject { 
     /* Array of all consumers */
-    private ArrayList<HolonElement> elements;
+    private List<HolonElement> elements = new ArrayList<HolonElement>();
     /* Array for tracking Production */
     private float[] trackingProd;
     /* Array for tracking Consumption */
@@ -32,7 +33,6 @@ public class HolonObject extends AbstractCanvasObject {
      */
     public HolonObject(String objName) {
         super(objName);
-        setElements(new ArrayList<>());
         setTrackingProd(new float[100]);
         setTrackingCons(new float[100]);
     }
@@ -42,9 +42,11 @@ public class HolonObject extends AbstractCanvasObject {
      *
      * @param obj object to be copied
      */
-    public HolonObject(AbstractCanvasObject obj) {
+    public HolonObject(HolonObject obj) {
         super(obj);
-        setElements(copyElements(((HolonObject) obj).getElements()));
+        for(HolonElement ele : obj.getElements()) {
+        	this.addElement(new HolonElement(ele));
+        }
         setTrackingProd(new float[100]);
         setTrackingCons(new float[100]);
     }
@@ -56,18 +58,10 @@ public class HolonObject extends AbstractCanvasObject {
      *
      * @return the elements ArrayList
      */
-    public ArrayList<HolonElement> getElements() {
+    public List<HolonElement> getElements() {
         return elements;
     }
 
-    /**
-     * Set a new ArrayList with HolonElements into the HolonObject.
-     *
-     * @param elements the elements to set
-     */
-    public void setElements(ArrayList<HolonElement> elements) {
-        this.elements = elements;
-    }
 
     /**
      * adds an Element to the Object.
@@ -76,6 +70,7 @@ public class HolonObject extends AbstractCanvasObject {
      */
     public void addElement(HolonElement element) {
         elements.add(element);
+        element.parentObject = this;
     }
 
     /**
@@ -110,19 +105,6 @@ public class HolonObject extends AbstractCanvasObject {
     }
 
 
-    /**
-     * Copy all Elements into a New Array.
-     *
-     * @param arr to copy
-     * @return the copy of arr
-     */
-    public ArrayList<HolonElement> copyElements(ArrayList<HolonElement> arr) {
-        ArrayList<HolonElement> newArr = new ArrayList<>();
-        for (HolonElement t : arr) {
-            newArr.add(new HolonElement(t));
-        }
-        return newArr;
-    }
 
 
     /**
@@ -376,4 +358,8 @@ public class HolonObject extends AbstractCanvasObject {
 	public int getNumberOfElements() {
 		return (int) elements.stream().count();
 	}
+
+	public void setElements(List<HolonElement> list) {
+		this.elements = list;
+	}
 }

+ 4 - 3
src/ui/controller/CategoryController.java

@@ -1,6 +1,7 @@
 package ui.controller;
 
 import java.util.ArrayList;
+import java.util.List;
 
 import classes.Category;
 import classes.AbstractCanvasObject;
@@ -153,15 +154,15 @@ public class CategoryController {
 	 *            Category
 	 * @param object
 	 *            New Object Name
-	 * @param elements
+	 * @param list
 	 *            Array of Elements
 	 * @param image
 	 *            the image Path
 	 */
-	public void addNewHolonObject(Category category, String object, ArrayList<HolonElement> elements, String image) {
+	public void addNewHolonObject(Category category, String object, List<HolonElement> list, String image) {
 		HolonObject obj = new HolonObject(object);
 		obj.setImage(image);
-		obj.setElements(elements);
+		obj.setElements(list);
 		obj.setSav(category.getName());
 		addObject(category, obj);
 	}

+ 4 - 3
src/ui/controller/Control.java

@@ -4,6 +4,7 @@ import java.awt.datatransfer.UnsupportedFlavorException;
 import java.io.File;
 import java.io.IOException;
 import java.util.ArrayList;
+import java.util.List;
 import java.util.stream.Collectors;
 
 import javax.swing.JFrame;
@@ -210,12 +211,12 @@ public class Control {
      *
      * @param cat Category
      * @param obj New Object Name
-     * @param ele Array of Elements
+     * @param list Array of Elements
      * @param img the image Path
      * @throws IOException
      */
-    public void addObject(Category cat, String obj, ArrayList<HolonElement> ele, String img) throws IOException {
-        categoryController.addNewHolonObject(cat, obj, ele, img);
+    public void addObject(Category cat, String obj, List<HolonElement> list, String img) throws IOException {
+        categoryController.addNewHolonObject(cat, obj, list, img);
         saveCategory();
     }
 

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

@@ -256,11 +256,18 @@ public class LoadController {
      * @param eleDispatch 
      */
     private void loadCategoryElements(JsonElement jsonElement, HashMap<Integer, HolonElement> eleDispatch) {
-        HolonElement temp = model.getGson().fromJson(jsonElement.getAsJsonObject().get("properties").getAsJsonObject(),
-                HolonElement.class);
-        eleDispatch.put(temp.getId(), temp);
-        initElements(temp);
-        objC.addElementIntoCategoryObject(temp.getSaving().getKey(), temp.getSaving().getValue(), temp);
+        JsonObject object = jsonElement.getAsJsonObject();
+        HolonElement ele = model.getGson().fromJson(object.get("properties"), HolonElement.class); 
+        eleDispatch.put(ele.getId(), ele);
+        initElements(ele);
+        ele.flexList = model.getGson().fromJson(object.get("FlexList"), new TypeToken<List<Flexibility>>() {}.getType());
+        if(ele.flexList == null) ele.flexList = new ArrayList<Flexibility>();
+        ele.flexList.stream().forEach(flex -> {
+        	flex.setElement(ele);
+        	flex.constrainList.forEach(con -> con.fixJson());
+        	
+        });
+        objC.addElementIntoCategoryObject(ele.getSaving().getKey(), ele.getSaving().getValue(), ele);
     }
 
     /**
@@ -322,8 +329,7 @@ public class LoadController {
         HolonElement ele = model.getGson().fromJson(object.get("properties"), HolonElement.class);
         initElements(ele);
         ele.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())));
+        if(ele.flexList == null) ele.flexList = new ArrayList<>();
         ele.flexList.stream().forEach(flex -> {
         	flex.setElement(ele);
         	flex.constrainList.forEach(con -> con.fixJson());
@@ -360,7 +366,6 @@ public class LoadController {
      */
     private void loadUnitGraph(GRAPHTYPE type, JsonElement jsonElement, HashMap<Integer, AbstractCanvasObject> objDispatch,
                                HashMap<Integer, HolonElement> eleDispatch) {
-    	//TODO Make UnitGraph unterscheidung hinf�llig!!!!
         JsonObject object = jsonElement.getAsJsonObject();
         List<String> keys = getKeys(object);
         String p;

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

@@ -47,7 +47,7 @@ public class ObjectController {
      * @param element the Element
      */
     public void addElement(HolonObject object, HolonElement element) {
-        object.getElements().add(element);
+        object.addElement(element);
     }
 
     /**

+ 2 - 1
src/ui/view/AbstractCanvas.java

@@ -8,6 +8,7 @@ import java.awt.Image;
 import java.awt.Point;
 import java.awt.event.MouseEvent;
 import java.util.ArrayList;
+import java.util.List;
 import java.util.TimerTask;
 
 import javax.swing.CellEditor;
@@ -75,7 +76,7 @@ public abstract class AbstractCanvas extends JPanel {
 	boolean toolTip; // Tooltip on or off
 	Vector2Int toolTipPos = new Vector2Int(); // Tooltip Position
 	String toolTipText = "";
-	ArrayList<HolonElement> dataSelected = new ArrayList<>();
+	List<HolonElement> dataSelected = new ArrayList<>();
 	ArrayList<AbstractCanvasObject> tempSelected = new ArrayList<>();
 	boolean[] showedInformation = new boolean[5];
 	boolean showConnectionInformation;

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

@@ -1681,7 +1681,7 @@ public class GUI{
 
 								AbstractCanvasObject h = null;
 								if (tempCps instanceof HolonObject) {
-									h = new HolonObject(tempCps);
+									h = new HolonObject((HolonObject)tempCps);
 								}
 								if (tempCps instanceof HolonSwitch) {
 									h = new HolonSwitch(tempCps);
@@ -1713,7 +1713,7 @@ public class GUI{
 
 							AbstractCanvasObject h = null;
 							if (tempCps instanceof HolonObject) {
-								h = new HolonObject(tempCps);
+								h = new HolonObject((HolonObject)tempCps);
 							}
 							if (tempCps instanceof HolonSwitch) {
 								h = new HolonSwitch(tempCps);

+ 20 - 20
tests/tests/PraktikumHolonsTestCanvasController.java

@@ -50,13 +50,13 @@ public class PraktikumHolonsTestCanvasController {
 	public void testAddingObjects() {
 		// just adding a few things
 		assertTrue("Number of Objects does not Match", model.getObjectsOnCanvas().size() == 0);
-		HolonObject a = new HolonObject(mp.searchCatObj(mp.searchCat("Energy"), "Power Plant"));
+		HolonObject a = new HolonObject((HolonObject)mp.searchCatObj(mp.searchCat("Energy"), "Power Plant"));
 		a.setPosition(100, 100);
 		controller.addNewObject(a);
 		assertTrue("ID of the Object does not Match", mp.searchByID(1).getName().equals("Power Plant"));
 		assertTrue("Type of the Object does not Match", mp.searchByID(1) instanceof HolonObject);
 		assertTrue("Number of Objects does not Match", model.getObjectsOnCanvas().size() == 1);
-		controller.addNewObject(new HolonObject(mp.searchCatObj(mp.searchCat("Energy"), "Power Plant")));
+		controller.addNewObject(new HolonObject((HolonObject)mp.searchCatObj(mp.searchCat("Energy"), "Power Plant")));
 		/* trick drag&drop replace by moving a to 100 100 and then back to a, so
 		 * the rest of new objects won't know which one to replace
 		 */
@@ -64,27 +64,27 @@ public class PraktikumHolonsTestCanvasController {
 		assertTrue("ID of the Object does not Match", mp.searchByID(2).getName().equals("Power Plant"));
 		assertTrue("Type of the Object does not Match", mp.searchByID(2) instanceof HolonObject);
 		assertTrue("Number of Objects does not Match", model.getObjectsOnCanvas().size() == 2);
-		controller.addNewObject(new HolonObject(mp.searchCatObj(mp.searchCat("Energy"), "Power Plant")));
+		controller.addNewObject(new HolonObject((HolonObject)mp.searchCatObj(mp.searchCat("Energy"), "Power Plant")));
 		assertTrue("ID of the Object does not Match", mp.searchByID(3).getName().equals("Power Plant"));
 		assertTrue("Type of the Object does not Match", mp.searchByID(3) instanceof HolonObject);
 		assertTrue("Number of Objects does not Match", model.getObjectsOnCanvas().size() == 3);
-		controller.addNewObject(new HolonObject(mp.searchCatObj(mp.searchCat("Energy"), "Power Plant")));
+		controller.addNewObject(new HolonObject((HolonObject)mp.searchCatObj(mp.searchCat("Energy"), "Power Plant")));
 		assertTrue("Number of Objects does not Match", model.getObjectsOnCanvas().size() == 4);
-		controller.addNewObject(new HolonObject(mp.searchCatObj(mp.searchCat("Energy"), "Power Plant")));
+		controller.addNewObject(new HolonObject((HolonObject)mp.searchCatObj(mp.searchCat("Energy"), "Power Plant")));
 		assertTrue("Number of Objects does not Match", model.getObjectsOnCanvas().size() == 5);
-		controller.addNewObject(new HolonObject(mp.searchCatObj(mp.searchCat("Building"), "House")));
+		controller.addNewObject(new HolonObject((HolonObject)mp.searchCatObj(mp.searchCat("Building"), "House")));
 		assertTrue("ID of the Object does not Match", mp.searchByID(6).getName().equals("House"));
 		assertTrue("Type of the Object does not Match", mp.searchByID(6) instanceof HolonObject);
 		assertTrue("Number of Objects does not Match", model.getObjectsOnCanvas().size() == 6);
-		controller.addNewObject(new HolonObject(mp.searchCatObj(mp.searchCat("Building"), "House")));
+		controller.addNewObject(new HolonObject((HolonObject)mp.searchCatObj(mp.searchCat("Building"), "House")));
 		assertTrue("Number of Objects does not Match", model.getObjectsOnCanvas().size() == 7);
-		controller.addNewObject(new HolonObject(mp.searchCatObj(mp.searchCat("Building"), "House")));
+		controller.addNewObject(new HolonObject((HolonObject)mp.searchCatObj(mp.searchCat("Building"), "House")));
 		assertTrue("Number of Objects does not Match", model.getObjectsOnCanvas().size() == 8);
-		controller.addNewObject(new HolonSwitch(mp.searchCatObj(mp.searchCat("Component"), "Switch")));
+		controller.addNewObject(new HolonSwitch((HolonObject)mp.searchCatObj(mp.searchCat("Component"), "Switch")));
 		assertTrue("ID of the Object does not Match", mp.searchByID(9).getName().equals("Switch"));
 		assertTrue("Type of the Object does not Match", mp.searchByID(9) instanceof HolonSwitch);
 		assertTrue("Number of Objects does not Match", model.getObjectsOnCanvas().size() == 9);
-		controller.addNewObject(new HolonSwitch(mp.searchCatObj(mp.searchCat("Component"), "Switch")));
+		controller.addNewObject(new HolonSwitch((HolonObject)mp.searchCatObj(mp.searchCat("Component"), "Switch")));
 		assertTrue("Number of Objects does not Match", model.getObjectsOnCanvas().size() == 10);
 	}
 	
@@ -94,16 +94,16 @@ public class PraktikumHolonsTestCanvasController {
 	@Test(expected = NullPointerException.class)
 	public void testDeletingObject() {
 		// Adding Objects on Canvas.. without Coordinates
-		controller.addNewObject(new HolonObject(mp.searchCatObj(mp.searchCat("Energy"), "Power Plant")));
-		controller.addNewObject(new HolonObject(mp.searchCatObj(mp.searchCat("Energy"), "Power Plant")));
-		controller.addNewObject(new HolonObject(mp.searchCatObj(mp.searchCat("Building"), "House")));
-		controller.addNewObject(new HolonObject(mp.searchCatObj(mp.searchCat("Energy"), "Power Plant")));
-		controller.addNewObject(new HolonObject(mp.searchCatObj(mp.searchCat("Energy"), "Power Plant")));
-		controller.addNewObject(new HolonObject(mp.searchCatObj(mp.searchCat("Building"), "House")));
-		controller.addNewObject(new HolonSwitch(mp.searchCatObj(mp.searchCat("Component"), "Switch")));
-		controller.addNewObject(new HolonObject(mp.searchCatObj(mp.searchCat("Energy"), "Power Plant")));
-		controller.addNewObject(new HolonObject(mp.searchCatObj(mp.searchCat("Building"), "House")));
-		controller.addNewObject(new HolonSwitch(mp.searchCatObj(mp.searchCat("Component"), "Switch")));
+		controller.addNewObject(new HolonObject((HolonObject)mp.searchCatObj(mp.searchCat("Energy"), "Power Plant")));
+		controller.addNewObject(new HolonObject((HolonObject)mp.searchCatObj(mp.searchCat("Energy"), "Power Plant")));
+		controller.addNewObject(new HolonObject((HolonObject)mp.searchCatObj(mp.searchCat("Building"), "House")));
+		controller.addNewObject(new HolonObject((HolonObject)mp.searchCatObj(mp.searchCat("Energy"), "Power Plant")));
+		controller.addNewObject(new HolonObject((HolonObject)mp.searchCatObj(mp.searchCat("Energy"), "Power Plant")));
+		controller.addNewObject(new HolonObject((HolonObject)mp.searchCatObj(mp.searchCat("Building"), "House")));
+		controller.addNewObject(new HolonSwitch((HolonObject)mp.searchCatObj(mp.searchCat("Component"), "Switch")));
+		controller.addNewObject(new HolonObject((HolonObject)mp.searchCatObj(mp.searchCat("Energy"), "Power Plant")));
+		controller.addNewObject(new HolonObject((HolonObject)mp.searchCatObj(mp.searchCat("Building"), "House")));
+		controller.addNewObject(new HolonSwitch((HolonObject)mp.searchCatObj(mp.searchCat("Component"), "Switch")));
 
 		controller.deleteObjectOnCanvas(mp.searchByID(4));
 		assertTrue("Object:4 was not deleted", mp.searchByID(4) == null);

+ 0 - 3
tests/tests/PraktikumHolonsTestClasses.java

@@ -71,9 +71,6 @@ public class PraktikumHolonsTestClasses {
 		assertTrue("String not corrent", str.equals("Element, Element"));
 		test2.deleteElement(0);
 		test2.addElement(ele);
-
-		assertTrue("Should be Empty", test3.getElements().isEmpty());
-		test3.setElements(test2.copyElements(test2.getElements()));
 		
 		assertTrue("Element not Found", test3.searchElement("Element") != null);
 		test1.setElements(new ArrayList<>());

+ 1 - 39
tests/tests/PraktikumHolonsTestObjectController.java

@@ -74,30 +74,6 @@ public class PraktikumHolonsTestObjectController {
 					((HolonObject) mp.searchCatObj(mp.searchCat("Building"), "House")).getElements().size() == 6 + i);
 		}
 
-//		controller.deleteElementInCategory("Building", "House", "B");
-//		controller.deleteElementInCategory("Building", "House", "D");
-//		controller.deleteElementInCategory("Building", "House", "F");
-//		controller.deleteElementInCategory("Building", "House", "G");
-//		controller.deleteElementInCategory("Building", "House", "H");
-//		controller.deleteElementInCategory("Building", "House", "I");
-//		controller.deleteElementInCategory("Building", "House", "Z");
-//		controller.deleteElementInCategory("Building", "House", "TV");
-//		assertTrue("Element:B was Found",
-//				mp.searchEle((HolonObject) mp.searchCatObj(mp.searchCat("Building"), "House"), "B") == null);
-//		assertTrue("Element:D was Found",
-//				mp.searchEle((HolonObject) mp.searchCatObj(mp.searchCat("Building"), "House"), "D") == null);
-//		assertTrue("Element:F was Found",
-//				mp.searchEle((HolonObject) mp.searchCatObj(mp.searchCat("Building"), "House"), "F") == null);
-//		assertTrue("Element:G was Found",
-//				mp.searchEle((HolonObject) mp.searchCatObj(mp.searchCat("Building"), "House"), "G") == null);
-//		assertTrue("Element:H was Found",
-//				mp.searchEle((HolonObject) mp.searchCatObj(mp.searchCat("Building"), "House"), "H") == null);
-//		assertTrue("Element:I was Found",
-//				mp.searchEle((HolonObject) mp.searchCatObj(mp.searchCat("Building"), "House"), "I") == null);
-//		assertTrue("Element:Z was Found",
-//				mp.searchEle((HolonObject) mp.searchCatObj(mp.searchCat("Building"), "House"), "Z") == null);
-//		assertTrue("Element:TV was Found",
-//				mp.searchEle((HolonObject) mp.searchCatObj(mp.searchCat("Building"), "House"), "TV") == null);
 	}
 
 	/**
@@ -106,22 +82,8 @@ public class PraktikumHolonsTestObjectController {
 	@Test
 	public void testAddingAndDeletingInCanvas() {
 		for (int i = 0; i < 100; i++) {
-			cvs.addNewObject(new HolonObject(mp.searchCatObj(mp.searchCat("Building"), "House")));
+			cvs.addNewObject(new HolonObject((HolonObject)mp.searchCatObj(mp.searchCat("Building"), "House")));
 		}
-//		for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
-//			for (int i = 0; i < 27; i++) {
-//				controller.addNewElementIntoCanvasObject(cps.getId(), adapter.generate(i), 1, -100);
-//				assertTrue("Element:" + adapter.generate(i) + " was not Created",
-//						mp.searchEle((HolonObject) mp.searchByID(cps.getId()), adapter.generate(i)) != null);
-//			}
-//			assertTrue("Element:B was not Found", mp.searchEle((HolonObject) mp.searchByID(cps.getId()), "B") != null);
-//			assertTrue("Element:D was not Found", mp.searchEle((HolonObject) mp.searchByID(cps.getId()), "D") != null);
-//			assertTrue("Element:F was not Found", mp.searchEle((HolonObject) mp.searchByID(cps.getId()), "F") != null);
-//			assertTrue("Element:G was not Found", mp.searchEle((HolonObject) mp.searchByID(cps.getId()), "G") != null);
-//			assertTrue("Element:H was not Found", mp.searchEle((HolonObject) mp.searchByID(cps.getId()), "H") != null);
-//			assertTrue("Element:I was not Found", mp.searchEle((HolonObject) mp.searchByID(cps.getId()), "I") != null);
-//			assertTrue("Element:B was not Found", mp.searchEle((HolonObject) mp.searchByID(cps.getId()), "B") != null);
-//		}
 
 		for (AbstractCanvasObject cps : model.getObjectsOnCanvas()) {
 			int size = model.getSelectedCpsObjects().size();