Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #################################################################
- ## A short julia program that generates the graph of all subtypes
- ## of a given type, in png format. Uses graphviz.
- ## Example: makegraph(Number, "result.dot")
- ## returns a png file named result.dot.png that contains the Number subgraph.
- ## https://imgur.com/a/CkoOA#0
- ## Applies recursively a function fn to typ and its subtypes.
- ## The collection of all encountered types are returned.
- ## Example: length(typeit((x,y)-> println("$x -> $y"), Number))
- function typeit(fn, typ)
- let seen = Dict{Type,Bool}() # returns true for already seen types
- doit = function rec (fn, tp)
- if ~get(seen,tp,false)
- seen[tp] = true
- for child in subtypes(tp)
- fn(tp,child)
- rec(fn, child)
- end
- end
- end
- doit(fn, typ)
- collect(keys(seen))
- end
- end
- ## graphviz doesn't like non alphanumeric characters.
- function edge (typ1, typ2)
- if all(isalnum,string(typ1)) & all(isalnum,string(typ2))
- "$typ1 -> $typ2;\n"
- else ""
- end
- end
- ## Produces two files named file and file.png. file is in graphviz dot format.
- ## The png file contains the graph of the subtypes of the IO type.
- function makegraph(typ, file)
- fh = open(file, "w")
- write(fh, "digraph{\n")
- typeit((x,y)-> write(fh, edge(x,y)), typ)
- write(fh,"}")
- close(fh)
- run(`dot -Tpng -O $file`) # graphviz needed here.
- print("$file.png")
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement