1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- package bk;
- import java.util.ArrayList;
- import animation.AlgorithmStage;
- import animation.BackwardAction;
- import graph.LayeredGraphEdge;
- import graph.LayeredGraphNode;
- public class ConflictDetection implements AlgorithmStage {
- private LayeredGraphNode graph;
- private ArrayList< BackwardAction > actions;
-
- private int i;
- private int l1;
-
- ConflictDetection( LayeredGraphNode graph )
- {
- this.graph = graph;
- actions = new ArrayList<>();
- i = 1;
- l1 = 0;
- }
-
- @Override
- public StageStatus forwardStep() {
- int oldI = i;
- int oldL1 = l1;
- LayeredGraphNode curr = graph.getContainedLayers().get( i + 1 ).get( l1 ); // TODO: fix IndexOutOfBoundsException
- curr.setSelected( null );
- ArrayList< LayeredGraphEdge > edges = curr.getIncomingEdges();
- LayeredGraphEdge dummyEdge = null;
- for( LayeredGraphEdge e : edges )
- {
- if( e.isDummyEdge() )
- {
- dummyEdge = e;
- break;
- }
- }
- ArrayList< LayeredGraphEdge > conflicts = new ArrayList<>();
- if( dummyEdge != null )
- {
- for( LayeredGraphEdge e : edges )
- {
- if( e.isDummyEdge() )
- {
- ArrayList< LayeredGraphEdge > conf = e.calcConflictedEdges();
- for( LayeredGraphEdge ce : conf )
- {
- if( !ce.isDummyEdge() )
- conflicts.add( ce );
- }
- }
- }
- }
- for( LayeredGraphEdge c : conflicts )
- c.setConflicted( true, null );
- StageStatus status = calcNextStatus();
- actions.add( ()->{
- i = oldI;
- l1 = oldL1;
- for( LayeredGraphEdge c : conflicts )
- c.setConflicted( false, null );
- });
- return status;
- }
-
- private StageStatus calcNextStatus()
- {
- l1++;
- if( l1 >= graph.getContainedLayers().get( i + 1 ).size() )
- {
- i++;
- if( i >= graph.getContainedLayers().size() - 2 )
- {
- i--;
- l1--;
- return StageStatus.FINISHED;
- }
- l1 = 0;
- }
- return StageStatus.UNFINISHED;
- }
- @Override
- public StageStatus backwardStep() {
- if( actions.size() == 0 )
- return StageStatus.FINISHED;
- actions.get( 0 ).reverse();
- actions.remove( 0 );
- return StageStatus.UNFINISHED;
- }
- }
|