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.
19 # Provide a configurable view to a model.
21 # This can be useful when you need to filter some mentities from a model
22 # like private or fictive.
28 # The model to view through `self`.
31 # MPackages visible through `self`.
32 var mpackages
: Set[MPackage] is lazy
do
33 var mpackages
= new HashSet[MPackage]
34 for mpackage
in model
.mpackages
do
35 if not accept_mentity
(mpackage
) then continue
36 mpackages
.add mpackage
41 # MGroups visible through `self`.
42 var mgroups
: Set[MGroup] is lazy
do
43 var mgroups
= new HashSet[MGroup]
44 for mpackage
in mpackages
do
45 for mgroup
in mpackage
.mgroups
do
46 if not accept_mentity
(mgroup
) then continue
53 # MModules visible through `self`.
54 var mmodules
: Set[MModule] is lazy
do
55 var mmodules
= new HashSet[MModule]
56 for mmodule
in model
.mmodules
do
57 if not accept_mentity
(mmodule
) then continue
63 # MClasses visible through `self`.
64 var mclasses
: Set[MClass] is lazy
do
65 var mclasses
= new HashSet[MClass]
66 for mclass
in model
.mclasses
do
67 if not accept_mentity
(mclass
) then continue
73 # MClassDefs visible through `self`.
74 var mclassdefs
: Set[MClassDef] is lazy
do
75 var mclassdefs
= new HashSet[MClassDef]
76 for mclass
in mclasses
do
77 for mclassdef
in mclass
.mclassdefs
do
78 if not accept_mentity
(mclassdef
) then continue
79 mclassdefs
.add mclassdef
85 # MProperties visible through `self`.
86 var mproperties
: Set[MProperty] is lazy
do
87 var mproperties
= new HashSet[MProperty]
88 for mproperty
in model
.mproperties
do
89 if not accept_mentity
(mproperty
) then continue
90 mproperties
.add mproperty
95 # MPropdefs visible through `self`.
96 var mpropdefs
: Set[MPropDef] is lazy
do
97 var mpropdefs
= new HashSet[MPropDef]
98 for mproperty
in mproperties
do
99 for mpropdef
in mproperty
.mpropdefs
do
100 if not accept_mentity
(mpropdef
) then continue
101 mpropdefs
.add mpropdef
107 # Lists all MEntities visible through `self`.
108 var mentities
: Set[MEntity] is lazy
do
109 var res
= new HashSet[MEntity]
110 res
.add_all mpackages
114 res
.add_all mclassdefs
115 res
.add_all mproperties
116 res
.add_all mpropdefs
120 private fun init_visitor
(v
: ModelVisitor) do
121 v
.min_visibility
= self.min_visibility
122 v
.include_fictive
= self.include_fictive
123 v
.include_empty_doc
= self.include_empty_doc
124 v
.include_attribute
= self.include_attribute
125 v
.include_test_suite
= self.include_test_suite
128 # Searches MEntities that match `name`.
129 fun mentities_by_name
(name
: String): Array[MEntity] do
130 var res
= new Array[MEntity]
131 for mentity
in mentities
do if mentity
.name
== name
then res
.add mentity
135 # Searches the MEntity that matches `full_name`.
136 fun mentity_by_full_name
(full_name
: String): nullable MEntity do
137 for mentity
in mentities
do
138 if mentity
.full_name
== full_name
then return mentity
143 # Looks up a MEntity by its full `namespace`.
145 # Usefull when `mentities_by_name` returns conflicts.
147 # Namespaces must be of the form `package::core::module::Class::prop`.
148 fun mentities_by_namespace
(namespace
: String): Array[MEntity] do
149 var v
= new LookupNamespaceVisitor(namespace
)
151 for mpackage
in mpackages
do
152 v
.enter_visit
(mpackage
)
157 # Build an concerns tree with from `self`
158 fun to_tree
: MEntityTree do
159 var v
= new ModelTreeVisitor
161 for mpackage
in mpackages
do
162 v
.enter_visit
(mpackage
)
167 # Build the POSet of `mmodules` importation.
168 fun mmodules_poset
(mmodules
: Set[MModule]): POSet[MModule] do
169 return model
.mmodule_importation_hierarchy
.sub
(mmodules
)
172 # Build the POSet of `mmodules` hierarchy.
173 fun mclasses_poset
(mainmodule
: MModule, mclasses
: Set[MClass]): POSet[MClass] do
174 return mainmodule
.flatten_mclass_hierarchy
.sub
(mclasses
)
178 class LookupNamespaceVisitor
181 var namespace
: String
183 private var parts
: Array[String] is lazy
do return namespace
.split_with
("::")
185 var results
= new Array[MEntity]
187 redef fun visit
(mentity
) do mentity
.accept_namespace_visitor
(self)
190 class ModelTreeVisitor
193 var tree
= new MEntityTree
195 redef fun visit
(mentity
) do mentity
.accept_tree_visitor
(self)
200 # Get a public view of the model
201 fun public_view
: ModelView do
202 var view
= new ModelView(self.model
)
203 view
.min_visibility
= public_visibility
207 # Get a public view of the model
208 fun protected_view
: ModelView do
209 var view
= new ModelView(self.model
)
210 view
.min_visibility
= protected_visibility
214 # Get a public view of the model
215 fun private_view
: ModelView do
216 var view
= new ModelView(self.model
)
217 view
.min_visibility
= private_visibility
221 private fun accept_namespace_visitor
(v
: LookupNamespaceVisitor) do
222 if v
.parts
.is_empty
then return
223 if name
!= v
.parts
.first
then return
225 if v
.parts
.is_empty
then
232 private fun accept_tree_visitor
(v
: ModelTreeVisitor) do end
236 redef fun accept_tree_visitor
(v
) do
237 v
.tree
.add
(null, self)
243 redef fun accept_tree_visitor
(v
) do
244 var parent
= self.parent
245 if parent
!= null then
246 v
.tree
.add
(parent
, self)
248 v
.tree
.add
(mpackage
, self)
255 redef fun accept_tree_visitor
(v
) do
256 v
.tree
.add
(mgroup
, self)
262 # We don't want to collect classes from full namespace.
263 redef fun accept_namespace_visitor
(v
) do end
266 redef class MClassDef
267 redef fun accept_tree_visitor
(v
) do
268 v
.tree
.add
(mmodule
, self)
273 redef class MProperty
274 # We don't want to collect properties from full namespace.
275 redef fun accept_namespace_visitor
(v
) do end
279 redef fun accept_tree_visitor
(v
) do
280 v
.tree
.add
(mclassdef
, self)