1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 # Example of model_visitor
16 module test_model_visitor
23 # Example visitor that just count kind of entities.
24 class TestModelVisitor
30 if not e
isa Model then
31 var name
= e
.full_name
32 var old
= names
.get_or_null
(name
)
34 names
[name
+ "!CONFLICT!" + old
.class_name
] = old
35 name
= name
+ "!CONFLICT!" + e
.class_name
43 # Counter of visited entities (by classnames)
44 var cpt
= new Counter[String]
46 # Dictionary of full_names
47 var names
= new Map[String, MEntity]
50 # The body of the specific work.
51 # The main entry point is provided by `test_phase`,
52 # This function is then automatically (unless errors where found).
53 redef fun do_work
(mainmodule
, given_mmodules
, modelbuilder
)
55 var model
= modelbuilder
.model
57 print
"All entities, including fictive ones:"
58 var v
= new TestModelVisitor
59 v
.min_visibility
= private_visibility
60 v
.include_fictive
= true
62 v
.cpt
.print_elements
(10)
66 v
= new TestModelVisitor
67 v
.min_visibility
= private_visibility
69 v
.cpt
.print_elements
(10)
71 print
"\nAll non-private entities:"
72 v
= new TestModelVisitor
73 v
.min_visibility
= protected_visibility
75 v
.cpt
.print_elements
(10)
77 print
"\nAll documented non-private entities:"
78 v
= new TestModelVisitor
79 v
.min_visibility
= protected_visibility
80 v
.include_empty_doc
= false
82 v
.cpt
.print_elements
(10)
84 print
"\nAll public entities:"
85 v
= new TestModelVisitor
86 v
.min_visibility
= public_visibility
88 v
.cpt
.print_elements
(10)
90 print
"\nAll documented public entities:"
91 v
= new TestModelVisitor
92 v
.min_visibility
= public_visibility
93 v
.include_empty_doc
= false
95 v
.cpt
.print_elements
(10)
98 print
"\n# Classes of entities"
100 cpt
= new Counter[String]
102 cpt
.inc
(e
.class_name
)
105 cpt
.print_elements
(10)
107 print
"\n# Name length of entities"
108 cpt
= new Counter[String]
113 cpt
.print_elements
(10)
115 print
"\n# All entities"
118 var d
= e
.mdoc_or_fallback
119 if d
!= null and d
.content
.not_empty
then c
= d
.content
.first
120 print
"{n}\t{e.class_name}\t{c}"