Kaynağa Gözat

Big Simulation Update

dominik.rieder 7 yıl önce
ebeveyn
işleme
e619a5bc30
2 değiştirilmiş dosya ile 66 ekleme ve 24 silme
  1. 31 3
      src/classes/CpsEdge.java
  2. 35 21
      src/ui/controller/SimulationManager.java

+ 31 - 3
src/classes/CpsEdge.java

@@ -26,6 +26,8 @@ public class CpsEdge {
 	boolean isWorking;
 	// for internal use --> flow of electricity (simulation state)
 	ArrayList<Integer> tags;
+	// for internal use --> flow of electricity (simulation state)
+	ArrayList<Integer> pseudoTags;
 	// Source
 	AbstractCpsObject a;
 	// Destination
@@ -47,6 +49,7 @@ public class CpsEdge {
 		this.maxCapacity = 100;
 		flow = 0;
 		isWorking = true;
+		pseudoTags = new ArrayList<Integer>();
 	}
 
 	/**
@@ -67,6 +70,7 @@ public class CpsEdge {
 		this.maxCapacity = maxCap;
 		flow = 0;
 		isWorking = true;
+		pseudoTags = new ArrayList<Integer>();
 	}
 
 	/**
@@ -210,7 +214,9 @@ public class CpsEdge {
 	 * @param tag tag for the ArrayList
 	 */
 	public void addTag(int tag) {
-		tags.add(tag);
+		if(!tags.contains(tag)){
+			tags.add(tag);
+		}
 	}
 	
 	/**
@@ -220,17 +226,39 @@ public class CpsEdge {
 	 * @return
 	 * 		true if all are contained in tags, false otherwise
 	 */
-	public boolean containsTags(ArrayList<Integer> toCheck){
+	public boolean containsTags(ArrayList<Integer> list, ArrayList<Integer> toCheck){
 		if(toCheck.size() == 0){
 			return true;
 		}else{
 			for(Integer i: toCheck){
-				if(!(tags.contains(i))){
+				if(!(list.contains(i))){
 					return false;
 				}
 			}
 			return true;
 		}
 	}
+	
+	public void addPseudoTag(int tag){
+		if(!pseudoTags.contains(tag)){
+			pseudoTags.add(tag);
+		}
+	}
+	
+	public void setPseudoTag(ArrayList<Integer> list){
+		pseudoTags = list;
+	}
+	
+	public ArrayList<Integer> getPseudoTags(){
+		return pseudoTags;
+	}
+	
+	public void recalculateTags(){
+		for(Integer i: pseudoTags){
+			if(!tags.contains(i)){
+				tags.add(i);
+			}
+		}
+	}
 
 }

+ 35 - 21
src/ui/controller/SimulationManager.java

@@ -135,9 +135,11 @@ public class SimulationManager {
 		ArrayList<AbstractCpsObject> producers = new ArrayList<AbstractCpsObject>();
 		AbstractCpsObject tmp = null;
 		tagTable = new HashMap<Integer, Float>();
+		System.out.println("producers:");
 		for (HolonObject hl : sN.getObjects()) {
 			float energy = hl.getCurrentEnergyAtTimeStep(timeStep);
 			if (energy > 0) {
+				System.out.println(hl.getID());
 				tagTable.put(hl.getID(), energy);
 				hl.addTag(hl.getID());
 				for (CpsEdge edge : hl.getConnections()) {
@@ -166,6 +168,7 @@ public class SimulationManager {
 				}
 			}
 		}
+		printNodes(producers);
 		setFlowSimRec(producers, 0);
 	}
 
@@ -180,27 +183,32 @@ public class SimulationManager {
 	public void setFlowSimRec(ArrayList<AbstractCpsObject> nodes, int iter) {
 		System.out.println("iteration: " + iter);
 		ArrayList<AbstractCpsObject> newNodes = new ArrayList<AbstractCpsObject>();
+		ArrayList<CpsEdge> changedEdges = new ArrayList<CpsEdge>();
 		AbstractCpsObject tmp;
 		if(nodes.size() != 0){
 			for(AbstractCpsObject cps: nodes){
 				if(legitState(cps)){
 					for(CpsEdge edge: cps.getConnections()){
-						if(edge.getState() && !(edge.containsTags(cps.getTag()))){
+						if(edge.getPseudoTags() == null){
+							System.out.println("edge null");
+						}
+						if(edge.getState() && (!(edge.containsTags(edge.getTags(), cps.getTag())))){
 							if(edge.getA().getID() == cps.getID()){
 								tmp = edge.getB();
 							}else{
 								tmp = edge.getA();
 							}	
 							for(Integer tag: cps.getTag()){
-								if(!(edge.getTags().contains(tag))){
+								if(!(edge.getTags().contains(tag)) && !(edge.getPseudoTags().contains(tag))){
 									edge.setFlow(edge.getFlow() + tagTable.get(tag));
 									edge.addTag(tag);
 								}
 							}
 							for(Integer tag: tmp.getTag()){
-								if(!(edge.getTags().contains(tag)) && tagTable.get(tag) != null){
+								if(!(edge.getTags().contains(tag)) && tagTable.get(tag) != null && !(edge.getPseudoTags().contains(tag))){
 									edge.setFlow(edge.getFlow() + tagTable.get(tag));
-									edge.addTag(tag);
+									edge.addPseudoTag(tag);
+									changedEdges.add(edge);
 								}
 							}
 							edge.calculateState(true);
@@ -214,7 +222,7 @@ public class SimulationManager {
 					}
 				}
 			}
-			setPseudoTags(newNodes);
+			setPseudoTags(newNodes, changedEdges);
 			printNodes(newNodes);
 			setFlowSimRec(newNodes, iter+1);
 		}
@@ -226,11 +234,15 @@ public class SimulationManager {
 	 * @param nodes
 	 *            Array of AbstractCpsObjects
 	 */
-	public void setPseudoTags(ArrayList<AbstractCpsObject> nodes) {
+	public void setPseudoTags(ArrayList<AbstractCpsObject> nodes, ArrayList<CpsEdge> edges) {
 		for (AbstractCpsObject node : nodes) {
 			node.recalculateTags();
 			node.setPseudoTags(new ArrayList<Integer>());
 		}
+		for(CpsEdge edge : edges){
+			edge.recalculateTags();
+			edge.setPseudoTag(new ArrayList<Integer>());
+		}
 	}
 
 	/**
@@ -425,23 +437,25 @@ public class SimulationManager {
 		AbstractCpsObject a;
 		AbstractCpsObject b;
 		for (CpsEdge edge : cps.getConnections()) {
-			a = edge.getA();
-			b = edge.getB();
-			if (!(cps instanceof HolonSwitch)) {
-				if (!(sN.getEdges().contains(edge))) {
-					sN.getEdges().add(edge);
+			if(!(simMode && !edge.getState())){
+				a = edge.getA();
+				b = edge.getB();
+				if (!(cps instanceof HolonSwitch)) {
+					if (!(sN.getEdges().contains(edge))) {
+						sN.getEdges().add(edge);
+					}
 				}
-			}
-			if(cps instanceof HolonSwitch && ((HolonSwitch)cps).getState(timeStep)){
-				if (!(sN.getEdges().contains(edge))) {
-					sN.getEdges().add(edge);
+				if(cps instanceof HolonSwitch && ((HolonSwitch)cps).getState(timeStep)){
+					if (!(sN.getEdges().contains(edge))) {
+						sN.getEdges().add(edge);
+					}
+				}
+				if (!visited.contains(a.getID()) && legitState(cps)) {
+					sN = buildSubNet(a, visited, sN);
+				}
+				if (!visited.contains(b.getID()) && legitState(cps)) {
+					sN = buildSubNet(b, visited, sN);
 				}
-			}
-			if (!visited.contains(a.getID()) && legitState(cps)) {
-				sN = buildSubNet(a, visited, sN);
-			}
-			if (!visited.contains(b.getID()) && legitState(cps)) {
-				sN = buildSubNet(b, visited, sN);
 			}
 		}
 		return sN;