nituml: use ModelView
authorAlexandre Terrasa <alexandre@moz-code.org>
Wed, 16 Dec 2015 05:54:12 +0000 (00:54 -0500)
committerAlexandre Terrasa <alexandre@moz-code.org>
Sat, 19 Dec 2015 05:55:17 +0000 (00:55 -0500)
Signed-off-by: Alexandre Terrasa <alexandre@moz-code.org>

src/nituml.nit
src/uml/uml_base.nit
src/uml/uml_class.nit
src/uml/uml_module.nit
tests/sav/nituml_args4.res

index 9ea9048..330370f 100644 (file)
@@ -20,12 +20,19 @@ import frontend
 import uml
 
 redef class ToolContext
+
+       # Phase that generates UML diagrams from model entities.
        var umlphase: Phase = new UMLPhase(self, null)
 
+       # What to generate?
        var opt_gen = new OptionEnum(["class", "package"], "Choose which type of uml diagram to generate", 0, "--diagram")
 
+       # Generate private?
+       var opt_privacy = new OptionBool("Generates private API", "-p", "--private")
+
        redef init do
                option_context.add_option opt_gen
+               option_context.add_option opt_privacy
                super
        end
 end
@@ -34,7 +41,12 @@ private class UMLPhase
        super Phase
        redef fun process_mainmodule(mainmodule, mmodules)
        do
-               var d = new UMLModel(mainmodule.model, mainmodule, toolcontext)
+               var view = new ModelView(mainmodule.model)
+               if not toolcontext.opt_privacy.value then
+                       view.min_visibility = protected_visibility
+               end
+
+               var d = new UMLModel(view, mainmodule)
                if toolcontext.opt_gen.value == 0 then
                        print d.generate_class_uml.write_to_string
                else if toolcontext.opt_gen.value == 1 then
index 3cb5e92..2580d95 100644 (file)
 module uml_base
 
 import toolcontext
-import model
+import model::model_collect
 
-redef class ToolContext
-       # -p
-       var opt_privacy = new OptionBool("Generates private API", "-p", "--private")
-
-       # Shortcut for the value of `self.opt_privacy`
-       fun private_gen: Bool do return opt_privacy.value
+# UML model builder.
+class UMLModel
 
-       redef init do
-               option_context.add_option opt_privacy
-               super
-       end
-end
+       # Model view
+       var view: ModelView
 
-class UMLModel
-       var model: Model
+       # Main module used for linearization.
        var mainmodule: MModule
-       var ctx: ToolContext
 end
