Răsfoiți Sursa

fehler in combine behoben

Kolja Strohm 6 ani în urmă
părinte
comite
464d172f92
1 a modificat fișierele cu 39 adăugiri și 14 ștergeri
  1. 39 14
      src/bk/Balancing.java

+ 39 - 14
src/bk/Balancing.java

@@ -3,6 +3,7 @@ package bk;
 import java.awt.Color;
 import java.util.ArrayList;
 import java.util.Collections;
+import java.util.HashMap;
 import java.util.List;
 
 import javax.swing.JTree;
@@ -36,7 +37,7 @@ public class Balancing {
      * @return the code
      */
     public static PseudoCodeNode combine( JTree tree ) {
-        String[] vars = { "graph", "l", "l_min", "v", "positions" };
+        String[] vars = { "graph", "l", "l_min", "v", "positions", "min", "shift", "max", "width" };
 
         PseudoCodeNode root = new PseudoCodeNode( "function combine( graph )", vars, tree, new FunctionDefinition( new String[]{ "graph" } ) ){
             private static final long serialVersionUID = -3067185213650162055L;
@@ -78,6 +79,24 @@ public class Balancing {
         recursiveLoop.add( ifNode );
         ifNode.add( new PseudoCodeNode( "call combine( v );", vars, tree, new FunctionCall( root, new String[]{ "v" } ) ) );
         root.add( new PseudoCodeNode( "-- Align all Layouts to the one with the smallest width --", vars, tree, new Comment()));
+        root.add( new PseudoCodeNode( "max = [];", vars, tree, new DeclareVariable<HashMap<String,Integer>>( "max") {
+            @Override
+            protected HashMap<String,Integer> value(ReadOnlyMemory m) {
+                return new HashMap<String,Integer>();
+            }
+        }));
+        root.add( new PseudoCodeNode( "min = [];", vars, tree, new DeclareVariable<HashMap<String,Integer>>( "min") {
+            @Override
+            protected HashMap<String,Integer> value(ReadOnlyMemory m) {
+                return new HashMap<String,Integer>();
+            }
+        }));
+        root.add( new PseudoCodeNode( "width = [];", vars, tree, new DeclareVariable<HashMap<String,Integer>>( "width") {
+            @Override
+            protected HashMap<String,Integer> value(ReadOnlyMemory m) {
+                return new HashMap<String,Integer>();
+            }
+        }));
         PseudoCodeNode firstLoop = new PseudoCodeNode( "foreach l in ['RIGHTMOST_LOWER', 'LEFTMOST_LOWER', 'RIGHTMOST_UPPER', 'LEFTMOST_UPPER'] do", vars, tree, new ForEachLoop<String>( "l" ) {
             @Override
             protected List<String> list(ReadOnlyMemory m) {
@@ -94,9 +113,9 @@ public class Balancing {
             @Override
             public ControlFlow runForward(Memory m) {
                 String layout = m.read( "l", Visibility.LOCAL );
-                m.declare( "min[" + layout + "]", calcMinX( m.read( "graph", Visibility.LOCAL ), LayoutType.fromString( layout ) ), Visibility.GLOBAL );
+                m.<HashMap<String,Integer>>read( "min", Visibility.LOCAL ).put( layout, calcMinX( m.read( "graph", Visibility.LOCAL ), LayoutType.fromString( layout ) ));
                 actions.add( (Memory mem) -> {
-                    mem.undeclare( "min[" + layout + "]", Visibility.GLOBAL );
+                    mem.<HashMap<String,Integer>>read( "min", Visibility.LOCAL ).remove( layout );
                 });
                 return new ControlFlow( ControlFlow.STEP_OVER );
             }
@@ -105,9 +124,9 @@ public class Balancing {
             @Override
             public ControlFlow runForward(Memory m) {
                 String layout = m.read( "l", Visibility.LOCAL );
-                m.declare( "max[" + layout + "]", calcMaxX( m.read( "graph", Visibility.LOCAL ), LayoutType.fromString( layout ) ), Visibility.GLOBAL );
+                m.<HashMap<String,Integer>>read( "max", Visibility.LOCAL ).put( layout, calcMaxX( m.read( "graph", Visibility.LOCAL ), LayoutType.fromString( layout ) ));
                 actions.add( (Memory mem) -> {
-                    mem.undeclare( "max[" + layout + "]", Visibility.GLOBAL );
+                    mem.<HashMap<String,Integer>>read( "max", Visibility.LOCAL ).remove( layout );
                 });
                 return new ControlFlow( ControlFlow.STEP_OVER );
             }
@@ -116,9 +135,9 @@ public class Balancing {
             @Override
             public ControlFlow runForward(Memory m) {
                 String layout = m.read( "l", Visibility.LOCAL );
-                m.declare( "width[" + layout + "]", m.<Integer>read( "max[" + layout + "]", Visibility.GLOBAL ) - m.<Integer>read( "min[" + layout + "]", Visibility.GLOBAL ), Visibility.GLOBAL );
+                m.<HashMap<String,Integer>>read( "width", Visibility.LOCAL ).put( layout, m.<HashMap<String,Integer>>read( "max", Visibility.LOCAL ).get( layout ) - m.<HashMap<String,Integer>>read( "min", Visibility.LOCAL ).get( layout ) );
                 actions.add( (Memory mem) -> {
-                    mem.undeclare( "width[" + layout + "]", Visibility.GLOBAL );
+                    mem.<HashMap<String,Integer>>read( "width", Visibility.LOCAL ).remove( layout );
                 });
                 return new ControlFlow( ControlFlow.STEP_OVER );
             }
@@ -127,18 +146,24 @@ public class Balancing {
             @Override
             protected String value(ReadOnlyMemory m) {
                 String min = "RIGHTMOST_LOWER";
-                int minW = m.read( "width[RIGHTMOST_LOWER]", Visibility.GLOBAL );
+                int minW = m.<HashMap<String,Integer>>read( "width", Visibility.LOCAL ).get( min );
                 for( String l : new String[]{ "LEFTMOST_LOWER", "RIGHTMOST_UPPER", "LEFTMOST_UPPER" } )
                 {
-                    if( minW > m.<Integer>read( "width[" + l + "]", Visibility.GLOBAL ) )
+                    if( minW > m.<HashMap<String,Integer>>read( "width", Visibility.LOCAL ).get( l ) )
                     {
-                        minW = m.<Integer>read( "width[" + l + "]", Visibility.GLOBAL );
+                        minW = m.<HashMap<String,Integer>>read( "width", Visibility.LOCAL ).get( l );
                         min = l;
                     }
                 }
                 return min;
             }
         }));
+        root.add( new PseudoCodeNode( "shift = [];", vars, tree, new DeclareVariable<HashMap<String,Integer>>( "shift") {
+            @Override
+            protected HashMap<String,Integer> value(ReadOnlyMemory m) {
+                return new HashMap<String,Integer>();
+            }
+        }));
         PseudoCodeNode secondLoop = new PseudoCodeNode( "foreach l in ['RIGHTMOST_LOWER', 'LEFTMOST_LOWER', 'RIGHTMOST_UPPER', 'LEFTMOST_UPPER'] do", vars, tree, new ForEachLoop<String>( "l" ) {
             @Override
             protected List<String> list(ReadOnlyMemory m) {
@@ -157,11 +182,11 @@ public class Balancing {
                 String layout = m.read( "l", Visibility.LOCAL );
                 String lMin = m.read( "l_min", Visibility.LOCAL );
                 if( layout.contains( "RIGHT" ) )
-                    m.declare( "shift[" + layout + "]", m.<Integer>read( "min[" + lMin + "]", Visibility.GLOBAL ) - m.<Integer>read( "min[" + layout + "]", Visibility.GLOBAL ), Visibility.GLOBAL );
+                    m.<HashMap<String,Integer>>read( "shift", Visibility.LOCAL ).put( layout, m.<HashMap<String,Integer>>read( "min", Visibility.LOCAL ).get( lMin ) - m.<HashMap<String,Integer>>read( "min", Visibility.LOCAL ).get( layout ) );
                 else
-                    m.declare( "shift[" + layout + "]", m.<Integer>read( "max[" + lMin + "]", Visibility.GLOBAL ) - m.<Integer>read( "max[" + layout + "]", Visibility.GLOBAL ), Visibility.GLOBAL );
+                    m.<HashMap<String,Integer>>read( "shift", Visibility.LOCAL ).put( layout, m.<HashMap<String,Integer>>read( "max", Visibility.LOCAL ).get( lMin ) - m.<HashMap<String,Integer>>read( "max", Visibility.LOCAL ).get( layout ) );
                 actions.add( (Memory mem) -> {
-                    mem.undeclare( "shift[" + layout + "]", Visibility.GLOBAL );
+                    mem.<HashMap<String,Integer>>read( "shift", Visibility.LOCAL ).remove( layout );
                 });
                 return new ControlFlow( ControlFlow.STEP_OVER );
             }
@@ -195,7 +220,7 @@ public class Balancing {
             public ControlFlow runForward(Memory m) {
                 String layout = m.read( "l", Visibility.LOCAL );
                 ArrayList<Integer> positions = m.read( "positions", Visibility.LOCAL );
-                positions.add( (int)m.<LayeredGraphNode>read( "v", Visibility.LOCAL ).getX( LayoutType.fromString( layout ) ) + m.<Integer>read( "shift[" + layout + "]", Visibility.GLOBAL ) );
+                positions.add( (int)m.<LayeredGraphNode>read( "v", Visibility.LOCAL ).getX( LayoutType.fromString( layout ) ) + m.<HashMap<String,Integer>>read( "shift", Visibility.LOCAL ).get( layout ) );
                 actions.add( (Memory mem) -> {
                     positions.remove( positions.size() - 1 );
                 });