Переглянути джерело

Merge branch 'master' of https://git.tk.informatik.tu-darmstadt.de/carlos.garcia/praktikum-holons

dominik.rieder 8 роки тому
батько
коміт
ffd41e04ad

+ 1 - 1
src/classes/HolonElement.java

@@ -65,7 +65,7 @@ public class HolonElement {
 	 * @param element
 	 *            element to copy
 	 */
-	public HolonElement(HolonElement element) {
+	public HolonElement(HolonElement element)	 {
 		setEleName(element.getEleName());
 		setAmount(element.getAmount());
 		setEnergy(element.getEnergy());

+ 7 - 1
src/classes/HolonObject.java

@@ -31,8 +31,14 @@ public class HolonObject extends AbstractCpsObject {
 	/* Array for tracking Consumption */
 	private float[] trackingCons;
 	/*
-	 * 0 = no energy, 1 = not supplied, 2 = supplied, 3 producer
+	 * 0 = no energy, 1 = not supplied, 2 = supplied, 3 producer, 4 Partially Supplied
 	 */
+	public final static int NO_ENERGY = 0;
+	public final static int NOT_SUPPLIED = 1;
+	public final static int SUPPLIED = 2;
+	public final static int PRODUCER = 3;
+	public final static int PARTIALLY_SUPPLIED = 4;
+	
 	private int state = 0;
 
 	/**

+ 24 - 5
src/ui/controller/Control.java

@@ -110,7 +110,7 @@ public class Control {
 	public AbstractCpsObject searchByID(int id) {
 		return multiPurposeController.searchByID(id);
 	}
-	
+
 	/**
 	 * Search for Object by ID in upperNode
 	 * 
@@ -722,8 +722,8 @@ public class Control {
 		}
 	}
 
-	//========================= MANAGING TRACKED OBJECTS ====================
-	
+	// ========================= MANAGING TRACKED OBJECTS ====================
+
 	public void setTrackingObj(ArrayList<AbstractCpsObject> objArr) {
 		statsController.setTrackingObj(objArr);
 	}
@@ -739,8 +739,8 @@ public class Control {
 	public void removeTrackingObj(AbstractCpsObject obj) {
 		statsController.removeTrackingObj(obj);
 	}
-	
-	//========================== MANAGING TRACKED OBJECTS END ================
+
+	// ========================== MANAGING TRACKED OBJECTS END ================
 
 	/**
 	 * Controlling Nodes of Nodes
@@ -777,4 +777,23 @@ public class Control {
 	public void disconnectNodes(CpsEdge edge, CpsUpperNode upperNode) {
 		nodeController.disconnectNodes(edge, upperNode);
 	}
+
+	/**
+	 * Get the number of HolonObjects in the given List
+	 * 
+	 * @param list
+	 */
+	public int getNumberHolonObjects(ArrayList<AbstractCpsObject> list) {
+		return objectController.getNumberHolonObjects(list);
+	}
+	
+	/**
+	 * Get the number of HolonObjects with the given state in the given List
+	 * 
+	 * @param list
+	 * @param state
+	 */
+	public int getNumberStateObjects(ArrayList<AbstractCpsObject> list, int state) {
+		return objectController.getNumberStateObjects(list, state);
+	}
 }

+ 24 - 8
src/ui/controller/NodeController.java

@@ -241,13 +241,13 @@ public class NodeController {
 		}
 		node.getOldEdges().removeAll(lostEdges);
 
-		// LOST = DIE IN NODE SIND, TOSEARCH = IHRE PARTNER DIE IN ANDEREN NODES
-		// SIND
-
+		// für alle Edges für die nix passendes gefunden ist
 		for (CpsEdge edge : lostEdges) {
 			AbstractCpsObject toSearch = null;
 			AbstractCpsObject lost = null;
+			boolean foundCps = false;
 
+			// bestimmung welcher verloren ist und wen man suchen muss
 			if (lostChildren.contains(edge.getA())) {
 				toSearch = edge.getB();
 				lost = edge.getA();
@@ -255,12 +255,20 @@ public class NodeController {
 				toSearch = edge.getA();
 				lost = edge.getB();
 			}
+			// wenn der zu suchende ein CpsUpperNode war
 			if (toSearch instanceof CpsUpperNode) {
+
+				// guck einfach in den Connections des Verlorenen nach Edges die
+				// auf der Canvas sind.
 				for (CpsEdge e : lost.getConnections()) {
-					if ((found.contains(e.getA()) || found.contains(e.getB())) && !node.getOldEdges().contains(e))
+					if (( (found.contains(e.getA()) && !found.equals(lost))  || (found.contains(e.getB()) && !found.equals(lost)) ) && !node.getOldEdges().contains(e)) {
 						node.getOldEdges().add(e);
+						foundCps = true;	
+					}
+					
 				}
-			} else
+
+			} if (!foundCps)
 				outerLoop: for (AbstractCpsObject cps : found) {
 					if (!cps.equals(node) && !lostChildren.contains(cps))
 						if (backtrackLostChild(cps, toSearch, lost)) {
@@ -271,11 +279,11 @@ public class NodeController {
 									continue outerLoop;
 
 							if (!lookforDuplicates(cps, lost, node.getOldEdges())) {
-								
+
 								CpsEdge temp = new CpsEdge(cps, lost, edge.getCapacity());
 								node.getOldEdges().add(temp);
-
-								//break;
+//								if(cps instanceof CpsUpperNode)
+//									((CpsUpperNode)cps).getOldEdges().add(edge);
 							}
 
 						}
@@ -289,6 +297,14 @@ public class NodeController {
 
 	}
 
+	/**
+	 * Just checking if an Egde already exists
+	 * 
+	 * @param a
+	 * @param b
+	 * @param list
+	 * @return
+	 */
 	private boolean lookforDuplicates(AbstractCpsObject a, AbstractCpsObject b, ArrayList<CpsEdge> list) {
 		for (CpsEdge cpsEdge : list) {
 			if ((a.equals(cpsEdge.getA()) && b.equals(cpsEdge.getB()))

+ 40 - 0
src/ui/controller/ObjectController.java

@@ -3,6 +3,7 @@ package ui.controller;
 import java.util.ArrayList;
 
 import classes.Category;
+import classes.CpsUpperNode;
 import classes.AbstractCpsObject;
 import classes.HolonElement;
 import classes.HolonObject;
@@ -221,4 +222,43 @@ public class ObjectController {
 	public void setClipboardObjects(ArrayList<AbstractCpsObject> list) {
 		model.setClipboradObjects(list);
 	}
+
+	/**
+	 * Get the number of HolonObjects in the given List
+	 * 
+	 * @param list
+	 */
+	public int getNumberHolonObjects(ArrayList<AbstractCpsObject> list) {
+		int val = 0;
+
+		for (AbstractCpsObject obj : list) {
+			if (obj instanceof HolonObject) {
+				val++;
+			} else if (obj instanceof CpsUpperNode) {
+				val += getNumberHolonObjects(((CpsUpperNode) obj).getNodes());
+			}
+		}
+		return val;
+	}
+
+	/**
+	 * Get the number of HolonObjects with the given state in the given List
+	 * 
+	 * @param list
+	 * @param state
+	 */
+	public int getNumberStateObjects(ArrayList<AbstractCpsObject> list, int state) {
+		int val = 0;
+
+		for (AbstractCpsObject obj : list) {
+			if (obj instanceof HolonObject) {
+				if (((HolonObject) obj).getState() == state || (state == 2 && ((HolonObject) obj).getState() == 3)) {
+					val++;
+				}
+			} else if (obj instanceof CpsUpperNode) {
+				val += getNumberStateObjects(((CpsUpperNode) obj).getNodes(), state);
+			}
+		}
+		return val;
+	}
 }

+ 141 - 10
src/ui/view/StatisticGraph.java

@@ -15,6 +15,8 @@ import javax.swing.ImageIcon;
 import javax.swing.JPanel;
 import javax.swing.Timer;
 
+import classes.AbstractCpsObject;
+import classes.CpsUpperNode;
 import classes.HolonElement;
 import classes.HolonObject;
 import classes.HolonSwitch;
@@ -30,7 +32,7 @@ public class StatisticGraph extends JPanel {
 	private static final long serialVersionUID = 1L;
 
 	// Maximum y Value
-	double maximum = 0;
+	float maximum = 0;
 
 	// is the Simulation running?
 	private boolean isSimRunning;
@@ -115,7 +117,7 @@ public class StatisticGraph extends JPanel {
 				case TrackedDataSet.PERCENT_SUPPLIED:
 				case TrackedDataSet.PERCENT_NOT_SUPPLIED:
 				case TrackedDataSet.PERCENT_PARTIAL_SUPPLIED:
-					//TODO Percentage Method
+					createPathPercent(set);
 					break;
 				default:
 					break;
@@ -181,7 +183,7 @@ public class StatisticGraph extends JPanel {
 	public void calcMaximum() {
 		maximum = 0;
 		for (TrackedDataSet set : objects) {
-			int val = 0;
+			float val = 0;
 			switch (set.getProperty()) {
 			case TrackedDataSet.CONSUMPTION:
 				for (HolonElement h : ((HolonObject) set.getCpsObject()).getElements()) {
@@ -208,10 +210,11 @@ public class StatisticGraph extends JPanel {
 				val = 1;
 				break;
 			case TrackedDataSet.TOTAL_PRODUCTION:
-				//TODO
+				val = getMaxTotalProduction(model.getObjectsOnCanvas());
 				break;
 			case TrackedDataSet.TOTAL_CONSUMPTION:
-				//TODO
+				val = getMaxTotalConsumption(model.getObjectsOnCanvas());
+				val *= -1;
 				break;
 			case TrackedDataSet.PERCENT_SUPPLIED:
 			case TrackedDataSet.PERCENT_NOT_SUPPLIED:
@@ -276,19 +279,19 @@ public class StatisticGraph extends JPanel {
 				}
 				break;
 			case TrackedDataSet.TOTAL_PRODUCTION:
-
+				set.setValAt(getTotalProductionAt(model.getObjectsOnCanvas(), model.getCurIteration()), model.getCurIteration());
 				break;
 			case TrackedDataSet.TOTAL_CONSUMPTION:
-
+				set.setValAt(-getTotalConsumptionAt(model.getObjectsOnCanvas(), model.getCurIteration()), model.getCurIteration());
 				break;
 			case TrackedDataSet.PERCENT_SUPPLIED:
-
+				set.setValAt(getPercentState(model.getObjectsOnCanvas(), HolonObject.SUPPLIED), model.getCurIteration());
 				break;
 			case TrackedDataSet.PERCENT_NOT_SUPPLIED:
-
+				set.setValAt(getPercentState(model.getObjectsOnCanvas(), HolonObject.NOT_SUPPLIED), model.getCurIteration());
 				break;
 			case TrackedDataSet.PERCENT_PARTIAL_SUPPLIED:
-
+				set.setValAt(getPercentState(model.getObjectsOnCanvas(), HolonObject.PARTIALLY_SUPPLIED), model.getCurIteration());
 				break;
 			default:
 				break;
@@ -344,5 +347,133 @@ public class StatisticGraph extends JPanel {
 			}
 		}
 	}
+	
+	/**
+	 * create Path for percent values
+	 * 
+	 * @param set
+	 */
+	private void createPathPercent(TrackedDataSet set) {
+		boolean init = true;
+		path.moveTo(0, 0);
+		for (int i = 0; i < model.getCurIteration(); i++) {
+			if (init && set.getValues()[i] != -1) {
+				path.moveTo(i * this.getWidth() / model.getIterations() - 1, convertToCanvasY(set.getValues()[i]*maximum));
+				init = false;
+			}
+			if (!init) {
+				if (set.getValues()[i + 1] != -1) {
+					path.lineTo((i + 1) * this.getWidth() / model.getIterations(),
+							convertToCanvasY(set.getValues()[i + 1]*maximum));
+				} else {
+					break;
+				}
+			}
+
+		}
+	}
+	
+	/**
+	 * get the max total production of the given Objects
+	 *
+	 *  @param objects List of Objects
+	 */
+	private float getMaxTotalProduction(ArrayList<AbstractCpsObject> objects) {
+		float val = 0;
+		
+		for (AbstractCpsObject obj: objects) {
+			if (obj instanceof HolonObject) {
+				for (HolonElement ele: ((HolonObject) obj).getElements()) {
+					if (ele.getEnergy() > 0) {
+						val += ele.getEnergy() * ele.getAmount();
+					}
+				}
+			} else if (obj instanceof CpsUpperNode) {
+				val += getMaxTotalProduction(((CpsUpperNode) obj).getNodes());
+			}
+		}
+		return val;
+	}
+	
+	/**
+	 * get the max total consumption of the given Objects
+	 *
+	 *  @param objects List of Objects
+	 */
+	private float getMaxTotalConsumption(ArrayList<AbstractCpsObject> objects) {
+		float val = 0;
+		
+		for (AbstractCpsObject obj: objects) {
+			if (obj instanceof HolonObject) {
+				for (HolonElement ele: ((HolonObject) obj).getElements()) {
+					if (ele.getEnergy() < 0) {
+						val += ele.getEnergy() * ele.getAmount();
+					}
+				}
+			} else if (obj instanceof CpsUpperNode) {
+				val += getMaxTotalConsumption(((CpsUpperNode) obj).getNodes());
+			}
+		}
+		return val;
+	}
+	
+	/**
+	 * get the max total production of the given Objects
+	 *
+	 *  @param objects List of Objects
+	 *  @param tStep
+	 */
+	private float getTotalProductionAt(ArrayList<AbstractCpsObject> objects, int tStep) {
+		float val = 0;
+		
+		for (AbstractCpsObject obj: objects) {
+			if (obj instanceof HolonObject) {
+				for (HolonElement ele: ((HolonObject) obj).getElements()) {
+					if (ele.getEnergyAt()[tStep] > 0 && ele.getActive()) {
+						val += ele.getEnergyAt()[tStep] * ele.getAmount();
+					}
+				}
+			} else if (obj instanceof CpsUpperNode) {
+				val += getTotalProductionAt(((CpsUpperNode) obj).getNodes(), tStep);
+			}
+		}
+		return val;
+	}
+	
+	/**
+	 * get the total consumption of the given Objects at the given timestep
+	 *
+	 *  @param objects List of Objects
+	 *  @param tStep
+	 */
+	private float getTotalConsumptionAt(ArrayList<AbstractCpsObject> objects, int tStep) {
+		float val = 0;
+		
+		for (AbstractCpsObject obj: objects) {
+			if (obj instanceof HolonObject) {
+				for (HolonElement ele: ((HolonObject) obj).getElements()) {
+					if (ele.getEnergyAt()[tStep] < 0 && ele.getActive()) {
+						val += ele.getEnergyAt()[tStep] * ele.getAmount();
+					}
+				}
+			} else if (obj instanceof CpsUpperNode) {
+				val += getTotalConsumptionAt(((CpsUpperNode) obj).getNodes(), tStep);
+			}
+		}
+		return val;
+	}
+	
+	/**
+	 * get the Percentage of how many objects with the given state are in the given List
+	 *
+	 *  @param objects List of Objects
+	 */
+	private float getPercentState(ArrayList<AbstractCpsObject> objects, int state) {
+		float count = controller.getNumberHolonObjects(objects);
+		float stateObjectss= controller.getNumberStateObjects(objects, state);
+		
+		
+		return stateObjectss/count;
+	}
 
 }

+ 124 - 6
src/ui/view/UpperNodeCanvas.java

@@ -99,6 +99,19 @@ public class UpperNodeCanvas extends JPanel implements MouseListener, MouseMotio
 
 	private UpdateController updCon;
 
+	javax.swing.Timer animT; // animation Timer
+	private final int ANIMTIME = 500; // animation Time
+
+	private ArrayList<AbstractCpsObject> animCps = null;
+	private int animFPS = 60;
+	private int animDuration = ANIMTIME; // animation Duration
+	private int animDelay = 1000 / animFPS; // animation Delay
+	private int animSteps = animDuration / animDelay; // animation Steps;
+	private long start = 0; // for time
+	private long elapsedTime = 0; // outprint
+	private Position unPos;
+	private ArrayList<Position> savePos;
+
 	/**
 	 * Constructor.
 	 * 
@@ -151,16 +164,121 @@ public class UpperNodeCanvas extends JPanel implements MouseListener, MouseMotio
 		itemGroup.addActionListener(new ActionListener() {
 			@Override
 			public void actionPerformed(ActionEvent e) {
-				controller.addUpperNode("NodeOfNode", upperNode, model.getSelectedCpsObjects());
-				repaint();
+				// calculate uppernode pos (taken from the controller)
+				unPos = new Position(0, 0);
+				animCps = new ArrayList<>();
+				for (AbstractCpsObject cps : model.getSelectedCpsObjects()) {
+					animCps.add(cps); // add to animation Cps ArrayList
+					unPos.x += cps.getPosition().x;
+					unPos.y += cps.getPosition().y;
+				}
+				unPos.x /= animCps.size();
+				unPos.y /= animCps.size();
+
+				// save old Position
+				savePos = new ArrayList<>();
+				for (int i = 0; i < animCps.size(); i++) {
+					savePos.add(new Position(0, 0));
+					savePos.get(i).x = animCps.get(i).getPosition().x;
+					savePos.get(i).y = animCps.get(i).getPosition().y;
+				}
+
+				animT = new javax.swing.Timer(animDelay, new ActionListener() {
+
+					@Override
+					public void actionPerformed(ActionEvent e) {
+						if (animDuration - animDelay > 0 && animCps.size() > 1) {
+							for (int i = 0; i < animCps.size(); i++) {
+								double x1 = animCps.get(i).getPosition().x - unPos.x;
+								double y1 = animCps.get(i).getPosition().y - unPos.y;
+								animCps.get(i).getPosition().x -= x1 / animSteps;
+								animCps.get(i).getPosition().y -= y1 / animSteps;
+							}
+							repaint();
+							animDuration -= animDelay;
+							animSteps--;
+						} else {
+							animDuration = ANIMTIME;
+							animSteps = animDuration / animDelay;
+							animT.stop();
+							for (int i = 0; i < animCps.size(); i++) {
+								animCps.get(i).getPosition().x = savePos.get(i).x;
+								animCps.get(i).getPosition().y = savePos.get(i).y;
+							}
+							controller.addUpperNode("NodeOfNode", upperNode, model.getSelectedCpsObjects());
+							controller.calculateStateForCurrentTimeStep();
+							repaint();
+							elapsedTime = System.currentTimeMillis() - start;
+							controller.addTextToConsole(elapsedTime + "");
+						}
+					}
+				});
+				start = System.currentTimeMillis();
+				animT.start();
 			}
 		});
 
 		itemUngroup.addActionListener(new ActionListener() {
 			@Override
 			public void actionPerformed(ActionEvent e) {
+				// save old Position
+				JTabbedPane tabbedPane = (JTabbedPane) getParent().getParent().getParent();
+				for (int i = 3; i < tabbedPane.getTabCount(); i++) {
+					if (((UpperNodeCanvas) ((JScrollPane) tabbedPane.getComponentAt(i)).getViewport()
+							.getComponent(0)).upperNode.getID() == ((CpsUpperNode) tempCps).getID()) {
+						tabbedPane.remove(i);
+						break;
+					}
+				}
+				
+				savePos = new ArrayList<>();
+				animCps = ((CpsUpperNode) tempCps).getNodes();
 				controller.delUpperNode((CpsUpperNode) tempCps, upperNode);
-				repaint();
+
+				for (int i = 0; i < animCps.size(); i++) {
+					savePos.add(new Position(0, 0));
+					savePos.get(i).x = animCps.get(i).getPosition().x;
+					savePos.get(i).y = animCps.get(i).getPosition().y;
+				}
+				for (AbstractCpsObject cps : animCps) {
+					int x = ((CpsUpperNode) tempCps).getPosition().x;
+					int y = ((CpsUpperNode) tempCps).getPosition().y;
+
+					cps.setPosition(new Position(x, y));
+				}
+
+				animT = new javax.swing.Timer(animDelay, new ActionListener() {
+
+					@Override
+					public void actionPerformed(ActionEvent e) {
+						if (animDuration - animDelay >= 0) {
+							for (int i = 0; i < animCps.size(); i++) {
+								double x1 = animCps.get(i).getPosition().x - savePos.get(i).x;
+								double y1 = animCps.get(i).getPosition().y - savePos.get(i).y;
+								animCps.get(i).getPosition().x -= x1 / animSteps;
+								animCps.get(i).getPosition().y -= y1 / animSteps;
+							}
+							repaint();
+							animDuration -= animDelay;
+							animSteps--;
+						} else {
+							animDuration = ANIMTIME;
+							animSteps = animDuration / animDelay;
+							animT.stop();
+							for (int i = 0; i < animCps.size(); i++) {
+								animCps.get(i).getPosition().x = savePos.get(i).x;
+								animCps.get(i).getPosition().y = savePos.get(i).y;
+							}
+
+							controller.calculateStateForCurrentTimeStep();
+							repaint();
+							elapsedTime = System.currentTimeMillis() - start;
+							controller.addTextToConsole(elapsedTime + "");
+						}
+					}
+				});
+				start = System.currentTimeMillis();
+				animT.start();
 			}
 		});
 
@@ -172,10 +290,10 @@ public class UpperNodeCanvas extends JPanel implements MouseListener, MouseMotio
 						boolean found = false;
 						if (controller.getTrackingObj() != null) {
 							for (AbstractCpsObject obj : controller.getTrackingObj()) {
-								if(obj instanceof HolonObject){
+								if (obj instanceof HolonObject) {
 									if (obj.getID() == o.getID()) {
 										found = true;
-									}	
+									}
 								}
 							}
 						}
@@ -201,7 +319,7 @@ public class UpperNodeCanvas extends JPanel implements MouseListener, MouseMotio
 						boolean found = false;
 						if (controller.getTrackingObj() != null) {
 							for (AbstractCpsObject obj : controller.getTrackingObj()) {
-								if(obj instanceof HolonObject){
+								if (obj instanceof HolonObject) {
 									if (obj.getID() == o.getID()) {
 										found = true;
 									}