package Algorithms.Animated.BK; import java.util.ArrayList; import Algorithms.Animated.AlgorithmStep; import Algorithms.Animated.BackwordAction; import Model.LayeredGraphEdge; import Model.LayeredGraphNode; public class BlockCalc implements AlgorithmStep { private int layerIndex; private int nodeIndex; private LayeredGraphNode graph; private ArrayList< ArrayList< BKNodePlacement > > subgraphAlgs; private ArrayList< BackwordAction > backwards; public BlockCalc( LayeredGraphNode graph ) { this.graph = graph; layerIndex = 0; nodeIndex = 0; subgraphAlgs = new ArrayList<>(); for( ArrayList l : graph.getContainedLayers() ) { ArrayList< BKNodePlacement > algs = new ArrayList<>(); for( int i = 0; i < l.size(); i++ ) algs.add( null ); subgraphAlgs.add( algs ); } backwards = new ArrayList<>(); } @Override public StepStatus forwardStep() { System.out.println( "test" ); LayeredGraphNode current = graph.getContainedLayers().get( layerIndex ).get( nodeIndex ); current.setSelected(); if( current.getContainedNodes().size() > 0 ) { if( subgraphAlgs.get( layerIndex ).get( nodeIndex ) == null ) subgraphAlgs.get( layerIndex ).set( nodeIndex, new BKNodePlacement( null, current ) ); if( subgraphAlgs.get( layerIndex ).get( nodeIndex ).forwardStep() == StepStatus.UNFINISHED ) return StepStatus.UNFINISHED; } ArrayList< LayeredGraphEdge > incommingEdges = current.getIncomingEdges(); if( incommingEdges.size() == 0 ) { current.update(); backwards.add( 0, () -> { System.out.println( "Performing Empty Backwards Step..." ); }); return calcNextState(); } for( LayeredGraphEdge e : incommingEdges ) { // TODO } backwards.add( 0, () -> { System.out.println( "Stepping Backwards..." ); // TODO }); current.update(); return calcNextState(); } private StepStatus calcNextState() { if( layerIndex >= graph.getContainedLayers().size() - 1 ) { if( nodeIndex >= graph.getContainedLayers().get( layerIndex ).size() -1 ) return StepStatus.FINISHED; } nodeIndex++; if( nodeIndex >= graph.getContainedLayers().get( layerIndex ).size() ) { layerIndex++; nodeIndex = 0; } return StepStatus.UNFINISHED; } @Override public StepStatus backwardStep() { if( subgraphAlgs.get( layerIndex ).get( nodeIndex ) != null ) { if( subgraphAlgs.get( layerIndex ).get( nodeIndex ).backwardStep() == StepStatus.UNFINISHED ) { LayeredGraphNode current = graph.getContainedLayers().get( layerIndex ).get( nodeIndex ); current.setSelected(); current.update(); return StepStatus.UNFINISHED; } } StepStatus status = calcBeforeState(); if( status == StepStatus.FINISHED ) return status; LayeredGraphNode current = graph.getContainedLayers().get( layerIndex ).get( nodeIndex ); current.setSelected(); current.update(); backwards.get( 0 ).reverse(); backwards.remove( 0 ); return status; } private StepStatus calcBeforeState() { if( layerIndex == 0 ) { if( nodeIndex == 0 ) return StepStatus.FINISHED; } nodeIndex--; if( nodeIndex < 0 ) { layerIndex--; nodeIndex = graph.getContainedLayers().get( layerIndex ).size() - 1; } return StepStatus.UNFINISHED; } }