Ver Fonte

Added safer method for accessing HolonSwitch arrays as classes kept
trying to access the wrong index.

Updated all classes accordingly.

Ludwig Tietze há 7 anos atrás
pai
commit
3dc627da07

+ 15 - 2
src/classes/HolonElement.java

@@ -2,7 +2,9 @@ package classes;
 
 import com.google.gson.annotations.Expose;
 
+import ui.controller.SingletonControl;
 import ui.model.Model;
+import ui.view.UnitGraph;
 
 import java.awt.*;
 import java.util.LinkedList;
@@ -60,6 +62,8 @@ public class HolonElement implements IGraphedElement{
     
     @Expose
     private int localPeriod;
+    @Expose
+    private boolean stretch;
 
     /**
      * Create a new HolonElement with a user-defined name, amount of the same
@@ -267,7 +271,7 @@ public class HolonElement implements IGraphedElement{
         if (flexible) {
             return ((float) amount) * energyPerElement;
         } else {
-            return ((float) amount) * availableEnergyPerElementAt[x%localPeriod];//TODO: use an attribute
+            return ((float) amount) * availableEnergyPerElementAt[UnitGraph.getEffectiveIndex(this, x)];
         }
     }
 
@@ -404,11 +408,20 @@ public class HolonElement implements IGraphedElement{
 	@Override
 	public void setLocalPeriod(int period) {
 		localPeriod=period;
-		availableEnergyPerElementAt=new float[period];//TODO do they get rewritten?
 	}
 
 	@Override
 	public int getLocalPeriod() {
 		return localPeriod;
 	}
+
+	@Override
+	public boolean isStretching() {
+		return stretch;
+	}
+
+	@Override
+	public void setStretching(boolean stretch) {
+		this.stretch=stretch;
+	}
 }

+ 27 - 4
src/classes/HolonSwitch.java

@@ -5,6 +5,9 @@ import java.util.LinkedList;
 
 import com.google.gson.annotations.Expose;
 
+import ui.controller.SingletonControl;
+import ui.view.UnitGraph;
+
 /**
  * The class HolonSwitch represents a Switch, which can be turned on and off.
  * 
@@ -43,6 +46,9 @@ public class HolonSwitch extends AbstractCpsObject implements IGraphedElement {
 	
 	@Expose
 	int localPeriod;
+	
+	@Expose
+	boolean stretch;
 
 	/*
 	 * Energy at each point of the graph with 50 predefined points. At the
@@ -61,6 +67,10 @@ public class HolonSwitch extends AbstractCpsObject implements IGraphedElement {
 	 */
 	public HolonSwitch(String objName) {
 		super(objName);
+		setLocalPeriod(SingletonControl.getInstance().getControl()==null?
+				100:
+				SingletonControl.getInstance().getControl().getModel().getGraphIterations()
+		);
 		activeAt=new boolean[100];
 		setManualState(true);
 		setAutoState(true);
@@ -83,7 +93,7 @@ public class HolonSwitch extends AbstractCpsObject implements IGraphedElement {
 		setLocalPeriod(((IGraphedElement)obj).getLocalPeriod());
 		setActiveAt(true);
 		for (int i = 0; i < activeAt.length; i++) {
-			activeAt[i] = ((HolonSwitch) obj).getActiveAt()[i];
+			activeAt[i] = ((HolonSwitch) obj).getActiveAt(i);
 		}
 		setGraphPoints(new LinkedList<Point>());
 		for (Point p : ((HolonSwitch) obj).getGraphPoints()) {
@@ -119,7 +129,7 @@ public class HolonSwitch extends AbstractCpsObject implements IGraphedElement {
 		if (manualMode) {
 			return this.manualActive;
 		} else {
-			return getActiveAt()[timeStep];
+			return getActiveAt(UnitGraph.getEffectiveIndex(this));
 		}
 	}
 
@@ -203,7 +213,11 @@ public class HolonSwitch extends AbstractCpsObject implements IGraphedElement {
 	 * 
 	 * @return boolean[] the States of each Iteration
 	 */
-	public boolean[] getActiveAt() {
+	public boolean/*[]*/ getActiveAt(int i) {
+		return activeAt[i];
+	}
+	
+	public boolean[] getValueArray() {
 		return activeAt;
 	}
 
@@ -251,11 +265,20 @@ public class HolonSwitch extends AbstractCpsObject implements IGraphedElement {
 	@Override
 	public void setLocalPeriod(int period) {
 		localPeriod=period;
-		activeAt=new boolean[localPeriod];
 	}
 
 	@Override
 	public int getLocalPeriod() {
 		return localPeriod;
 	}
+
+	@Override
+	public boolean isStretching() {
+		return stretch;
+	}
+
+	@Override
+	public void setStretching(boolean stretch) {
+		this.stretch=stretch;
+	}
 }

+ 7 - 0
src/classes/IGraphedElement.java

@@ -3,4 +3,11 @@ package classes;
 public interface IGraphedElement {
 	void setLocalPeriod(int period);
 	int getLocalPeriod();
+	
+	/**
+	 * @return Whether the given points should be stretched over the entire graph
+	 * as opposed to repeated once the period is over.
+	 */
+	boolean isStretching();
+	void setStretching(boolean stretch);
 }

+ 2 - 2
src/classes/SubNet.java

@@ -75,7 +75,7 @@ public class SubNet {
         sb.append("  Switches:");
         for (int j = 0; j < getSwitches().size(); j++) {
             HolonSwitch sw = getSwitches().get(j);
-            sb.append("    " + sw.getName() + " " + sw.getId() + " State:" + sw.getActiveAt()[timeStep]);
+            sb.append("    " + sw.getName() + " " + sw.getId() + " State:" + sw.getActiveAt(timeStep));
         }
 
         return sb.toString();
@@ -94,7 +94,7 @@ public class SubNet {
         }
         sb.append("\nSwitches:");
         for (HolonSwitch sw : getSwitches()) {
-            sb.append("    " + sw.getName() + "[ID:" + sw.getId()+ "]" + " State:" + sw.getActiveAt());
+            sb.append("    " + sw.getName() + "[ID:" + sw.getId()+ "]" + " State:" + sw.getValueArray());//TODO: I don't like this
         }
         sb.append("\nBatteries:");
         for (HolonBattery hB: getBatteries()) {

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

@@ -315,7 +315,7 @@ public class UpdateController {
 					model.getPropertyTable().setCellEditable(3, 1, true);
 				} else {
 					Object[] tempActive = { Languages.getLanguage()[75],
-							((HolonSwitch) obj).getActiveAt()[model.getCurIteration()] };
+							((HolonSwitch) obj).getActiveAt(model.getCurIteration()) };
 					model.getPropertyTable().addRow(tempActive);
 					model.getPropertyTable().setCellEditable(3, 1, false);
 				}

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

@@ -280,7 +280,7 @@ public abstract class AbstractCanvas extends JPanel {
 			img = Util.loadImage(this, "/Images/node_selected.png");
 		} else {
 			if (cps instanceof HolonSwitch) {
-				if (((HolonSwitch) cps).getActiveAt()[model.getCurIteration()]) {
+				if (((HolonSwitch) cps).getActiveAt(model.getCurIteration())) {
 					((HolonSwitch) cps).setAutoState(true);
 				} else {
 					((HolonSwitch) cps).setAutoState(false);

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

@@ -354,7 +354,7 @@ public class StatisticGraph extends JPanel {
                             set.setValAt(0, model.getCurIteration());
                         }
                     } else {
-                        if (((HolonSwitch) set.getCpsObject()).getActiveAt()[model.getCurIteration()]) {
+                        if (((HolonSwitch) set.getCpsObject()).getActiveAt(model.getCurIteration())) {
                             set.setValAt(1, model.getCurIteration());
                         } else {
                             set.setValAt(0, model.getCurIteration());

+ 32 - 18
src/ui/view/UnitGraph.java

@@ -2,6 +2,7 @@ package ui.view;
 
 import classes.*;
 import ui.controller.Control;
+import ui.controller.SingletonControl;
 import ui.model.Model;
 
 import javax.swing.*;
@@ -104,6 +105,8 @@ public class UnitGraph extends JPanel implements MouseListener, MouseMotionListe
         }
         g2.drawLine(border, this.getHeight() - border, this.getWidth() - border, this.getHeight() - border);
 
+        int effectiveX=getEffectiveIndex(current, model.getCurIteration());
+        
         if (isElement) {
             // fill array with values from the pointList in a HolonElement
             fillArrayofValue();
@@ -131,17 +134,17 @@ public class UnitGraph extends JPanel implements MouseListener, MouseMotionListe
 
                 // Iteration Value
                 //TODO: added function getGraphIterations see if it works
-                textWidth = g.getFontMetrics().stringWidth("" + arrayOfFloats[model.getCurIteration()%model.getGraphIterations()]) + 2;
+                textWidth = g.getFontMetrics().stringWidth("" + arrayOfFloats[effectiveX]) + 2;
                 if (textWidth
-                        + (model.getCurIteration()) * (this.getWidth() - (border * 2)) / (model.getIterations() - 1) + 2
+                        + (effectiveX) * (this.getWidth() - (border * 2)) / (model.getIterations() - 1) + 2
                         + border <= this.getWidth()) {
-                    g2.drawString("" + arrayOfFloats[model.getCurIteration()%model.getGraphIterations()],
-                            (model.getCurIteration()) * (this.getWidth() - (border * 2)) / (model.getIterations() - 1)
+                    g2.drawString("" + arrayOfFloats[effectiveX],
+                            (effectiveX) * (this.getWidth() - (border * 2)) / (model.getIterations() - 1)
                                     + 2 + border,
                             this.getHeight() - 10);
                 } else {
-                    g2.drawString("" + arrayOfFloats[model.getCurIteration()%model.getGraphIterations()],
-                            (model.getCurIteration()%model.getGraphIterations()) * (this.getWidth() - (border * 2)) / (model.getIterations() - 1)
+                    g2.drawString("" + arrayOfFloats[effectiveX],
+                            (effectiveX) * (this.getWidth() - (border * 2)) / (model.getIterations() - 1)
                                     + border - textWidth,
                             this.getHeight() - 10);
                 }
@@ -215,17 +218,17 @@ public class UnitGraph extends JPanel implements MouseListener, MouseMotionListe
 
                 // Iteration Value
                 g2.setColor(Color.BLUE);
-                textWidth = g.getFontMetrics().stringWidth("" + arrayOfBooleans[model.getCurIteration()]) + 2;
+                textWidth = g.getFontMetrics().stringWidth("" + arrayOfBooleans[effectiveX]) + 2;
                 if (textWidth
-                        + (model.getCurIteration()) * (this.getWidth() - (border * 2)) / (model.getIterations() - 1) + 2
+                        + (effectiveX) * (this.getWidth() - (border * 2)) / (/*model.getIterations()*/100 - 1) + 2
                         + border <= this.getWidth()) {
-                    g2.drawString("" + arrayOfBooleans[model.getCurIteration()],
-                            (model.getCurIteration()) * (this.getWidth() - (border * 2)) / (model.getIterations() - 1)
+                    g2.drawString("" + arrayOfBooleans[effectiveX],
+                            (effectiveX) * (this.getWidth() - (border * 2)) / (/*model.getIterations()*/100 - 1)
                                     + 2 + border,
                             this.getHeight() - 10);
                 } else {
-                    g2.drawString("" + arrayOfBooleans[model.getCurIteration()],
-                            (model.getCurIteration()) * (this.getWidth() - (border * 2)) / (model.getIterations() - 1)
+                    g2.drawString("" + arrayOfBooleans[effectiveX],
+                            (effectiveX) * (this.getWidth() - (border * 2)) / (/*model.getIterations()*/100 - 1)
                                     + border - textWidth,
                             this.getHeight() - 10);
                 }
@@ -237,7 +240,7 @@ public class UnitGraph extends JPanel implements MouseListener, MouseMotionListe
             {
                 try {
                     int i;
-                    for (i = 0; (i * (this.getWidth() - (border * 2)) / (model.getIterations() - 1)
+                    for (i = 0; (i * (this.getWidth() - (border * 2)) / (/*model.getIterations()*/100 - 1)
                             + border < getMousePosition().getX()); i++) {
                     }
                     dragInformation = "" + i;
@@ -255,15 +258,15 @@ public class UnitGraph extends JPanel implements MouseListener, MouseMotionListe
             }
         }
 
-        // Iteration Line
+        // Iteration Line TODO: repeat
         g2.setColor(Color.BLUE);
         g2.setStroke(new BasicStroke(1));
-        g2.drawLine(border + (model.getCurIteration()) * (this.getWidth() - border * 2) / (model.getIterations() - 1),
-                0, border + (model.getCurIteration()) * (this.getWidth() - border * 2) / (model.getIterations() - 1),
+        g2.drawLine(border + (effectiveX) * (this.getWidth() - border * 2) / /*(model.getIterations() - 1)*/100-1,
+                0, border + (effectiveX) * (this.getWidth() - border * 2) / /*(model.getIterations() - 1)*/100-1,
                 this.getHeight());
 
         // algorithmus
-        controller.calculateStateForTimeStep(model.getCurIteration());
+        controller.calculateStateForTimeStep(effectiveX);
     }
 
     @Override
@@ -603,6 +606,7 @@ public class UnitGraph extends JPanel implements MouseListener, MouseMotionListe
     public void repaintWithNewElement(ArrayList<HolonElement> selectedElement) {
         arrayOfFloats = selectedElement.get(selectedElement.size() - 1).getAvailableEnergyPerElementAt();
         current = selectedElement.get(selectedElement.size()-1);
+        tempElements=selectedElement;
         pointList = selectedElement.get(selectedElement.size() - 1).getGraphPoints();
         isSwitch = false;
         isElement = true;
@@ -622,7 +626,7 @@ public class UnitGraph extends JPanel implements MouseListener, MouseMotionListe
      */
     public void repaintWithNewSwitch(HolonSwitch s) {
     	current=s;
-        arrayOfBooleans = s.getActiveAt();
+        arrayOfBooleans = s.getValueArray();
         pointList = s.getGraphPoints();
         isSwitch = true;
         isElement = false;
@@ -856,4 +860,14 @@ public class UnitGraph extends JPanel implements MouseListener, MouseMotionListe
     	if(current!=null)return current.getLocalPeriod();
     	else return model.getGraphIterations();//TODO: maybe rename
     }
+    
+    public static int getEffectiveIndex(IGraphedElement e, int timeStep){
+    	Model m=SingletonControl.getInstance().getControl().getModel();
+    	if(e.isStretching())return timeStep*100/m.getIterations();
+    	else return timeStep%e.getLocalPeriod()*100/e.getLocalPeriod();
+    }
+    
+    public static int getEffectiveIndex(IGraphedElement e){
+    	return getEffectiveIndex(e,SingletonControl.getInstance().getControl().getModel().getCurIteration());
+    }
 }

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

@@ -478,7 +478,7 @@ public class UpperNodeCanvas extends AbstractCanvas implements MouseListener, Mo
             	img = Util.loadImage(this,"/Images/node_selected.png");
             } else {
                 if (cps instanceof HolonSwitch) {
-                    if (((HolonSwitch) cps).getActiveAt()[model.getCurIteration()]) {
+                    if (((HolonSwitch) cps).getActiveAt(model.getCurIteration())) {
                         ((HolonSwitch) cps).setAutoState(true);
                     } else {
                         ((HolonSwitch) cps).setAutoState(false);