Browse Source

Flow + Subnet Detection

dominik.rieder 8 years ago
parent
commit
c5d61eda27
3 changed files with 164 additions and 0 deletions
  1. 21 0
      src/classes/subNet.java
  2. 133 0
      src/ui/controller/SimulationManager.java
  3. 10 0
      src/ui/view/GUI.java

+ 21 - 0
src/classes/subNet.java

@@ -0,0 +1,21 @@
+package classes;
+
+import java.util.ArrayList;
+
+public class subNet {
+	private ArrayList<HolonObject> subNetObjects;
+	private ArrayList<CpsEdge> subNetEdges;
+	
+	public subNet(ArrayList<HolonObject> objects, ArrayList<CpsEdge> edges){
+		subNetObjects = objects;
+		subNetEdges = edges;
+	}
+	
+	public ArrayList<HolonObject> getObjects(){
+		return subNetObjects;
+	}
+	
+	public ArrayList<CpsEdge> getEdges(){
+		return subNetEdges;
+	}
+}

+ 133 - 0
src/ui/controller/SimulationManager.java

@@ -0,0 +1,133 @@
+package ui.controller;
+
+import java.util.ArrayList;
+
+import classes.CpsEdge;
+import classes.CpsNode;
+import classes.CpsObject;
+import classes.HolonObject;
+import classes.subNet;
+import ui.model.Model;
+
+public class SimulationManager {
+	private Model model;
+	private ArrayList<CpsObject> objectsToHandle;
+	private ArrayList<subNet> subNets;
+	
+	public SimulationManager(Model m){
+		model = m;
+		copyObjects(m.getObjectsOnCanvas());
+		subNets = new ArrayList<subNet>();
+	}
+	
+	public void calculateStateForTimeStep(){
+		searchForSubNets();
+		for(subNet singleSubNet: subNets){
+			float production = calculateEnergy("prod", singleSubNet);
+			float consumption = calculateEnergy("cons", singleSubNet);
+			for(CpsEdge e: singleSubNet.getEdges()){
+				e.setFlow(production);
+			}
+		}
+	}
+	
+	public float calculateEnergy(String type, subNet sN){
+		float energy = 0;
+		for(HolonObject hl: sN.getObjects()){
+			if(type.equals("prod")){
+				if(hl.getCurrentEnergy() > 0){
+					energy = energy + hl.getCurrentEnergy();
+				}
+			}
+			if(type.equals("cons")){
+				if(hl.getCurrentEnergy() < 0){
+					energy = energy + hl.getCurrentEnergy();
+				}
+			}
+		}
+		return energy;
+	}
+	
+	public void searchForSubNets(){
+		subNets = new ArrayList<subNet>();
+		boolean end = false;
+		int i = 0;
+		CpsObject cps;
+		if(objectsToHandle.size() > 0){
+			while(!end){
+				cps = objectsToHandle.get(i);
+				subNet singleSubNet = new subNet(new ArrayList<HolonObject>(), new ArrayList<CpsEdge>());
+				singleSubNet = buildSubNet(cps, new ArrayList<Integer>(), singleSubNet);
+				if(singleSubNet.getObjects().size() != 0){
+					subNets.add(singleSubNet);
+				}
+				if(0 == objectsToHandle.size()){
+					end = true;
+				}
+			}
+		}
+		printNet();
+	}
+	
+	public subNet buildSubNet(CpsObject cps, ArrayList<Integer> visited, subNet sN){
+		visited.add(cps.getID());
+		if(cps instanceof HolonObject){
+			sN.getObjects().add((HolonObject) cps);
+		}
+		removeFromToHandle(cps.getID());
+		for(CpsEdge edge: cps.getConnections()){
+			if(!(sN.getEdges().contains(edge))){
+				sN.getEdges().add(edge);
+			}
+			if(!visited.contains(edge.getA().getID())){
+				sN = buildSubNet(edge.getA(), visited, sN);
+			}
+			if(!visited.contains(edge.getB().getID())){
+				sN = buildSubNet(edge.getB(), visited, sN);
+			}
+		}
+		return sN;
+	}
+	
+	public void removeFromToHandle(int id){
+		for(int i = 0; i < objectsToHandle.size(); i++){
+			if(objectsToHandle.get(i).getID() == id){
+				objectsToHandle.remove(i);
+			}
+		}
+	}
+	
+	public void cleanObjectsToHandle(){
+		for(int i = 0; i < objectsToHandle.size(); i++){
+			if(!(objectsToHandle.get(i) instanceof HolonObject)){
+				objectsToHandle.remove(i);
+			}
+		}
+	}
+	
+	public void copyObjects(ArrayList<CpsObject> toCopy){
+		objectsToHandle = new ArrayList<CpsObject>();
+		for(CpsObject cps: toCopy){
+			objectsToHandle.add(cps);
+		}
+	}
+	
+	public void printNet(){
+		for(int i = 0; i < subNets.size(); i++){
+			System.out.println("SUBNET NR:" + i);
+			System.out.println("  Objects:");
+			for(int j = 0; j < subNets.get(i).getObjects().size(); j++){
+				HolonObject hl = subNets.get(i).getObjects().get(j);
+				System.out.println("  " + hl.getName() + " " + hl.getID());
+			}
+			System.out.println("  Edges:");
+			for(int j = 0; j < subNets.get(i).getEdges().size(); j++){
+				CpsEdge edge = subNets.get(i).getEdges().get(j);
+				System.out.println("  " + edge.getA().getName() + " connected To " + edge.getB().getName());
+			}
+			
+		}
+	}
+	
+	
+}

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

@@ -61,6 +61,7 @@ import classes.HolonObject;
 import classes.HolonSwitch;
 import classes.HolonTransformer;
 import ui.controller.Control;
+import ui.controller.SimulationManager;
 import ui.model.Model;;
 
 public class GUI<E> implements CategoryListener {
@@ -176,6 +177,7 @@ public class GUI<E> implements CategoryListener {
 	private int yBTHIS;
 	private int xBTHIS;
 	private CpsObject temp = null;
+	private final JButton btnTest = new JButton("test");
 
 	/**
 	 * Create the application.
@@ -946,6 +948,14 @@ public class GUI<E> implements CategoryListener {
 		split_Graph_HolonEl.setBorder(null);
 		scrollPane_2.setBorder(null);
 		panel_HolonEl.setBorder(null);
+		btnTest.addActionListener(new ActionListener() {
+			public void actionPerformed(ActionEvent arg0) {
+				SimulationManager sm = new SimulationManager(model);
+				sm.calculateStateForTimeStep();
+			}
+		});
+		
+		panel_HolonEl.add(btnTest);
 		tableHolonElementScrollPane.setBorder(null);
 
 		frmCyberPhysical.getContentPane().add(timePanel, BorderLayout.SOUTH);