Phil Trelford's Array
POKE 36879, 255

UML Sequence Diagram: F# Script

April 26, 2010 23:55 by Phil

A UML Sequence diagram can be a great tool for modelling interaction:

UML sequence diagrams model the flow of logic within your system in a visual manner, enabling you both to document and validate your logic, and are commonly used for both analysis and design purposes.  Sequence diagrams are the most popular UML artifact for dynamic modeling, which focuses on identifying the behavior within your system.

Yesterday I fancied moving some hand drawn diagrams to mouse drawn form; but instead found myself battling against a somewhat stubborn and forgetful modelling tool. It left me wondering if there might be an easier way to do this; perhaps by separating the concern of model description from layout. A few moments later I had a description language using an F# class and an F# discriminated union:

type Object(name) =     
    member this.Name = name    

type SequenceAction =
    | Activate of Object
    | Deactivate of Object
    | MessageCall of string * Object * Object
    | MessageAsync of string * Object * Object

Together the Object and SequenceAction types describe an internal Domain Specific Language (DSL) for Sequence Diagrams.

The following is a simple restaurant sample definition (taken from Wikipedia) using the DSL:

let fred = Object("Fred\r\nPatron")
let bob = Object("Bob\r\nWaiter")
let hank = Object("Hank\r\nCook")
let renee = Object("Renee\r\nCashier")
let objects = [fred;bob;hank;renee]   
let actions = 
    [
    MessageCall("order food",fred,bob)
    MessageCall("order food",bob,hank)
    MessageCall("serve wine",bob,fred)
    MessageCall("pickup",hank,bob)
    MessageCall("serve food",bob,fred)
    MessageCall("pay",fred,renee)
    ]

 

Onto the layout, and the .Net Framework has the necessary batteries included to easily do this using either WPF or Silverlight with the:

To get started I used Visual Studio 2010’s WPF XAML designer for prototyping the layout. Then several hours of hacking later I had an F# script (attached) for drawing basic diagrams.

The following was generated using the simple restaurant description above:

simple restaurant uml sequence diagram sample

The colouring of the objects was achieved by extending the Object to allow arbitary dynamic properties to be set which can later be fed to the control:

type DynamicObject () =
    let properties = System.Collections.Specialized.HybridDictionary()
    member this.Item 
        with get (index:string) = properties.[index] 
        and set (index:string) (value:obj) = properties.[index] <- value
    member this.Items =        
        let keys = seq { for key in properties.Keys do yield key :?> string }
        let values = seq { for value in properties.Values do yield value }
        Seq.zip keys values

// Define dynamic lookup get and set operators
let (?) (o:DynamicObject) (property:string) = 
    o.[property]
let (?<-) (o:DynamicObject) (property:string) (value:'a) = 
    o.[property] <- value

type Object(name) =  
    inherit DynamicObject()   
    member this.Name = name    

 

The object’s background colours are set thus:

fred?Background <- Brushes.LightGreen
bob?Background <- Brushes.LightBlue
hank?Background <- Brushes.LightBlue
renee?Background <- Brushes.Pink

 

Conclusion

Whiteboard and paper are still probably the best tools for modelling sequence diagrams. However the DSL approach does have the advantage of allowing easy insertion of objects and messages without the pain of manual layout (constant erasing and redrawing).

Resources

SequenceDiagram.fsx (11.97 kb)


Tags:
Categories: .Net | Architecture | F# | UML
Actions: E-mail | Permalink | Comments (6) | Comment RSSRSS comment feed

Comments

April 27. 2010 00:19

pingback

Pingback from topsy.com

Twitter Trackbacks for
        
        UML Sequence Diagram: F# Script
        [trelford.com]
        on Topsy.com

topsy.com

April 27. 2010 05:20

trackback

Social comments and analytics for this post

This post was mentioned on Twitter by ptrelford: UML Sequence Diagrams from an #fsharp script using internal DSL for description and WPF for layout: http://tinyurl.com/seqdiag

uberVU - social comments

April 27. 2010 06:18

pingback

Pingback from alvinashcraft.com

Dew Drop – April 27, 2010 | Alvin Ashcraft's Morning Dew

alvinashcraft.com

May 2. 2010 14:43

trackback

F# Discoveries This Week 05/02/2010

Back this week with another huge batch of F# links.  While there’s way too much great stuff to mention,

Rick Minerich's Development Wonderland

May 26. 2012 12:45

trackback

GOTO Copenhagen 2012 Conference

GOTO Copenhagen 2012 Conference

Phil Trelford's Array

July 5. 2012 17:44

pingback

Pingback from listofteamsports.mothersforafreeamerica.com

1997 Coach House 192ks – Rainbow city, AL | List of Team Sports

listofteamsports.mothersforafreeamerica.com

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading