BlockCalc.java 7.4 KB

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