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
20 # A DocCommand based on a Catalog
21 abstract class CmdCatalog
24 autoinit
(model
, catalog
, filter
)
30 # A CmdSearch command using a Catalog
31 class CmdCatalogSearch
35 autoinit
(model
, catalog
, filter
, query
, limit
, page
, count
, max
)
37 redef fun init_results
do
38 if results
!= null then return new CmdSuccess
41 if not res
isa CmdSuccess then return res
43 var query
= self.query
44 if query
== null then return new ErrorNoQuery
47 var index
= model
.index
49 # lookup by name prefix
50 var matches
= index
.find_by_name_prefix
(query
).uniq
.
51 sort
(lname_sorter
, name_sorter
, kind_sorter
)
52 matches
= matches
.rerank
.sort
(vis_sorter
, score_sorter
)
55 var malus
= matches
.length
56 if catalog
.tag2proj
.has_key
(query
) then
57 for mpackage
in catalog
.tag2proj
[query
] do
58 matches
.add
new IndexMatch(mpackage
, malus
)
61 matches
= matches
.uniq
.rerank
.sort
(vis_sorter
, score_sorter
)
64 # lookup by full_name prefix
65 malus
= matches
.length
66 var full_matches
= new IndexMatches
67 for match
in index
.find_by_full_name_prefix
(query
).
68 sort
(lfname_sorter
, fname_sorter
) do
70 full_matches
.add match
72 matches
= matches
.uniq
74 # lookup by similarity
75 malus
= matches
.length
76 var sim_matches
= new IndexMatches
77 for match
in index
.find_by_similarity
(query
).sort
(score_sorter
, lname_sorter
, name_sorter
) do
78 if match
.score
> query
.length
then break
82 matches
.add_all sim_matches
83 matches
= matches
.uniq
84 results
= matches
.rerank
.sort
(vis_sorter
, score_sorter
).mentities
88 private var score_sorter
= new ScoreComparator
89 private var vis_sorter
= new VisibilityComparator
90 private var name_sorter
= new NameComparator
91 private var lname_sorter
= new NameLengthComparator
92 private var fname_sorter
= new FullNameComparator
93 private var lfname_sorter
= new FullNameLengthComparator
94 private var kind_sorter
= new MEntityComparator
97 # Retrieve the catalog metadata for a MPackage
101 # MPackage metadata retrieved
102 var metadata
: nullable MPackageMetadata = null is optional
, writable
104 redef fun init_command
do
105 if metadata
!= null then return new CmdSuccess
108 if not res
isa CmdSuccess then return res
109 var mentity
= self.mentity
.as(not null)
111 if mentity
isa MPackage then
112 metadata
= mentity
.metadata
114 return new WarningNoMetadata(mentity
)
120 # No metadata for `mentity`
121 class WarningNoMetadata
127 redef fun to_s
do return "No metadata for `{mentity.full_name}`"
130 # Retrieve the packages in the catalog
131 class CmdCatalogPackages
135 autoinit
(model
, catalog
, filter
, limit
, page
, count
, max
)
137 redef var sorter
= new CatalogScoreSorter(catalog
) is lazy
139 redef fun init_results
do
140 if results
!= null then return new CmdSuccess
143 if not res
isa CmdSuccess then return res
145 results
= catalog
.mpackages
.values
.to_a
150 # Retrieve the catalog stats
151 class CmdCatalogStats
154 # Retrieved catalog statistics
155 var stats
: nullable CatalogStats = null is optional
, writable
157 redef fun init_command
do
159 self.stats
= catalog
.catalog_stats
160 return new CmdSuccess
164 # Retrieve the catalog tags list
168 # Sorter to sort tags alphabetically
169 var tags_sorter
= new CatalogTagsSorter is optional
, writable
171 # Count of packages by tag
172 var packages_count_by_tags
: nullable ArrayMap[String, Int] = null is optional
, writable
174 redef fun init_command
do
176 var tags_to_projects
= new ArrayMap[String, Int]
177 var tags
= catalog
.tag2proj
.keys
.to_a
178 tags_sorter
.sort
(tags
)
180 if not catalog
.tag2proj
.has_key
(tag
) then continue
181 tags_to_projects
[tag
] = catalog
.tag2proj
[tag
].length
183 packages_count_by_tags
= tags_to_projects
184 return new CmdSuccess
188 # Retrieve the packages for a tag
190 super CmdCatalogPackages
192 autoinit
(model
, catalog
, filter
, tag
, limit
, page
, count
, max
)
194 # The tag to retrieve
195 var tag
: nullable String = null is optional
, writable
197 redef fun init_command
do
199 if tag
== null then return new ErrorNoTag
201 if not catalog
.tag2proj
.has_key
(tag
) then return new ErrorTagNotFound(tag
)
205 redef fun init_results
do
206 if results
!= null then return new CmdSuccess
209 if not res
isa CmdSuccess then return res
211 results
= catalog
.tag2proj
[tag
].to_a
216 # No tag name provided
220 redef fun to_s
do return "No tag name provided"
223 # No tag with this name in the catalog
224 class ErrorTagNotFound
227 # The tag that was not found
230 redef fun to_s
do return "No tag found for `{tag}`"
233 # Retrieve a person from the catalog
234 class CmdCatalogPerson
239 # You can also pass a `person_name`.
240 var person
: nullable Person = null is optional
, writable
242 # Name of the person to retrieve
244 # You can also pass a `person` instance.
245 var person_name
: nullable String = null is optional
, writable
247 # Initialize the `person` result
248 fun init_person
: CmdMessage do
249 var person
= self.person
250 if person
!= null then
251 person_name
= person
.name
252 return new CmdSuccess
255 var name
= self.person_name
256 if name
== null then return new ErrorNoPerson
257 if not catalog
.name2person
.has_key
(name
) then return new ErrorPersonNotFound(name
)
258 self.person
= catalog
.name2person
[name
]
259 return new CmdSuccess
262 redef fun init_command
do
268 # No person instance or name provided
272 redef fun to_s
do return "No person provided"
275 # No person found with this name
276 class ErrorPersonNotFound
279 # Name of the person that was not found
282 redef fun to_s
do return "No person found for `{name}`"
285 # Retrieve the packages maintained by a person
286 class CmdCatalogMaintaining
287 super CmdCatalogPerson
288 super CmdCatalogPackages
290 autoinit
(model
, catalog
, filter
, person
, person_name
, limit
, page
, count
, max
)
292 redef fun init_command
do return super
294 redef fun init_results
do
295 if results
!= null then return new CmdSuccess
297 if not res
isa CmdSuccess then return res
298 var person
= self.person
.as(not null)
300 if not catalog
.maint2proj
.has_key
(person
) then return res
301 results
= catalog
.maint2proj
[person
]
306 # Retrieve the packages contributed by a person
307 class CmdCatalogContributing
308 super CmdCatalogPerson
309 super CmdCatalogPackages
311 autoinit
(model
, catalog
, filter
, person
, person_name
, limit
, page
, count
, max
)
313 # Include maintained packages?
315 # Default is `false`.
316 var maintaining
= false is optional
, writable
318 # FIXME linearization
319 redef fun init_command
do return super
321 redef fun init_results
do
322 if results
!= null then return new CmdSuccess
325 if not res
isa CmdSuccess then return res
326 var person
= self.person
.as(not null)
328 if not catalog
.contrib2proj
.has_key
(person
) then return res
330 var maint2proj
= null
331 if catalog
.maint2proj
.has_key
(person
) then
332 maint2proj
= catalog
.maint2proj
[person
]
335 var results
= new Array[MPackage]
336 for mpackage
in catalog
.contrib2proj
[person
] do
337 if not maintaining
and maint2proj
!= null and maint2proj
.has
(mpackage
) then continue
340 self.results
= results