Brief introduction
Using Clojure to encapsulate the use of Graphviz, the main implementation of UML is currently drawing
Use
Take the UML of command mode as an example to demonstrate the use of Cdraw
Installing Graphviz
Cdraw is a simple package for Graphviz, please first install the Graphviz
Add dependency
- Create a new Clojure project using Leiningen UML
- Add the following dependencies in the PROJECT.CLJ
[com.ivaneye/cdraw "0.2.0"]
Defining classes
- Write the following code in Uml.core
(ns uml.core (: Require [cdraw.uml : Refer :all])) (defclass Client) (defclass inboker) (defclass Receiver {: M ["Action ()"]}) (defclass Command {: M ["Execute ()"]}) (defclass concretecommand {: F["state"] : M ["Execute () "]} )
- The first, two lines, introduced the Cdraw
- Defclass defines the class, its fields (: f), and the method (: M), as follows:
Add dependency
- Continue to add dependent code in Uml.core
(defrelati On Client :u Receiver) (defrelation concretecommand :u Receiver {:t " Reveiver "} ) (defrelation Client :d concretecommand) (defrelation Concretecommand :e Command) ( defrelation Invoker :p Command)
- Client associated receiver
- Concretecommand Associated Receiver
- Client Dependent Concretecommand
- Concretecommand inherit command
- Invoker Aggregation Command
关联 :u:d:p:c:e:i
Add label
- Add the following code in the Uml.core
(label ConcreteCommand "receive-\\>Action()")
Define a child package
(defsub "Sub Command" Command ConcreteCommand)
- The first parameter is a child package name
- Follow the classes that need to be included in the child package
Generated
- Add the following code in the Uml.core
(watch (to-file "/t.dot") "/t.png")
- To-file generate Graphviz-compliant dot file
- Watch generates the desired final file, here is the PNG image generated
Final code
(ns uml.core (: Require [cdraw.uml : Refer :all]))(defclass Client)(defclass Invoker)(defclass Receiver {: M ["Action ()"]})(defclass Command {: M ["Execute ()"]})(defclass concretecommand {: F["state"] : M [" Execute () "]} )(defrelation Client : U Receiver)(defrelation concretecommand : U Receiver {: t "Reveiver"})(defrelation Client :d concretecommand)(defrelation concretecommand : E Command)(defrelation Invoker :p Command)(label concretecommand "receive-\\>action ()")(defsub "Sub command" command Concretecommand)(watch (to-file "/t.dot") "/t.png")
Clojure Drawing UML