Skip to content

Instantly share code, notes, and snippets.

@kurogelee
Created May 14, 2014 19:55
Show Gist options
  • Save kurogelee/0556e474365563ed0a6e to your computer and use it in GitHub Desktop.
Save kurogelee/0556e474365563ed0a6e to your computer and use it in GitHub Desktop.
dorothyの魔法でクラス図を描く ref: http://qiita.com/kurogelee/items/aa5bb531ff87de0fe99e
(ns classzu.core
(:require [dorothy.core :refer :all]
[clojure.set :as set]
[clojure.walk :as walk]))
(defn show-diagram [& class-defs]
(->> (for [[class extends & implements] class-defs]
(concat [class]
(when extends [[class extends]])
(map (fn [i] [class i {:style :dashed}]) implements)))
(apply concat)
(list* (node-attrs {:shape :record})
(edge-attrs {:arrowhead :empty}))
digraph dot show!))
(show-diagram [:classA :classB :interface1 :interface2] [:classZ nil :interface9])
(-> [[:node1 {:style :filled :shape :record}] :node2
[:a :b :c :d :a {:arrowhead :empty}]
[:e :f]]
digraph dot show!)
(-> [(node-attrs {:color :red})
(edge-attrs {:style :dashed})
[:node1 {:style :filled :shape :record}] :node2
[:a :b :c :d :a {:arrowhead :empty}]
[:e :f]]
digraph dot show!)
(-> [(node-attrs {:color :red})
(edge-attrs {:style :dashed})
[:node1 {:style :filled :shape :record}] :node2
[:a :b :c :d :a {:arrowhead :empty}]
[:e :f]]
digraph dot (save! "out.png" {:format :png}))
(defn get-classdef [^Class c]
(->> (list* c (.getSuperclass c) (apply set/difference (supers c) #{(.getSuperclass c)} (map supers (supers c))))
(walk/postwalk #(if (or (nil? %) (sequential? %)) % (keyword (.getSimpleName %))))))
(defn get-all-classdefs [& classes]
(distinct (concat (map get-classdef classes)
(mapcat #(apply get-all-classdefs (supers %)) classes))))
(import '[java.util ArrayList LinkedList])
(apply show-diagram (get-all-classdefs ArrayList LinkedList))
(apply show-diagram (apply get-all-classdefs (map class [() '(1) (range 1) (seq "a") (cons 1 [])
(seq {1 2}) (keys {1 2}) (vals {1 2})])))
(ns classzu.sample
(:require [dorothy.core :refer :all]))
(-> [[:node1] :node2
[:node1 :node2]]
graph dot show!)
(-> [[:node1] :node2
[:a :b :c :d :a]]
digraph dot show!)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment