Advertisement
Guest User

julialang type graph generation

a guest
Sep 27th, 2013
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. #################################################################
  2. ## A short julia program that generates the graph of all subtypes
  3. ## of a given type, in png format. Uses graphviz.
  4. ## Example: makegraph(Number, "result.dot")
  5. ## returns a png file named result.dot.png that contains the Number subgraph.
  6. ## https://imgur.com/a/CkoOA#0
  7.  
  8. ## Applies recursively a function fn to typ and its subtypes.
  9. ## The collection of all encountered types are returned.
  10. ## Example: length(typeit((x,y)-> println("$x -> $y"), Number))
  11. function typeit(fn, typ)
  12. let seen = Dict{Type,Bool}() # returns true for already seen types
  13. doit = function rec (fn, tp)
  14. if ~get(seen,tp,false)
  15. seen[tp] = true
  16. for child in subtypes(tp)
  17. fn(tp,child)
  18. rec(fn, child)
  19. end
  20. end
  21. end
  22. doit(fn, typ)
  23. collect(keys(seen))
  24. end
  25. end
  26.  
  27. ## graphviz doesn't like non alphanumeric characters.
  28. function edge (typ1, typ2)
  29. if all(isalnum,string(typ1)) & all(isalnum,string(typ2))
  30. "$typ1 -> $typ2;\n"
  31. else ""
  32. end
  33. end
  34.  
  35. ## Produces two files named file and file.png. file is in graphviz dot format.
  36. ## The png file contains the graph of the subtypes of the IO type.
  37. function makegraph(typ, file)
  38. fh = open(file, "w")
  39. write(fh, "digraph{\n")
  40. typeit((x,y)-> write(fh, edge(x,y)), typ)
  41. write(fh,"}")
  42. close(fh)
  43. run(`dot -Tpng -O $file`) # graphviz needed here.
  44. print("$file.png")
  45. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement