Ver código fonte

removed the abstact algorithm class becous it was no longer needet

Kolja Strohm 6 anos atrás
pai
commit
8ef518e089

+ 0 - 131
src/algorithm/Algorithm.java

@@ -1,131 +0,0 @@
-package algorithm;
-
-import java.awt.event.ComponentEvent;
-import java.awt.event.ComponentListener;
-
-import javax.swing.JFrame;
-import javax.swing.JTree;
-import javax.swing.SwingUtilities;
-
-import graph.LayeredGraphNode;
-import processor.PseudoCodeNode;
-import processor.PseudoCodeProcessor;
-import processor.PseudoCodeProcessor.CodeStatus;
-
-/**
- * represents an algorithm that can be run by an {@link PseudoCodeProcessor}
- * @author kolja
- *
- */
-public abstract class Algorithm extends Thread {
-
-    protected AnimationController ac;
-    protected LayeredGraphNode graph;
-    private JFrame view;
-    protected PseudoCodeNode root;
-    protected PseudoCodeProcessor processor;
-    private boolean renderImage = false;
-
-    /**
-     * creates an {@link Algorithm} and initializes its fields
-     * @param controller The controller that this algorithm is controlled by
-     * @param graph The graph that this algorithm works with
-     * @param view The view where the progress is displayed
-     */
-    public Algorithm( AnimationController controller, LayeredGraphNode graph, JFrame view )
-    {
-        this.ac = controller;
-        this.graph = graph;
-        this.view = view;
-        root = null;
-        renderImage = true;
-    }
-
-    private synchronized void update()
-    {
-        if( renderImage )
-        {
-            renderImage = false;
-            SwingUtilities.invokeLater(new Runnable() {
-                public void run() {
-                    for( ComponentListener l : view.getComponentListeners() )
-                    {
-                        l.componentResized( new ComponentEvent(view, 0) );
-                        synchronized( Algorithm.this )
-                        {
-                            renderImage = true;
-                        }
-                    }
-                }
-            });
-        }
-    }
-
-    @Override
-    public void run()
-    {
-        while( true ) // if this loop would end we could not undo steps any more
-        {
-        	CodeStatus status = null;
-            try {
-                if( processor != null )
-                {
-                    Action action = ac.getNextAction();
-                    graph.unselectGraph();
-                    switch( action )
-                    {
-                    case FORWARD:
-                    	status = processor.forwardStep();
-                        break;
-                    case FORWARD_OUT:
-                        status = processor.forwardStepOut();
-                        break;
-                    case FORWARD_OVER:
-                        status = processor.forwardStepOver();
-                        break;
-                    case BACKWARD:
-                        status = processor.backwardStep();
-                        break;
-                    case BACKWARD_OUT:
-                        status = processor.backwardStepOut();
-                        break;
-                    case BACKWARD_OVER:
-                        status = processor.backwardStepOver();
-                        break;
-                    default:
-                        assert false;
-                        break;
-                    }
-                }
-                else
-                    status = CodeStatus.FINISHED;
-            } catch (InterruptedException e) {
-                e.printStackTrace();
-                return;
-            }
-            update();
-            if( status == CodeStatus.FINISHED )
-            {
-        		ac.setContinuous( false );
-        		ac.setNextAction( null );
-            }
-        }
-    }
-
-    public abstract PseudoCodeNode createPseudocodeTree( JTree tree );
-    
-    public PseudoCodeProcessor getProcessor() {
-        return processor;
-    }
-    
-    /**
-     * returns a string with debugging information about the current status algorithm
-     * @return some information
-     */
-    public String getDebugString()
-    {
-        if( processor == null )
-            return "";
-        return processor.getDebugOutput();
-    }
-}

+ 4 - 11
src/bk/BKNodePlacement.java

@@ -1,10 +1,7 @@
 package bk;
 
-import javax.swing.JFrame;
 import javax.swing.JTree;
 
-import algorithm.Algorithm;
-import algorithm.AnimationController;
 import algorithm.CodeLine;
 import codelines.DeclareVariable;
 import codelines.FunctionCall;
@@ -16,7 +13,6 @@ import lib.TextLayoutHelper;
 import processor.ControlFlow;
 import processor.Memory;
 import processor.PseudoCodeNode;
-import processor.PseudoCodeProcessor;
 import processor.Memory.ReadOnlyMemory;
 import processor.Memory.Visibility;
 
@@ -25,7 +21,7 @@ import processor.Memory.Visibility;
  * @author kolja
  *
  */
