home *** CD-ROM | disk | FTP | other *** search
/ BURKS 2 / BURKS_AUG97.ISO / BURKS / LANGUAGE / SMALTALK / TEXTBOOK / AP28.ST (.txt) < prev    next >
Text File  |  1997-04-22  |  11KB  |  411 lines

  1.  
  2. 'Smalltalk Textbook Appendix 28'!
  3.  
  4.  
  5.  
  6.  
  7.  
  8. EngiGraph subclass: #EngiDirectedGraph
  9.     instanceVariableNames: ''
  10.     classVariableNames: ''
  11.     poolDictionaries: ''
  12.     category: 'Engi-Graph'!
  13. EngiDirectedGraph comment:
  14. '
  15.  
  16. Engi 0.07 (24 March 1994)
  17. Copyright (C) 1994 by Atsushi Aoki
  18.  
  19. '!
  20.  
  21.  
  22. !EngiDirectedGraph methodsFor: 'testing'!
  23.  
  24. isDirectedGraph
  25.     ^true! !
  26.  
  27. !EngiDirectedGraph methodsFor: 'connecting'!
  28.  
  29. connect: startVertex with: endVertex 
  30.     | aBranch |
  31.     aBranch := self defaultBranch.
  32.     aBranch startVertex: startVertex.
  33.     aBranch endVertex: endVertex.
  34.     (self includesBranch: aBranch)
  35.         ifFalse: [self addBranch: aBranch].
  36.     (self includesVertex: startVertex)
  37.         ifFalse: [self addVertex: startVertex].
  38.     (self includesVertex: endVertex)
  39.         ifFalse: [self addVertex: endVertex]!
  40.  
  41. disconnect: startVertex with: endVertex 
  42.     | aBranch |
  43.     aBranch := self defaultBranch.
  44.     aBranch startVertex: startVertex.
  45.     aBranch endVertex: endVertex.
  46.     (self includesBranch: aBranch)
  47.         ifTrue: [self removeBranch: aBranch]! !
  48.  
  49. !EngiDirectedGraph methodsFor: 'defaults'!
  50.  
  51. defaultBranchClass
  52.     ^EngiBranchWithArrow!
  53.  
  54. defaultVertexClass
  55.     ^EngiVertex! !
  56. "-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!
  57.  
  58. EngiDirectedGraph class
  59.     instanceVariableNames: ''!
  60.  
  61.  
  62. !EngiDirectedGraph class methodsFor: 'instance creation'!
  63.  
  64. new
  65.     ^super new initialize! !
  66.  
  67. !EngiDirectedGraph class methodsFor: 'examples'!
  68.  
  69. example1
  70.     "EngiDirectedGraph example1."
  71.  
  72.     | directedGraph aVertex activeWindow activeSensor previousPoint currentPoint colorValue |
  73.     directedGraph := EngiDirectedGraph exampleGraph.
  74.     aVertex := directedGraph atVertex: 4.
  75.     activeWindow := directedGraph class activeWindow.
  76.     activeWindow clear.
  77.     activeSensor := activeWindow sensor.
  78.     previousPoint := nil.
  79.     [activeSensor anyButtonPressed]
  80.         whileFalse: 
  81.             [currentPoint := activeSensor cursorPoint.
  82.             previousPoint = currentPoint
  83.                 ifFalse: 
  84.                     [previousPoint := currentPoint.
  85.                     aVertex vertexPoint: currentPoint.
  86.                     colorValue := directedGraph class sampleColor.
  87.                     directedGraph doVertices: [:vertex | vertex strokeColor: colorValue].
  88.                     directedGraph doBranches: [:branch | branch strokeColor: colorValue].
  89.                     activeWindow clear.
  90.                     directedGraph display]].
  91.     activeWindow display.
  92.     ^directedGraph!
  93.  
  94. example2
  95.     "EngiDirectedGraph example2."
  96.  
  97.     | directedGraph activeWindow activeSensor |
  98.     directedGraph := EngiDirectedGraph exampleGraph.
  99.     activeWindow := directedGraph class activeWindow.
  100.     activeWindow clear.
  101.     directedGraph
  102.         visitDepthFirstVertexDo: 
  103.             [:vertex | 
  104.             vertex strokeColor: directedGraph class sampleColor.
  105.             vertex display.
  106.             (Delay forMilliseconds: 500) wait]
  107.         branchDo: 
  108.             [:branch | 
  109.             branch strokeColor: directedGraph class sampleColor.
  110.             branch display.
  111.             (Delay forMilliseconds: 500) wait].
  112.     activeSensor := activeWindow sensor.
  113.     [activeSensor anyButtonPressed] whileFalse.
  114.     activeWindow display.
  115.     ^directedGraph!
  116.  
  117. example3
  118.     "EngiDirectedGraph example3."
  119.  
  120.     | directedGraph activeWindow activeSensor |
  121.     directedGraph := EngiDirectedGraph exampleGraph.
  122.     activeWindow := directedGraph class activeWindow.
  123.     activeWindow clear.
  124.     directedGraph
  125.         visitBreadthFirstVertexDo: 
  126.             [:vertex | 
  127.             vertex strokeColor: directedGraph class sampleColor.
  128.             vertex display.
  129.             (Delay forMilliseconds: 500) wait]
  130.         branchDo: 
  131.             [:branch | 
  132.             branch strokeColor: directedGraph class sampleColor.
  133.             branch display.
  134.             (Delay forMilliseconds: 500) wait].
  135.     activeSensor := activeWindow sensor.
  136.     [activeSensor anyButtonPressed] whileFalse.
  137.     activeWindow display.
  138.     ^directedGraph!
  139.  
  140. example4
  141.     "EngiDirectedGraph example4."
  142.  
  143.     | directedGraph activeWindow activeSensor |
  144.     directedGraph := EngiDirectedGraph exampleGraph.
  145.     activeWindow := directedGraph class activeWindow.
  146.     activeWindow clear.
  147.     directedGraph visitBreadthFirstInitialize.
  148.     directedGraph
  149.         visitDepthFirst: (directedGraph atVertex: 7)
  150.         vertexDo: 
  151.             [:vertex | 
  152.             vertex display.
  153.             (Delay forMilliseconds: 500) wait]
  154.         branchDo: 
  155.             [:branch | 
  156.             branch display.
  157.             (Delay forMilliseconds: 500) wait].
  158.     activeSensor := activeWindow sensor.
  159.     [activeSensor anyButtonPressed] whileFalse.
  160.     activeWindow display.
  161.     ^directedGraph!
  162.  
  163. example5
  164.     "EngiDirectedGraph example5."
  165.  
  166.     | directedGraph activeWindow activeSensor |
  167.     directedGraph := EngiDirectedGraph exampleGraph.
  168.     activeWindow := directedGraph class activeWindow.
  169.     activeWindow clear.
  170.     directedGraph visitBreadthFirstInitialize.
  171.     directedGraph
  172.         visitBreadthFirst: (directedGraph atVertex: 7)
  173.         vertexDo: 
  174.             [:vertex | 
  175.             vertex display.
  176.             (Delay forMilliseconds: 500) wait]
  177.         branchDo: 
  178.             [:branch | 
  179.             branch display.
  180.             (Delay forMilliseconds: 500) wait].
  181.     activeSensor := activeWindow sensor.
  182.     [activeSensor anyButtonPressed] whileFalse.
  183.     activeWindow display.
  184.     ^directedGraph!
  185.  
  186. example6
  187.     "EngiDirectedGraph example6."
  188.  
  189.     | directedGraph activeWindow activeSensor |
  190.     directedGraph := EngiDirectedGraph exampleGraph.
  191.     directedGraph arrangeScan.
  192.     activeWindow := directedGraph class activeWindow.
  193.     activeWindow clear.
  194.     directedGraph display.
  195.     activeSensor := activeWindow sensor.
  196.     [activeSensor anyButtonPressed] whileFalse.
  197.     activeWindow display.
  198.     ^directedGraph! !
  199.  
  200.  
  201.  
  202.  
  203.  
  204. EngiGraph subclass: #EngiUndirectedGraph
  205.     instanceVariableNames: ''
  206.     classVariableNames: ''
  207.     poolDictionaries: ''
  208.     category: 'Engi-Graph'!
  209. EngiUndirectedGraph comment:
  210. '
  211.  
  212. Engi 0.07 (24 March 1994)
  213. Copyright (C) 1994 by Atsushi Aoki
  214.  
  215. '!
  216.  
  217.  
  218. !EngiUndirectedGraph methodsFor: 'testing'!
  219.  
  220. isUndirectedGraph
  221.     ^true! !
  222.  
  223. !EngiUndirectedGraph methodsFor: 'connecting'!
  224.  
  225. connect: startVertex with: endVertex 
  226.     | aBranch |
  227.     aBranch := self defaultBranch.
  228.     aBranch startVertex: startVertex.
  229.     aBranch endVertex: endVertex.
  230.     (self includesBranch: aBranch)
  231.         ifFalse: 
  232.             [self addBranch: aBranch.
  233.             self addVertex: startVertex.
  234.             self addVertex: endVertex].
  235.     aBranch := self defaultBranch.
  236.     aBranch startVertex: endVertex.
  237.     aBranch endVertex: startVertex.
  238.     (self includesBranch: aBranch)
  239.         ifFalse: 
  240.             [self addBranch: aBranch.
  241.             self addVertex: startVertex.
  242.             self addVertex: endVertex]!
  243.  
  244. disconnect: startVertex with: endVertex 
  245.     | aBranch |
  246.     aBranch := self defaultBranch.
  247.     aBranch startVertex: startVertex.
  248.     aBranch endVertex: endVertex.
  249.     (self includesBranch: aBranch)
  250.         ifTrue: [self removeBranch: aBranch].
  251.     aBranch := self defaultBranch.
  252.     aBranch startVertex: endVertex.
  253.     aBranch endVertex: startVertex.
  254.     (self includesBranch: aBranch)
  255.         ifTrue: [self removeBranch: aBranch]! !
  256.  
  257. !EngiUndirectedGraph methodsFor: 'defaults'!
  258.  
  259. defaultBranchClass
  260.     ^EngiBranch!
  261.  
  262. defaultVertexClass
  263.     ^EngiVertex! !
  264. "-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!
  265.  
  266. EngiUndirectedGraph class
  267.     instanceVariableNames: ''!
  268.  
  269.  
  270. !EngiUndirectedGraph class methodsFor: 'instance creation'!
  271.  
  272. new
  273.     ^super new initialize! !
  274.  
  275. !EngiUndirectedGraph class methodsFor: 'examples'!
  276.  
  277. example1
  278.     "EngiUndirectedGraph example1."
  279.  
  280.     | undirectedGraph aVertex activeWindow activeSensor previousPoint currentPoint colorValue |
  281.     undirectedGraph := EngiUndirectedGraph exampleGraph.
  282.     aVertex := undirectedGraph atVertex: 4.
  283.     activeWindow := undirectedGraph class activeWindow.
  284.     activeWindow clear.
  285.     activeSensor := activeWindow sensor.
  286.     previousPoint := nil.
  287.     [activeSensor anyButtonPressed]
  288.         whileFalse: 
  289.             [currentPoint := activeSensor cursorPoint.
  290.             previousPoint = currentPoint
  291.                 ifFalse: 
  292.                     [previousPoint := currentPoint.
  293.                     aVertex vertexPoint: currentPoint.
  294.                     colorValue := undirectedGraph class sampleColor.
  295.                     undirectedGraph doVertices: [:vertex | vertex strokeColor: colorValue].
  296.                     undirectedGraph doBranches: [:branch | branch strokeColor: colorValue].
  297.                     activeWindow clear.
  298.                     undirectedGraph display]].
  299.     activeWindow display.
  300.     ^undirectedGraph!
  301.  
  302. example2
  303.     "EngiUndirectedGraph example2."
  304.  
  305.     | undirectedGraph activeWindow activeSensor |
  306.     undirectedGraph := EngiUndirectedGraph exampleGraph.
  307.     activeWindow := undirectedGraph class activeWindow.
  308.     activeWindow clear.
  309.     undirectedGraph
  310.         visitDepthFirstVertexDo: 
  311.             [:vertex | 
  312.             vertex strokeColor: undirectedGraph class sampleColor.
  313.             vertex display.
  314.             (Delay forMilliseconds: 500) wait]
  315.         branchDo: 
  316.             [:branch | 
  317.             branch strokeColor: undirectedGraph class sampleColor.
  318.             branch display.
  319.             (Delay forMill