BlockCalc.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. package Algorithms.Animated.BK;
  2. import java.awt.Color;
  3. import java.util.ArrayList;
  4. import Algorithms.Animated.AlgorithmStage;
  5. import Algorithms.Animated.BackwardAction;
  6. import Model.LayeredGraphEdge;
  7. import Model.LayeredGraphNode;
  8. /**
  9. * The stage of the BK node placement algorithm where the blocks are computed.
  10. * @author kolja
  11. *
  12. */
  13. public class BlockCalc implements AlgorithmStage {
  14. private int layerIndex;
  15. private int nodeIndex;
  16. private int r;
  17. private LayeredGraphNode graph;
  18. private ArrayList< ArrayList< BKNodePlacement > > subgraphAlgs;
  19. private ArrayList< BackwardAction > backwards;
  20. int step;
  21. public BlockCalc( LayeredGraphNode graph )
  22. {
  23. step = 0;
  24. this.graph = graph;
  25. layerIndex = 0;
  26. nodeIndex = 0;
  27. r = 0;
  28. subgraphAlgs = new ArrayList<>();
  29. for( ArrayList<LayeredGraphNode> l : graph.getContainedLayers() )
  30. {
  31. ArrayList< BKNodePlacement > algs = new ArrayList<>();
  32. for( int i = 0; i < l.size(); i++ )
  33. algs.add( null );
  34. subgraphAlgs.add( algs );
  35. }
  36. backwards = new ArrayList<>();
  37. }
  38. @Override
  39. public StageStatus forwardStep() {
  40. LayeredGraphNode current = graph.getContainedLayers().get( layerIndex ).get( nodeIndex );
  41. current.setSelected();
  42. if( current.getContainedNodes().size() > 0 )
  43. {
  44. if( subgraphAlgs.get( layerIndex ).get( nodeIndex ) == null )
  45. subgraphAlgs.get( layerIndex ).set( nodeIndex, new BKNodePlacement( null, current ) );
  46. if( subgraphAlgs.get( layerIndex ).get( nodeIndex ).forwardStep() == StageStatus.UNFINISHED )
  47. return StageStatus.UNFINISHED;
  48. }
  49. ArrayList< LayeredGraphEdge > incommingEdges = current.getIncomingEdges();
  50. if( incommingEdges.size() == 0 )
  51. {
  52. backwards.add( 0, () -> {
  53. System.out.println( "Performing Empty Backwards Step..." );
  54. });
  55. return calcNextState();
  56. }
  57. int[] ms = {(incommingEdges.size() + 1) / 2, (int)( (incommingEdges.size() + 1) / 2.0 + 0.5 )};
  58. boolean backwardsAdded = false;
  59. for( int m : ms )
  60. {
  61. if( current.getAlignedTo() == current )
  62. {
  63. LayeredGraphNode u = incommingEdges.get( m - 1 ).getSources().get( 0 );
  64. ArrayList<LayeredGraphEdge> conflicts = incommingEdges.get( m - 1 ).calcConflictedEdges();
  65. if( !incommingEdges.get( m - 1 ).isConflicted() && r < graph.getContainedLayers().get( layerIndex - 1 ).indexOf( u ) + 1 )
  66. {
  67. System.out.println( "" );
  68. ArrayList< Boolean > oldConflicts = new ArrayList<>();
  69. for( LayeredGraphEdge e : conflicts )
  70. {
  71. oldConflicts.add( e.isConflicted() );
  72. e.setConflicted( true );
  73. }
  74. LayeredGraphNode oldAlignU = u.getAlignedTo();
  75. Color oldColorCurrent = current.getColor();
  76. LayeredGraphNode oldRootCurrent = current.getRoot();
  77. LayeredGraphNode oldAlignCurrent = current.getAlignedTo();
  78. int oldR = r;
  79. u.setAlignTo( current );
  80. current.setColor( u.getRoot().getColor() );
  81. current.setRoot( u.getRoot() );
  82. current.setAlignTo( current.getRoot() );
  83. r = graph.getContainedLayers().get( layerIndex - 1 ).indexOf( u );
  84. int oldStep = step++;
  85. backwards.add( 0, () -> {
  86. System.out.println( "Stepping Backwards... (Step " + oldStep + ")" );
  87. for( int i = 0; i < conflicts.size(); i++ )
  88. conflicts.get( i ).setConflicted( oldConflicts.get( i ) );
  89. u.setAlignTo( oldAlignU );
  90. current.setColor( oldColorCurrent );
  91. current.setRoot( oldRootCurrent );
  92. current.setAlignTo( oldAlignCurrent );
  93. r = oldR;
  94. });
  95. backwardsAdded = true;
  96. }
  97. }
  98. }
  99. if( !backwardsAdded )
  100. {
  101. backwards.add( 0, () -> {
  102. System.out.println( "Performing Empty Backwards Step..." );
  103. });
  104. }
  105. //current.update();
  106. return calcNextState();
  107. }
  108. private StageStatus calcNextState()
  109. {
  110. if( layerIndex >= graph.getContainedLayers().size() - 1 )
  111. {
  112. if( nodeIndex >= graph.getContainedLayers().get( layerIndex ).size() -1 )
  113. return StageStatus.FINISHED;
  114. }
  115. nodeIndex++;
  116. if( nodeIndex >= graph.getContainedLayers().get( layerIndex ).size() )
  117. {
  118. layerIndex++;
  119. nodeIndex = 0;
  120. int oldR = r;
  121. r = 0;
  122. backwards.add(0, ()->{
  123. this.r = oldR;
  124. });
  125. }
  126. return StageStatus.UNFINISHED;
  127. }
  128. @Override
  129. public StageStatus backwardStep() {
  130. if( subgraphAlgs.get( layerIndex ).get( nodeIndex ) != null )
  131. {
  132. if( subgraphAlgs.get( layerIndex ).get( nodeIndex ).backwardStep() == StageStatus.UNFINISHED )
  133. {
  134. LayeredGraphNode current = graph.getContainedLayers().get( layerIndex ).get( nodeIndex );
  135. current.setSelected();
  136. //current.update();
  137. return StageStatus.UNFINISHED;
  138. }
  139. }
  140. StageStatus status = calcBeforeState();
  141. if( status == StageStatus.FINISHED )
  142. return status;
  143. LayeredGraphNode current = graph.getContainedLayers().get( layerIndex ).get( nodeIndex );
  144. current.setSelected();
  145. //current.update();
  146. if( !backwards.isEmpty() )
  147. {
  148. backwards.get( 0 ).reverse();
  149. backwards.remove( 0 );
  150. }
  151. return status;
  152. }
  153. private StageStatus calcBeforeState()
  154. {
  155. if( layerIndex == 0 )
  156. {
  157. if( nodeIndex == 0 )
  158. return StageStatus.FINISHED;
  159. }
  160. nodeIndex--;
  161. if( nodeIndex < 0 )
  162. {
  163. layerIndex--;
  164. backwards.get( 0 ).reverse();
  165. backwards.remove( 0 );
  166. nodeIndex = graph.getContainedLayers().get( layerIndex ).size() - 1;
  167. }
  168. return StageStatus.UNFINISHED;
  169. }
  170. }