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.
15 # Commands to retrieve Catalog related data
16 module commands_catalog
21 # A DocCommand based on a Catalog
22 abstract class CmdCatalog
25 autoinit
(model
, catalog
, filter
)
31 # A CmdSearch command using a Catalog
32 class CmdCatalogSearch
36 autoinit
(model
, catalog
, filter
, query
, limit
, page
, count
, max
)
38 redef fun init_results
do
39 if results
!= null then return new CmdSuccess
42 if not res
isa CmdSuccess then return res
44 var query
= self.query
45 if query
== null then return new ErrorNoQuery
48 var filter
= self.filter
49 var index
= model
.index
51 # lookup by name prefix
52 var matches
= index
.find_by_name_prefix
(query
, filter
).uniq
.
53 sort
(lname_sorter
, name_sorter
, kind_sorter
)
54 matches
= matches
.rerank
.sort
(vis_sorter
, score_sorter
)
57 var malus
= matches
.length
58 if catalog
.tag2proj
.has_key
(query
) then
59 for mpackage
in catalog
.tag2proj
[query
] do
60 if filter
!= null and not filter
.accept_mentity
(mpackage
) then continue
61 matches
.add
new IndexMatch(mpackage
, malus
)
64 matches
= matches
.uniq
.rerank
.sort
(vis_sorter
, score_sorter
)
67 # lookup by full_name prefix
68 malus
= matches
.length
69 var full_matches
= new IndexMatches
70 for match
in index
.find_by_full_name_prefix
(query
, filter
).
71 sort
(lfname_sorter
, fname_sorter
) do
73 full_matches
.add match
75 matches
= matches
.uniq
77 # lookup by similarity
78 malus
= matches
.length
79 var sim_matches
= new IndexMatches
80 for match
in index
.find_by_similarity
(query
, filter
).sort
(score_sorter
, kind_sorter
, lname_sorter
, name_sorter
) do
81 if match
.score
> query
.length
then break
85 matches
.add_all sim_matches
86 matches
= matches
.uniq
87 results
= matches
.rerank
.sort
(vis_sorter
, score_sorter
).mentities
91 private var score_sorter
= new ScoreComparator
92 private var vis_sorter
= new VisibilityComparator
93 private var name_sorter
= new NameComparator
94 private var lname_sorter
= new NameLengthComparator
95 private var fname_sorter
= new FullNameComparator
96 private var lfname_sorter
= new FullNameLengthComparator
97 private var kind_sorter
= new MEntityComparator
100 # Retrieve the catalog metadata for a MPackage
104 # MPackage metadata retrieved
105 var metadata
: nullable MPackageMetadata = null is optional
, writable
107 redef fun init_command
do
108 if metadata
!= null then return new CmdSuccess
111 if not res
isa CmdSuccess then return res
112 var mentity
= self.mentity
.as(not null)
114 if mentity
isa MPackage then
115 metadata
= mentity
.metadata
117 return new WarningNoMetadata(mentity
)
123 # No metadata for `mentity`
124 class WarningNoMetadata
130 redef fun to_s
do return "No metadata for `{mentity.full_name}`"
133 # Retrieve the packages in the catalog
134 class CmdCatalogPackages
138 autoinit
(model
, catalog
, filter
, limit
, page
, count
, max
)
140 redef var sorter
= new CatalogScoreSorter(catalog
) is lazy
142 redef fun init_results
do
143 if results
!= null then return new CmdSuccess
146 if not res
isa CmdSuccess then return res
148 results
= catalog
.mpackages
.values
.to_a
153 # Retrieve the catalog stats
154 class CmdCatalogStats
157 # Retrieved catalog statistics
158 var stats
: nullable CatalogStats = null is optional
, writable
160 redef fun init_command
do
162 self.stats
= catalog
.catalog_stats
163 return new CmdSuccess
167 # Retrieve the catalog tags list
171 # Sorter to sort tags alphabetically
172 var tags_sorter
= new CatalogTagsSorter is optional
, writable
174 # Count of packages by tag
175 var packages_count_by_tags
: nullable ArrayMap[String, Int] = null is optional
, writable
177 redef fun init_command
do
179 var tags_to_projects
= new ArrayMap[String, Int]
180 var tags
= catalog
.tag2proj
.keys
.to_a
181 tags_sorter
.sort
(tags
)
183 if not catalog
.tag2proj
.has_key
(tag
) then continue
184 tags_to_projects
[tag
] = catalog
.tag2proj
[tag
].length
186 packages_count_by_tags
= tags_to_projects
187 return new CmdSuccess
191 # Retrieve the packages for a tag
193 super CmdCatalogPackages
195 autoinit
(model
, catalog
, filter
, tag
, limit
, page
, count
, max
)
197 # The tag to retrieve
198 var tag
: nullable String = null is optional
, writable
200 redef fun init_command
do
202 if tag
== null then return new ErrorNoTag
204 if not catalog
.tag2proj
.has_key
(tag
) then return new ErrorTagNotFound(tag
)
208 redef fun init_results
do
209 if results
!= null then return new CmdSuccess
212 if not res
isa CmdSuccess then return res
214 results
= catalog
.tag2proj
[tag
].to_a
219 # No tag name provided
223 redef fun to_s
do return "No tag name provided"
226 # No tag with this name in the catalog
227 class ErrorTagNotFound
230 # The tag that was not found
233 redef fun to_s
do return "No tag found for `{tag}`"
236 # Retrieve a person from the catalog
237 class CmdCatalogPerson
242 # You can also pass a `person_name`.
243 var person
: nullable Person = null is optional
, writable
245 # Name of the person to retrieve
247 # You can also pass a `person` instance.
248 var person_name
: nullable String = null is optional
, writable
250 # Initialize the `person` result
251 fun init_person
: CmdMessage do
252 var person
= self.person
253 if person
!= null then
254 person_name
= person
.name
255 return new CmdSuccess
258 var name
= self.person_name
259 if name
== null then return new ErrorNoPerson
260 if not catalog
.name2person
.has_key
(name
) then return new ErrorPersonNotFound(name
)
261 self.person
= catalog
.name2person
[name
]
262 return new CmdSuccess
265 redef fun init_command
do
271 # No person instance or name provided
275 redef fun to_s
do return "No person provided"
278 # No person found with this name
279 class ErrorPersonNotFound
282 # Name of the person that was not found
285 redef fun to_s
do return "No person found for `{name}`"
288 # Retrieve the packages maintained by a person
289 class CmdCatalogMaintaining
290 super CmdCatalogPerson
291 super CmdCatalogPackages
293 autoinit
(model
, catalog
, filter
, person
, person_name
, limit
, page
, count
, max
)
295 redef fun init_command
do return super
297 redef fun init_results
do
298 if results
!= null then return new CmdSuccess
300 if not res
isa CmdSuccess then return res
301 var person
= self.person
.as(not null)
303 if not catalog
.maint2proj
.has_key
(person
) then return res
304 results
= catalog
.maint2proj
[person
]
309 # Retrieve the packages contributed by a person
310 class CmdCatalogContributing
311 super CmdCatalogPerson
312 super CmdCatalogPackages
314 autoinit
(model
, catalog
, filter
, person
, person_name
, limit
, page
, count
, max
)
316 # Include maintained packages?
318 # Default is `false`.
319 var maintaining
= false is optional
, writable
321 # FIXME linearization
322 redef fun init_command
do return super
324 redef fun init_results
do
325 if results
!= null then return new CmdSuccess
328 if not res
isa CmdSuccess then return res
329 var person
= self.person
.as(not null)
331 if not catalog
.contrib2proj
.has_key
(person
) then return res
333 var maint2proj
= null
334 if catalog
.maint2proj
.has_key
(person
) then
335 maint2proj
= catalog
.maint2proj
[person
]
338 var results
= new Array[MPackage]
339 for mpackage
in catalog
.contrib2proj
[person
] do
340 if not maintaining
and maint2proj
!= null and maint2proj
.has
(mpackage
) then continue
343 self.results
= results