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.
18 # Sort beers by their availability
22 # 1st sorting priority
23 var map1
: HashMap[COMPARED, Comparable]
25 # 2nd sorting priority
26 var map2
: HashMap[COMPARED, Comparable]
29 redef fun compare
(a
, b
) do return if map1
[a
] == map1
[b
] then
31 else map1
[a
] <=> map1
[b
]
35 var db_path
= "benitlux_sherbrooke.db"
36 var db
= new DB.open
(db_path
)
41 print
"{beers.length} known beers"
44 var all_days
= db
.days
45 assert all_days
!= null
46 print
"{all_days.length} days, from {all_days.first} to {all_days.last}"
48 # Beers availability by days
49 var beer2days
= new HashMap[Beer, Array[String]]
51 var days
= db
.days
(beer
)
53 default_comparator
.sort days
54 beer2days
[beer
] = days
57 # Sort beers by their availability and first date of appearance
58 var availability
= new HashMap[Beer, Int]
59 var appearances
= new HashMap[Beer, String]
61 var days
= beer2days
[beer
]
62 if days
.not_empty
then
63 appearances
[beer
] = days
.first
64 availability
[beer
] = -days
.length
# Opposite for inverse sort
66 appearances
[beer
] = "err"
67 availability
[beer
] = 1
71 # Sort by availability then appearance
72 var sorter
: Comparator = new BeerComparator(availability
, appearances
)
78 var days
= beer2days
[beer
]
80 # Skip never-available beers, usually name errors
81 if days
.is_empty
then continue
84 if from
== all_days
.first
then from
= " ... "
87 if to
== all_days
.last
then to
= " ... "
89 print
"- {days.length}\t{from} {to}\t{beer.name}: {beer.desc}"
92 # Sort by appearance then availability
93 sorter
= new BeerComparator(appearances
, availability
)
96 # Display the batch graph
99 # Compute `column_width` days from all the known days
100 var column_width
= 70
101 var days_sample
= [for i
in column_width
.times
do all_days
[i
*all_days
.length
/column_width
]]
104 var days
= beer2days
[beer
]
106 # Skip never-available beers, usually name errors
107 if days
.is_empty
then continue
109 # Print a line looking like: " ############ ###### ######## : Beer"
110 for s
in days_sample
do printn
if days
.has
(s
) then "#" else " "
111 print
": {beer.name}"