need to draw finite state machines

Göran Krampe goran at krampe.se
Wed Apr 18 09:16:12 UTC 2007


Hi!

> I am writing an open source project in Squeak for constructing
> finite state machines from regular expressions.  It contains
> a large number of algorithms (I am comparing them to determine
> the best ones).  To help me debug the FSM minimization algorithms
> and for other reasons I want a package that can draw a finite state
> machine
> given the FSM as input.

Just use the Graphviz package, available on SM. We use it in Gjallar btw
and let it produce gifs (but it can produce lots of formats, including
svg), here are some screenshots:

http://www.gjallar.se/gjallar/8

> But I don't want to implement this from scratch.

The code needed is trivial, here is the method building the graphs shown
above:

	| graph stageStyle transitionStyle fillcolor |
	graph := GraphViz new.
	graph beDirected; name: self name.
	stageStyle := {#shape -> #ellipse. #fontsize -> 12. #style -> #filled}.
	graph add: #node with: stageStyle.
	transitionStyle := {#arrowsize -> 1.2. #fontsize -> 10}.
	graph add: #edge with: transitionStyle.
	stages do: [:stage |
		fillcolor := #gray.
		stage isClosed ifTrue: [fillcolor := #palegreen].
		stage isInbox ifTrue: [fillcolor := #red].
		stage isNew ifTrue: [fillcolor := #orange].
		graph add: stage name with: {#fillcolor -> fillcolor. #label -> (stage
name, '\n(',stage numberOfCases asString,')') }.
		graph ].
	self transitionsDo: [:trans |
		graph add: (trans from name -> trans to name) with: {#label -> trans
name}].
	^graph


And then we just do "graph make: #gif" to get it out.

regards, Göran





More information about the Squeak-dev mailing list