BKNodePlacement.java 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package bk;
  2. import javax.swing.JFrame;
  3. import javax.swing.JTree;
  4. import animation.AnimatedAlgorithm;
  5. import animation.AnimationController;
  6. import animation.CodeLine;
  7. import animation.ControlFlow;
  8. import animation.Memory;
  9. import animation.PseudoCodeNode;
  10. import animation.PseudoCodeProcessor;
  11. import animation.Memory.MemoryType;
  12. import codelines.FunctionCall;
  13. import codelines.FunctionDefinition;
  14. import graph.LayeredGraphNode;
  15. import lib.TextLayoutHelper;
  16. /**
  17. * The main stage of the BK node placement algorithm.
  18. * @author kolja
  19. *
  20. */
  21. public class BKNodePlacement extends AnimatedAlgorithm {
  22. public enum State
  23. {
  24. CONFLICTS,
  25. LAYOUT1,
  26. LAYOUT2,
  27. LAYOUT3,
  28. LAYOUT4,
  29. COMBINE
  30. }
  31. private State state;
  32. public BKNodePlacement(AnimationController controller, LayeredGraphNode graph, JFrame view) {
  33. super(controller, graph, view);
  34. state = State.CONFLICTS;
  35. }
  36. public State getAlgorithmState()
  37. {
  38. return state;
  39. }
  40. public void setAlgorithmState( State s )
  41. {
  42. state = s;
  43. }
  44. @Override
  45. public PseudoCodeNode createPseudocodeTree( JTree tree )
  46. {
  47. String[] vars = { "graph" };
  48. PseudoCodeNode mainFunction = new PseudoCodeNode( TextLayoutHelper.setupPseudoCode("function bkNodePlacement( graph )", vars ), tree, new FunctionDefinition( new String[]{"graph"} ) );
  49. root = new PseudoCodeNode( TextLayoutHelper.setupPseudoCodeStage("-- BK Node Placement Algorithm --" ), tree, new CodeLine() {
  50. @Override
  51. public ControlFlow runForward(Memory m) {
  52. m.declare( "param1", graph, MemoryType.GLOBAL );
  53. if( m.isDefined( "Called", MemoryType.GLOBAL ) )
  54. {
  55. actions.push( (Memory mem) -> {
  56. m.undeclare( "param1", MemoryType.GLOBAL );
  57. } );
  58. return new ControlFlow( ControlFlow.STEP_OVER );
  59. }
  60. m.declare( "Called", true, MemoryType.GLOBAL );
  61. actions.push( (Memory mem) -> {
  62. m.undeclare( "param1", MemoryType.GLOBAL );
  63. m.undeclare( "Called", MemoryType.GLOBAL );
  64. } );
  65. return new ControlFlow( mainFunction );
  66. }
  67. } );
  68. root.setSelected( true );
  69. PseudoCodeNode conflictDetectionFunction = new ConflictDetection( this ).createPseudocodeTree( tree );
  70. PseudoCodeNode node1 = new PseudoCodeNode( TextLayoutHelper.setupPseudoCode( "call detectConflicts( graph )", vars ), tree, new FunctionCall( conflictDetectionFunction, new String[]{ "graph" } ) );
  71. root.add( mainFunction );
  72. mainFunction.add( node1 );
  73. root.add( conflictDetectionFunction );
  74. processor = new PseudoCodeProcessor( root );
  75. return root;
  76. }
  77. }