-
Notifications
You must be signed in to change notification settings - Fork 0
/
Data.hs
160 lines (114 loc) · 3.04 KB
/
Data.hs
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
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE EmptyDataDeriving #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TemplateHaskell #-}
module TPCC.Data where
import Data.Aeson
import Data.Map (Map)
import qualified Data.Map as Map
import Data.Maybe (catMaybes)
import Data.CARD
import Data.CARD.Counter
import Data.CARD.Map
import Data.CARD.Maybe
import GHC.Generics
import Lens.Micro.Platform
type UC = UniversalC
type CustomerId = String
type ItemId = String
type ReplicaId = Int
type OrderId = (ReplicaId,Int)
type CarrierId = String
type WarehouseId = String
data OrderLine
= OrderLine { _olItemId :: ItemId
, _olQuantity :: Int
, _olAmount :: Int
}
deriving (Show,Read,Eq,Ord,Generic)
makeLenses ''OrderLine
data OrderInfo
= OrderInfo { _oiCId :: CustomerId
, _oiDate :: String
, _oiLines :: [OrderLine]
}
deriving (Show,Read,Eq,Ord,Generic)
makeLenses ''OrderInfo
type Order = (OrderInfo, Maybe CarrierId)
mkOrder :: CustomerId -> String -> [OrderLine] -> Order
mkOrder a b c = (OrderInfo a b c,Nothing)
oCId :: Lens' Order CustomerId
oCId = _1 . oiCId
oDate :: Lens' Order String
oDate = _1 . oiDate
oLines :: Lens' Order [OrderLine]
oLines = _1 . oiLines
oCarrierId :: Lens' (a,b) b
oCarrierId = _2
type OrderE = ((), MaybeE () CarrierId)
type OrderC = (UC (), UC (MaybeC ()))
setCarrierId :: CarrierId -> OrderE
setCarrierId i = mempty & oCarrierId .~ (insertE i)
data Item
= Item { _iName :: String
, _iPrice :: Int
}
deriving (Show,Read,Eq,Ord,Generic)
makeLenses ''Item
type Stock = (Int,Int,Int,Int)
sQuantity :: Lens' (a,b,c,d) a
sQuantity = _1
sYtd :: Lens' (a,b,c,d) b
sYtd = _2
sOrderCount :: Lens' (a,b,c,d) c
sOrderCount = _3
sRemoteCount :: Lens' (a,b,c,d) d
sRemoteCount = _4
type IntE = CounterE Int
type IntC = CounterC Int
type StockE
= ( IntE
, IntE
, IntE
, IntE
)
type StockC = (IntC, IntC, IntC, IntC)
data CustomerInfo
= CustomerInfo { _ciName :: String }
deriving (Show,Read,Eq,Ord,Generic)
makeLenses ''CustomerInfo
type Customer = (CustomerInfo, Int)
type CustomerE = ((), IntE)
type CustomerC = (UC (), IntC)
cName :: Lens' Customer String
cName = _1 . ciName
cBalance :: Lens' Customer Int
cBalance = _2
type Tpcc
= ( Map (WarehouseId, ItemId) Stock -- stock
, Map ItemId Item -- items
, Map CustomerId Customer -- customers
, Map OrderId Order -- orders
)
type TpccE
= ( MapE (WarehouseId, ItemId) StockE Stock
, MapE ItemId () Item
, MapE CustomerId CustomerE Customer
, MapE OrderId OrderE Order
)
type TpccC
= ( MapC (WarehouseId, ItemId) StockC
, MapC ItemId (UC ())
, MapC CustomerId CustomerC
, MapC OrderId OrderC
)
tpccStock :: Lens' Tpcc (Map (WarehouseId, ItemId) Stock)
tpccStock = _1
tpccItems :: Lens' Tpcc (Map ItemId Item)
tpccItems = _2
tpccCustomers :: Lens' Tpcc (Map CustomerId Customer)
tpccCustomers = _3
tpccOrders :: Lens' Tpcc (Map OrderId Order)
tpccOrders = _4