index aa93740..c6d4870 100644 (file)
@@ -35,35 +35,23 @@ redef class UMLModel
                                        fontname = "Bitstream Vera Sans"
                                        fontsize = 8
                                ]\n"""
-               tpl.add model.tpl_class(ctx, mainmodule)
+               for mclass in view.mclasses do
+                       tpl.add mclass.tpl_class(self)
+                       tpl.add "\n"
+               end
                tpl.add "\}"
                return tpl
        end
 end
 
-redef class Model
-
-       # Generates a UML Class diagram from the entities of a `Model`
-       redef fun tpl_class(ctx, main) do
-               var t = new Template
-               for i in mclasses do
-                       if not ctx.private_gen and i.visibility != public_visibility then continue
-                       t.add i.tpl_class(ctx, main)
-                       t.add "\n"
-               end
-               return t
-       end
-
-end
-
 redef class MEntity
        # Generates a dot-compatible `Writable` UML Class diagram from `self`
-       fun tpl_class(ctx: ToolContext, main: MModule): Writable is abstract
+       fun tpl_class(model: UMLModel): Writable is abstract
 end
 
 redef class MClass
 
-       redef fun tpl_class(ctx, main) do
+       redef fun tpl_class(model) do
                var t = new Template
                t.add "{name} [\n label = \"\{"
                if kind == abstract_kind then
@@ -83,33 +71,22 @@ redef class MClass
                        t.add "]"
                end
                t.add "|"
-               var props: Collection[MProperty]
-               if ctx.private_gen then
-                       props = collect_intro_mproperties(none_visibility)
-               else
-                       props = collect_intro_mproperties(public_visibility)
-               end
+               var props = collect_intro_mproperties(model.view)
                for i in props do
-                       if i isa MAttribute then
-                               t.add i.tpl_class(ctx, main)
-                               t.add "\\l"
-                       end
+                       if not i isa MAttribute then continue
+                       t.add i.tpl_class(model)
+                       t.add "\\l"
                end
                t.add "|"
-               var meths
-               if ctx.private_gen then
-                       meths = collect_intro_mmethods(none_visibility)
-               else
-                       meths = collect_intro_mmethods(public_visibility)
-               end
-               for i in meths do
-                       t.add i.tpl_class(ctx, main)
+               for i in props do
+                       if not i isa MMethod then continue
+                       t.add i.tpl_class(model)
                        t.add "\\l"
                end
                t.add "\}\"\n]\n"
-               var g = in_hierarchy(main).direct_greaters
+               var g = in_hierarchy(model.mainmodule).direct_greaters
                for i in g do
-                       if not ctx.private_gen and i.visibility != public_visibility then continue
+                       if not model.view.accept_mentity(i) then continue
                        t.add "{i.name} -> {name} [dir=back"
                        if i.kind == interface_kind then
                                t.add " arrowtail=open style=dashed"
@@ -124,19 +101,19 @@ redef class MClass
 end
 
 redef class MMethod
-       redef fun tpl_class(ctx, main) do
+       redef fun tpl_class(model) do
                var tpl = new Template
                tpl.add visibility.tpl_class
                tpl.add " "
                tpl.add name.escape_to_dot
-               tpl.add intro.msignature.tpl_class(ctx, main)
+               tpl.add intro.msignature.tpl_class(model)
                return tpl
        end
 end
 
 redef class MSignature
 
-       redef fun tpl_class(ctx, main) do
+       redef fun tpl_class(model) do
                var t = new Template
                t.add "("
                var params = new Array[MParameter]
@@ -146,31 +123,31 @@ redef class MSignature
                if params.length > 0 then
                        t.add params.first.name
                        t.add ": "
-                       t.add params.first.mtype.tpl_class(ctx, main)
+                       t.add params.first.mtype.tpl_class(model)
                        for i in [1 .. params.length [ do
                                t.add ", "
                                t.add params[i].name
                                t.add ": "
-                               t.add params[i].mtype.tpl_class(ctx, main)
+                               t.add params[i].mtype.tpl_class(model)
                        end
                end
                t.add ")"
                if return_mtype != null then
                        t.add ": "
-                       t.add return_mtype.tpl_class(ctx, main)
+                       t.add return_mtype.tpl_class(model)
                end
                return t
        end
 end
 
 redef class MAttribute
-       redef fun tpl_class(ctx, main) do
+       redef fun tpl_class(model) do
                var tpl = new Template
                tpl.add visibility.tpl_class
                tpl.add " "
                tpl.add name
                tpl.add ": "
-               tpl.add intro.static_mtype.tpl_class(ctx, main)
+               tpl.add intro.static_mtype.tpl_class(model)
                return tpl
        end
 end
@@ -194,20 +171,20 @@ redef class MVisibility
 end
 
 redef class MClassType
-       redef fun tpl_class(c, m) do
+       redef fun tpl_class(model) do
                return name
        end
 end
 
 redef class MGenericType
-       redef fun tpl_class(c, m) do
+       redef fun tpl_class(model) do
                var t = new Template
                t.add name.substring(0, name.index_of('['))
                t.add "["
-               t.add arguments.first.tpl_class(c, m)
+               t.add arguments.first.tpl_class(model)
                for i in [1 .. arguments.length[ do
                        t.add ", "
-                       t.add arguments[i].tpl_class(c, m)
+                       t.add arguments[i].tpl_class(model)
                end
                t.add "]"
                return t
@@ -215,22 +192,22 @@ redef class MGenericType
 end
 
 redef class MParameterType
-       redef fun tpl_class(c, m) do
+       redef fun tpl_class(model) do
                return name
        end
 end
 
 redef class MVirtualType
-       redef fun tpl_class(c, m) do
+       redef fun tpl_class(model) do
                return name
        end
 end
 
 redef class MNullableType
-       redef fun tpl_class(c, m) do
+       redef fun tpl_class(model) do
                var t = new Template
                t.add "nullable "
-               t.add mtype.tpl_class(c, m)
+               t.add mtype.tpl_class(model)
                return t
        end
 end
index eda98e3..1720ff0 100644 (file)
@@ -34,38 +34,31 @@ redef class UMLModel
                                fontname = "Bitstream Vera Sans"
                                fontsize = 8
                        ]\n"""
