Browse Source

big simulation Update

dominik.rieder 7 years ago
parent
commit
3419941b59
3 changed files with 103 additions and 49 deletions
  1. 38 5
      src/classes/AbstractCpsObject.java
  2. 20 0
      src/classes/CpsEdge.java
  3. 45 44
      src/ui/controller/SimulationManager.java

+ 38 - 5
src/classes/AbstractCpsObject.java

@@ -33,12 +33,11 @@ public abstract class AbstractCpsObject {
 	Color borderColor = Color.WHITE;
 	/* a Tag that can be used */
 	ArrayList<Integer> tags;
-	
 	/* a Tag that can be used */
 	ArrayList<Integer> pseudoTags;
 	
 	/**
-	 * Constructor for a CpsObejct
+	 * Constructor for a CpsObejct with an unique ID.
 	 * 
 	 * @param objName
 	 *            of the Object
@@ -47,6 +46,8 @@ public abstract class AbstractCpsObject {
 		setObjName(objName);
 		setName(objName);
 		setImage("/Images/Dummy_House.png");
+		tags = new ArrayList<Integer>();
+		pseudoTags = new ArrayList<Integer>();
 	}
 
 	/**
@@ -256,7 +257,15 @@ public abstract class AbstractCpsObject {
 	 *            for internal purpose
 	 */
 	public void addTag(int tag) {
-		this.tags.add(tag);
+		if(!(tags.contains(tag))){
+			this.tags.add(tag);
+		}
+	}
+	
+	public void addAllTags(ArrayList<Integer> tags){
+		for(Integer tag: tags){
+			addTag(tag);
+		}
 	}
 
 	/**
@@ -273,6 +282,7 @@ public abstract class AbstractCpsObject {
 	 */
 	public void resetTags() {
 		this.tags = new ArrayList<Integer>();
+		this.pseudoTags = new ArrayList<Integer>();
 	}
 
 	/**
@@ -309,8 +319,31 @@ public abstract class AbstractCpsObject {
 	 * 
 	 * @return ArrayList
 	 */
-	public ArrayList<Integer> addPseudoTag() {
-		return this.pseudoTags;
+	public void addPseudoTag(int tag) {
+		if(!pseudoTags.contains(tag)){
+			pseudoTags.add(tag);
+		}
+	}
+	
+	/**
+	 * adds all given pseudotags to the tags
+	 * @param pseudoTags
+	 */
+	public void addAllPseudoTags(ArrayList<Integer> pseudoTags){
+		for(Integer tag: pseudoTags){
+			addPseudoTag(tag);
+		}
+	}
+	
+	/**
+	 * adds All pseudoTags to tags without duplicates
+	 */
+	public void recalculateTags(){
+		for(Integer tag: pseudoTags){
+			if(!tags.contains(tag)){
+				tags.add(tag);
+			}
+		}
 	}
 	
 }

+ 20 - 0
src/classes/CpsEdge.java

@@ -212,5 +212,25 @@ public class CpsEdge {
 	public void addTag(int tag) {
 		tags.add(tag);
 	}
+	
+	/**
+	 * checks wether tags contains all given tags
+	 * @param toCheck 
+	 * 				tags that are checked
+	 * @return
+	 * 		true if all are contained in tags, false otherwise
+	 */
+	public boolean containsTags(ArrayList<Integer> toCheck){
+		if(toCheck.size() == 0){
+			return true;
+		}else{
+			for(Integer i: toCheck){
+				if(!(tags.contains(i))){
+					return false;
+				}
+			}
+			return true;
+		}
+	}
 
 }

+ 45 - 44
src/ui/controller/SimulationManager.java

@@ -154,7 +154,13 @@ public class SimulationManager {
 						edge.calculateState(true);
 						edge.addTag(hl.getID());
 						if (edge.getState() && !producers.contains(tmp)) {
-							producers.add(tmp);
+							if(tmp instanceof HolonSwitch){
+								if(((HolonSwitch)tmp).getState(timeStep)){
+									producers.add(tmp);
+								}
+							}else{
+								producers.add(tmp);
+							}
 						}
 					}
 				}
