-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathNewspaperExample.hs
162 lines (154 loc) · 8.6 KB
/
NewspaperExample.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
161
162
{-# LANGUAGE OverloadedStrings #-}
module ClientServerSpec where
import Types
import Data.Map as M
import TypeHelpers
outputDirectory = "NewspaperExample"
articleType :: CustomT
articleType = ct -- helper to make custom types
"Article" -- name of type (Elm syntax rules)
[("Article",[dt StringT "title"{-valid function name, used in helper functions-} "title of the article"
,dt StringT "author" "author"
,dt (IntRangeT 0 999999999) "timestamp" "seconds since 1970" -- warning Y2.286K bug
,dt StringT "body" "article body"
]
)
,("Letter",[dt StringT "title" "title of the article being referred to"
,dt StringT "author" "author"
,dt (IntRangeT 0 999999999) "timestamp" "seconds since 1970" -- warning Y2.286K bug
,dt StringT "body" "article body"
]
)
]
draftType :: CustomT
draftType = ct -- helper to make custom types
"Draft" -- name of type (Elm syntax rules)
[("DraftArticle", [dt StringT "title"{-valid function name, used in helper functions-} "title of the article"
,dt StringT "author" "author"
,dt (IntRangeT 0 999999999) "timestamp" "seconds since 1970" -- warning Y2.286K bug
,dt StringT "body" "article body"
,dt (ListT $ dt (PairT (dt (IntRangeT 0 99999) "uid" "userid")
(dt StringT "comment" "comment")
)
"uidComment" "(uid,comment)"
) "comments" "[(uid,comment)]"
]
)
,("DraftLetter", [dt StringT "title"{-valid function name, used in helper functions-} "title of the article"
,dt StringT "author" "author"
,dt (IntRangeT 0 999999999) "timestamp" "seconds since 1970" -- warning Y2.286K bug
,dt StringT "body" "article body"
,dt (ListT $ dt (PairT (dt (IntRangeT 0 99999) "uid" "userid")
(dt StringT "comment" "comment")
)
"uidComment" "(uid,comment)"
) "comments" "[(uid,comment)]"
]
)
]
newspaperNet :: Net
newspaperNet =
let
mainStreet =
HybridPlace "MainStreet"
[] --server state
[] --player state
[] --client state
Nothing
(Nothing, Nothing)
Nothing
readingRoom =
HybridPlace "ReadingRoom"
[dt (ListT $ dt (TypeT "Article") "article" "") "articles" ""] --server state
[dt StringT "nowReading" "title of current article being read"] --player state
[dt (ListT $ dt (TypeT "Article") "article" "") "articles" "" -- partial list of articles
,dt (ListT $ dt StringT "title" "") "titles" "" -- all article titles
,dt (MaybeT (dt StringT "viewing" "title of article begin viewed")) "maybeViewing" "article being viewed or Nothing for index"] --client state
Nothing
(Nothing, Nothing)
Nothing
editingRoom =
HybridPlace "EditingRoom"
[dt (ListT $ dt (TypeT "Draft") "drafts" "") "articles" ""] --server state
[dt (MaybeT (dt StringT "nowEditing" "title of current article being read")) "maybeEditing" "article being edited or Nothing for index"] --player state
[dt (MaybeT (dt (TypeT "Draft") "article" "article currently being edited")) "maybeEditing" "article being edited or Nothing for index"
,dt (ListT $ dt StringT "title" "") "titles" "" -- all article titles
] --client state
Nothing
(Nothing, Nothing)
Nothing
enterRR =
Transition
(constructor "EnterReadingRoom" [])
[("MainStreet", Just ("ReadingRoom", constructor "DidEnterReadingRoom" [dt (ListT $ dt (TypeT "Article") "article" "") "articles" ""]))]
Nothing
enterER =
Transition
(constructor "EnterEditingRoom" [])
[("MainStreet", Just ("ReadingRoom", constructor "DidEnterEditingRoom" [dt (ListT $ dt StringT "title" "") "articles" ""]))]
Nothing
startEditing =
Transition
(constructor "StartEditing" [dt StringT "title" "article to start editing"])
[("EditingRoom", Just ("EditingRoom", constructor "DidStartEditing" [dt (TypeT "Draft") "draft" "article to edit"]))]
Nothing
leaveRR =
Transition
(constructor "LeaveReadingRoom" [])
[("ReadingRoom", Just ("MainStreet", constructor "DidLeaveReadingRoom" []))]
Nothing
leaveER =
Transition
(constructor "LeaveEditingRoom" [])
[("EditingRoom", Just ("MainStreet", constructor "DidLeaveEditingRoom" []))]
Nothing
publishArticle =
Transition
(constructor "PublishArticle" [])
[("EditingRoom", Just ("ReadingRoom", constructor "DidPublish" [dt (ListT $ dt (TypeT "Article") "article" "") "articles" ""]))
,("EditingRoom", Nothing)] -- if you are not editing, then go back to the same place
Nothing
saveDraft =
Transition
(constructor "SaveDraft" [dt (TypeT "Draft") "draft" "edited draft"])
[("EditingRoom", Just ("EditingRoom", constructor "DidSaveDraft" [dt (ListT $ dt StringT "article" "article title") "articles" "titles of all drafts"]))
] -- if you are not editing, then go back to the same place
Nothing
enterTitle =
Transition
(constructor "EnterTitle" [dt StringT "title" "edited title"])
[("EditingRoom", Just ("EditingRoom", constructor "DidEnterTitle" [dt (ListT $ dt StringT "article" "article title") "articles" "titles of all drafts"]))
] -- if you are not editing, then go back to the same place
Nothing
enterText =
Transition
(constructor "EnterText" [dt StringT "text" "edited text"])
[("EditingRoom", Just ("EditingRoom", constructor "DidEnterText" [dt (ListT $ dt StringT "article" "article title") "articles" "titles of all drafts"]))
] -- if you are not editing, then go back to the same place
Nothing
enterComment =
Transition
(constructor "EnterComment" [dt StringT "comment" "edited comment"])
[("EditingRoom", Just ("EditingRoom", constructor "DidEnterComment" [dt StringT "comment" "edited comment"]))
] -- if you are not editing, then go back to the same place
Nothing
postComment =
Transition
(constructor "PostComment" [dt StringT "comment" "edited comment"])
[("EditingRoom", Just ("EditingRoom", constructor "DidPostComment" [dt StringT "comment" "edited comment"]))
] -- if you are not editing, then go back to the same place
Nothing
in
HybridNet
"NewspaperExample"
"MainStreet"
[mainStreet,readingRoom,editingRoom]
[(HybridTransition,enterRR),(HybridTransition,enterER),(HybridTransition,startEditing),(HybridTransition,leaveRR),(HybridTransition,leaveER),(HybridTransition,publishArticle),(HybridTransition,saveDraft),(HybridTransition,enterTitle),(HybridTransition,enterText),(HybridTransition,enterComment),(HybridTransition,postComment)]
[]
clientServerApp :: ClientServerApp
clientServerApp =
( "NewspaperExample" --starting net for a client
, [newspaperNet] --all the nets in this client/server app
, [articleType,draftType] --extra client types used in states or messages
, [articleType,draftType] --extra server types used in states or messages
)