-               tpl.add model.tpl_module(ctx, mainmodule)
+               tpl.add mainmodule.tpl_module(self)
                tpl.add "\}"
                return tpl
        end
 end
 
-redef class Model
-       # Returns a UML package diagram of `main`
-       redef fun tpl_module(ctx, main) do
-               return main.tpl_module(ctx, main)
-       end
+redef class MEntity
+       # Builds a dot UML package diagram entity from `self`
+       fun tpl_module(model: UMLModel): Writable is abstract
 end
 
 redef class MModule
-       redef fun tpl_module(ctx, main) do
+       redef fun tpl_module(model) do
                var t = new Template
                t.add "subgraph cluster{name} \{\n"
                t.add "label = \"{name}\"\n"
                for i in mclassdefs do
-                       if not ctx.private_gen and i.mclass.visibility != public_visibility then continue
-                       t.add i.tpl_module(ctx, main)
+                       if not model.view.accept_mentity(i) then continue
+                       t.add i.tpl_module(model)
                end
                t.add "\}\n"
                return t
        end
 end
 
-redef class MEntity
-       # Builds a dot UML package diagram entity from `self`
-       fun tpl_module(ctx: ToolContext, main: MModule): Writable is abstract
-end
-
 redef class MClassDef
 
        # Colour for the border of a class when first introduced
@@ -78,7 +71,7 @@ redef class MClassDef
        # Defaults to a shade of red
        var redef_colour = "#B24758"
 
-       redef fun tpl_module(ctx, main) do
+       redef fun tpl_module(model) do
                var t = new Template
                t.add "{mmodule}{name} [\n\tlabel = \"\{"
                if mclass.kind == abstract_kind then
@@ -101,15 +94,15 @@ redef class MClassDef
                t.add "|"
                for i in mpropdefs do
                        if not i isa MAttributeDef then continue
-                       if not ctx.private_gen and i.mproperty.visibility != public_visibility then continue
-                       t.add i.tpl_module(ctx, main)
+                       if not model.view.accept_mentity(i) then continue
+                       t.add i.tpl_module(model)
                        t.add "\\l"
                end
                t.add "|"
                for i in mpropdefs do
                        if not i isa MMethodDef then continue
-                       if not ctx.private_gen and i.mproperty.visibility != public_visibility then continue
-                       t.add i.tpl_module(ctx, main)
+                       if not model.view.accept_mentity(i) then continue
+                       t.add i.tpl_module(model)
                        t.add "\\l"
                end
                t.add "\}\""
@@ -135,24 +128,24 @@ redef class MClassDef
 end
 
 redef class MMethodDef
-       redef fun tpl_module(ctx, main) do
+       redef fun tpl_module(model) do
                var t = new Template
                t.add mproperty.visibility.tpl_class
                t.add " "
                t.add name.escape_to_dot
-               t.add msignature.tpl_class(ctx, main)
+               t.add msignature.tpl_class(model)
                return t
        end
 end
 
 redef class MAttributeDef
-       redef fun tpl_module(ctx, main) do
+       redef fun tpl_module(model) do
                var t = new Template
                t.add mproperty.visibility.tpl_class
                t.add " "
                t.add name
                t.add ": "
-               t.add static_mtype.tpl_class(ctx, main)
+               t.add static_mtype.tpl_class(model)
                return t
        end
 end
index c846cb7..181046e 100644 (file)
@@ -78,7 +78,7 @@ Task [
 Object -> Task [dir=back arrowtail=open style=dashed];
 
 A [
- label = "{A||+ pubA(a: A)\l+ pubA2(): A\l+ vpubA(): nullable A\l+ vpubA=(vpubA: nullable A)\l+ vpubA2(): A\l+ vpubA2=(vpubA2: A)\l}"
+ label = "{A||+ pubA(a: A)\l# proA(a: A)\l+ pubA2(): A\l# proA2(): A\l+ vpubA(): nullable A\l+ vpubA=(vpubA: nullable A)\l# vproA(): nullable A\l# vproA=(vproA: nullable A)\l+ vpubA2(): A\l+ vpubA2=(vpubA2: A)\l# vproA2(): A\l# vproA2=(vproA2: A)\l}"
 ]
 Object -> A [dir=back arrowtail=open style=dashed];