-public class BKNodePlacement extends Algorithm {
+public class BKNodePlacement {
 
     /**
      * A stage of the BK node placement algorithm.
@@ -44,8 +40,7 @@ public class BKNodePlacement extends Algorithm {
 
     private Stage stage;
 
-    public BKNodePlacement(AnimationController controller, LayeredGraphNode graph, JFrame view) {
-        super(controller, graph, view);
+    public BKNodePlacement() {
         stage = Stage.CONFLICT_DETECTION;
     }
     
@@ -54,16 +49,15 @@ public class BKNodePlacement extends Algorithm {
         return stage;
     }
 
-    @Override
     public PseudoCodeNode createPseudocodeTree( JTree tree )
     {
     	String[] vars = { "layout", "graph" };
     	PseudoCodeNode mainFunction = new PseudoCodeNode( "function bkNodePlacement( graph )", vars, tree, new FunctionDefinition( new String[]{"graph"} ) );
-    	root = new PseudoCodeNode( "-- BK Node Placement Algorithm --", vars, tree, new CodeLine() {
+    	PseudoCodeNode root = new PseudoCodeNode( "-- BK Node Placement Algorithm --", vars, tree, new CodeLine() {
 
 			@Override
 			public ControlFlow runForward(Memory m) {
-				m.declare( "param1", graph, Visibility.GLOBAL );
+				m.declare( "param1", m.read( "graph", Visibility.GLOBAL ), Visibility.GLOBAL );
 				if( m.isDefined( "Called", Visibility.GLOBAL ) )
 				{
 					actions.push( (Memory mem) -> {
@@ -208,7 +202,6 @@ public class BKNodePlacement extends Algorithm {
         };
         root.add( balancingStage );
         balancingStage.add( combine );
-        processor = new PseudoCodeProcessor( root );
         return root;
     }
 }

+ 100 - 9
src/processor/PseudoCodeProcessor.java

@@ -1,11 +1,19 @@
 package processor;
 
+import java.awt.event.ComponentEvent;
+import java.awt.event.ComponentListener;
 import java.util.Stack;
 
+import javax.swing.JFrame;
+import javax.swing.SwingUtilities;
+
+import algorithm.Action;
+import algorithm.AnimationController;
+import graph.LayeredGraphNode;
 import processor.Memory.Visibility;
 import processor.StackFrame.FrameType;
 
-public class PseudoCodeProcessor {
+public class PseudoCodeProcessor extends Thread {
 
     public static enum CodeStatus
     {
@@ -13,20 +21,33 @@ public class PseudoCodeProcessor {
         BREAKPOINT,
         FINISHED
     }
-    
+
+    private LayeredGraphNode graph;
     private String currentDebugOutput;
     private Memory mem;
     private PseudoCodeNode programPointer;
     private Stack<ControlFlow> controlStack;
     private boolean skip = false;
+    private boolean renderImage = true;
+    private JFrame view;
+    private AnimationController controller;
     
-    public PseudoCodeProcessor( PseudoCodeNode start )
+    public PseudoCodeProcessor( PseudoCodeNode start, LayeredGraphNode graph, JFrame view )
     {
         mem = new Memory();
         mem.addFrame( new StackFrame( FrameType.FUNCTION ) );
+        mem.declare( "graph", graph, Visibility.GLOBAL );
         programPointer = start;
         currentDebugOutput = "";
         controlStack = new Stack<>();
+        this.graph = graph;
+        controller = new AnimationController();
+        this.view = view;
+    }
+    
+    public AnimationController getController()
+    {
+    	return controller;
     }
     
     private CodeStatus selectNextNode( PseudoCodeNode next, PseudoCodeNode last )
@@ -127,7 +148,7 @@ public class PseudoCodeProcessor {
         return status;
     }
     
-    public CodeStatus forwardStepOver()
+    private CodeStatus forwardStepOver()
     {
         if( programPointer == null )
             return CodeStatus.FINISHED;
@@ -139,7 +160,7 @@ public class PseudoCodeProcessor {
         return status;
     }
     
-    public CodeStatus forwardStepOut()
+    private CodeStatus forwardStepOut()
     {
         if( programPointer == null )
             return CodeStatus.FINISHED;
@@ -168,7 +189,7 @@ public class PseudoCodeProcessor {
         return CodeStatus.UNFINISHED;
     }
     
-    public CodeStatus backwardStep()
+    private CodeStatus backwardStep()
     {
         if( programPointer == null || controlStack.isEmpty() )
             return CodeStatus.FINISHED;
@@ -180,7 +201,7 @@ public class PseudoCodeProcessor {
         return selectBeforeNode( nextPC, programPointer );
     }
     
-    public CodeStatus backwardStepOverUntilNotSkip()
+    private CodeStatus backwardStepOverUntilNotSkip()
     {
         skip = true;
         if( programPointer == null )
@@ -193,7 +214,7 @@ public class PseudoCodeProcessor {
         return status;
     }
     
-    public CodeStatus backwardStepOver()
+    private CodeStatus backwardStepOver()
     {
         if( programPointer == null )
             return CodeStatus.FINISHED;
@@ -205,7 +226,7 @@ public class PseudoCodeProcessor {
         return status;
     }
     
-    public CodeStatus backwardStepOut()
+    private CodeStatus backwardStepOut()
     {
         if( programPointer == null )
             return CodeStatus.FINISHED;
@@ -216,6 +237,76 @@ public class PseudoCodeProcessor {
         } while( mem.getSize() >= stackSize && status == CodeStatus.UNFINISHED );
         return status;
     }
+
+    private synchronized void update()
+    {
+        if( renderImage )
+        {
+            renderImage = false;
+            SwingUtilities.invokeLater(new Runnable() {
+                public void run() {
+                    for( ComponentListener l : view.getComponentListeners() )
+                    {
+                        l.componentResized( new ComponentEvent(view, 0) );
+                        synchronized( this )
+                        {
+                            renderImage = true;
+                        }
+                    }
+                }
+            });
+        }
+    }
+    
+    @Override
+    public void run()
+    {
+        while( true ) // if this loop would end we could not undo steps any more
+        {
+        	CodeStatus status = null;
+            try {
+                Action action = controller.getNextAction();
+                graph.unselectGraph();
+                switch( action )
+                {
+                case FORWARD:
+                	status = forwardStep();
+                    break;
+                case FORWARD_OUT:
+                    status = forwardStepOut();
+                    graph.unselectGraph();
+                    break;
+                case FORWARD_OVER:
+                    status = forwardStepOver();
+                    graph.unselectGraph();
+                    break;
+                case BACKWARD:
+                    status = backwardStep();
+                    break;
+                case BACKWARD_OUT:
+                    status = backwardStepOut();
+                    graph.unselectGraph();
+                    break;
+                case BACKWARD_OVER:
+                    status = backwardStepOver();
+                    graph.unselectGraph();
+                    break;
+                default:
+                    assert false;
+                    break;
+                }
+            } catch (InterruptedException e) {
+                e.printStackTrace();
+                return;
+            }
+            update();
+            if( status == CodeStatus.FINISHED )
+            {
+            	controller.setContinuous( false );
+            	controller.setNextAction( null );
+            }
+        }
+    }
     
     public Memory getMemory()
     {

+ 11 - 8
src/view/MainView.java

@@ -49,6 +49,7 @@ import graph.io.Writer;
 import lib.SimpleNodePlacement;
 import lib.TextLayoutHelper;
 import processor.PseudoCodeNode;
+import processor.PseudoCodeProcessor;
 
 /**
  * The main window of the application.
@@ -170,7 +171,6 @@ public class MainView {
         graph.setColor( null, null );
         frameCounter++;
         this.graph = graph;
-        controller = new AnimationController();
         frame = new JFrame( "NodeShuffler" );
         frame.addWindowListener(new java.awt.event.WindowAdapter() {
             @Override
@@ -181,7 +181,12 @@ public class MainView {
             }
         });
         
-        BKNodePlacement algorithm = new BKNodePlacement( controller, graph, frame );
+        // Create Pseudo Code and Processor
+        BKNodePlacement algorithm = new BKNodePlacement();
+        pseudoTree = new JTree();
+        PseudoCodeNode tree = algorithm.createPseudocodeTree( pseudoTree );
+        PseudoCodeProcessor processor = new PseudoCodeProcessor( tree, graph, frame );
+        controller = processor.getController();
         
         // Create Menu GUI
         stepForward = new NiceButton( "stepForward" );
@@ -387,9 +392,7 @@ public class MainView {
             }
             
         });
-        pseudoTree = new JTree();
-        pseudoTree.setBackground(RenderHelper.BACKGROUND_COLOR);
-        PseudoCodeNode tree = algorithm.createPseudocodeTree( pseudoTree );
+        pseudoTree.setBackground(RenderHelper.BACKGROUND_COLOR);        
         tree.setController( controller );
         pseudoTree.setModel( new DefaultTreeModel( tree ) );
         pseudoTree.setCellRenderer( new PseudoCodeRenderer() );
@@ -516,7 +519,7 @@ public class MainView {
             }
         } );
         pseudoTree.setRowHeight(15);
-        ((PseudoCodeRenderer)pseudoTree.getCellRenderer()).setMemory( algorithm.getProcessor().getMemory());
+        ((PseudoCodeRenderer)pseudoTree.getCellRenderer()).setMemory( processor.getMemory());
         JScrollPane treeView = new JScrollPane( pseudoTree );
         PseudoCodeLines lineView = new PseudoCodeLines( pseudoTree );
         treeView.setRowHeaderView( lineView );
@@ -630,7 +633,7 @@ public class MainView {
                 combined.setSize( layne.getWidth() / 3, layne.getHeight() / 3 );
                 combined.setLocation( layne.getWidth() / 3, layne.getHeight() / 3 );
 
-                debugText.setText( algorithm.getDebugString().trim() );
+                debugText.setText( processor.getDebugOutput().trim() );
                 layne.remove( pl );
                 layne.add( pl, 1 );
                 if( optionsDialog != null && optionsDialog.getLayerDisplayOption() == 1 && old != algorithm.getAlgorithmState() )
@@ -734,7 +737,7 @@ public class MainView {
             
         });
         
-        algorithm.start();
+        processor.start(); // start running the algorithm
     }
 
     private NodeView createNodeView( LayeredGraphNode gNode, LayoutType lt )