Browse Source

conflict detection in subgraphes

Kolja Strohm 6 years ago
parent
commit
3be5be9f0c
3 changed files with 339 additions and 117 deletions
  1. 12 28
      src/bk/Combine.java
  2. 326 88
      src/bk/ConflictDetection.java
  3. 1 1
      src/main/Main.java

+ 12 - 28
src/bk/Combine.java

@@ -159,16 +159,14 @@ public class Combine implements AlgorithmStage {
                     case BREAKPOINT:
                         return StageStatus.BREAKPOINT;
                     case UNFINISHED:
-                        if( breakpoint )
-                            return StageStatus.BREAKPOINT;
-                        return StageStatus.UNFINISHED;
+                        break;
                     case FINISHED:
-                        inside = false;
+                        insideSubgraph = false;
                         subgraphNodes.get( vIndex ).setSelected( false );
                         ac = null;
                         break;
                     }
-                } while( ac == CodeAction.SKIP && !breakPoint );
+                } while( ac == CodeAction.SKIP && !breakpoint );
             }
             ArrayList< Integer > positions = new ArrayList<>();
             positions.add( (Integer)(int)current.getX( LayoutType.TOP_BOTTOM_LEFT ) + tblOffset );
@@ -240,9 +238,7 @@ public class Combine implements AlgorithmStage {
                 case UNFINISHED:
                     LayeredGraphNode current = graph.getContainedNodes().get( vIndex );
                     current.setSelected( null );
-                    if( breakpoint )
-                        return StageStatus.BREAKPOINT;
-                    return StageStatus.UNFINISHED;
+                    break;
                 case FINISHED:
                     insideSubgraph = false;
                     subgraphNodes.get( vIndex ).setSelected( false );
@@ -317,11 +313,9 @@ public class Combine implements AlgorithmStage {
                     case BREAKPOINT:
                         return StageStatus.BREAKPOINT;
                     case UNFINISHED:
-                        if( breakpoint )
-                            return StageStatus.BREAKPOINT;
-                        return StageStatus.UNFINISHED;
+                        break;
                     case FINISHED:
-                        inside = false;
+                        insideSubgraph = false;
                         subgraphNodes.get( vIndex ).setSelected( false );
                         action = null;
                         break;
@@ -364,11 +358,9 @@ public class Combine implements AlgorithmStage {
                     case BREAKPOINT:
                         return StageStatus.BREAKPOINT;
                     case UNFINISHED:
-                        if( breakpoint )
-                            return StageStatus.BREAKPOINT;
-                        return StageStatus.UNFINISHED;
+                        break;
                     case FINISHED:
-                        inside = false;
+                        insideSubgraph = false;
                         subgraphNodes.get( vIndex ).setSelected( false );
                         action = null;
                         break;
@@ -406,13 +398,9 @@ public class Combine implements AlgorithmStage {
                     case BREAKPOINT:
                         return StageStatus.BREAKPOINT;
                     case UNFINISHED:
-                        LayeredGraphNode current = graph.getContainedNodes().get( vIndex );
-                        current.setSelected( null );
-                        if( breakpoint )
-                            return StageStatus.BREAKPOINT;
-                        return StageStatus.UNFINISHED;
+                        break;
                     case FINISHED:
-                        inside = false;
+                        insideSubgraph = false;
                         subgraphNodes.get( vIndex ).setSelected( false );
                         action = null;
                         break;
@@ -455,13 +443,9 @@ public class Combine implements AlgorithmStage {
                     case BREAKPOINT:
                         return StageStatus.BREAKPOINT;
                     case UNFINISHED:
-                        LayeredGraphNode current = graph.getContainedNodes().get( vIndex );
-                        current.setSelected( null );
-                        if( breakpoint )
-                            return StageStatus.BREAKPOINT;
-                        return StageStatus.UNFINISHED;
+                        break;
                     case FINISHED:
-                        inside = false;
+                        insideSubgraph = false;
                         subgraphNodes.get( vIndex ).setSelected( false );
                         action = null;
                         break;

+ 326 - 88
src/bk/ConflictDetection.java

@@ -1,5 +1,6 @@
 package bk;
 
+import java.util.ArrayList;
 import java.util.Stack;
 
 import javax.swing.JTree;
@@ -37,6 +38,10 @@ public class ConflictDetection implements AlgorithmStage {
     private PseudoCodeNode lines[];
     private boolean breakPoint;
     private boolean skip;
+    private ArrayList< PseudoCodeNode > subgraphNodes;
+    private ArrayList< ConflictDetection > subgraphAlgs;
+    private int vIndex;
+    private boolean insideSubgraph;
 
     public ConflictDetection(LayeredGraphNode graph) {
         this.graph = graph;
@@ -51,6 +56,15 @@ public class ConflictDetection implements AlgorithmStage {
         pseudo_line = 1;
         breakPoint = false;
         skip = false;
+        subgraphAlgs = new ArrayList<>();
+        subgraphNodes = new ArrayList<>();
+        vIndex = 0;
+        insideSubgraph = false;
+        for( @SuppressWarnings("unused") LayeredGraphNode n : graph.getContainedNodes() )
+        {
+            subgraphAlgs.add( null );
+            subgraphNodes.add( null );
+        }
     }
 
 
@@ -75,12 +89,7 @@ public class ConflictDetection implements AlgorithmStage {
             if (i <= graph.getContainedLayers().size() - 2) {
                 pseudo_line++;
             } else {
-                for( PseudoCodeNode p : lines )
-                {
-                    if( p != null )
-                        p.setSelected( false );
-                }
-                return StageStatus.FINISHED;
+                pseudo_line = 16;
             }
             break;
         case 2:
@@ -212,6 +221,50 @@ public class ConflictDetection implements AlgorithmStage {
             k0 = k1;
             pseudo_line = 3;
             break;
+        case 16:
+            if( vIndex < graph.getContainedNodes().size() )
+            {
+                while( vIndex < graph.getContainedNodes().size() && graph.getContainedNodes().get( vIndex ).getContainedNodes().size() == 0 )
+                    vIndex++;
+                if( vIndex < graph.getContainedNodes().size() )
+                {
+                    LayeredGraphNode current = graph.getContainedNodes().get( vIndex );
+                    current.setSelected( null );
+                    if( current.getContainedNodes().size() > 0 )
+                    {
+                        insideSubgraph = true;
+                        breakPoint = false;
+                        boolean sel = subgraphNodes.get( vIndex ).isSelected();
+                        CodeAction ac = subgraphNodes.get( vIndex ).setSelected( true );
+                        if( !sel )
+                            breakPoint |= ac == CodeAction.STOP;
+                        do {
+                            switch( subgraphAlgs.get( vIndex ).forwardStep() )
+                            {
+                            case BREAKPOINT:
+                                return StageStatus.BREAKPOINT;
+                            case UNFINISHED:
+                                break;
+                            case FINISHED:
+                                insideSubgraph = false;
+                                subgraphNodes.get( vIndex ).setSelected( false );
+                                vIndex++;
+                                ac = null;
+                                break;
+                            }
+                        } while( ac == CodeAction.SKIP && !breakPoint );
+                    }
+                }
+            }
+            if( vIndex >= graph.getContainedNodes().size() )
+            {
+                for( PseudoCodeNode p : lines )
+                {
+                    if( p != null )
+                        p.setSelected( false );
+                }
+                return StageStatus.FINISHED;
+            }
         }
 
         selectNodes(pseudo_line);
@@ -223,24 +276,27 @@ public class ConflictDetection implements AlgorithmStage {
     }
 
     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 (next_line >= 4) {
-            graph.getContainedLayers().get(i + 1).get(l1).setSelected(null);
-        }
-
-        if (next_line >= 3) {
-            graph.getContainedLayers().get(i).get(k0).setSelected(null);
-        }
-
-        if (next_line >= 6) {
-            graph.getContainedLayers().get(i).get(k1).setSelected(null);
-        }
-
-        if (next_line == 10 || next_line == 11) {
-            graph.getContainedLayers().get(i).get(k).setSelected(null);
+        if( pseudo_line < 16 )
+        {
+            if (next_line >= 3 && l < graph.getContainedLayers().get(i + 1).size()) {
+                graph.getContainedLayers().get(i + 1).get(l).setSelected(null);
+            }
+    
+            if (next_line >= 4) {
+                graph.getContainedLayers().get(i + 1).get(l1).setSelected(null);
+            }
+    
+            if (next_line >= 3) {
+                graph.getContainedLayers().get(i).get(k0).setSelected(null);
+            }
+    
+            if (next_line >= 6) {
+                graph.getContainedLayers().get(i).get(k1).setSelected(null);
+            }
+    
+            if (next_line == 10 || next_line == 11) {
+                graph.getContainedLayers().get(i).get(k).setSelected(null);
+            }
         }
         breakPoint = false;
         skip = false;
@@ -254,13 +310,15 @@ public class ConflictDetection implements AlgorithmStage {
     
     private int getCodeDepth( PseudoCodeNode n )
     {
-        if( n == lines[ 0 ] )
+        if( n == lines[ 0 ].getParent() )
             return 0;
         return 1 + getCodeDepth( (PseudoCodeNode)n.getParent() );
     }
     
     private void markCode( PseudoCodeNode n )
     {
+        if( n == lines[ 0 ].getRoot() )
+            return;
         if( !n.hasSelectedSubnode() )
         {
             CodeAction action = n.setSelected( true );
@@ -279,15 +337,65 @@ public class ConflictDetection implements AlgorithmStage {
 
     @Override
     public StageStatus backwardStep() {
-        if (actions.size() == 0) {
-            for( PseudoCodeNode p : lines )
+        if( pseudo_line == 16 )
+        {
+            while( vIndex >= graph.getContainedNodes().size() || ( vIndex >= 0 && graph.getContainedNodes().get( vIndex ).getContainedNodes().size() == 0 )  )
+                vIndex--;
+            if( vIndex >= 0 )
             {
-                if( p != null )
-                    p.setSelected( false );
+                LayeredGraphNode current = graph.getContainedNodes().get( vIndex );
+                current.setSelected( null );
+                if( subgraphAlgs.get( vIndex ) != null )
+                {
+                    insideSubgraph = true;
+                    breakPoint = false;
+                    boolean selected = subgraphNodes.get( vIndex ).isSelected();
+                    CodeAction action = subgraphNodes.get( vIndex ).setSelected( true );
+                    if( !selected )
+                        breakPoint |= action == CodeAction.STOP;
+                    do {
+                        switch( subgraphAlgs.get( vIndex ).backwardStep() )
+                        {
+                        case BREAKPOINT:
+                            return StageStatus.BREAKPOINT;
+                        case UNFINISHED:
+                            break;
+                        case FINISHED:
+                            insideSubgraph = false;
+                            subgraphNodes.get( vIndex ).setSelected( false );
+                            vIndex--;
+                            action = null;
+                            break;
+                        }
+                    } while( !breakPoint && action == CodeAction.SKIP );
+                }
+            }
+            if( vIndex < 0 )
+            {
+                vIndex = 0;
+                if (actions.size() == 0) {
+                    for( PseudoCodeNode p : lines )
+                    {
+                        if( p != null )
+                            p.setSelected( false );
+                    }
+                    return StageStatus.FINISHED;
+                }
+                actions.pop().reverse();
+            }
+        }
+        else
+        {
+            if (actions.size() == 0) {
+                for( PseudoCodeNode p : lines )
+                {
+                    if( p != null )
+                        p.setSelected( false );
+                }
+                return StageStatus.FINISHED;
             }
-            return StageStatus.FINISHED;
+            actions.pop().reverse();
         }
-        actions.pop().reverse();
         if( breakPoint )
             return StageStatus.BREAKPOINT;
         if( skip )
@@ -298,8 +406,9 @@ public class ConflictDetection implements AlgorithmStage {
     @Override
     public PseudoCodeNode createPseudocodeTree(JTree tree) {
         PseudoCodeNode root = new PseudoCodeNode("Preprocessing (mark type 1 conflicts)", tree);
-        lines = new PseudoCodeNode[15];
+        lines = new PseudoCodeNode[16];
         lines[ 0 ] = new PseudoCodeNode( "for i=1 to |L|-2 do", tree );
+        root.add( lines[ 0 ] );
         lines[ 1 ] = new PseudoCodeNode( "k0 = 0; l = 0;", tree );
         lines[ 0 ].add( lines[ 1 ] );
         lines[ 2 ] = new PseudoCodeNode( "for l1=0 to |L[i+1]|-1 do", tree );
@@ -322,80 +431,209 @@ public class ConflictDetection implements AlgorithmStage {
         lines[ 8 ].add( lines[ 12 ] );
         lines[ 14 ] = new PseudoCodeNode( "k0 = k1;", tree );
         lines[ 3 ].add( lines[ 14 ] );
-        root.add( lines[ 0 ] );
+        lines[ 15 ] = new PseudoCodeNode( "mark conflicts in aubgraphs", tree );
+        root.add( lines[ 15 ] );
+        for( int i = 0; i < graph.getContainedNodes().size(); i++ )
+        {
+            LayeredGraphNode current = graph.getContainedNodes().get( i );
+            if( current.getContainedNodes().size() > 0 )
+            {
+                ConflictDetection extcalc = new ConflictDetection( current );
+                PseudoCodeNode subNode = extcalc.createPseudocodeTree( tree );
+                lines[ 15 ].add( subNode );
+                subgraphAlgs.set( i, extcalc );
+                subgraphNodes.set( i, subNode );
+            }
+        }
         return root;
     }
 
     @Override
     public StageStatus forwardStepOver() {
-        int depth = getCodeDepth( lines[ pseudo_line - 1 ] );
-        do
+        if( !insideSubgraph )
         {
-            switch( forwardStep() )
+            int depth = getCodeDepth( lines[ pseudo_line - 1 ] );
+            do
             {
-            case BREAKPOINT:
-                return StageStatus.BREAKPOINT;
-            case FINISHED:
-                return StageStatus.FINISHED;
-            case UNFINISHED:
-                break;
-            }
-        } while( depth < getCodeDepth( lines[ pseudo_line - 1 ] ) );
-        return StageStatus.UNFINISHED;
+                switch( forwardStep() )
+                {
+                case BREAKPOINT:
+                    return StageStatus.BREAKPOINT;
+                case FINISHED:
+                    return StageStatus.FINISHED;
+                case UNFINISHED:
+                    break;
+                }
+            } while( depth < getCodeDepth( lines[ pseudo_line - 1 ] ) );
+            return StageStatus.UNFINISHED;
+        }
+        else
+        {
+            boolean breakpoint = false;
+            boolean selected = subgraphNodes.get( vIndex ).isSelected();
+            CodeAction action = subgraphNodes.get( vIndex ).setSelected( true );
+            if( !selected )
+                breakpoint |= action == CodeAction.STOP;
+            do {
+                switch( subgraphAlgs.get( vIndex ).forwardStepOver() )
+                {
+                case BREAKPOINT:
+                    return StageStatus.BREAKPOINT;
+                case UNFINISHED:
+                    if( breakpoint )
+                        return StageStatus.BREAKPOINT;
+                    return StageStatus.UNFINISHED;
+                case FINISHED:
+                    insideSubgraph = false;
+                    subgraphNodes.get( vIndex ).setSelected( false );
+                    action = null;
+                    break;
+                }
+            } while( !breakpoint && action == CodeAction.SKIP );
+            return StageStatus.UNFINISHED;
+        }
     }
 
     @Override
     public StageStatus forwardStepOut() {
-        int depth = getCodeDepth( lines[ pseudo_line - 1 ] );
-        do
+        if( !insideSubgraph )
         {
-            switch( forwardStep() )
+            int depth = getCodeDepth( lines[ pseudo_line - 1 ] );
+            do
             {
-            case BREAKPOINT:
-                return StageStatus.BREAKPOINT;
-            case FINISHED:
-                return StageStatus.FINISHED;
-            case UNFINISHED:
-                break;
-            }
-        } while( depth <= getCodeDepth( lines[ pseudo_line - 1 ] ) );
-        return StageStatus.UNFINISHED;
+                switch( forwardStep() )
+                {
+                case BREAKPOINT:
+                    return StageStatus.BREAKPOINT;
+                case FINISHED:
+                    return StageStatus.FINISHED;
+                case UNFINISHED:
+                    break;
+                }
+            } while( depth <= getCodeDepth( lines[ pseudo_line - 1 ] ) );
+            return StageStatus.UNFINISHED;
+        }
+        else
+        {
+            boolean breakpoint = false;
+            boolean selected = subgraphNodes.get( vIndex ).isSelected();
+            CodeAction action = subgraphNodes.get( vIndex ).setSelected( true );
+            if( !selected )
+                breakpoint |= action == CodeAction.STOP;
+            do {
+                switch( subgraphAlgs.get( vIndex ).forwardStepOut() )
+                {
+                case BREAKPOINT:
+                    return StageStatus.BREAKPOINT;
+                case UNFINISHED:
+                    if( breakpoint )
+                        return StageStatus.BREAKPOINT;
+                    return StageStatus.UNFINISHED;
+                case FINISHED:
+                    insideSubgraph = false;
+                    subgraphNodes.get( vIndex ).setSelected( false );
+                    action = null;
+                    break;
+                }
+            } while( !breakpoint && action == CodeAction.SKIP );
+            return StageStatus.UNFINISHED;
+        }
     }
 
     @Override
     public StageStatus backwardStepOver() {
-        int depth = getCodeDepth( lines[ pseudo_line - 1 ] );
-        do
+        if( !insideSubgraph )
         {
-            switch( backwardStep() )
+            int depth = getCodeDepth( lines[ pseudo_line - 1 ] );
+            do
             {
-            case BREAKPOINT:
-                return StageStatus.BREAKPOINT;
-            case FINISHED:
-                return StageStatus.FINISHED;
-            case UNFINISHED:
-                break;
-            }
-        } while( depth < getCodeDepth( lines[ pseudo_line - 1 ] ) );
-        return StageStatus.UNFINISHED;
+                switch( backwardStep() )
+                {
+                case BREAKPOINT:
+                    return StageStatus.BREAKPOINT;
+                case FINISHED:
+                    return StageStatus.FINISHED;
+                case UNFINISHED:
+                    break;
+                }
+            } while( depth < getCodeDepth( lines[ pseudo_line - 1 ] ) );
+            return StageStatus.UNFINISHED;
+        }
+        else
+        {
+            boolean breakpoint = false;
+            boolean selected = subgraphNodes.get( vIndex ).isSelected();
+            CodeAction action = subgraphNodes.get( vIndex ).setSelected( true );
+            if( !selected )
+                breakpoint |= action == CodeAction.STOP;
+            do {
+                switch( subgraphAlgs.get( vIndex ).backwardStepOver() )
+                {
+                case BREAKPOINT:
+                    return StageStatus.BREAKPOINT;
+                case UNFINISHED:
+                    LayeredGraphNode current = graph.getContainedNodes().get( vIndex );
+                    current.setSelected( null );
+                    if( breakpoint )
+                        return StageStatus.BREAKPOINT;
+                    return StageStatus.UNFINISHED;
+                case FINISHED:
+                    insideSubgraph = false;
+                    subgraphNodes.get( vIndex ).setSelected( false );
+                    action = null;
+                    break;
+                }
+            } while( !breakpoint && action == CodeAction.SKIP );
+            return StageStatus.UNFINISHED;
+        }
     }
 
     @Override
     public StageStatus backwardStepOut() {
-        int depth = getCodeDepth( lines[ pseudo_line - 1 ] );
-        do
+        if( !insideSubgraph )
         {
-            switch( backwardStep() )
+            int depth = getCodeDepth( lines[ pseudo_line - 1 ] );
+            do
             {
-            case BREAKPOINT:
-                return StageStatus.BREAKPOINT;
-            case FINISHED:
-                return StageStatus.FINISHED;
-            case UNFINISHED:
-                break;
-            }
-        } while( depth <= getCodeDepth( lines[ pseudo_line - 1 ] ) );
-        return StageStatus.UNFINISHED;
+                switch( backwardStep() )
+                {
+                case BREAKPOINT:
+                    return StageStatus.BREAKPOINT;
+                case FINISHED:
+                    return StageStatus.FINISHED;
+                case UNFINISHED:
+                    break;
+                }
+            } while( depth <= getCodeDepth( lines[ pseudo_line - 1 ] ) );
+            return StageStatus.UNFINISHED;
+        }
+        else
+        {
+            boolean breakpoint = false;
+            boolean selected = subgraphNodes.get( vIndex ).isSelected();
+            CodeAction action = subgraphNodes.get( vIndex ).setSelected( true );
+            if( !selected )
+                breakpoint |= action == CodeAction.STOP;
+            do {
+                switch( subgraphAlgs.get( vIndex ).backwardStepOut() )
+                {
+                case BREAKPOINT:
+                    return StageStatus.BREAKPOINT;
+                case UNFINISHED:
+                    LayeredGraphNode current = graph.getContainedNodes().get( vIndex );
+                    current.setSelected( null );
+                    if( breakpoint )
+                        return StageStatus.BREAKPOINT;
+                    return StageStatus.UNFINISHED;
+                case FINISHED:
+                    insideSubgraph = false;
+                    subgraphNodes.get( vIndex ).setSelected( false );
+                    action = null;
+                    break;
+                }
+            } while( !breakpoint && action == CodeAction.SKIP );
+            return StageStatus.UNFINISHED;
+        }
     }
 
     @Override
@@ -408,13 +646,13 @@ public class ConflictDetection implements AlgorithmStage {
                 "|" + TextLayoutHelper.strToLen( "" + k0, 4 ) + 
                 "|" + TextLayoutHelper.strToLen( "" + k1, 4 ) + 
                 "|" + TextLayoutHelper.strToLen( "" + k, 4 ) + "|\n";
-//        if( insideSubgraph && vIndex < graph.getContainedNodes().size() )
-//        {
-//            info += "Subgraph of " + graph.getContainedNodes().get( vIndex ).getName() + ":\n";
-//            String tmp = subgraphAlgs.get( vIndex ).getDebugString();
-//            info += tmp;
-//            return info;
-//        }
+        if( insideSubgraph && vIndex < graph.getContainedNodes().size() )
+        {
+            info += "Subgraph of " + graph.getContainedNodes().get( vIndex ).getName() + ":\n";
+            String tmp = subgraphAlgs.get( vIndex ).getDebugString();
+            info += tmp;
+            return info;
+        }
         return info;
     }
 

+ 1 - 1
src/main/Main.java

@@ -17,7 +17,7 @@ public class Main {
      * @param args the command line arguments, currently not in use
      */
     public static void main(String[] args) {
-        Reader r = new Reader( "error_after_combine3.json" );
+        Reader r = new Reader( "logo.json" );
         LayeredGraphNode graph = r.readInputGraph();
         InitializeNodePositions.placeNodes( graph );
         new MainView( graph );