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
= self.include_test
128 # Searches the MEntity that matches `full_name`.
129 fun mentity_by_full_name
(full_name
: String): nullable MEntity do
130 for mentity
in mentities
do
131 if mentity
.full_name
== full_name
then return mentity
136 # Build an concerns tree with from `self`
137 fun to_tree
: MEntityTree do
138 var v
= new ModelTreeVisitor
140 for mpackage
in mpackages
do
141 v
.enter_visit
(mpackage
)
146 # Build the POSet of `mmodules` importation.
147 fun mmodules_poset
(mmodules
: Set[MModule]): POSet[MModule] do
148 return model
.mmodule_importation_hierarchy
.sub
(mmodules
)
151 # Build the POSet of `mclasses` hierarchy.
152 fun mclasses_poset
(mainmodule
: MModule, mclasses
: Set[MClass]): POSet[MClass] do
153 return mainmodule
.flatten_mclass_hierarchy
.sub
(mclasses
)
157 class LookupNamespaceVisitor
160 var namespace
: String
162 private var parts
: Array[String] is lazy
do return namespace
.split_with
("::")
164 var results
= new Array[MEntity]
166 redef fun visit
(mentity
) do mentity
.accept_namespace_visitor
(self)
169 class ModelTreeVisitor
172 var tree
= new MEntityTree
174 redef fun visit
(mentity
) do mentity
.accept_tree_visitor
(self)
179 # Get a public view of the model
180 fun public_view
: ModelView do
181 var view
= new ModelView(self.model
)
182 view
.min_visibility
= public_visibility
186 # Get a public view of the model
187 fun protected_view
: ModelView do
188 var view
= new ModelView(self.model
)
189 view
.min_visibility
= protected_visibility
193 # Get a public view of the model
194 fun private_view
: ModelView do
195 var view
= new ModelView(self.model
)
196 view
.min_visibility
= private_visibility
200 private fun accept_namespace_visitor
(v
: LookupNamespaceVisitor) do
201 if v
.parts
.is_empty
then return
202 if name
!= v
.parts
.first
then return
204 if v
.parts
.is_empty
then
211 private fun accept_tree_visitor
(v
: ModelTreeVisitor) do end
215 redef fun accept_tree_visitor
(v
) do
216 v
.tree
.add
(null, self)
222 redef fun accept_tree_visitor
(v
) do
223 var parent
= self.parent
224 if parent
!= null then
225 v
.tree
.add
(parent
, self)
227 v
.tree
.add
(mpackage
, self)
234 redef fun accept_tree_visitor
(v
) do
235 v
.tree
.add
(mgroup
, self)
241 # We don't want to collect classes from full namespace.
242 redef fun accept_namespace_visitor
(v
) do end
245 redef class MClassDef
246 redef fun accept_tree_visitor
(v
) do
247 v
.tree
.add
(mmodule
, self)
252 redef class MProperty
253 # We don't want to collect properties from full namespace.
254 redef fun accept_namespace_visitor
(v
) do end
258 redef fun accept_tree_visitor
(v
) do
259 v
.tree
.add
(mclassdef
, self)