Browse Source

animate conflict detection

Eren Yilmaz 6 years ago
parent
commit
939e9c67c9
1 changed files with 107 additions and 39 deletions
  1. 107 39
      src/bk/ConflictDetection.java

+ 107 - 39
src/bk/ConflictDetection.java

@@ -1,6 +1,7 @@
 package bk;
 
 import java.util.ArrayList;
+import java.util.Stack;
 
 import javax.swing.JTree;
 
@@ -20,7 +21,7 @@ import graph.LayeredGraphNode;
 public class ConflictDetection implements AlgorithmStage {
 
     private LayeredGraphNode graph;
-    private ArrayList<BackwardAction> actions;
+    private Stack<BackwardAction> actions;
 
     private int i;
     private int l1;
@@ -37,7 +38,7 @@ public class ConflictDetection implements AlgorithmStage {
 
     public ConflictDetection(LayeredGraphNode graph) {
         this.graph = graph;
-        actions = new ArrayList<>();
+        actions = new Stack<>();
         i = 0; // will be increased before first iteration
         l1 = -1; // will be increased before first iteration
         k0 = 0;
@@ -49,19 +50,28 @@ public class ConflictDetection implements AlgorithmStage {
     }
 
     // TODO: im psuedocode anzeigen
-    // TODO: knoten highlighten
     // TODO: rekursion auf subgraphen
-    // TODO: backwards actions
     // TODO: work with breakpoints
     @Override
     public StageStatus forwardStep() {
         int old_line = pseudo_line;
-        
+        int old_k = k;
+        int old_k0 = k0;
+        int old_k1 = k1;
+        int old_l = l;
+        int old_l1 = l1;
+        int old_hidden_k = hidden_k;
+
         switch (pseudo_line) {
         case 1:
-            i += 1;
+            i++;
+            actions.push(() -> {
+                i--;
+                pseudo_line = old_line;
+                selectNodes(pseudo_line);
+            });
             if (i <= graph.getContainedLayers().size() - 2) {
-                pseudo_line += 1;
+                pseudo_line++;
             } else {
                 return StageStatus.FINISHED;
             }
@@ -70,10 +80,22 @@ public class ConflictDetection implements AlgorithmStage {
             k0 = 0;
             l = 0;
             l1 = -1; // will be increased before first iteration
-            pseudo_line += 1;
+            pseudo_line++;
+            actions.push(() -> {
+                pseudo_line = old_line;
+                k0 = old_k0;
+                l = old_l;
+                l1 = old_l1;
+                selectNodes(pseudo_line);
+            });
             break;
         case 3:
-            l1 += 1;
+            l1++;
+            actions.push(() -> {
+                l1--;
+                pseudo_line = old_line;
+                selectNodes(pseudo_line);
+            });
             if (l1 < graph.getContainedLayers().get(i + 1).size()) {
                 pseudo_line += 1;
             } else {
@@ -81,88 +103,135 @@ public class ConflictDetection implements AlgorithmStage {
             }
             break;
         case 4:
+            actions.push(() -> {
+                pseudo_line = old_line;
+                selectNodes(pseudo_line);
+            });
             if (graph.getContainedLayers().get(i + 1).size() == l1 || incidentToInnerSegmentBetweenLiPlusOneAndLi()) {
-                pseudo_line += 1;
+                pseudo_line++;
             } else {
                 pseudo_line = 3;
             }
             break;
         case 5:
+            actions.push(() -> {
+                k1 = old_k1;
+                pseudo_line = old_line;
+                selectNodes(pseudo_line);
+            });
             k1 = graph.getContainedLayers().get(i).size() - 1;
-            pseudo_line += 1;
+            pseudo_line++;
             break;
         case 6:
+            actions.push(() -> {
+                pseudo_line = old_line;
+                selectNodes(pseudo_line);
+            });
             if (incidentToInnerSegmentBetweenLiPlusOneAndLi()) {
-                pseudo_line += 1;
+                pseudo_line++;
             } else {
                 pseudo_line = 9;
             }
             break;
         case 7:
+            actions.push(() -> {
+                k1 = old_k1;
+                pseudo_line = old_line;
+                selectNodes(pseudo_line);
+            });
             k1 = graph.getContainedLayers().get(i).indexOf(
                     graph.getContainedLayers().get(i + 1).get(l1).getSortedIncomingEdges().get(0).getSources().get(0));
             pseudo_line = 9;
             break;
         case 9:
+            actions.push(() -> {
+                hidden_k = old_hidden_k;
+                pseudo_line = old_line;
+                selectNodes(pseudo_line);
+            });
             if (l <= l1) {
-                pseudo_line += 1;
-                // initialize the for loop o next line
-                hidden_k -= 1; // because it will be increased next line
+                pseudo_line++;
+                hidden_k = 0; // will not be increased before first iteration
+                k = graph.getContainedLayers().get(i).indexOf(graph.getContainedLayers().get(i + 1).get(l)
+                        .getSortedIncomingEdges().get(hidden_k).getSources().get(0));
             } else {
                 pseudo_line = 15;
             }
             break;
         case 10:
-            hidden_k += 1;
+            actions.push(() -> {
+                k = old_k;
+                pseudo_line = old_line;
+                selectNodes(pseudo_line);
+            });
             if (hidden_k < graph.getContainedLayers().get(i + 1).get(l).getSortedIncomingEdges().size()) {
                 k = graph.getContainedLayers().get(i).indexOf(graph.getContainedLayers().get(i + 1).get(l)
                         .getSortedIncomingEdges().get(hidden_k).getSources().get(0));
-                pseudo_line += 1;
+                pseudo_line++;
             } else {
                 pseudo_line = 13;
             }
             break;
         case 11:
-            hidden_k += 1;
+            actions.push(() -> {
+                hidden_k--;
+                graph.getContainedLayers().get(i + 1).get(l).getSortedIncomingEdges().get(hidden_k).setConflicted(false,
+                        null);
+                pseudo_line = old_line;
+                selectNodes(pseudo_line);
+            });
             if (k < k0 || k > k1) {
                 graph.getContainedLayers().get(i + 1).get(l).getSortedIncomingEdges().get(hidden_k).setConflicted(true,
                         null);
             }
+            hidden_k++;
             pseudo_line = 10;
             break;
         case 13:
-            l += 1;
-            pseudo_line = 9; 
+            actions.push(() -> {
+                l--;
+                pseudo_line = old_line;
+                selectNodes(pseudo_line);
+            });
+            l++;
+            pseudo_line = 9;
             break;
         case 15:
+            actions.push(() -> {
+                k0 = old_k0;
+                pseudo_line = old_line;
+                selectNodes(pseudo_line);
+            });
             k0 = k1;
-            pseudo_line = 3; 
+            pseudo_line = 3;
             break;
         }
-        
-        if (old_line >= 2) {
-            graph.getContainedLayers().get(i+1).get(l).setSelected(null);
+
+        selectNodes(pseudo_line);
+
+        return StageStatus.UNFINISHED;
+    }
+
+    private void selectNodes(int next_line) {
+        if (next_line >= 3 && l < graph.getContainedLayers().get(i + 1).size()) {
+            graph.getContainedLayers().get(i + 1).get(l).setSelected(null);
         }
-        
-        if (old_line >= 3) {
-            graph.getContainedLayers().get(i+1).get(l1).setSelected(null);
+
+        if (next_line >= 4) {
+            graph.getContainedLayers().get(i + 1).get(l1).setSelected(null);
         }
-        
-        if (old_line >= 2) {
+
+        if (next_line >= 3) {
             graph.getContainedLayers().get(i).get(k0).setSelected(null);
         }
-        
-        if (old_line >= 5) {
+
+        if (next_line >= 6) {
             graph.getContainedLayers().get(i).get(k1).setSelected(null);
         }
-        
-        if ((pseudo_line == 10 && old_line == 11) || pseudo_line == 11) {
+
+        if (next_line == 10 || next_line == 11) {
             graph.getContainedLayers().get(i).get(k).setSelected(null);
         }
-        
-        System.out.println("going from line " + old_line +" to " + pseudo_line);
-        
-        return StageStatus.UNFINISHED;
     }
 
     @Override
@@ -175,8 +244,7 @@ public class ConflictDetection implements AlgorithmStage {
             markNode.setSelected(false);
             return StageStatus.FINISHED;
         }
-        actions.get(0).reverse();
-        actions.remove(0);
+        actions.pop().reverse();
         if (breakPoint)
             return StageStatus.BREAKPOINT;
         if (action == CodeAction.SKIP)