ConflictDetection.java 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package bk;
  2. import java.util.ArrayList;
  3. import animation.AlgorithmStage;
  4. import animation.BackwardAction;
  5. import graph.LayeredGraphEdge;
  6. import graph.LayeredGraphNode;
  7. public class ConflictDetection implements AlgorithmStage {
  8. private LayeredGraphNode graph;
  9. private ArrayList< BackwardAction > actions;
  10. private int i;
  11. private int l1;
  12. ConflictDetection( LayeredGraphNode graph )
  13. {
  14. this.graph = graph;
  15. actions = new ArrayList<>();
  16. i = 1;
  17. l1 = 0;
  18. }
  19. @Override
  20. public StageStatus forwardStep() {
  21. int oldI = i;
  22. int oldL1 = l1;
  23. LayeredGraphNode curr = graph.getContainedLayers().get( i + 1 ).get( l1 ); // TODO: fix IndexOutOfBoundsException
  24. curr.setSelected( null );
  25. ArrayList< LayeredGraphEdge > edges = curr.getIncomingEdges();
  26. LayeredGraphEdge dummyEdge = null;
  27. for( LayeredGraphEdge e : edges )
  28. {
  29. if( e.isDummyEdge() )
  30. {
  31. dummyEdge = e;
  32. break;
  33. }
  34. }
  35. ArrayList< LayeredGraphEdge > conflicts = new ArrayList<>();
  36. if( dummyEdge != null )
  37. {
  38. for( LayeredGraphEdge e : edges )
  39. {
  40. if( e.isDummyEdge() )
  41. {
  42. ArrayList< LayeredGraphEdge > conf = e.calcConflictedEdges();
  43. for( LayeredGraphEdge ce : conf )
  44. {
  45. if( !ce.isDummyEdge() )
  46. conflicts.add( ce );
  47. }
  48. }
  49. }
  50. }
  51. for( LayeredGraphEdge c : conflicts )
  52. c.setConflicted( true, null );
  53. StageStatus status = calcNextStatus();
  54. actions.add( ()->{
  55. i = oldI;
  56. l1 = oldL1;
  57. for( LayeredGraphEdge c : conflicts )
  58. c.setConflicted( false, null );
  59. });
  60. return status;
  61. }
  62. private StageStatus calcNextStatus()
  63. {
  64. l1++;
  65. if( l1 >= graph.getContainedLayers().get( i + 1 ).size() )
  66. {
  67. i++;
  68. if( i >= graph.getContainedLayers().size() - 2 )
  69. {
  70. i--;
  71. l1--;
  72. return StageStatus.FINISHED;
  73. }
  74. l1 = 0;
  75. }
  76. return StageStatus.UNFINISHED;
  77. }
  78. @Override
  79. public StageStatus backwardStep() {
  80. if( actions.size() == 0 )
  81. return StageStatus.FINISHED;
  82. actions.get( 0 ).reverse();
  83. actions.remove( 0 );
  84. return StageStatus.UNFINISHED;
  85. }
  86. }