src: use `as_notnullable` in code
[nit.git] / src / model_viz.nit
index 6b6db4c..20be8c3 100644 (file)
@@ -30,7 +30,7 @@ class MProjectTree
 
        redef fun display(a) do
                if a isa MGroup then
-                       if a.parent == null then return "{a.mproject.name} ({a.filepath})"
+                       if a.parent == null then return "{a.mproject.name} ({a.filepath.to_s})"
                        return a.name + " (group)"
                else if a isa MModule then
                        return a.name
@@ -39,9 +39,7 @@ class MProjectTree
                end
        end
 
-       var alpha_comparator = new AlphaComparator
-
-       var linex_comparator: nullable LinexComparator = null
+       private var linex_comparator: nullable LinexComparator = null
 
        # Sort modules and groups with their names
        fun sort_with_alpha
@@ -61,16 +59,10 @@ class MProjectTree
        end
 end
 
-# Just compare objects by using the `to_s` method
-private class AlphaComparator
-       super AbstractSorter[Object]
-       redef fun compare(a,b) do return a.to_s <=> b.to_s
-end
-
 # Compare modules and groups using the
 # FIXME do not use Object, but a better common interface of MModule and MGroup
 private class LinexComparator
-       super AbstractSorter[Object]
+       super Comparator[Object]
        var mins = new HashMap [MGroup, nullable MModule]
        var maxs = new HashMap [MGroup, nullable MModule]
        fun min(o: Object): nullable MModule do
@@ -143,6 +135,8 @@ end
 # Interessing elements must be selected. See `mmodules`, ``
 # Display configuration can be set. See `cluster_group`, `project_group`
 class MProjectDot
+       super Streamable
+
        # The model where to look for information
        var model: Model
 
@@ -154,7 +148,7 @@ class MProjectDot
        # Initially empty, modules can be added
        var mmodules = new HashSet[MModule]
 
-       private fun node_for(mmodule: MModule): nullable String
+       private fun node_for(mmodule: MModule): String
        do
                return "m_{mmodule.object_id}"
        end
@@ -172,7 +166,7 @@ class MProjectDot
                if mgroup.parent == null then
                        # is is a root group, so display the project
                        if project_group then
-                               o.write("subgraph cluster_{mgroup.object_id} \{\nlabel=\"{mgroup.mproject.name}\\n({mgroup.filepath})\"\ncolor=black\nstyle=dotted\n")
+                               o.write("subgraph cluster_{mgroup.object_id} \{\nlabel=\"{mgroup.mproject.name}\\n({mgroup.filepath.to_s})\"\ncolor=black\nstyle=dotted\n")
                        end
                else
                        if cluster_group then
@@ -218,8 +212,8 @@ class MProjectDot
                end
        end
 
-       # Generate the dot-file named `filepath` with the current configuration
-       fun render(filepath: String)
+       # Generate the dot content with the current configuration
+       redef fun write_to(stream)
        do
                # Collect interessing nodes
                for m in model.mmodules do
@@ -243,13 +237,11 @@ class MProjectDot
                        end
                end
 
-               print "generating {filepath}"
-               var dot = new OFStream.open(filepath)
-               dot.write("digraph g \{\n")
-               dot.write("rankdir=BT;node[shape=box];\n")
+               stream.write("digraph g \{\n")
+               stream.write("rankdir=BT;node[shape=box];\n")
                # Generate the nodes
                for p in model.mprojects do
-                       dot_cluster(dot, p.root.as(not null))
+                       dot_cluster(stream, p.root.as(not null))
                end
                # Generate the edges
                for m in mmodules do
@@ -257,14 +249,12 @@ class MProjectDot
                                var nm = node_for(m)
                                var nsm = node_for(sm)
                                if m.in_importation.direct_greaters.has(sm) then
-                                       dot.write("\t{nm} -> {nsm}[style=bold]\n")
+                                       stream.write("\t{nm} -> {nsm}[style=bold]\n")
                                else
-                                       dot.write("\t{nm} -> {nsm}[style=solid]\n")
+                                       stream.write("\t{nm} -> {nsm}[style=solid]\n")
                                end
                        end
                end
-               dot.write("\}\n")
-               dot.close
-               # sys.system("xdot -f dot {filepath}")
+               stream.write("\}\n")
        end
 end