|
| 1 | +# |
| 2 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 3 | +# or more contributor license agreements. See the NOTICE file |
| 4 | +# distributed with this work for additional information |
| 5 | +# regarding copyright ownership. The ASF licenses this file |
| 6 | +# to you under the Apache License, Version 2.0 (the |
| 7 | +# "License"); you may not use this file except in compliance |
| 8 | +# with the License. You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, |
| 13 | +# software distributed under the License is distributed on an |
| 14 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | +# KIND, either express or implied. See the License for the |
| 16 | +# specific language governing permissions and limitations |
| 17 | +# under the License. |
| 18 | +# |
| 19 | + |
| 20 | +define |
| 21 | + |
| 22 | +entity book @abstract, |
| 23 | + owns isbn @card(0..2), |
| 24 | + owns isbn-13 @key, |
| 25 | + owns isbn-10 @unique, |
| 26 | + owns title, |
| 27 | + owns page-count, |
| 28 | + owns genre @card(0..), |
| 29 | + owns price, |
| 30 | + plays contribution:work, |
| 31 | + plays publishing:published, |
| 32 | + plays promotion-inclusion:item, |
| 33 | + plays order-line:item, |
| 34 | + plays rating:rated, |
| 35 | + plays recommendation:recommended; |
| 36 | + |
| 37 | +entity hardback sub book, |
| 38 | + owns stock; |
| 39 | + |
| 40 | +entity paperback sub book, |
| 41 | + owns stock; |
| 42 | + |
| 43 | +entity ebook sub book; |
| 44 | + |
| 45 | +entity contributor, |
| 46 | + owns name, |
| 47 | + plays contribution:contributor, |
| 48 | + plays authoring:author, |
| 49 | + plays editing:editor, |
| 50 | + plays illustrating:illustrator; |
| 51 | + |
| 52 | +entity company @abstract, |
| 53 | + owns name; |
| 54 | + |
| 55 | +entity publisher sub company, |
| 56 | + plays publishing:publisher; |
| 57 | + |
| 58 | +entity courier sub company, |
| 59 | + plays delivery:deliverer; |
| 60 | + |
| 61 | +entity publication, |
| 62 | + owns year, |
| 63 | + plays publishing:publication, |
| 64 | + plays locating:located; |
| 65 | + |
| 66 | +entity user, |
| 67 | + owns id @key, |
| 68 | + owns name, |
| 69 | + owns birth-date, |
| 70 | + owns total-spending, |
| 71 | + owns loyalty-tier, |
| 72 | + plays action-execution:executor, |
| 73 | + plays locating:located, |
| 74 | + plays recommendation:recipient; |
| 75 | + |
| 76 | +entity order, |
| 77 | + owns id @key, |
| 78 | + owns status, |
| 79 | + plays order-line:order, |
| 80 | + plays action-execution:action, |
| 81 | + plays delivery:delivered; |
| 82 | + |
| 83 | +entity promotion, |
| 84 | + owns code @key, |
| 85 | + owns name, |
| 86 | + owns start-timestamp, |
| 87 | + owns end-timestamp, |
| 88 | + plays promotion-inclusion:promotion; |
| 89 | + |
| 90 | +entity review, |
| 91 | + owns id @key, |
| 92 | + owns score, |
| 93 | + owns verified, |
| 94 | + plays rating:review, |
| 95 | + plays action-execution:action; |
| 96 | + |
| 97 | +entity login, |
| 98 | + owns success, |
| 99 | + plays action-execution:action; |
| 100 | + |
| 101 | +entity address, |
| 102 | + owns street, |
| 103 | + plays delivery:destination, |
| 104 | + plays locating:located; |
| 105 | + |
| 106 | +entity place @abstract, |
| 107 | + owns name, |
| 108 | + plays locating:located, |
| 109 | + plays locating:location; |
| 110 | + |
| 111 | +entity city sub place; |
| 112 | + |
| 113 | +entity state sub place; |
| 114 | + |
| 115 | +entity country sub place; |
| 116 | + |
| 117 | +relation contribution, |
| 118 | + relates contributor, |
| 119 | + relates work; |
| 120 | + |
| 121 | +relation authoring sub contribution, |
| 122 | + relates author as contributor; |
| 123 | + |
| 124 | +relation editing sub contribution, |
| 125 | + relates editor as contributor; |
| 126 | + |
| 127 | +relation illustrating sub contribution, |
| 128 | + relates illustrator as contributor; |
| 129 | + |
| 130 | +relation publishing, |
| 131 | + relates publisher, |
| 132 | + relates published, |
| 133 | + relates publication; |
| 134 | + |
| 135 | +relation promotion-inclusion, |
| 136 | + relates promotion, |
| 137 | + relates item, |
| 138 | + owns discount; |
| 139 | + |
| 140 | +relation order-line, |
| 141 | + relates order, |
| 142 | + relates item, |
| 143 | + owns quantity; |
| 144 | + |
| 145 | +relation rating, |
| 146 | + relates review, |
| 147 | + relates rated; |
| 148 | + |
| 149 | +relation action-execution, |
| 150 | + relates action, |
| 151 | + relates executor, |
| 152 | + owns timestamp; |
| 153 | + |
| 154 | +relation delivery, |
| 155 | + relates deliverer, |
| 156 | + relates delivered, |
| 157 | + relates destination; |
| 158 | + |
| 159 | +relation locating, |
| 160 | + relates located, |
| 161 | + relates location; |
| 162 | + |
| 163 | +relation recommendation, |
| 164 | + relates recommended, |
| 165 | + relates recipient; |
| 166 | + |
| 167 | +attribute isbn @abstract, value string; |
| 168 | +attribute isbn-13 sub isbn; |
| 169 | +attribute isbn-10 sub isbn; |
| 170 | +attribute title, value string; |
| 171 | +attribute page-count, value integer; |
| 172 | +attribute genre, value string; |
| 173 | +attribute stock, value integer; |
| 174 | +attribute price, value double; |
| 175 | +attribute discount, value double; |
| 176 | +attribute id, value string; |
| 177 | +attribute code, value string; |
| 178 | +attribute name, value string; |
| 179 | +attribute birth-date, value datetime; |
| 180 | +attribute street, value string; |
| 181 | +attribute year, value integer; |
| 182 | +attribute quantity, value integer; |
| 183 | +attribute score, value integer; |
| 184 | +attribute verified, value boolean; |
| 185 | +attribute timestamp, value datetime; |
| 186 | +attribute start-timestamp, value datetime; |
| 187 | +attribute end-timestamp, value datetime; |
| 188 | +attribute status, value string @values("invalid", "pending", "paid", "dispatched", "delivered", "returned", "canceled"); |
| 189 | +attribute success, value boolean; |
| 190 | +attribute total-spending, value double; |
| 191 | +attribute loyalty-tier, value integer @range(0..5); |
| 192 | + |
| 193 | +# TODO: Change to check |
| 194 | +fun is_review_verified_by_purchase($review: review) -> { order }: |
| 195 | + match |
| 196 | + ($review, $product) isa rating; |
| 197 | + ($order, $product) isa order-line; |
| 198 | + ($user, $review) isa action-execution, has timestamp $review-time; |
| 199 | + ($user, $order) isa action-execution, has timestamp $order-time; |
| 200 | + $review-time > $order-time; |
| 201 | + return { $order }; |
| 202 | + |
| 203 | +fun book_recommendations_for($user: user) -> {book}: |
| 204 | + match |
| 205 | + $new-book isa book; |
| 206 | + { |
| 207 | + let $new-book in book_recommendations_by_author($user); |
| 208 | + } or { |
| 209 | + let $new-book in book_recommendations_by_genre($user); |
| 210 | + }; |
| 211 | + return { $new-book }; |
| 212 | + |
| 213 | +fun book_recommendations_by_genre($user: user) -> { book }: |
| 214 | +match |
| 215 | + $user isa user; |
| 216 | + $liked-book isa book; |
| 217 | + { |
| 218 | + ($user, $order-for-liked) isa action-execution; |
| 219 | + ($order-for-liked, $liked-book) isa order-line; |
| 220 | + } or { |
| 221 | + ($user, $review-for-liked) isa action-execution; |
| 222 | + ($review-for-liked, $liked-book) isa rating; |
| 223 | + $review-for-liked has score >= 7; |
| 224 | + }; |
| 225 | + $new-book isa book; |
| 226 | + not { { |
| 227 | + ($user, $order-for-new) isa action-execution; |
| 228 | + ($order-for-new, $new-book) isa order-line; |
| 229 | + } or { |
| 230 | + ($user, $review-for-new) isa action-execution; |
| 231 | + ($review-for-new, $new-book) isa rating; |
| 232 | + }; }; |
| 233 | + $liked-book has genre $shared-genre; |
| 234 | + $new-book has genre $shared-genre; |
| 235 | + not { { |
| 236 | + $shared-genre == "fiction"; |
| 237 | + } or { |
| 238 | + $shared-genre == "nonfiction"; |
| 239 | + }; }; |
| 240 | + return { $new-book }; |
| 241 | + |
| 242 | +fun book_recommendations_by_author($user: user) -> { book }: |
| 243 | + match |
| 244 | + $user isa user; |
| 245 | + $liked-book isa book; |
| 246 | + { |
| 247 | + ($user, $order-for-liked) isa action-execution; |
| 248 | + ($order-for-liked, $liked-book) isa order-line; |
| 249 | + } or { |
| 250 | + ($user, $review-for-liked) isa action-execution; |
| 251 | + ($review-for-liked, $liked-book) isa rating; |
| 252 | + $review-for-liked has score >= 7; |
| 253 | + }; |
| 254 | + $new-book isa book; |
| 255 | + not { { |
| 256 | + ($user, $order-for-new) isa action-execution; |
| 257 | + ($order-for-new, $new-book) isa order-line; |
| 258 | + } or { |
| 259 | + ($user, $review-for-new) isa action-execution; |
| 260 | + ($review-for-new, $new-book) isa rating; |
| 261 | + }; }; |
| 262 | + ($liked-book, $shared-author) isa authoring; |
| 263 | + ($new-book, $shared-author) isa authoring; |
| 264 | + return { $new-book }; |
| 265 | + |
| 266 | +fun order_line_best_price($line: order-line) -> { double }: |
| 267 | + match |
| 268 | + ($order) isa action-execution, has timestamp $order-time; |
| 269 | + $line isa order-line, links ($order, $item); |
| 270 | + $item has price $retail-price; |
| 271 | + let $time_value = $order-time; |
| 272 | + let $best-discount = best_discount_for_item($item, $time_value); |
| 273 | + let $discounted-price = round(100 * $retail-price * (1 - $best-discount)) / 100; |
| 274 | + $line has quantity $quantity; |
| 275 | + let $line-total = $quantity * $discounted-price; |
| 276 | + return { $line-total }; |
| 277 | + |
| 278 | +fun best_discount_for_item($item: book, $order-time: datetime) -> double: |
| 279 | + match |
| 280 | + { |
| 281 | + $inclusion isa promotion-inclusion, |
| 282 | + links ($promotion, $item), |
| 283 | + has discount $discount-attr; |
| 284 | + $promotion has start-timestamp <= $order-time, |
| 285 | + has end-timestamp >= $order-time; |
| 286 | + let $discount = $discount-attr; |
| 287 | + } or { |
| 288 | + let $discount = 0.0; # default |
| 289 | + }; |
| 290 | +return max($discount); |
| 291 | + |
| 292 | +fun transitive_places($place: place) -> { place }: |
| 293 | + match |
| 294 | + { |
| 295 | + locating (located: $place, location: $parent); |
| 296 | + } or { |
| 297 | + locating (located: $place, location: $middle); |
| 298 | + let $parent in transitive_places($middle); |
| 299 | + }; |
| 300 | + return { $parent }; |
0 commit comments