PostgresV3 Protocol State Machine using GraphViz

28 Apr 2013

In the previous post I wrote about visualizing the PostgresV3 pure Smalltalk implementation of the PostgreSQL v3 wire protocol. That "visualization" was in text, which is not nearly as visual as seeing the protocol state machine graphically.

In this post, let's use GraphViz to do that. Building on the code previously written:

| skip states statesMap gv |

skip := #('PG3NoticeResponse' 'PG3NotificationResponse' 'PG3ParameterStatus').
states := OrderedCollection new.

PG3ServerState createStateGraph valuesDo: [ :inst |
  inst transitions keysAndValuesDo: [ :k :v |
    (skip includes: k asString) ifFalse: [
      states add: (Array with: inst with: k with: v first with: v second) ]]].

states do: [ :ea |
  Transcript show: 'From ', ea first name; 
    show: ' on ', ea second asString.
  ea fourth ifNotNil: [ Transcript show: ' perform #', ea fourth ].
  Transcript show: ' goto ', ea third name; cr; flush ].

statesMap := Dictionary new.
states do: [ :ea |
  (statesMap includesKey: ea first name) ifFalse: [
    statesMap at: ea first name put: OrderedCollection new ].
  (statesMap at: ea first name) add: ea ].

gv := GraphViz new.
gv beDirected;
  name: 'PostgresV3 Protocol State Machine';
  add: #graph with: { #overlap -> #scale. #concentrate -> #true };
  add: #edge with: { #arrowsize -> 0.5 }.
	
statesMap keysAndValuesDo: [ :k :v |
  gv add: k with: { #shape -> #box. #fontsize -> 10 }.
  v do: [ :ea |
    gv add: ea third name with: { #shape -> #box. #fontsize -> 10 }.
    gv add: k -> ea third name with: { #label -> ea second asString. #fontsize -> 8 } ]].

gv openInWindow.
PostgresV3 State Machine Tags: GraphViz, PostgreSQL, visualization