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 # MModule used to flatten mclass hierarchy
32 var mainmodule
: MModule
34 # MPackages visible through `self`.
35 var mpackages
: Set[MPackage] is lazy
do
36 var mpackages
= new HashSet[MPackage]
37 for mpackage
in model
.mpackages
do
38 if not accept_mentity
(mpackage
) then continue
39 mpackages
.add mpackage
44 # MGroups visible through `self`.
45 var mgroups
: Set[MGroup] is lazy
do
46 var mgroups
= new HashSet[MGroup]
47 for mpackage
in mpackages
do
48 for mgroup
in mpackage
.mgroups
do
49 if not accept_mentity
(mgroup
) then continue
56 # MModules visible through `self`.
57 var mmodules
: Set[MModule] is lazy
do
58 var mmodules
= new HashSet[MModule]
59 for mmodule
in model
.mmodules
do
60 if not accept_mentity
(mmodule
) then continue
66 # MClasses visible through `self`.
67 var mclasses
: Set[MClass] is lazy
do
68 var mclasses
= new HashSet[MClass]
69 for mclass
in model
.mclasses
do
70 if not accept_mentity
(mclass
) then continue
76 # MClassDefs visible through `self`.
77 var mclassdefs
: Set[MClassDef] is lazy
do
78 var mclassdefs
= new HashSet[MClassDef]
79 for mclass
in mclasses
do
80 for mclassdef
in mclass
.mclassdefs
do
81 if not accept_mentity
(mclassdef
) then continue
82 mclassdefs
.add mclassdef
88 # MProperties visible through `self`.
89 var mproperties
: Set[MProperty] is lazy
do
90 var mproperties
= new HashSet[MProperty]
91 for mproperty
in model
.mproperties
do
92 if not accept_mentity
(mproperty
) then continue
93 mproperties
.add mproperty
98 # MPropdefs visible through `self`.
99 var mpropdefs
: Set[MPropDef] is lazy
do
100 var mpropdefs
= new HashSet[MPropDef]
101 for mproperty
in mproperties
do
102 for mpropdef
in mproperty
.mpropdefs
do
103 if not accept_mentity
(mpropdef
) then continue
104 mpropdefs
.add mpropdef
110 # Lists all MEntities visible through `self`.
111 var mentities
: Set[MEntity] is lazy
do
112 var res
= new HashSet[MEntity]
113 res
.add_all mpackages
117 res
.add_all mclassdefs
118 res
.add_all mproperties
119 res
.add_all mpropdefs
123 private fun init_visitor
(v
: ModelVisitor) do
124 v
.min_visibility
= self.min_visibility
125 v
.include_fictive
= self.include_fictive
126 v
.include_empty_doc
= self.include_empty_doc
127 v
.include_attribute
= self.include_attribute
128 v
.include_test
= self.include_test
131 # Searches the MEntity that matches `full_name`.
132 fun mentity_by_full_name
(full_name
: String): nullable MEntity do
133 for mentity
in mentities
do
134 if mentity
.full_name
== full_name
then return mentity
139 # Build an concerns tree with from `self`
140 fun to_tree
: MEntityTree do
141 var v
= new ModelTreeVisitor
143 for mpackage
in mpackages
do
144 v
.enter_visit
(mpackage
)
149 # Build the POSet of `mmodules` importation.
150 fun mmodules_poset
(mmodules
: Set[MModule]): POSet[MModule] do
151 return model
.mmodule_importation_hierarchy
.sub
(mmodules
)
154 # Build the POSet of `mclasses` hierarchy.
155 fun mclasses_poset
(mainmodule
: MModule, mclasses
: Set[MClass]): POSet[MClass] do
156 return mainmodule
.flatten_mclass_hierarchy
.sub
(mclasses
)
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 private fun accept_namespace_visitor
(v
: LookupNamespaceVisitor) do
183 if v
.parts
.is_empty
then return
184 if name
!= v
.parts
.first
then return
186 if v
.parts
.is_empty
then
193 private fun accept_tree_visitor
(v
: ModelTreeVisitor) do end
197 redef fun accept_tree_visitor
(v
) do
198 v
.tree
.add
(null, self)
204 redef fun accept_tree_visitor
(v
) do
205 var parent
= self.parent
206 if parent
!= null then
207 v
.tree
.add
(parent
, self)
209 v
.tree
.add
(mpackage
, self)
216 redef fun accept_tree_visitor
(v
) do
217 v
.tree
.add
(mgroup
, self)
223 # We don't want to collect classes from full namespace.
224 redef fun accept_namespace_visitor
(v
) do end
227 redef class MClassDef
228 redef fun accept_tree_visitor
(v
) do
229 v
.tree
.add
(mmodule
, self)
234 redef class MProperty
235 # We don't want to collect properties from full namespace.
236 redef fun accept_namespace_visitor
(v
) do end
240 redef fun accept_tree_visitor
(v
) do
241 v
.tree
.add
(mclassdef
, self)