ソースを参照

BFS algorithm for cleaning CpsUpperNodes

Teh-Hai Julian Zheng 8 年 前
コミット
668a5e70e7
1 ファイル変更61 行追加6 行削除
  1. 61 6
      src/ui/controller/CanvasController.java

+ 61 - 6
src/ui/controller/CanvasController.java

@@ -2,7 +2,7 @@ package ui.controller;
 
 import java.awt.Point;
 import java.util.ArrayList;
-
+import sun.misc.Queue;
 import classes.CpsEdge;
 import classes.CpsNode;
 import classes.CpsUpperNode;
@@ -12,7 +12,6 @@ import classes.HolonSwitch;
 import classes.Position;
 import interfaces.ObjectListener;
 import ui.model.Model;
-import ui.view.UpperNodeCanvas;
 
 /**
  * Controller for the Canvas.
@@ -226,6 +225,7 @@ public class CanvasController {
 
 	/**
 	 * In Case if a One or Both Side of the to Removing Edge is a CpsUpperNode
+	 * 
 	 * @param edge
 	 * @param upperNode
 	 */
@@ -238,21 +238,21 @@ public class CanvasController {
 
 			upper = (CpsUpperNode) edge.getA();
 
-			//wenn in OldEdges eine B enhält
+			// wenn in OldEdges eine B enhält
 			for (CpsEdge cpsEdge : upper.getOldEdges()) {
 				if (cpsEdge.getA().equals(edge.getB()) || cpsEdge.getB().equals(edge.getB()))
 					toDelete.add(cpsEdge);
 			}
-			//lösche alle Edges mit B
+			// lösche alle Edges mit B
 			upper.getOldEdges().removeAll(toDelete);
-			//lösche hier alle Connections
+			// lösche hier alle Connections
 			for (CpsEdge cpsEdge : toDelete) {
 				cpsEdge.getA().getConnections().remove(cpsEdge);
 				cpsEdge.getB().getConnections().remove(cpsEdge);
 			}
 			toDelete.clear();
 		}
-		//Hier analog
+		// Hier analog
 		if (edge.getB() instanceof CpsUpperNode) {
 
 			upper = (CpsUpperNode) edge.getB();
@@ -270,4 +270,59 @@ public class CanvasController {
 		}
 
 	}
+
+	/**
+	 * Some cleaning Algorithm which traverses the UpperNode through BFS Can be
+	 * extended with other cleaning stuff No need for coloring since there tree
+	 * is only directed in one direction
+	 * 
+	 * @param node
+	 */
+	public void bfsNodeCleaner(CpsUpperNode node) {
+
+		Queue<AbstractCpsObject> queue = new Queue<>();
+		AbstractCpsObject u = node;
+
+		queue.enqueue(u);
+		while (!queue.isEmpty()) {
+			try {
+				u = queue.dequeue();
+			} catch (InterruptedException e) {
+				// TODO Auto-generated catch block
+				e.printStackTrace();
+			}
+			deleteConnections(u);
+			if (u instanceof CpsUpperNode)
+				for (AbstractCpsObject adjacent : ((CpsUpperNode) u).getNodes()) {
+					queue.enqueue(adjacent);
+				}
+		}
+
+	}
+
+	/**
+	 * Deletes all Connections to Objects inside the to deleting UpperNode
+	 * 
+	 * @param obj
+	 */
+	private void deleteConnections(AbstractCpsObject obj) {
+
+		Queue<CpsEdge> queue = new Queue<>();
+		CpsEdge e = null;
+
+		for (CpsEdge edge : obj.getConnections()) {
+			queue.enqueue(edge);
+		}
+		while (!queue.isEmpty()) {
+			try {
+				e = queue.dequeue();
+			} catch (InterruptedException e1) {
+				// TODO Auto-generated catch block
+				e1.printStackTrace();
+			}
+			e.getA().getConnections().remove(e);
+			e.getB().getConnections().remove(e);
+		}
+
+	}
 }