Browse Source

GraphPoints update init

Tom Troppmann 6 years ago
parent
commit
c82717f19d

+ 73 - 41
src/classes/HolonElement.java

@@ -6,6 +6,7 @@ import ui.model.Model;
 import ui.view.UnitGraph;
 
 import java.awt.*;
+import java.util.Iterator;
 import java.util.LinkedList;
 
 /**
@@ -16,35 +17,43 @@ import java.util.LinkedList;
  */
 public class HolonElement implements IGraphedElement{
 
-    /* Points on the UnitGraph */
+    /** Points on the UnitGraph */
     private LinkedList<Point> graphPoints;
-    /* Name of the Object: House, 1 */
+    
+    /** Points of new TestGraph 
+     * Represent the Graph 
+     * the X component from a Point is period
+     * the Y component from a Point is the percentage
+     * currently saved in int -> TODO serelized float point class
+     * */
+    private LinkedList<Point> testGraphPoints;
+    /** Name of the object, e.g. House, 1 */
     @Expose
     private String objName;
-    /* Name of the gadget: TV */
+    /** Name of the gadget, e.g. TV */
     @Expose
     private String eleName;
-    /* Quantity */
+    /** Amount of same elements */
     @Expose
     private int amount;
-    /* Currently used energy per element (- indicates consumation of energy, + indicates production of energy)*/
+    /** Currently used energy per element (- indicates consumation of energy, + indicates production of energy)*/
     @Expose
     private float energyPerElement;
-    /* Whether the gadget is active or not (currently uses/produces the energy in energyPerElement) */
+    /** Whether the gadget is active or not (currently uses/produces the energy in energyPerElement) */
     @Expose
     private boolean active;
-    /* Gives us whether this element is flexible and can flexibly use any part of the energy in flexibleEnergyAvailable */
+    /** 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 */
+    /** Flexibility (meaning the actual */
     @Expose
     private float flexibleEnergyAvailable;
 
     
-    /* Place where the Object is Stored */
+    /** Place where the Object is Stored */
     @Expose
     private Pair<String, String> saving;
-    /* ID */
+    /** ID */
     @Expose
     private int id;
     
@@ -76,29 +85,12 @@ public class HolonElement implements IGraphedElement{
     	this(eleName, amount, energy, IdCounterElem.nextId(),model);
     }
     
-    /**
-     * Create a new HolonElement with a user-defined name, amount of the same
-     * element and energyPerElement.
-     *
-     * @param eleName String
-     * @param amount  int
-     * @param energy  float
-     * @param model Model
-     * @deprecated Constructing a HolonElement without a model
-     * make sit detached from a real simulation.
-     * 
-     * This constructor only exists for dry tests
-     * for whether HolonElements generally function as intended.
-     */
-    @Deprecated public HolonElement(String eleName, int amount, float energy) {
-    	this(eleName, amount, energy, IdCounterElem.nextId(),null);//TODO: This is just for the old tests...
-    }
 
     /**
      * same as standard constructor, but with already given id (so the counter is not increased twice)
      */
     public HolonElement(String eleName, int amount, float energy, int id, Model model){
-    	setLocalPeriod(model==null?UnitGraph.STANDARD_GRAPH_ACCURACY:model.getGraphIterations());
+    	setLocalPeriod(model==null? UnitGraph.STANDARD_GRAPH_ACCURACY : model.getGraphIterations());
     	setStretching(IGraphedElement.STRETCH_BY_DEFAULT);
     	setEleName(eleName);
         setAmount(amount);
@@ -106,6 +98,9 @@ public class HolonElement implements IGraphedElement{
         setActive(true);
         setAvailableEnergyPerElementAt(energy);
         setGraphPoints(new LinkedList<>());
+        System.out.println("heiNEW");
+        setTestGraphPoints(new LinkedList<>());
+        initTestGraphPoints(100);
         setId(id);
         setFlexibleEnergyAvailable(0);
         setFlexible(false);
@@ -129,9 +124,14 @@ public class HolonElement implements IGraphedElement{
             availableEnergyPerElementAt[i] = element.getAvailableEnergyAt(i);
         }
         setGraphPoints(new LinkedList<>());
+        setTestGraphPoints(new LinkedList<>());
         for (Point p : element.getGraphPoints()) {
             this.graphPoints.add(new Point((int) p.getX(), (int) p.getY()));
         }
+        System.out.println("hei");
+        for (Point p : element.getTestGraphPoints()) {
+            this.testGraphPoints.add(new Point((int) p.getX(), (int) p.getY()));
+        }
         setSaving(null);
         setId(IdCounterElem.nextId());
         setFlexibleEnergyAvailable(0);
@@ -146,14 +146,6 @@ public class HolonElement implements IGraphedElement{
 		this.objName = objName;
 	}
 
-	/*
-     * Get the Array of energyPerElement (100 values).
-     *
-     * @return availableEnergyPerElementAt Array of Floats
-     */
-    /*public float[] getAvailableEnergyPerElementAt() {
-        return availableEnergyPerElementAt;
-    }*/
 
     /**
      * Set energyPerElement to any value to the whole array.
@@ -174,6 +166,21 @@ public class HolonElement implements IGraphedElement{
         return this.availableEnergyPerElementAt[UnitGraph.getEffectiveIndex(this, timestep)];
     }
 
+    
+    /**
+     * 
+     * @param timestep
+     * @return
+     */
+    public float testFunctiongetAvailableEnergyAt(int timestep) {
+		if(getTestGraphPoints()==null)return 0;
+    	this.getTestGraphPoints().iterator();
+		for(Iterator<Point> iterator = getTestGraphPoints().iterator(); iterator.hasNext();)
+		{
+			System.out.println(iterator.next());
+		}
+    	return 0;
+    }
     /**
      * Set energyPerElement to any value at a given position.
      *
@@ -230,13 +237,22 @@ public class HolonElement implements IGraphedElement{
     }
     
     /**
-     *  Return if the HolonElemnt is a Producer
-     * 	@return boolean true when the energy used be each element is higher then 0
+     *  Check the HolonElemnet is a Producer
+     * 	@return true when the energy used be each element is higher then 0
      */
     public boolean isProducer()
     {
     	return (energyPerElement > 0);
     }
+    
+    /**
+     *  Check the HolonElemnet is a Consumer
+     * 	@return true when the energy used be each element is lower then 0
+     */
+    public boolean isConsumer()
+    {
+    	return (energyPerElement < 0);
+    }
 
     /**
      * Set the energyPerElement value of the selected Element.
@@ -280,14 +296,14 @@ public class HolonElement implements IGraphedElement{
     /**
      * Get the energyPerElement value at a selected time x.
      *
-     * @param x int
+     * @param timestep int
      * @return energyPerElement value
      */
-    public float getOverallEnergyAtTimeStep(int x) {
+    public float getOverallEnergyAtTimeStep(int timestep) {
         if (flexible) {
             return ((float) amount) * energyPerElement;
         } else {
-            return ((float) amount) * availableEnergyPerElementAt[UnitGraph.getEffectiveIndex(this, x)];
+            return ((float) amount) * availableEnergyPerElementAt[UnitGraph.getEffectiveIndex(this, timestep)];
         }
     }
 
@@ -420,4 +436,20 @@ public class HolonElement implements IGraphedElement{
 	public void setStretching(boolean stretch) {
 		this.stretch=stretch;
 	}
+
+//--> Test Area --> TODO Neue float Point Klasse
+	public void initTestGraphPoints(int percentage)
+	{
+		testGraphPoints.clear();
+		testGraphPoints.add(new Point(0,percentage));
+		testGraphPoints.add(new Point(100,percentage));
+	}
+	public LinkedList<Point> getTestGraphPoints() {
+		return testGraphPoints;
+	}
+
+
+	public void setTestGraphPoints(LinkedList<Point> testGraphPoints) {
+		this.testGraphPoints = testGraphPoints;
+	}
 }

+ 1 - 1
src/classes/HolonObject.java

@@ -517,7 +517,7 @@ public class HolonObject extends AbstractCpsObject {
                 if (e.isActive() && e.isProducer()) {
                     valueProd += e.getOverallEnergyAtTimeStep(i);
                 }
-                if (e.isActive() && !e.isProducer()) {
+                if (e.isActive() && e.isConsumer()) {
                     valueCons += e.getOverallEnergyAtTimeStep(i);
                 }
             }

+ 2 - 2
src/ui/controller/CanvasController.java

@@ -225,10 +225,11 @@ public class CanvasController {
 		}
 
 		ArrayList<AbstractCpsObject> tempList = new ArrayList<>();
-
+		System.out.println("okay copy");
 		// Objects
 		for (AbstractCpsObject cps : model.getClipboradObjects()) {
 			if (cps instanceof HolonObject) {
+				System.out.println("copy");
 				tCps = new HolonObject((HolonObject) cps);
 			} else if (cps instanceof HolonSwitch) {
 				tCps = new HolonSwitch((HolonSwitch) cps);
@@ -239,7 +240,6 @@ public class CanvasController {
 			tCps.setSav(cps.getSav());
 			tempList.add(tCps);
 			addObject(tCps, false);
-			// MODEL.getSelectedCpsObjects().add(tCps);
 		}
 
 		// Edges

+ 9 - 2
src/ui/controller/ClipboardController.java

@@ -60,7 +60,7 @@ public class ClipboardController {
         for (AbstractCpsObject abs : model.getSelectedCpsObjects()) {
             queue.add(abs);
         }
-        
+        System.out.println("heiCopy");
         while (!queue.isEmpty()) {
 
             u = queue.pop();
@@ -191,6 +191,8 @@ public class ClipboardController {
                 loadUnitGraph(GRAPHTYPE.SWITCH, json.get(key), objDispatch, null);
             if (key.contains("ELEUNITGRAPH"))
                 loadUnitGraph(GRAPHTYPE.ELEMENT, json.get(key), null, eleDispatch);
+            if (key.contains("ELETESTUNITGRAPH"))
+                loadUnitGraph(GRAPHTYPE.TESTELEMENT, json.get(key), null, eleDispatch);
         }
     }
 
@@ -253,7 +255,7 @@ public class ClipboardController {
         List<String> keys = load.getKeys(object);
         String p;
         int mid, x, y;
-
+        System.out.println("loadcontroller");
         LinkedList<Point> graphpoint = new LinkedList<>();
         int sav = 0;
         // foreach Point in the graph restore it
@@ -281,6 +283,11 @@ public class ClipboardController {
                 HolonElement ele = eleDispatch.get(sav);
                 ele.setGraphPoints(graphpoint);
                 break;
+            case TESTELEMENT:
+                sav = eleIDMap.get(sav);
+                HolonElement ele1 = eleDispatch.get(sav);
+                ele1.setTestGraphPoints(graphpoint);
+                break;
             default:
                 break;
         }

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

@@ -176,6 +176,8 @@ public class LoadController {
                 loadUnitGraph(GRAPHTYPE.SWITCH, json.get(key), objDispatch, null);
             if (key.contains("ELEUNITGRAPH"))
                 loadUnitGraph(GRAPHTYPE.ELEMENT, json.get(key), null, eleDispatch);
+            if (key.contains("ELETESTUNITGRAPH"))
+                loadUnitGraph(GRAPHTYPE.ELEMENT, json.get(key), null, eleDispatch);
             if (key.contains("TRACKED"))
                 loadTracked(json.get(key), objDispatch);
             if (key.contains("STATSGRAPH"))
@@ -388,7 +390,6 @@ public class LoadController {
             } else
                 // else its an ID
                 sav = object.get(k).getAsInt();
-
         }
 
         switch (type) {
@@ -400,6 +401,10 @@ public class LoadController {
                 HolonElement ele = eleDispatch.get(sav);
                 ele.setGraphPoints(graphpoint);//TODO?
                 break;
+            case TESTELEMENT:
+                HolonElement ele1 = eleDispatch.get(sav);
+                ele1.setTestGraphPoints(graphpoint);
+                break;
             default:
                 break;
         }
@@ -518,6 +523,7 @@ public class LoadController {
     void initElements(HolonElement ele) {
         ele.setAvailableEnergyPerElementAt(ele.getEnergyPerElement());
         ele.setGraphPoints(new LinkedList<>());
+        ele.setTestGraphPoints(new LinkedList<>());
     }
 
     /**
@@ -554,7 +560,7 @@ public class LoadController {
     }
 
     public enum GRAPHTYPE {
-        SWITCH, ELEMENT
+        SWITCH, ELEMENT, TESTELEMENT
     }
 
 }

+ 7 - 2
src/ui/controller/SaveController.java

@@ -266,8 +266,10 @@ public class SaveController {
             }
             file.add(key, model.getGson().toJsonTree(temp));
             // if there are gps add them into
-            if (ele.getGraphPoints().size() != 0)
+            if (!ele.getGraphPoints().isEmpty())
                 unitgraphToJson(GRAPHTYPE.ELEMENT, file, ele.getId(), ele.getGraphPoints());
+            if (!ele.getTestGraphPoints().isEmpty())
+                unitgraphToJson(GRAPHTYPE.TESTELEMENT, file, ele.getId(), ele.getTestGraphPoints());
             temp = new JsonObject();
         }
 
@@ -292,6 +294,9 @@ public class SaveController {
             case ELEMENT:
                 key = "ELEUNITGRAPH" + getNumerator(NUMTYPE.UNITGRAPH);
                 break;
+            case TESTELEMENT:
+                key = "ELETESTUNITGRAPH" + getNumerator(NUMTYPE.UNITGRAPH);
+                break;
             default:
                 break;
         }
@@ -496,7 +501,7 @@ public class SaveController {
     }
 
     public enum GRAPHTYPE {
-        SWITCH, ELEMENT
+        SWITCH, ELEMENT, TESTELEMENT
     }
 
 }

+ 66 - 4
src/ui/view/UnitGraph.java

@@ -13,6 +13,7 @@ import java.awt.geom.GeneralPath;
 import java.awt.geom.Line2D;
 import java.util.ArrayDeque;
 import java.util.ArrayList;
+import java.util.Iterator;
 import java.util.LinkedList;
 
 /**
@@ -606,6 +607,7 @@ public class UnitGraph extends JPanel implements MouseListener, MouseMotionListe
      * @param selectedElement which should be visualized
      */
     public void repaintWithNewElement(ArrayList<HolonElement> selectedElement) {
+    	//maybe linkedlist better?
         //arrayOfFloats = selectedElement.get(selectedElement.size() - 1).getAvailableEnergyPerElementAt();
         current = selectedElement.get(selectedElement.size()-1);
         tempElements=selectedElement;
@@ -689,18 +691,78 @@ public class UnitGraph extends JPanel implements MouseListener, MouseMotionListe
      * Fills the Arrays of each HolonElement.
      */
     @SuppressWarnings("unchecked")
-    public void fillArrayofValue() {
+	public void fillArrayofValue() {
         for (HolonElement he : tempElements) {
             maximum = getMaximum(he);
             he.setGraphPoints((LinkedList<Point>) pointList.clone());
+            //foreach(Point p: pointList)
+            System.out.println("------------");
+            System.out.println("pointList[");
+            for(Point p: pointList)
+    		{
+    			System.out.println(p);
+    		}
+            System.out.println("]");
             for (int i = 0; i < STANDARD_GRAPH_ACCURACY; i++) {//!!!!!
             	he.setAvailableEnergyPerElementAt(i, convertToValueY(getYValueAt2((int) (i * width / (100 - 1)))));
-                //he.getAvailableEnergyPerElementAt()[i] = convertToValueY(getYValueAt2((int) (i * width / (100 - 1))));
             }
-            //arrayOfFloats = he.getAvailableEnergyPerElementAt();
+            
+            he.setTestGraphPoints(convertUnitGraphToHolonElemntPointList(pointList));
+            
+            System.out.println("TestgraphPoints:");
+            he.testFunctiongetAvailableEnergyAt(0);
+            LinkedList<Point> testUnitGraph = this.convertHolonElementPointListToUnitGraph(he.getTestGraphPoints());
+            System.out.println("TestUnitGraph[");
+            for(Point p: testUnitGraph)
+    		{
+    			System.out.println(p);
+    		}
+            System.out.println("NochmalChecknachGraohPoint");
+            LinkedList<Point> testGraphPoint = this.convertUnitGraphToHolonElemntPointList(testUnitGraph);
+            for(Point p: testGraphPoint)
+    		{
+    			System.out.println(p);
+    		}
         }
     }
 
+    
+    
+    /**
+     * Convert the graph widget point to a pointlist from a holon element
+     * @param unitgraph 
+     */
+    public LinkedList<Point> convertUnitGraphToHolonElemntPointList(LinkedList<Point> unitgraph)
+    {
+    	LinkedList<Point> graphpoints = new LinkedList<Point>();
+    	if(width == 0 || height == 0) return graphpoints;
+    	for(Point p: unitgraph)
+ 		{
+ 			//CalcX
+ 			int x = (int )((p.getX() / width)* 100.0); 
+ 			//CalcY 
+ 			int y = (int )((1.0-(p.getY() / height))* 100.0); //1.0- because 0 is at the top, width is at the bottom
+ 			//AddPoint 
+ 			graphpoints.add(new Point(x, y));
+ 		}
+    	return graphpoints;
+    }
+    
+    
+    public LinkedList<Point> convertHolonElementPointListToUnitGraph(LinkedList<Point> graphPoints)
+    {
+    	LinkedList<Point> unitgraph = new LinkedList<Point>();
+    	for(Point p: graphPoints)
+    	{
+    		//CalcX
+    		int x = (int )((p.getX() / 100.0)* width); 
+    		//CalcY
+    		int y = (int )((1.0-(p.getY() / 100.0))* height); //1.0- because 0 is at the top, width is at the bottom
+    		//AddPoint
+    		unitgraph.add(new Point(x, y));
+    	}
+    	return unitgraph;
+    }
     /**
      * Get the Y Value at the x Coordination.
      *
@@ -895,7 +957,7 @@ public class UnitGraph extends JPanel implements MouseListener, MouseMotionListe
      * to determine the total number of iterations(for "use global").
      */
     public static int getEffectiveIndex(IGraphedElement e, int timeStep){
-    	return getEffectiveIndex(SingletonControl.getInstance().getControl()==null?null:SingletonControl.getInstance().getControl().getModel(),e,timeStep);
+    	return getEffectiveIndex(SingletonControl.getInstance().getControl()==null ? null : SingletonControl.getInstance().getControl().getModel(),e,timeStep);
     }
     
     /**

+ 2 - 1
tests/tests/PraktikumHolonsAdapter.java

@@ -7,6 +7,7 @@ import classes.Category;
 import classes.AbstractCpsObject;
 import classes.HolonElement;
 import classes.HolonObject;
+import classes.IdCounterElem;
 
 /**
  * This Class Generates Stuff for the Tests.
@@ -42,7 +43,7 @@ public class PraktikumHolonsAdapter {
 	 */
 	public void generateElements(ArrayList<HolonElement> arr) {
 		for (int i = 1; i < 18279; i++) {
-			arr.add(new HolonElement(generate(i), 1, 10));
+			arr.add(new HolonElement(generate(i), 1, 10, IdCounterElem.nextId(),null));
 		}
 	}
 	

+ 7 - 10
tests/tests/PraktikumHolonsTestClasses.java

@@ -52,7 +52,7 @@ public class PraktikumHolonsTestClasses {
 		HolonObject test1 = new HolonObject("Test1");
 		HolonObject test2 = new HolonObject("Test2");
 		HolonObject test3 = new HolonObject(test1);
-		HolonElement ele = new HolonElement("Element", 1, 10);
+		HolonElement ele = new HolonElement("Element", 1, 10, IdCounterElem.nextId(),null);
 		ArrayList<HolonElement> arr = new ArrayList<>();
 		assertTrue("Should be Empty", test1.getElements().isEmpty());
 		test1.setElements(arr);
@@ -85,16 +85,16 @@ public class PraktikumHolonsTestClasses {
 		assertTrue("Element not Found", test3.searchElement("Element") != null);
 		test1.setElements(new ArrayList<>());
 		assertTrue("Not Empty", !test1.checkIfPartiallySupplied(1));
-		test3.addElement(new HolonElement("Element2", 1, -10));
+		test3.addElement(new HolonElement("Element2", 1, -10, IdCounterElem.nextId(),null));
 		assertTrue("Not Partially Supplied", test3.checkIfPartiallySupplied(1));
-		test3.addElement(new HolonElement("Element2", 2, -10));
+		test3.addElement(new HolonElement("Element2", 2, -10, IdCounterElem.nextId(),null));
 		assertTrue("Not Partially Supplied", test3.checkIfPartiallySupplied(1));
-		test1.addElement(new HolonElement("Element2", 2, -10));
+		test1.addElement(new HolonElement("Element2", 2, -10, IdCounterElem.nextId(),null));
 		assertTrue("minSupply < 0", !test1.checkIfPartiallySupplied(1));
 		Color color = test3.getColor();
 		test3.setColor(new Color(0, 255, 255));
 		assertTrue(color.getBlue() != test3.getColor().getBlue());
-		test3.addElement(new HolonElement("Element3", 3, 50));
+		test3.addElement(new HolonElement("Element3", 3, 50, IdCounterElem.nextId(),null));
 		test3.setState();
 		assertTrue("Should be state 3", test3.getState() == 3);
 		test1.setState();
@@ -181,9 +181,8 @@ public class PraktikumHolonsTestClasses {
 	 */
 	@Test
 	public void testHolonElement() {
-		HolonElement ele1 = new HolonElement("TV", 2, -20);
-		HolonElement ele2 = new HolonElement("Fridge", 1, -50);
-		HolonElement ele3 = new HolonElement(ele2);
+		HolonElement ele1 = new HolonElement("TV", 2, -20, IdCounterElem.nextId(),null);
+		HolonElement ele2 = new HolonElement("Fridge", 1, -50, IdCounterElem.nextId(),null);
 
         assertTrue("Array not empty", ele1.getAvailableEnergyAt(0) == -20);
         assertTrue("Array not empty", ele1.getAvailableEnergyAt(2) == -20);
@@ -196,8 +195,6 @@ public class PraktikumHolonsTestClasses {
 		ele2.setAmount(5);
 		assertTrue("Amount not correct", ele2.getAmount() == 5);
         assertTrue("Total Energy not Correct", ele2.getOverallEnergy() == ele2.getAmount() * ele2.getEnergyPerElement());
-//		ele3.setSav("CVS");
-//		assertTrue("SAV not correct", ele3.getSav().equals("CVS"));
 	}
 	/**
 	 * Test for Position.