Преглед изворни кода

Implemented stuff. Stuff not working. Looking at master for fix

Ludwig Tietze пре 7 година
родитељ
комит
1e0cce2c05
4 измењених фајлова са 47 додато и 14 уклоњено
  1. 20 6
      src/classes/HolonElement.java
  2. 18 2
      src/classes/HolonSwitch.java
  3. 1 1
      src/ui/view/GUI.java
  4. 8 5
      src/ui/view/UnitGraph.java

+ 20 - 6
src/classes/HolonElement.java

@@ -13,7 +13,7 @@ import java.util.LinkedList;
  *
  * @author Gruppe14
  */
-public class HolonElement {
+public class HolonElement implements IGraphedElement{
 
     /* Points on the UnitGraph */
     private LinkedList<Point> graphPoints;
@@ -48,7 +48,7 @@ public class HolonElement {
     @Expose
     private int id;
     
-    private static final int DEFAULT_GRAPH_LENGTH=100;
+    //private static final int DEFAULT_GRAPH_LENGTH=100;
     //private int graphLength=DEFAULT_GRAPH_LENGTH; Unimplementable due to former developer's dark magic.
     
     /*
@@ -57,6 +57,9 @@ public class HolonElement {
      * If switched to flexible, this represents the maximum of usable energy
      */
     private float[] availableEnergyPerElementAt;
+    
+    @Expose
+    private int localPeriod;
 
     /**
      * Create a new HolonElement with a user-defined name, amount of the same
@@ -79,8 +82,7 @@ public class HolonElement {
      * 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){
-    	//if(model!=null)graphLength=model.getGraphIterations();
-    	//if(graphLength==0)graphLength=DEFAULT_GRAPH_LENGTH;//This would work if the used HolonElements were actually made using a proper constructor.
+    	setLocalPeriod(model.getGraphIterations());
     	setEleName(eleName);
         setAmount(amount);
         setEnergyPerElement(energy);
@@ -101,6 +103,7 @@ public class HolonElement {
     public HolonElement(HolonElement element) {
     	//graphLength=element.graphLength;//Should I add a getter?
         setEleName(element.getEleName());
+        setLocalPeriod(element.getLocalPeriod());
         setAmount(element.getAmount());
         setEnergyPerElement(element.getEnergyPerElement());
         setActive(element.isActive());
@@ -156,7 +159,7 @@ public class HolonElement {
      * Get the energyPerElement currently available
      */
     public float getAvailableEnergyAt(int timestep) {
-        return this.availableEnergyPerElementAt[timestep%DEFAULT_GRAPH_LENGTH];//TODO:use an attribute here
+        return this.availableEnergyPerElementAt[timestep%localPeriod];//TODO:use an attribute here
     }
 
     /**
@@ -264,7 +267,7 @@ public class HolonElement {
         if (flexible) {
             return ((float) amount) * energyPerElement;
         } else {
-            return ((float) amount) * availableEnergyPerElementAt[x%DEFAULT_GRAPH_LENGTH];//TODO: use an attribute
+            return ((float) amount) * availableEnergyPerElementAt[x%localPeriod];//TODO: use an attribute
         }
     }
 
@@ -397,4 +400,15 @@ public class HolonElement {
 
         return sb.toString();
     }
+
+	@Override
+	public void setLocalPeriod(int period) {
+		localPeriod=period;
+		availableEnergyPerElementAt=new float[period];//TODO do they get rewritten?
+	}
+
+	@Override
+	public int getLocalPeriod() {
+		return localPeriod;
+	}
 }

+ 18 - 2
src/classes/HolonSwitch.java

@@ -11,7 +11,7 @@ import com.google.gson.annotations.Expose;
  * @author Gruppe14
  *
  */
-public class HolonSwitch extends AbstractCpsObject {
+public class HolonSwitch extends AbstractCpsObject implements IGraphedElement {
 
 	/**
 	 * The class HolonSwitch represents an Object in the system, that has the
@@ -40,12 +40,15 @@ public class HolonSwitch extends AbstractCpsObject {
 	 */
 	@Expose
 	boolean manualMode;
+	
+	@Expose
+	int localPeriod;
 
 	/*
 	 * Energy at each point of the graph with 50 predefined points. At the
 	 * beginning, it starts with all values at energy
 	 */
-	boolean[] activeAt = new boolean[100];
+	boolean[] activeAt;
 	// Points on the UnitGraph
 	LinkedList<Point> graphPoints = new LinkedList<>();
 
@@ -58,6 +61,7 @@ public class HolonSwitch extends AbstractCpsObject {
 	 */
 	public HolonSwitch(String objName) {
 		super(objName);
+		activeAt=new boolean[100];
 		setManualState(true);
 		setAutoState(true);
 		setActiveAt(true);
@@ -76,6 +80,7 @@ public class HolonSwitch extends AbstractCpsObject {
 		super.setName(obj.getName());
 		setManualState(((HolonSwitch) obj).getActiveManual());
 		setAutoState(true);
+		setLocalPeriod(((IGraphedElement)obj).getLocalPeriod());
 		setActiveAt(true);
 		for (int i = 0; i < activeAt.length; i++) {
 			activeAt[i] = ((HolonSwitch) obj).getActiveAt()[i];
@@ -242,4 +247,15 @@ public class HolonSwitch extends AbstractCpsObject {
 	public boolean getManualMode() {
 		return manualMode;
 	}
+
+	@Override
+	public void setLocalPeriod(int period) {
+		localPeriod=period;
+		activeAt=new boolean[localPeriod];
+	}
+
+	@Override
+	public int getLocalPeriod() {
+		return localPeriod;
+	}
 }

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

@@ -2145,7 +2145,7 @@ public class GUI implements CategoryListener {
 						((Container)timePanel.getComponent(1))//timePanel
 						.getComponent(1)//timeSlider
 				)
-		)//TODO: This hardcoded shit
+		)//TODO: This hardcoded stuff
 				.addChangeListener(changeEvent -> {
 					int i = model.getCurIteration();
 					controller.calculateStateForTimeStep(i);

+ 8 - 5
src/ui/view/UnitGraph.java

@@ -52,11 +52,10 @@ public class UnitGraph extends JPanel implements MouseListener, MouseMotionListe
     private Point tempP = null;
     private double x = 0, y = 0;
     private int x1, x2, y1, y2, ctrlx1, ctrly1, ctrlx2, ctrly2;
-
     private int border = 4;
     private int textWidth = 0;
     
-    private int localPeriod = 100;
+	private IGraphedElement current;
 
     /**
      * Constructor.
@@ -83,6 +82,7 @@ public class UnitGraph extends JPanel implements MouseListener, MouseMotionListe
      * @param g Graphics
      */
     public void paintComponent(Graphics g) {
+    	
         super.paintComponent(g);
         g2 = (Graphics2D) g;
         RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
@@ -602,7 +602,7 @@ public class UnitGraph extends JPanel implements MouseListener, MouseMotionListe
      */
     public void repaintWithNewElement(ArrayList<HolonElement> selectedElement) {
         arrayOfFloats = selectedElement.get(selectedElement.size() - 1).getAvailableEnergyPerElementAt();
-        tempElements = selectedElement;
+        current = selectedElement.get(selectedElement.size()-1);
         pointList = selectedElement.get(selectedElement.size() - 1).getGraphPoints();
         isSwitch = false;
         isElement = true;
@@ -621,6 +621,7 @@ public class UnitGraph extends JPanel implements MouseListener, MouseMotionListe
      * @param s which should be visualized
      */
     public void repaintWithNewSwitch(HolonSwitch s) {
+    	current=s;
         arrayOfBooleans = s.getActiveAt();
         pointList = s.getGraphPoints();
         isSwitch = true;
@@ -843,7 +844,8 @@ public class UnitGraph extends JPanel implements MouseListener, MouseMotionListe
      */
     public void setLocalPeriod(int localPeriod){
     	//TODO: local Graph length for each HolonElelemt
-    	this.localPeriod = localPeriod;
+    	this.current.setLocalPeriod(localPeriod);
+    	
     }
     
     /**
@@ -851,6 +853,7 @@ public class UnitGraph extends JPanel implements MouseListener, MouseMotionListe
      * @return localPeriod of the current Element or Switch
      */
     public int getLocalPeriod(){
-    	return localPeriod;
+    	if(current!=null)return current.getLocalPeriod();
+    	else return model.getGraphIterations();//TODO: maybe rename
     }
 }