model: views use filters
[nit.git] / src / model / model_views.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
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
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 module model_views
16
17 import model_visitor
18
19 # Provide a configurable view to a model.
20 #
21 # This can be useful when you need to filter some mentities from a model
22 # like private or fictive.
23 #
24 # TODO doc usage
25 class ModelView
26 super ModelVisitor
27
28 autoinit(model, mainmodule, filter)
29
30 # The model to view through `self`.
31 var model: Model
32
33 # MModule used to flatten mclass hierarchy
34 var mainmodule: MModule
35
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
42 end
43 return mpackages
44 end
45
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
52 mgroups.add mgroup
53 end
54 end
55 return mgroups
56 end
57
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
63 mmodules.add mmodule
64 end
65 return mmodules
66 end
67
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
73 mclasses.add mclass
74 end
75 return mclasses
76 end
77
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
85 end
86 end
87 return mclassdefs
88 end
89
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
96 end
97 return mproperties
98 end
99
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
107 end
108 end
109 return mpropdefs
110 end
111
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
116 res.add_all mgroups
117 res.add_all mmodules
118 res.add_all mclasses
119 res.add_all mclassdefs
120 res.add_all mproperties
121 res.add_all mpropdefs
122 return res
123 end
124
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
129 end
130 return null
131 end
132
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)
139 end
140 return v.tree
141 end
142
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)
146 end
147
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)
151 end
152 end
153
154 class LookupNamespaceVisitor
155 super ModelVisitor
156
157 var namespace: String
158
159 private var parts: Array[String] is lazy do return namespace.split_with("::")
160
161 var results = new Array[MEntity]
162
163 redef fun visit(mentity) do mentity.accept_namespace_visitor(self)
164 end
165
166 class ModelTreeVisitor
167 super ModelVisitor
168
169 var tree = new MEntityTree
170
171 redef fun visit(mentity) do mentity.accept_tree_visitor(self)
172 end
173
174 redef class MEntity
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
178 v.parts.shift
179 if v.parts.is_empty then
180 v.results.add self
181 return
182 end
183 visit_all(v)
184 end
185
186 private fun accept_tree_visitor(v: ModelTreeVisitor) do end
187 end
188
189 redef class MPackage
190 redef fun accept_tree_visitor(v) do
191 v.tree.add(null, self)
192 visit_all(v)
193 end
194 end
195
196 redef class MGroup
197 redef fun accept_tree_visitor(v) do
198 var parent = self.parent
199 if parent != null then
200 v.tree.add(parent, self)
201 else
202 v.tree.add(mpackage, self)
203 end
204 visit_all(v)
205 end
206 end
207
208 redef class MModule
209 redef fun accept_tree_visitor(v) do
210 v.tree.add(mgroup, self)
211 visit_all(v)
212 end
213 end
214
215 redef class MClass
216 # We don't want to collect classes from full namespace.
217 redef fun accept_namespace_visitor(v) do end
218 end
219
220 redef class MClassDef
221 redef fun accept_tree_visitor(v) do
222 v.tree.add(mmodule, self)
223 visit_all(v)
224 end
225 end
226
227 redef class MProperty
228 # We don't want to collect properties from full namespace.
229 redef fun accept_namespace_visitor(v) do end
230 end
231
232 redef class MPropDef
233 redef fun accept_tree_visitor(v) do
234 v.tree.add(mclassdef, self)
235 visit_all(v)
236 end
237 end