-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathreal_estate.clj
276 lines (234 loc) · 7.43 KB
/
real_estate.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
(ns examples.real-estate
"Read along: http://curtclifton.net/papers/MoseleyMarks06a.pdf"
(:require [com.wotbrew.relic :as rel]
[clojure.string :as str]))
;; *functional* relational programming
;; just use clojure functions & predicates on rows
(defn- price-band [price]
(condp >= price
300000M :low
650000M :med
1000000M :high
:premium))
(defn- area-code [address]
;; we are just pretending
(last (str/split address #" ")))
(defn- speed-band [date1 date2]
(let [day-ms (* 1000 60 60 24)]
(condp >= (/ (- (inst-ms date1) (inst-ms date2)) day-ms)
10 :very-fast
20 :fast
30 :medium
60 :slow
:very-slow)))
;; derived relvars
(def RoomInfo
[[:from ::Room]
[:extend [:room-size [* :width :breadth]]]])
(def Acceptance
[[:from ::Decision]
[:where :accepted]
[:without :accepted]])
(def Rejection
[[:from ::Decision]
[:where [not :accepted]]
[:without :accepted]])
(def PropertyInfo
[[:from ::Property]
[:extend
[:price-band [price-band :price]]
[:area-code [area-code :address]]
[[:number-of-rooms]
[rel/sel1
[[:from RoomInfo]
[:agg [:address] [:number-of-rooms count]]]
{:address :address}]]
[[:square-feet]
[rel/sel1
[[:from RoomInfo]
[:agg [:address] [:square-feet [rel/sum :room-size]]]]
{:address :address}]]]])
(def CurrentOffer
[[:from ::Offer]
[:agg [:address :bidder-name :bidder-address]
[:latest-date [max :offer-date]]]
[:join ::Offer {:address :address
:bidder-name :bidder-name
:bidder-address :bidder-address
:latest-date :offer-date}]])
(def RawSales
[[:from Acceptance]
[:join CurrentOffer {:address :address
:bidder-name :bidder-name
:bidder-address :bidder-address}]
[:join ::Property {:address :address}]
[:without :offer-date :bidder-name :bidder-address]])
(def SoldProperty
[[:from RawSales]
[:select :address]])
(def UnsoldProperty
[[:from ::Property]
[:select :address]
[:difference SoldProperty]])
(def SalesInfo
[[:from RawSales]
[:select
:address
:agent
[:area-code [area-code :address]]
[:sale-speed [speed-band :date-registered :decision-date]]
[:price-band [price-band :offer-price]]]])
(def SalesCommissions
[[:from SalesInfo]
[:join ::Commission {:agent :agent
:sale-speed :sale-speed
:price-band :price-band}]
[:select :address :agent :commission]])
;; external
(def OpenOffers
[[:from CurrentOffer]
[:join [[:from CurrentOffer]
[:without :offer-price :latest-date]
[:difference [[:from ::Decision]
[:without :accepted :decision-date]]]]
{:bidder-name :bidder-name
:bidder-address :bidder-address
:address :address}]])
(def PropertyForWebsite
[[:from UnsoldProperty]
[:join PropertyInfo {:address :address}]
[:select :address :price :photo :number-of-rooms :square-feet]])
(def CommissionDue
[[:from SalesCommissions]
[:agg [:agent]
[:total-commission [rel/sum :commission]]]
[:select :agent :total-commission]])
;; constraints
(def PropertyKey
[[:from ::Property]
[:unique :address]])
(def OfferKey
[[:from ::Offer]
[:unique :address :offer-date :bidder-name :bidder-address]])
(def DecisionKey
[[:from ::Decision]
[:unique :address :offer-date :bidder-name :bidder-address]])
(def RoomKey
[[:from ::Room]
[:unique :address :room-name]])
(def FloorKey
[[:from ::Floor]
[:unique :address :room-name]])
(def CommissionKey
[[:from ::Commission]
[:unique :price-band :area-code :sale-speed]])
(def OfferToProperty
[[:from ::Offer]
[:fk ::Property {:address :address}]])
(def DecisionToOffer
[[:from ::Decision]
[:fk ::Offer {:address :address
:offer-date :offer-date
:bidder-name :bidder-name
:bidder-address :bidder-address}]])
(def RoomToProperty
[[:from ::Room]
[:fk ::Property {:address :address}]])
(def FloorToProperty
[[:from ::Floor]
[:fk ::Property {:address :address}]])
(def PropertyHasToHaveAtLeastOneRoom
[[:from PropertyInfo]
[:check [:? <= 1 :number-of-rooms]]])
(def CannotBidOnOwnProperty
[[:from ::Offer]
[:check [not= :address :bidder-address]]])
(def CannotSubmitOfferOnceSaleAgreed
[[:from ::Offer]
[:join [[:from Acceptance] [:select :address :decision-date]] {:address :address}]
[:check {:pred [:? <= :offer-date :decision-date]
:error "Offer cannot be submitted after acceptance."}]])
(def NoMoreThan50AdvertisedPremiumProperties
[[:from PropertyForWebsite]
[:agg [] [:premium-count [count [= [:_ :premium] [price-band :price]]]]]
[:check {:pred [:<= :premium-count 50]
:error "At most 50 properties can be advertised on the website."}]])
(def NoSingleBidderCanSubmitMoreThan10OffersOnAProperty
[[:from ::Offer]
[:agg [:address :bidder-address :bidder-name] [:number-of-offers count]]
[:check {:pred [<= :number-of-offers 10]
:error "No single bidder can submit more than 10 offers on a property."}]])
(defn constrain [db]
(rel/mat
db
PropertyKey
OfferKey
DecisionKey
RoomKey
FloorKey
CommissionKey
OfferToProperty
DecisionToOffer
RoomToProperty
FloorToProperty
PropertyHasToHaveAtLeastOneRoom
CannotBidOnOwnProperty
CannotSubmitOfferOnceSaleAgreed
NoMoreThan50AdvertisedPremiumProperties
NoSingleBidderCanSubmitMoreThan10OffersOnAProperty))
(defn new-database []
(-> {}
(constrain)
(rel/mat PropertyInfo)))
(def data
(let [address1 "abc def 55"
alice-address "wonderland 42"]
{::Property [{:address address1
:price 344000M
:photo "foo.jpg"
:agent "bob"
:date-registered #inst "2021-10-26"}
{:address alice-address
:price 1690000M
:photo "foo.jpg"
:agent "bob"
:date-registered #inst "2021-10-27"}]
::Offer [{:address address1
:offer-date #inst "2021-10-27"
:offer-price 343000M
:bidder-name "alice"
:bidder-address alice-address}
{:address address1
:offer-date #inst "2021-10-26"
:offer-price 150000M
:bidder-name "alice"
:bidder-address alice-address}]
::Decision [{:address address1
:offer-date #inst "2021-10-27"
:bidder-name "alice"
:bidder-address alice-address
:decision-date #inst "2021-10-28"
:accepted true}]
::Room [{:address address1
:room-name "Room 1"
:width 10.0M
:breadth 10.0M
:type :living-room}
{:address alice-address
:room-name "Room 1"
:width 12.0M
:breadth 14.5M
:type :living-room}
{:address alice-address
:room-name "Kitchen"
:width 13.0M
:breadth 8.0M
:type :kitchen}]
::Floor [{:address address1
:room-name "Room 1"
:floor 0}]
::Commission [{:agent "bob"
:price-band :med
:area-code "55"
:sale-speed :very-fast
:commission 2000.0M}]}))