Selaa lähdekoodia

graph finished?

Kevin Trometer 7 vuotta sitten
vanhempi
commit
2f26b2fd6e
3 muutettua tiedostoa jossa 177 lisäystä ja 57 poistoa
  1. 19 2
      src/classes/TrackedDataSet.java
  2. 1 0
      src/ui/view/GUI.java
  3. 157 55
      src/ui/view/StatisticGraph.java

+ 19 - 2
src/classes/TrackedDataSet.java

@@ -8,12 +8,17 @@ public class TrackedDataSet {
 	public static  final int CONSUMPTION = 0;
 	public static final int PRODUCTION = 1;
 	public static final int ACTIVATED_ELEMENTS = 2;
-
+	public static final int ON_OFF = 3;
+	
 	//Variables of the Data Set
 	private AbstractCpsObject cps;
 	private int property;
 	private Color color;
 
+	//Value for each timeStep
+	
+	private float values[];
+	
 	/**
 	 * Data Set for the StatisticGraoh
 	 * 
@@ -28,6 +33,10 @@ public class TrackedDataSet {
 		this.cps = cps;
 		this.property = property;
 		this.color = color;
+		this.values = new float[100];
+		for (int i = 0; i < values.length; i++) {
+			values[i] = -1;
+		}
 	}
 
 	public AbstractCpsObject getCpsObject() {
@@ -41,4 +50,12 @@ public class TrackedDataSet {
 	public Color getColor() {
 		return this.color;
 	}
-}
+	
+	public float[] getValues() {
+		return this.values;
+	}
+	
+	public void setValAt(float val, int at){
+		this.values[at] = val;
+	}
+}

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

@@ -78,6 +78,7 @@ import classes.HolonSwitch;
 import classes.HolonTransformer;
 import classes.IdCounter;
 import classes.IdCounterElem;
+import classes.TrackedDataSet;
 import interfaces.CategoryListener;
 import ui.controller.Control;
 import ui.controller.UpdateController;

+ 157 - 55
src/ui/view/StatisticGraph.java

@@ -10,9 +10,13 @@ import java.util.ArrayList;
 
 import javax.swing.JPanel;
 
+import com.sun.javafx.css.CalculatedValue;
+
 import classes.AbstractCpsObject;
 import classes.HolonElement;
 import classes.HolonObject;
+import classes.HolonSwitch;
+import classes.TrackedDataSet;
 import javafx.util.Pair;
 import ui.controller.Control;
 import ui.model.Model;
@@ -23,7 +27,7 @@ public class StatisticGraph extends JPanel {
 	 * 
 	 */
 	private static final long serialVersionUID = 1L;
-	
+
 	// Max Objects
 	final int MAX_OBJECTS = 8;
 
@@ -45,8 +49,7 @@ public class StatisticGraph extends JPanel {
 	GeneralPath path = new GeneralPath();
 
 	// Data
-	private ArrayList<HolonObject> objects = new ArrayList<>();
-	private ArrayList<GeneralPath> objPaths = new ArrayList<>();
+	private ArrayList<TrackedDataSet> objects = new ArrayList<>();
 
 	/**
 	 * Constructor.
@@ -58,18 +61,7 @@ public class StatisticGraph extends JPanel {
 	 */
 	public StatisticGraph(final Model model, Control control) {
 		this.controller = control;
-		this.model = model;
-		colors.add(Color.BLUE);
-		colors.add(Color.RED);
-		colors.add(Color.GREEN);
-		colors.add(Color.CYAN);
-		colors.add(Color.ORANGE);
-		colors.add(Color.PINK);
-		colors.add(Color.MAGENTA);
-		colors.add(Color.YELLOW);
-		
-		
-		
+
 		this.setBackground(Color.WHITE);
 	}
 
@@ -98,42 +90,37 @@ public class StatisticGraph extends JPanel {
 			g2.drawLine(0, i, this.getWidth(), i);
 		}
 
-		// Reset?
-		if (!isSimRunning && model.getIsSimulation()) {
-			for (int i = 0; i < objects.size(); i++) {
-				objPaths.get(i).reset();
-				objPaths.get(i).moveTo(model.getCurIteration() * this.getWidth() / model.getIterations() - 1,
-						convertToCanvasY(Math.abs(objects.get(i).getCurrentEnergy())));
-			}
-		}
-
 		isSimRunning = model.getIsSimulation();
 
 		// if sim is on
 		if (isSimRunning) {
-			maximum = 0;
-			for (HolonObject obj : objects) {
-				if (maximum <= (Math.abs(obj.getCurrentEnergy()))) {
-					maximum = (Math.abs(obj.getCurrentEnergy()));
-				}
-			}
+			g2.setStroke(new BasicStroke(3));
+
+			// Calculate the Maximum
+			calcMaximum();
 
-			for (int i = 0; i < objects.size(); i++) {
-				g2.setColor(new Color((int) (Math.random() * 255), (int) (Math.random() * 255),
-						(int) (Math.random() * 255)));
-				objPaths.get(i).lineTo(model.getCurIteration() * this.getWidth() / model.getIterations() - 1,
-						convertToCanvasY(Math.abs(getEnergyAtCurrentTimeStep(objects.get(i)))));
-				objPaths.get(i).moveTo(model.getCurIteration() * this.getWidth() / model.getIterations() - 1,
-						convertToCanvasY(Math.abs(getEnergyAtCurrentTimeStep(objects.get(i)))));
+			// Calculate values for each set and add them
+			addValues();
 
+			// TODO Calculate Paths
+			for (TrackedDataSet set : objects) {
+				path.reset();
+				switch (set.getProperty()) {
+				case TrackedDataSet.CONSUMPTION:
+				case TrackedDataSet.PRODUCTION:
+				case TrackedDataSet.ACTIVATED_ELEMENTS:
+					createPathFloats(set);
+					break;
+				case TrackedDataSet.ON_OFF:
+					createPathBooleans(set);
+					break;
+				default:
+					break;
+				}
+				g2.setColor(set.getColor());
+				g2.draw(path);
 			}
 		}
-		g2.setStroke(new BasicStroke(3));
-		for (int i = 0; i < objPaths.size(); i++) {
-			g2.setColor(colors.get(i));
-			g2.draw(objPaths.get(i));
-		}
-
 	}
 
 	/**
@@ -142,13 +129,9 @@ public class StatisticGraph extends JPanel {
 	 * @param obj
 	 *            the Object to add
 	 */
-	public void addObject(HolonObject obj) {
-		if (objects.size() < MAX_OBJECTS && !objects.contains(obj)) {
-			objects.add((HolonObject) obj);
-			objPaths.add(new GeneralPath());
-			objPaths.get(objPaths.size() - 1)
-					.moveTo(model.getCurIteration() * this.getWidth() / model.getIterations() - 1, convertToCanvasY(Math
-							.abs(objects.get(objects.size() - 1).getCurrentEnergyAtTimeStep(model.getCurIteration()))));
+	public void addObject(TrackedDataSet set) {
+		if (objects.size() < MAX_OBJECTS && !objects.contains(set)) {
+			objects.add(set);
 		}
 	}
 
@@ -158,11 +141,15 @@ public class StatisticGraph extends JPanel {
 	 * @param obj
 	 *            the Object to remove
 	 */
-	public void removeObject(HolonObject obj) {
-		if (objects.contains(obj)) {
-			objPaths.remove(objects.indexOf(obj));
-			objects.remove(obj);
+	public void removeObject() {
+		for (TrackedDataSet set : objects) {
+			controller.addTextToConsole("Function not available");
 		}
+
+		/*
+		 * if (objects.contains(obj)) { objPaths.remove(objects.indexOf(obj));
+		 * objects.remove(obj); }
+		 */
 	}
 
 	/**
@@ -175,7 +162,7 @@ public class StatisticGraph extends JPanel {
 	public double convertToCanvasY(float d) {
 		return Math.abs((this.getHeight() - (d * (this.getHeight() / maximum))));
 	}
-	
+
 	/**
 	 * Does take into account which timestep is watched, calculates the max
 	 * values.
@@ -192,4 +179,119 @@ public class StatisticGraph extends JPanel {
 		return temp;
 	}
 
+	/**
+	 * Calculate the Max Value of the Graph
+	 */
+	private void calcMaximum() {
+		maximum = 0;
+		for (TrackedDataSet set : objects) {
+			int val = 0;
+			switch (set.getProperty()) {
+			case TrackedDataSet.CONSUMPTION:
+				for (HolonElement h : ((HolonObject) set.getCpsObject()).getElements()) {
+					if (h.getEnergy() < 0) {
+						val += h.getEnergy();
+					}
+				}
+				val *= -1;
+				break;
+			case TrackedDataSet.PRODUCTION:
+				for (HolonElement h : ((HolonObject) set.getCpsObject()).getElements()) {
+					if (h.getEnergy() > 0) {
+						val += h.getEnergy();
+					}
+				}
+
+				break;
+			case TrackedDataSet.ACTIVATED_ELEMENTS:
+				for (HolonElement h : ((HolonObject) set.getCpsObject()).getElements()) {
+					if (h.getEnergy() < 0) {
+						val += h.getAmount();
+					}
+				}
+				break;
+			default:
+				break;
+			}
+			if (val > maximum) {
+				maximum = val;
+			}
+		}
+	}
+
+	/**
+	 * Add the Current Values to each set
+	 */
+	private void addValues() {
+		for (TrackedDataSet set : objects) {
+			switch (set.getProperty()) {
+			case TrackedDataSet.CONSUMPTION:
+				for (HolonElement h : ((HolonObject) set.getCpsObject()).getElements()) {
+					if (h.getEnergy() < 0) {
+						set.setValAt(-h.getEnergyAt()[model.getCurIteration()], model.getCurIteration());
+					}
+				}
+				break;
+			case TrackedDataSet.PRODUCTION:
+				for (HolonElement h : ((HolonObject) set.getCpsObject()).getElements()) {
+					if (h.getEnergy() > 0) {
+						set.setValAt(h.getEnergyAt()[model.getCurIteration()], model.getCurIteration());
+					}
+				}
+
+				break;
+			case TrackedDataSet.ACTIVATED_ELEMENTS:
+				for (HolonElement h : ((HolonObject) set.getCpsObject()).getElements()) {
+					if (h.getEnergy() < 0) {
+
+					}
+				}
+				break;
+			case TrackedDataSet.ON_OFF:
+				if (((HolonSwitch) set.getCpsObject()).getState()) {
+					set.setValAt(1, model.getCurIteration());
+				} else {
+					set.setValAt(0, model.getCurIteration());
+				}
+				break;
+			default:
+				break;
+			}
+		}
+	}
+
+	/**
+	 * create Path with floats
+	 * 
+	 * @param set
+	 */
+	private void createPathFloats(TrackedDataSet set) {
+		boolean init = true;
+		for (int i = 0; i < set.getValues().length - 1; i++) {
+			if (init && set.getValues()[i] != -1) {
+				path.moveTo(model.getCurIteration() * this.getWidth() / model.getIterations() - 1,
+						convertToCanvasY(Math.abs(set.getValues()[i])));
+			}
+			path.lineTo(model.getCurIteration() * this.getWidth() / model.getIterations(),
+					convertToCanvasY(Math.abs(set.getValues()[i + 1])));
+		}
+	}
+
+	/**
+	 * create Path with booleans(0 and 1)
+	 * 
+	 * @param set
+	 */
+	private void createPathBooleans(TrackedDataSet set) {
+		boolean init = true;
+		for (int i = 0; i < set.getValues().length - 1; i++) {
+			if (init && set.getValues()[i] != -1) {
+				path.moveTo(model.getCurIteration() * this.getWidth() / model.getIterations() - 1,
+						convertToCanvasY((float) (set.getValues()[i] * maximum)));
+			}
+			path.lineTo(model.getCurIteration() * this.getWidth() / model.getIterations(),
+					convertToCanvasY((float) (set.getValues()[i + 1] * maximum)));
+		}
+	}
+
 }