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 index
= model
.index
50 # lookup by name prefix
51 var matches
= index
.find_by_name_prefix
(query
).uniq
.
52 sort
(lname_sorter
, name_sorter
, kind_sorter
)
53 matches
= matches
.rerank
.sort
(vis_sorter
, score_sorter
)
56 var malus
= matches
.length
57 if catalog
.tag2proj
.has_key
(query
) then
58 for mpackage
in catalog
.tag2proj
[query
] do
59 matches
.add
new IndexMatch(mpackage
, malus
)
62 matches
= matches
.uniq
.rerank
.sort
(vis_sorter
, score_sorter
)
65 # lookup by full_name prefix
66 malus
= matches
.length
67 var full_matches
= new IndexMatches
68 for match
in index
.find_by_full_name_prefix
(query
).
69 sort
(lfname_sorter
, fname_sorter
) do
71 full_matches
.add match
73 matches
= matches
.uniq
75 # lookup by similarity
76 malus
= matches
.length
77 var sim_matches
= new IndexMatches
78 for match
in index
.find_by_similarity
(query
).sort
(score_sorter
, lname_sorter
, name_sorter
) do
79 if match
.score
> query
.length
then break
83 matches
.add_all sim_matches
84 matches
= matches
.uniq
85 results
= matches
.rerank
.sort
(vis_sorter
, score_sorter
).mentities
89 private var score_sorter
= new ScoreComparator
90 private var vis_sorter
= new VisibilityComparator
91 private var name_sorter
= new NameComparator
92 private var lname_sorter
= new NameLengthComparator
93 private var fname_sorter
= new FullNameComparator
94 private var lfname_sorter
= new FullNameLengthComparator
95 private var kind_sorter
= new MEntityComparator
98 # Retrieve the catalog metadata for a MPackage
102 # MPackage metadata retrieved
103 var metadata
: nullable MPackageMetadata = null is optional
, writable
105 redef fun init_command
do
106 if metadata
!= null then return new CmdSuccess
109 if not res
isa CmdSuccess then return res
110 var mentity
= self.mentity
.as(not null)
112 if mentity
isa MPackage then
113 metadata
= mentity
.metadata
115 return new WarningNoMetadata(mentity
)
121 # No metadata for `mentity`
122 class WarningNoMetadata
128 redef fun to_s
do return "No metadata for `{mentity.full_name}`"
131 # Retrieve the packages in the catalog
132 class CmdCatalogPackages
136 autoinit
(model
, catalog
, filter
, limit
, page
, count
, max
)
138 redef var sorter
= new CatalogScoreSorter(catalog
) is lazy
140 redef fun init_results
do
141 if results
!= null then return new CmdSuccess
144 if not res
isa CmdSuccess then return res
146 results
= catalog
.mpackages
.values
.to_a
151 # Retrieve the catalog stats
152 class CmdCatalogStats
155 # Retrieved catalog statistics
156 var stats
: nullable CatalogStats = null is optional
, writable
158 redef fun init_command
do
160 self.stats
= catalog
.catalog_stats
161 return new CmdSuccess
165 # Retrieve the catalog tags list
169 # Sorter to sort tags alphabetically
170 var tags_sorter
= new CatalogTagsSorter is optional
, writable
172 # Count of packages by tag
173 var packages_count_by_tags
: nullable ArrayMap[String, Int] = null is optional
, writable
175 redef fun init_command
do
177 var tags_to_projects
= new ArrayMap[String, Int]
178 var tags
= catalog
.tag2proj
.keys
.to_a
179 tags_sorter
.sort
(tags
)
181 if not catalog
.tag2proj
.has_key
(tag
) then continue
182 tags_to_projects
[tag
] = catalog
.tag2proj
[tag
].length
184 packages_count_by_tags
= tags_to_projects
185 return new CmdSuccess
189 # Retrieve the packages for a tag
191 super CmdCatalogPackages
193 autoinit
(model
, catalog
, filter
, tag
, limit
, page
, count
, max
)
195 # The tag to retrieve
196 var tag
: nullable String = null is optional
, writable
198 redef fun init_command
do
200 if tag
== null then return new ErrorNoTag
202 if not catalog
.tag2proj
.has_key
(tag
) then return new ErrorTagNotFound(tag
)
206 redef fun init_results
do
207 if results
!= null then return new CmdSuccess
210 if not res
isa CmdSuccess then return res
212 results
= catalog
.tag2proj
[tag
].to_a
217 # No tag name provided
221 redef fun to_s
do return "No tag name provided"
224 # No tag with this name in the catalog
225 class ErrorTagNotFound
228 # The tag that was not found
231 redef fun to_s
do return "No tag found for `{tag}`"
234 # Retrieve a person from the catalog
235 class CmdCatalogPerson
240 # You can also pass a `person_name`.
241 var person
: nullable Person = null is optional
, writable
243 # Name of the person to retrieve
245 # You can also pass a `person` instance.
246 var person_name
: nullable String = null is optional
, writable
248 # Initialize the `person` result
249 fun init_person
: CmdMessage do
250 var person
= self.person
251 if person
!= null then
252 person_name
= person
.name
253 return new CmdSuccess
256 var name
= self.person_name
257 if name
== null then return new ErrorNoPerson
258 if not catalog
.name2person
.has_key
(name
) then return new ErrorPersonNotFound(name
)
259 self.person
= catalog
.name2person
[name
]
260 return new CmdSuccess
263 redef fun init_command
do
269 # No person instance or name provided
273 redef fun to_s
do return "No person provided"
276 # No person found with this name
277 class ErrorPersonNotFound
280 # Name of the person that was not found
283 redef fun to_s
do return "No person found for `{name}`"
286 # Retrieve the packages maintained by a person
287 class CmdCatalogMaintaining
288 super CmdCatalogPerson
289 super CmdCatalogPackages
291 autoinit
(model
, catalog
, filter
, person
, person_name
, limit
, page
, count
, max
)
293 redef fun init_command
do return super
295 redef fun init_results
do
296 if results
!= null then return new CmdSuccess
298 if not res
isa CmdSuccess then return res
299 var person
= self.person
.as(not null)
301 if not catalog
.maint2proj
.has_key
(person
) then return res
302 results
= catalog
.maint2proj
[person
]
307 # Retrieve the packages contributed by a person
308 class CmdCatalogContributing
309 super CmdCatalogPerson
310 super CmdCatalogPackages
312 autoinit
(model
, catalog
, filter
, person
, person_name
, limit
, page
, count
, max
)
314 # Include maintained packages?
316 # Default is `false`.
317 var maintaining
= false is optional
, writable
319 # FIXME linearization
320 redef fun init_command
do return super
322 redef fun init_results
do
323 if results
!= null then return new CmdSuccess
326 if not res
isa CmdSuccess then return res
327 var person
= self.person
.as(not null)
329 if not catalog
.contrib2proj
.has_key
(person
) then return res
331 var maint2proj
= null
332 if catalog
.maint2proj
.has_key
(person
) then
333 maint2proj
= catalog
.maint2proj
[person
]
336 var results
= new Array[MPackage]
337 for mpackage
in catalog
.contrib2proj
[person
] do
338 if not maintaining
and maint2proj
!= null and maint2proj
.has
(mpackage
) then continue
341 self.results
= results