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
)
160 class LookupNamespaceVisitor
163 var namespace
: String
165 private var parts
: Array[String] is lazy
do return namespace
.split_with
("::")
167 var results
= new Array[MEntity]
169 redef fun visit
(mentity
) do mentity
.accept_namespace_visitor
(self)
172 class ModelTreeVisitor
175 var tree
= new MEntityTree
177 redef fun visit
(mentity
) do mentity
.accept_tree_visitor
(self)
182 # Get a public view of the model
183 fun public_view
: ModelView do
184 var view
= new ModelView(self.model
)
185 view
.min_visibility
= public_visibility
189 # Get a public view of the model
190 fun protected_view
: ModelView do
191 var view
= new ModelView(self.model
)
192 view
.min_visibility
= protected_visibility
196 # Get a public view of the model
197 fun private_view
: ModelView do
198 var view
= new ModelView(self.model
)
199 view
.min_visibility
= private_visibility
203 private fun accept_namespace_visitor
(v
: LookupNamespaceVisitor) do
204 if v
.parts
.is_empty
then return
205 if name
!= v
.parts
.first
then return
207 if v
.parts
.is_empty
then
214 private fun accept_tree_visitor
(v
: ModelTreeVisitor) do end
218 redef fun accept_tree_visitor
(v
) do
219 v
.tree
.add
(null, self)
225 redef fun accept_tree_visitor
(v
) do
226 var parent
= self.parent
227 if parent
!= null then
228 v
.tree
.add
(parent
, self)
230 v
.tree
.add
(mpackage
, self)
237 redef fun accept_tree_visitor
(v
) do
238 v
.tree
.add
(mgroup
, self)
244 # We don't want to collect classes from full namespace.
245 redef fun accept_namespace_visitor
(v
) do end
248 redef class MClassDef
249 redef fun accept_tree_visitor
(v
) do
250 v
.tree
.add
(mmodule
, self)
255 redef class MProperty
256 # We don't want to collect properties from full namespace.
257 redef fun accept_namespace_visitor
(v
) do end
261 redef fun accept_tree_visitor
(v
) do
262 v
.tree
.add
(mclassdef
, self)