Parcourir la source

basic for loop

Kolja Strohm il y a 6 ans
Parent
commit
77d26da8cd
2 fichiers modifiés avec 96 ajouts et 142 suppressions
  1. 39 142
      src/bk/BlockCalc.java
  2. 57 0
      src/codelines/DefaultForLoop.java

+ 39 - 142
src/bk/BlockCalc.java

@@ -7,6 +7,9 @@ import javax.swing.JTree;
 
 import animation.AlgorithmStage;
 import animation.BackwardAction;
+import animation.CodeLine;
+import animation.ControlFlow;
+import animation.Memory;
 import animation.PseudoCodeNode;
 import animation.PseudoCodeNode.CodeAction;
 import bk.ExtremalLayoutCalc.LayoutType;
@@ -27,9 +30,7 @@ public class BlockCalc implements AlgorithmStage {
     private LayeredGraphNode graph;
     private ArrayList< ArrayList< PseudoCodeNode > > subgraphNodes;
     private ArrayList< ArrayList< ExtremalLayoutCalc > > subgraphAlgs;
-    private ArrayList< BackwardAction > backwards; // TODO: evtl richtigen "Stack" benutzen
     private LayoutType layout;
-    private PseudoCodeNode loopNode;
     private boolean inside;
     int step;
 
@@ -296,8 +297,42 @@ public class BlockCalc implements AlgorithmStage {
 
     @Override
     public PseudoCodeNode createPseudocodeTree( JTree tree ) {
-        PseudoCodeNode root = new PseudoCodeNode( TextLayoutHelper.setupPseudoCodeStage("Vertical alignment"), tree );
-        loopNode = new PseudoCodeNode( "Loop through all nodes...", tree );
+        String[] vars = { "graph" };
+        PseudoCodeNode root = new PseudoCodeNode( TextLayoutHelper.setupPseudoCode( "calculateBlockGraph( graph )", vars ), tree, CodeLine.DEFAULT_STEP_INTO_STACK );
+        PseudoCodeNode[] nodes = new PseudoCodeNode[12];
+        if( layout == LayoutType.TOP_BOTTOM_LEFT || layout == LayoutType.TOP_BOTTOM_RIGHT )
+            nodes[ 0 ] = new PseudoCodeNode( TextLayoutHelper.setupPseudoCode( "for i=0 to |L|-1 do", vars ), tree, new CodeLine() {
+
+                private boolean initialized = false;
+                private Integer i;
+                
+                @Override
+                public ControlFlow runForward(Memory m) {
+                    if( !initialized )
+                    {
+                        initialized = true;
+                        i = 0;
+                        m.declare( "i", i, false );
+                    }
+                    else
+                    {
+                        i++;
+                    }
+                    if( i >= m.<LayeredGraphNode>read( "graph", false ).getContainedLayers().size() )
+                    {
+                        m.undeclare( "i", false );
+                        return new ControlFlow( ControlFlow.STEP_OVER );
+                    }
+                    else
+                        return new ControlFlow( ControlFlow.STEP_INTO_LOOP );
+                }
+
+                @Override
+                public ControlFlow runBackward(Memory m) {
+                    
+                }
+                
+            });
         do {
             LayeredGraphNode current = graph.getContainedLayers().get( calcLayerIndex() ).get( calcNodeIndex( nodeIndex ) );
             if( current.getContainedNodes().size() > 0 )
@@ -315,144 +350,6 @@ public class BlockCalc implements AlgorithmStage {
         root.add( loopNode );
         return root;
     }
-
-    @Override
-    public StageStatus forwardStepOver() {
-        if( !inside )
-            return forwardStep();
-        else
-        {
-            boolean breakpoint = false;
-            boolean selected = subgraphNodes.get( calcLayerIndex() ).get( calcNodeIndex( nodeIndex ) ).isSelected();
-            CodeAction action = subgraphNodes.get( calcLayerIndex() ).get( calcNodeIndex( nodeIndex ) ).setSelected( true );
-            if( !selected )
-                breakpoint |= action == CodeAction.STOP;
-            do {
-                switch( subgraphAlgs.get( calcLayerIndex() ).get( calcNodeIndex( nodeIndex ) ).forwardStepOver() )
-                {
-                case BREAKPOINT:
-                    return StageStatus.BREAKPOINT;
-                case UNFINISHED:
-                    if( breakpoint )
-                        return StageStatus.BREAKPOINT;
-                    return StageStatus.UNFINISHED;
-                case FINISHED:
-                    inside = false;
-                    subgraphNodes.get( calcLayerIndex() ).get( calcNodeIndex( nodeIndex ) ).setSelected( false );
-                    action = null;
-                    break;
-                }
-            } while( !breakpoint && action == CodeAction.SKIP );
-            return StageStatus.UNFINISHED;
-        }
-    }
-
-    @Override
-    public StageStatus forwardStepOut() {
-        if( !inside )
-        {
-            StageStatus status = StageStatus.UNFINISHED;
-            while( status == StageStatus.UNFINISHED )
-                status = forwardStep();
-            return status;
-        }
-        else
-        {
-            boolean breakpoint = false;
-            boolean selected = subgraphNodes.get( calcLayerIndex() ).get( calcNodeIndex( nodeIndex ) ).isSelected();
-            CodeAction action = subgraphNodes.get( calcLayerIndex() ).get( calcNodeIndex( nodeIndex ) ).setSelected( true );
-            if( !selected )
-                breakpoint |= action == CodeAction.STOP;
-            do {
-                switch( subgraphAlgs.get( calcLayerIndex() ).get( calcNodeIndex( nodeIndex ) ).forwardStepOut() )
-                {
-                case BREAKPOINT:
-                    return StageStatus.BREAKPOINT;
-                case UNFINISHED:
-                    if( breakpoint )
-                        return StageStatus.BREAKPOINT;
-                    return StageStatus.UNFINISHED;
-                case FINISHED:
-                    inside = false;
-                    subgraphNodes.get( calcLayerIndex() ).get( calcNodeIndex( nodeIndex ) ).setSelected( false );
-                    action = null;
-                    break;
-                }
-            } while( !breakpoint && action == CodeAction.SKIP );
-            return StageStatus.UNFINISHED;
-        }
-    }
-
-    @Override
-    public StageStatus backwardStepOver() {
-        if( !inside )
-            return backwardStep();
-        else
-        {
-            boolean breakpoint = false;
-            boolean selected = subgraphNodes.get( calcLayerIndex() ).get( calcNodeIndex( nodeIndex ) ).isSelected();
-            CodeAction action = subgraphNodes.get( calcLayerIndex() ).get( calcNodeIndex( nodeIndex ) ).setSelected( true );
-            if( !selected )
-                breakpoint |= action == CodeAction.STOP;
-            do {
-                switch( subgraphAlgs.get( calcLayerIndex() ).get( calcNodeIndex( nodeIndex ) ).backwardStepOver() )
-                {
-                case BREAKPOINT:
-                    return StageStatus.BREAKPOINT;
-                case UNFINISHED:
-                    LayeredGraphNode current = graph.getContainedLayers().get( calcLayerIndex() ).get( nodeIndex );
-                    current.setSelected( layout );
-                    if( breakpoint )
-                        return StageStatus.BREAKPOINT;
-                    return StageStatus.UNFINISHED;
-                case FINISHED:
-                    inside = false;
-                    subgraphNodes.get( calcLayerIndex() ).get( calcNodeIndex( nodeIndex ) ).setSelected( false );
-                    action = null;
-                    break;
-                }
-            } while( !breakpoint && action == CodeAction.SKIP );
-            return StageStatus.UNFINISHED;
-        }
-    }
-
-    @Override
-    public StageStatus backwardStepOut() {
-        if( !inside )
-        {
-            StageStatus status = StageStatus.UNFINISHED;
-            while( status == StageStatus.UNFINISHED )
-                status = backwardStep();
-            return status;
-        }
-        else
-        {
-            boolean breakpoint = false;
-            boolean selected = subgraphNodes.get( calcLayerIndex() ).get( calcNodeIndex( nodeIndex ) ).isSelected();
-            CodeAction action = subgraphNodes.get( calcLayerIndex() ).get( calcNodeIndex( nodeIndex ) ).setSelected( true );
-            if( !selected )
-                breakpoint |= action == CodeAction.STOP;
-            do {
-                switch( subgraphAlgs.get( calcLayerIndex() ).get( calcNodeIndex( nodeIndex ) ).backwardStepOut() )
-                {
-                case BREAKPOINT:
-                    return StageStatus.BREAKPOINT;
-                case UNFINISHED:
-                    LayeredGraphNode current = graph.getContainedLayers().get( calcLayerIndex() ).get( nodeIndex );
-                    current.setSelected( layout );
-                    if( breakpoint )
-                        return StageStatus.BREAKPOINT;
-                    return StageStatus.UNFINISHED;
-                case FINISHED:
-                    inside = false;
-                    subgraphNodes.get( calcLayerIndex() ).get( calcNodeIndex( nodeIndex ) ).setSelected( false );
-                    action = null;
-                    break;
-                }
-            } while( !breakpoint && action == CodeAction.SKIP );
-            return StageStatus.UNFINISHED;
-        }
-    }
     
     @Override
     public String getDebugString()

+ 57 - 0
src/codelines/DefaultForLoop.java

@@ -0,0 +1,57 @@
+package codelines;
+
+import animation.CodeLine;
+import animation.ControlFlow;
+import animation.Memory;
+
+public class DefaultForLoop implements CodeLine {
+
+    private boolean initialized = false;
+    private Integer i;
+    private int start;
+    private int max;
+    private String vName;
+    
+    public DefaultForLoop( String vName, int start, int max )
+    {
+        this.start = start;
+        this.max = max;
+        this.vName = vName;
+    }
+    
+    @Override
+    public ControlFlow runForward(Memory m) {
+        if( !initialized )
+        {
+            initialized = true;
+            i = start;
+            m.declare( vName, i, false );
+        }
+        else
+        {
+            i++;
+        }
+        if( i > max )
+        {
+            m.undeclare( vName, false );
+            return new ControlFlow( ControlFlow.STEP_OVER );
+        }
+        else
+            return new ControlFlow( ControlFlow.STEP_INTO_LOOP );
+    }
+
+    @Override
+    public ControlFlow runBackward(Memory m) {
+        i--;
+        m.declare( vName, i, false );
+        if( i < start )
+        {
+            initialized = false;
+            m.undeclare( vName, false );
+            return new ControlFlow( ControlFlow.STEP_OVER );
+        }
+        else
+            return new ControlFlow( ControlFlow.STEP_INTO_LOOP );
+    }
+
+}