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 # Looks up a MEntity by its full `namespace`.
137 # Usefull when `mentities_by_name` returns conflicts.
139 # Namespaces must be of the form `package::core::module::Class::prop`.
140 fun mentities_by_namespace
(namespace
: String): Array[MEntity] do
141 var v
= new LookupNamespaceVisitor(namespace
)
143 for mpackage
in mpackages
do
144 v
.enter_visit
(mpackage
)
149 # Build an concerns tree with from `self`
150 fun to_tree
: MEntityTree do
151 var v
= new ModelTreeVisitor
153 for mpackage
in mpackages
do
154 v
.enter_visit
(mpackage
)
159 # Build the POSet of `mmodules` importation.
160 fun mmodules_poset
(mmodules
: Set[MModule]): POSet[MModule] do
161 return model
.mmodule_importation_hierarchy
.sub
(mmodules
)
164 # Build the POSet of `mmodules` hierarchy.
165 fun mclasses_poset
(mainmodule
: MModule, mclasses
: Set[MClass]): POSet[MClass] do
166 return mainmodule
.flatten_mclass_hierarchy
.sub
(mclasses
)
170 class LookupNamespaceVisitor
173 var namespace
: String
175 private var parts
: Array[String] is lazy
do return namespace
.split_with
("::")
177 var results
= new Array[MEntity]
179 redef fun visit
(mentity
) do mentity
.accept_namespace_visitor
(self)
182 class ModelTreeVisitor
185 var tree
= new MEntityTree
187 redef fun visit
(mentity
) do mentity
.accept_tree_visitor
(self)
192 # Get a public view of the model
193 fun public_view
: ModelView do
194 var view
= new ModelView(self.model
)
195 view
.min_visibility
= public_visibility
199 # Get a public view of the model
200 fun protected_view
: ModelView do
201 var view
= new ModelView(self.model
)
202 view
.min_visibility
= protected_visibility
206 # Get a public view of the model
207 fun private_view
: ModelView do
208 var view
= new ModelView(self.model
)
209 view
.min_visibility
= private_visibility
213 private fun accept_namespace_visitor
(v
: LookupNamespaceVisitor) do
214 if v
.parts
.is_empty
then return
215 if name
!= v
.parts
.first
then return
217 if v
.parts
.is_empty
then
224 private fun accept_tree_visitor
(v
: ModelTreeVisitor) do end
228 redef fun accept_tree_visitor
(v
) do
229 v
.tree
.add
(null, self)
235 redef fun accept_tree_visitor
(v
) do
236 var parent
= self.parent
237 if parent
!= null then
238 v
.tree
.add
(parent
, self)
240 v
.tree
.add
(mpackage
, self)
247 redef fun accept_tree_visitor
(v
) do
248 v
.tree
.add
(mgroup
, self)
254 # We don't want to collect classes from full namespace.
255 redef fun accept_namespace_visitor
(v
) do end
258 redef class MClassDef
259 redef fun accept_tree_visitor
(v
) do
260 v
.tree
.add
(mmodule
, self)
265 redef class MProperty
266 # We don't want to collect properties from full namespace.
267 redef fun accept_namespace_visitor
(v
) do end
271 redef fun accept_tree_visitor
(v
) do
272 v
.tree
.add
(mclassdef
, self)