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 autoinit
(model
, mainmodule
, filter
)
30 # The model to view through `self`.
33 # MModule used to flatten mclass hierarchy
34 var mainmodule
: MModule
36 # MPackages visible through `self`.
37 var mpackages
: Set[MPackage] is lazy
do
38 var mpackages
= new HashSet[MPackage]
39 for mpackage
in model
.mpackages
do
40 if not accept_mentity
(mpackage
) then continue
41 mpackages
.add mpackage
46 # MGroups visible through `self`.
47 var mgroups
: Set[MGroup] is lazy
do
48 var mgroups
= new HashSet[MGroup]
49 for mpackage
in mpackages
do
50 for mgroup
in mpackage
.mgroups
do
51 if not accept_mentity
(mgroup
) then continue
58 # MModules visible through `self`.
59 var mmodules
: Set[MModule] is lazy
do
60 var mmodules
= new HashSet[MModule]
61 for mmodule
in model
.mmodules
do
62 if not accept_mentity
(mmodule
) then continue
68 # MClasses visible through `self`.
69 var mclasses
: Set[MClass] is lazy
do
70 var mclasses
= new HashSet[MClass]
71 for mclass
in model
.mclasses
do
72 if not accept_mentity
(mclass
) then continue
78 # MClassDefs visible through `self`.
79 var mclassdefs
: Set[MClassDef] is lazy
do
80 var mclassdefs
= new HashSet[MClassDef]
81 for mclass
in mclasses
do
82 for mclassdef
in mclass
.mclassdefs
do
83 if not accept_mentity
(mclassdef
) then continue
84 mclassdefs
.add mclassdef
90 # MProperties visible through `self`.
91 var mproperties
: Set[MProperty] is lazy
do
92 var mproperties
= new HashSet[MProperty]
93 for mproperty
in model
.mproperties
do
94 if not accept_mentity
(mproperty
) then continue
95 mproperties
.add mproperty
100 # MPropdefs visible through `self`.
101 var mpropdefs
: Set[MPropDef] is lazy
do
102 var mpropdefs
= new HashSet[MPropDef]
103 for mproperty
in mproperties
do
104 for mpropdef
in mproperty
.mpropdefs
do
105 if not accept_mentity
(mpropdef
) then continue
106 mpropdefs
.add mpropdef
112 # Lists all MEntities visible through `self`.
113 var mentities
: Set[MEntity] is lazy
do
114 var res
= new HashSet[MEntity]
115 res
.add_all mpackages
119 res
.add_all mclassdefs
120 res
.add_all mproperties
121 res
.add_all mpropdefs
125 # Searches the MEntity that matches `full_name`.
126 fun mentity_by_full_name
(full_name
: String): nullable MEntity do
127 for mentity
in mentities
do
128 if mentity
.full_name
== full_name
then return mentity
133 # Build an concerns tree with from `self`
134 fun to_tree
: MEntityTree do
135 var v
= new ModelTreeVisitor
136 v
.filter
= self.filter
137 for mpackage
in mpackages
do
138 v
.enter_visit
(mpackage
)
143 # Build the POSet of `mmodules` importation.
144 fun mmodules_poset
(mmodules
: Set[MModule]): POSet[MModule] do
145 return model
.mmodule_importation_hierarchy
.sub
(mmodules
)
148 # Build the POSet of `mclasses` hierarchy.
149 fun mclasses_poset
(mainmodule
: MModule, mclasses
: Set[MClass]): POSet[MClass] do
150 return mainmodule
.flatten_mclass_hierarchy
.sub
(mclasses
)
154 class LookupNamespaceVisitor
157 var namespace
: String
159 private var parts
: Array[String] is lazy
do return namespace
.split_with
("::")
161 var results
= new Array[MEntity]
163 redef fun visit
(mentity
) do mentity
.accept_namespace_visitor
(self)
166 class ModelTreeVisitor
169 var tree
= new MEntityTree
171 redef fun visit
(mentity
) do mentity
.accept_tree_visitor
(self)
175 private fun accept_namespace_visitor
(v
: LookupNamespaceVisitor) do
176 if v
.parts
.is_empty
then return
177 if name
!= v
.parts
.first
then return
179 if v
.parts
.is_empty
then
186 private fun accept_tree_visitor
(v
: ModelTreeVisitor) do end
190 redef fun accept_tree_visitor
(v
) do
191 v
.tree
.add
(null, self)
197 redef fun accept_tree_visitor
(v
) do
198 var parent
= self.parent
199 if parent
!= null then
200 v
.tree
.add
(parent
, self)
202 v
.tree
.add
(mpackage
, self)
209 redef fun accept_tree_visitor
(v
) do
210 v
.tree
.add
(mgroup
, self)
216 # We don't want to collect classes from full namespace.
217 redef fun accept_namespace_visitor
(v
) do end
220 redef class MClassDef
221 redef fun accept_tree_visitor
(v
) do
222 v
.tree
.add
(mmodule
, self)
227 redef class MProperty
228 # We don't want to collect properties from full namespace.
229 redef fun accept_namespace_visitor
(v
) do end
233 redef fun accept_tree_visitor
(v
) do
234 v
.tree
.add
(mclassdef
, self)