BlockCalc.java 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package Algorithms.Animated.BK;
  2. import java.util.ArrayList;
  3. import Algorithms.Animated.AlgorithmStep;
  4. import Algorithms.Animated.BackwordAction;
  5. import Model.LayeredGraphEdge;
  6. import Model.LayeredGraphNode;
  7. public class BlockCalc implements AlgorithmStep {
  8. private int layerIndex;
  9. private int nodeIndex;
  10. private LayeredGraphNode graph;
  11. private ArrayList< ArrayList< BKNodePlacement > > subgraphAlgs;
  12. private ArrayList< BackwordAction > backwards;
  13. public BlockCalc( LayeredGraphNode graph )
  14. {
  15. this.graph = graph;
  16. layerIndex = 0;
  17. nodeIndex = 0;
  18. subgraphAlgs = new ArrayList<>();
  19. for( ArrayList<LayeredGraphNode> l : graph.getContainedLayers() )
  20. {
  21. ArrayList< BKNodePlacement > algs = new ArrayList<>();
  22. for( int i = 0; i < l.size(); i++ )
  23. algs.add( null );
  24. subgraphAlgs.add( algs );
  25. }
  26. backwards = new ArrayList<>();
  27. }
  28. @Override
  29. public StepStatus forwardStep() {
  30. System.out.println( "test" );
  31. LayeredGraphNode current = graph.getContainedLayers().get( layerIndex ).get( nodeIndex );
  32. current.setSelected();
  33. if( current.getContainedNodes().size() > 0 )
  34. {
  35. if( subgraphAlgs.get( layerIndex ).get( nodeIndex ) == null )
  36. subgraphAlgs.get( layerIndex ).set( nodeIndex, new BKNodePlacement( null, current ) );
  37. if( subgraphAlgs.get( layerIndex ).get( nodeIndex ).forwardStep() == StepStatus.UNFINISHED )
  38. return StepStatus.UNFINISHED;
  39. }
  40. ArrayList< LayeredGraphEdge > incommingEdges = current.getIncomingEdges();
  41. if( incommingEdges.size() == 0 )
  42. {
  43. current.update();
  44. backwards.add( 0, () -> {
  45. System.out.println( "Performing Empty Backwards Step..." );
  46. });
  47. return calcNextState();
  48. }
  49. for( LayeredGraphEdge e : incommingEdges )
  50. {
  51. // TODO
  52. }
  53. backwards.add( 0, () -> {
  54. System.out.println( "Stepping Backwards..." );
  55. // TODO
  56. });
  57. current.update();
  58. return calcNextState();
  59. }
  60. private StepStatus calcNextState()
  61. {
  62. if( layerIndex >= graph.getContainedLayers().size() - 1 )
  63. {
  64. if( nodeIndex >= graph.getContainedLayers().get( layerIndex ).size() -1 )
  65. return StepStatus.FINISHED;
  66. }
  67. nodeIndex++;
  68. if( nodeIndex >= graph.getContainedLayers().get( layerIndex ).size() )
  69. {
  70. layerIndex++;
  71. nodeIndex = 0;
  72. }
  73. return StepStatus.UNFINISHED;
  74. }
  75. @Override
  76. public StepStatus backwardStep() {
  77. if( subgraphAlgs.get( layerIndex ).get( nodeIndex ) != null )
  78. {
  79. if( subgraphAlgs.get( layerIndex ).get( nodeIndex ).backwardStep() == StepStatus.UNFINISHED )
  80. {
  81. LayeredGraphNode current = graph.getContainedLayers().get( layerIndex ).get( nodeIndex );
  82. current.setSelected();
  83. current.update();
  84. return StepStatus.UNFINISHED;
  85. }
  86. }
  87. StepStatus status = calcBeforeState();
  88. if( status == StepStatus.FINISHED )
  89. return status;
  90. LayeredGraphNode current = graph.getContainedLayers().get( layerIndex ).get( nodeIndex );
  91. current.setSelected();
  92. current.update();
  93. backwards.get( 0 ).reverse();
  94. backwards.remove( 0 );
  95. return status;
  96. }
  97. private StepStatus calcBeforeState()
  98. {
  99. if( layerIndex == 0 )
  100. {
  101. if( nodeIndex == 0 )
  102. return StepStatus.FINISHED;
  103. }
  104. nodeIndex--;
  105. if( nodeIndex < 0 )
  106. {
  107. layerIndex--;
  108. nodeIndex = graph.getContainedLayers().get( layerIndex ).size() - 1;
  109. }
  110. return StepStatus.UNFINISHED;
  111. }
  112. }