浏览代码

Block calculation nearly done

Kolja Strohm 6 年之前
父节点
当前提交
4875ce1429
共有 4 个文件被更改,包括 76 次插入7 次删除
  1. 46 6
      src/Algorithms/Animated/BK/BlockCalc.java
  2. 4 0
      src/Model/LayeredGraphNode.java
  3. 25 1
      src/Model/LayeredNode.java
  4. 1 0
      src/View/MainView.java

+ 46 - 6
src/Algorithms/Animated/BK/BlockCalc.java

@@ -1,5 +1,6 @@
 package Algorithms.Animated.BK;
 
+import java.awt.Color;
 import java.util.ArrayList;
 
 import Algorithms.Animated.AlgorithmStep;
@@ -11,6 +12,7 @@ public class BlockCalc implements AlgorithmStep {
 
 	private int layerIndex;
 	private int nodeIndex;
+	private int r;
 	private LayeredGraphNode graph;
 	private ArrayList< ArrayList< BKNodePlacement > > subgraphAlgs;
 	private ArrayList< BackwordAction > backwards;
@@ -20,6 +22,7 @@ public class BlockCalc implements AlgorithmStep {
 		this.graph = graph;
 		layerIndex = 0;
 		nodeIndex = 0;
+		r = 0;
 		subgraphAlgs = new ArrayList<>();
 		for( ArrayList<LayeredGraphNode> l : graph.getContainedLayers() )
 		{
@@ -52,14 +55,44 @@ public class BlockCalc implements AlgorithmStep {
 			});
 			return calcNextState();
 		}
-		for( LayeredGraphEdge e : incommingEdges )
+		int[] ms = {(incommingEdges.size() + 1) / 2, (int)( (incommingEdges.size() + 1) / 2.0 + 0.5 )};
+		boolean backwardsAdded = false;
+		for( int m : ms )
 		{
-			// TODO
+		    if( current.getAlignedTo() == current )
+		    {
+                LayeredGraphNode u = incommingEdges.get( m - 1 ).getSources().get( 0 );
+		        // TODO detect conflicts
+		        if( r < graph.getContainedLayers().get( layerIndex - 1 ).indexOf( u ) )
+		        {
+		            LayeredGraphNode oldAlignU = u.getAlignedTo();
+		            Color oldColorCurrent = current.getColor();
+		            LayeredGraphNode oldRootCurrent = current.getRoot();
+		            LayeredGraphNode oldAlignCurrent = current.getAlignedTo();
+		            int oldR = r;
+		            u.setAlignTo( current );
+		            current.setColor( u.getRoot().getColor() );
+		            current.setRoot( u.getRoot() );
+		            current.setAlignTo( current.getRoot() );
+		            r = graph.getContainedLayers().get( layerIndex - 1 ).indexOf( u );
+		            backwards.add( 0, () -> {
+		                System.out.println( "Stepping Backwards..." );
+		                u.setAlignTo( oldAlignU );
+		                current.setColor( oldColorCurrent );
+		                current.setRoot( oldRootCurrent );
+		                current.setAlignTo( oldAlignCurrent );
+		                r = oldR;
+		            });
+		            backwardsAdded = true;
+		        }
+		    }
+		}
+		if( !backwardsAdded )
+		{
+    		backwards.add( 0, () -> {
+                System.out.println( "Performing Empty Backwards Step..." );
+    		});
 		}
-		backwards.add( 0, () -> {
-			System.out.println( "Stepping Backwards..." );
-			// TODO
-		});
 		current.update();
 		return calcNextState();
 	}
@@ -76,6 +109,11 @@ public class BlockCalc implements AlgorithmStep {
 		{
 			layerIndex++;
 			nodeIndex = 0;
+			int oldR = r;
+			r = 0;
+			backwards.add(0, ()->{
+			   this.r = oldR;
+			});
 		}
 		return StepStatus.UNFINISHED;
 	}
@@ -114,6 +152,8 @@ public class BlockCalc implements AlgorithmStep {
 		if( nodeIndex < 0 )
 		{
 			layerIndex--;
+	        backwards.get( 0 ).reverse();
+	        backwards.remove( 0 );
 			nodeIndex = graph.getContainedLayers().get( layerIndex ).size() - 1;
 		}
 		return StepStatus.UNFINISHED;

+ 4 - 0
src/Model/LayeredGraphNode.java

@@ -23,6 +23,10 @@ public interface LayeredGraphNode {
      */
     ElkNode getOriginalNode();
     
+    public void setAlignTo( LayeredGraphNode align );
+    public LayeredGraphNode getAlignedTo();
+    public void setRoot( LayeredGraphNode root );
+    public LayeredGraphNode getRoot();
     public void setName( String n );
     public String getName();
     public void setColor( Color c );

+ 25 - 1
src/Model/LayeredNode.java

@@ -33,6 +33,9 @@ public class LayeredNode implements LayeredGraphNode {
     JPanel view;
     boolean selected;
     
+    LayeredGraphNode align;
+    LayeredGraphNode root;
+    
     // for subgraph in this node
     private ArrayList< LayeredGraphEdge > edges;
     private ArrayList< LayeredGraphNode > nodes;
@@ -53,7 +56,28 @@ public class LayeredNode implements LayeredGraphNode {
         	w = original.getWidth();
         	h = original.getHeight();
         }
-        
+        align = this;
+        root = this;
+    }
+    
+    public void setAlignTo( LayeredGraphNode align )
+    {
+        this.align = align;
+    }
+    
+    public LayeredGraphNode getAlignedTo()
+    {
+        return align;
+    }
+    
+    public void setRoot( LayeredGraphNode root )
+    {
+        this.root = root;
+    }
+    
+    public LayeredGraphNode getRoot()
+    {
+        return root;
     }
     
     public void update()

+ 1 - 0
src/View/MainView.java

@@ -63,6 +63,7 @@ public class MainView {
 		frame.setLayout(new BorderLayout());
 		frame.add(view, BorderLayout.CENTER);
 		frame.add( view );
+		frame.validate();
 		frame.repaint();
 		new BKNodePlacement( controller, graph );
 	}