ivaneye.com

Clojure绘制UML

简介

使用Clojure封装了Graphviz的使用,目前主要实现了UML的绘制

使用

以命令模式的UML为例,演示cdraw的使用

安装Graphviz

cdraw是对Graphviz的简单封装,请先安装Graphviz

添加依赖

[com.ivaneye/cdraw "0.2.0"]

定义类

(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()"]})

添加依赖

(defrelation Client :u Receiver) 
(defrelation ConcreteCommand :u Receiver {:t "reveiver"})  
(defrelation Client :d ConcreteCommand)
(defrelation ConcreteCommand :e Command)
(defrelation Invoker :p Command)
关联 :u 
依赖 :d
聚合 :p
组合 :c
继承 :e
实现 :i

添加label

(label ConcreteCommand "receive->Action()")

定义子包

(defsub "Sub Command" Command ConcreteCommand)

生成

(watch (to-file "/t.dot") "/t.png")

最终代码

(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")