@@ -172,50 +178,45 @@ public class SimulationManager {
 	 *            the Iteration
 	 */
 	public void setFlowSimRec(ArrayList<AbstractCpsObject> nodes, int iter) {
+		System.out.println("iteration: " + iter);
 		ArrayList<AbstractCpsObject> newNodes = new ArrayList<AbstractCpsObject>();
-		ArrayList<Integer> pseudoTags = new ArrayList<Integer>();
-		AbstractCpsObject tmp = null;
-		if (nodes.size() != 0) {
-			for (AbstractCpsObject cps : nodes) {
-				for (CpsEdge edge : cps.getConnections()) {
-					for (Integer tag : cps.getTag()) {
-						if (edge.getState() && !(edge.getTags().contains(tag))) {
-							edge.setFlow(edge.getFlow() + tagTable.get(tag));
-							edge.calculateState(true);
-							edge.addTag(tag);
-							if (edge.getA().getID() == cps.getID()) {
+		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.getA().getID() == cps.getID()){
 								tmp = edge.getB();
-							}
-							if (edge.getB().getID() == cps.getID()) {
+							}else{
 								tmp = edge.getA();
-							}
-							if (tmp.getPseudoTags() != null) {
-								pseudoTags.addAll(tmp.getPseudoTags());
-							}
-							pseudoTags.addAll(cps.getTag());
-							tmp.setPseudoTags(mergeLists(tmp.getTag(), pseudoTags));
-							if (!edge.getState()) {
-								newNodes.remove(edge.getA());
-								newNodes.remove(edge.getB());
-								if (edge.getA().getID() == cps.getID()) {
-									edge.getB().getPseudoTags().removeAll(edge.getTags());
-
+							}	
+							for(Integer tag: cps.getTag()){
+								if(!(edge.getTags().contains(tag))){
+									edge.setFlow(edge.getFlow() + tagTable.get(tag));
+									edge.addTag(tag);
 								}
-								if (edge.getB().getID() == cps.getID()) {
-									edge.getA().getPseudoTags().removeAll(edge.getTags());
+							}
+							for(Integer tag: tmp.getTag()){
+								if(!(edge.getTags().contains(tag))){
+									edge.setFlow(edge.getFlow() + tagTable.get(tag));
+									edge.addTag(tag);
 								}
 							}
-							if (edge.getState() && !(newNodes.contains(tmp))) {
+							edge.calculateState(true);
+							if(edge.getState()){
+								tmp.addAllPseudoTags(cps.getTag());
+								if(!newNodes.contains(tmp)){
 								newNodes.add(tmp);
+								}
 							}
 						}
 					}
-					edge.calculateState(true);
 				}
 			}
-			// printNodes(newNodes);
 			setPseudoTags(newNodes);
-			setFlowSimRec(newNodes, iter + 1);
+			printNodes(newNodes);
+			setFlowSimRec(newNodes, iter+1);
 		}
 	}
 
@@ -227,7 +228,8 @@ public class SimulationManager {
 	 */
 	public void setPseudoTags(ArrayList<AbstractCpsObject> nodes) {
 		for (AbstractCpsObject node : nodes) {
-			node.setTags(node.getPseudoTags());
+			node.recalculateTags();
+			node.setPseudoTags(new ArrayList<Integer>());
 		}
 	}
 
@@ -430,10 +432,15 @@ public class SimulationManager {
 					sN.getEdges().add(edge);
 				}
 			}
-			if (!visited.contains(a.getID()) && legitState(a, cps)) {
+			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(b, cps)) {
+			if (!visited.contains(b.getID()) && legitState(cps)) {
 				sN = buildSubNet(b, visited, sN);
 			}
 		}
@@ -447,17 +454,11 @@ public class SimulationManager {
 	 * @param current AbstractCpsObject
 	 * @return boolean
 	 */
-	public boolean legitState(AbstractCpsObject neighbor, AbstractCpsObject current) {
+	public boolean legitState(AbstractCpsObject current) {
 		if (current instanceof HolonSwitch) {
 			if (((HolonSwitch) current).getState(timeStep)) {
-				if (neighbor instanceof HolonSwitch) {
-					if (((HolonSwitch) neighbor).getState(timeStep)) {
-						return true;
-					} else {
-						return false;
-					}
-				}
-			} else {
+				return true;
+			}else{
 				return false;
 			}
 		}