-
Notifications
You must be signed in to change notification settings - Fork 0
/
family.maude
217 lines (169 loc) · 7.28 KB
/
family.maude
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
load parse_msg.maude
load socket.maude
mod OO-POPULATION is
protecting NAT .
protecting STRING-LIST .
protecting PARSE-MSG .
protecting SOCKET .
subsort String < Oid .
*** Message for separating from spouse:
op separate : Oid -> Msg [ctor] .
sort Status .
op single : -> Status [ctor] .
ops engaged married separated : Oid -> Status [ctor] .
op relationshipStatus : Oid Status -> Msg .
*** Socket stuff
op Client : -> Cid .
op aClient : -> Oid .
*** Socket-related equations
op createClientSocket : String Nat -> Msg .
op plQuery : String String Nat -> Msg . *** Prolog Query, IP, PORT
ops plMsg pendingMsg : String -> Msg .
*** need some way of saying if the terms brother(O, O') or (not brother(O, O')) does not exist,
*** then the prolog server should be queried
*** * use a function neg : Bool -> Bool (and subsort Bool < Msg) for not brother(O, O')
*** * then use the contains function to check if neither of brother(O, O') nor neg(brother(O, O') exists, then query prolog
vars X X' X'' X''' X'''' NAME1 NAME2 : String .
vars FATHER MOTHER : Oid .
vars N N' : Nat .
vars S S' : Status .
vars C C' Conf : Configuration .
vars O O' : Oid .
sort State .
op {_} : Configuration -> State [ctor] .
op contains : Configuration State -> Bool .
eq contains(C, {none}) = false . --- plQuery(c)
eq contains(C, {C' Conf}) = if C == C' then true else contains(C, {Conf}) fi .
crl [engagement] :
{ relationshipStatus(O, single) ---age(O, N)
relationshipStatus(O', single) ---age(O', N')
Conf }
=>
{ relationshipStatus(O, engaged(O')) ---age(O, N)
relationshipStatus(O', engaged(O)) ---age(O', N')
Conf }
if contains(neg(sibling(O, O')), {Conf}) or contains(neg(sibling(O', O)), {Conf})
/\ contains(neg(parent(O, O')), {Conf})
/\ contains(neg(parent(O', O)), {Conf}) .
vars SOCKET : Oid .
var A : AttributeSet .
vars IP MSG PLMSG QUERY : String .
vars PORT : Nat .
*** Sending queries to prolog works as follows:
*** - plQuery(QUERY, IP, PORT) is created
***
*** - plQuery is split into:
******* + plMsg(QUERY)
******* + createClientTcpSocket(socketManager, O, IP, PORT)
***
*** - createClientTcpSocket() turns into createdSocket(Oid, socketManager, SOCKET) if
*** maude successfully connected to the prolog server
*** (else socketError(ClientOid, socketManager, REASON))
***
*** - createdSocket(Oid, socketManager, SOCKET) and plMsg(QUERY) turns into:
******* + send(SOCKET, OID, QUERY)
******* + pendingMsg(QUERY) (without \n in the end).
******* pendingMsg() is used so each query to the prolog server is only done once
***
*** - send() turns into sent() and then received(Oid, SOCKET, PLMSG) if successful (prolog msg)
***
*** - received(Oid, SOCKET, PLMSG) and pendingMsg(QUERY) is turned into a term if
*** QUERY is a substring of PLMSG
rl [createSocket] :
plQuery(QUERY, IP, PORT)
< O : Client | A >
=>
plMsg(QUERY)
< O : Client | A >
createClientTcpSocket(socketManager, O, IP, PORT) .
--- må ha disse i alle regler hvor kommunikasjon med serveren er nødvendig?
rl [createdSocket] :
< O : Client | A >
createdSocket(O, socketManager, SOCKET)
plMsg(QUERY)
=>
< O : Client | A >
send(SOCKET, O, QUERY)
pendingMsg(removeTrailingNewline(QUERY)) .
rl [sent] :
< O : Client | A >
sent(O, SOCKET)
=>
< O : Client | A >
receive(SOCKET, O) .
*** be careful, this could lead to unwanted behavior? ***
op _substrof_ : String String -> Bool .
ceq X substrof X' = false if find(X', X, 0) = notFound .
---ceq X substrof X' = true if find(X', X, 0) >= 0 .
eq X substrof X' = true [owise] .
--- transform message received from prolog server to a term
crl [receivedReply] :
< O : Client | A >
received(O, SOCKET, PLMSG)
pendingMsg(QUERY)
=>
< O : Client | A >
msgToTerm(PLMSG)
if QUERY substrof PLMSG . *** make a better way to do this?
*** ^ doing it like this because
*** if sent message is "sibling(anna,thomas)\n"
*** the received message can be:
*** * sibling(anna,thomas)end_of_file or
*** * neg(sibling(anna,thomas))end_of_file
crl [engagementSiblingQuery] :
{ relationshipStatus(NAME1, single)
relationshipStatus(NAME2, single)
Conf }
=>
{ relationshipStatus(NAME1, single)
relationshipStatus(NAME2, single)
Conf
plQuery("sibling(" + NAME1 + "," + NAME2 + ")\n", "localhost", 3055) }
if not (contains(sibling(NAME1, NAME2), {Conf}))
/\ not (contains(sibling(NAME2, NAME1), {Conf}))
/\ not (contains(neg(sibling(NAME1, NAME2)), {Conf}))
/\ not (contains(neg(sibling(NAME2, NAME1)), {Conf}))
/\ not (contains(plQuery("sibling(" + NAME1 + "," + NAME2 + ")\n", "localhost", 3055), {Conf}))
/\ not (contains(plQuery("sibling(" + NAME2 + "," + NAME1 + ")\n", "localhost", 3055), {Conf}))
/\ not (contains(plMsg("sibling(" + NAME1 + "," + NAME2 + ")\n"), {Conf}))
/\ not (contains(plMsg("sibling(" + NAME2 + "," + NAME1 + ")\n"), {Conf}))
/\ not (contains(pendingMsg("sibling(" + NAME1 + "," + NAME2 + ")"), {Conf}))
/\ not (contains(pendingMsg("sibling(" + NAME2 + "," + NAME1 + ")"), {Conf})) .
crl [engagementParentQuery] :
{ relationshipStatus(NAME1, single)
relationshipStatus(NAME2, single)
Conf }
=>
{ relationshipStatus(NAME1, single)
relationshipStatus(NAME2, single)
Conf
plQuery("parent(" + NAME1 + "," + NAME2 + ")\n", "localhost", 3055)
plQuery("parent(" + NAME2 + "," + NAME1 + ")\n", "localhost", 3055) }
if not (contains(parent(NAME1, NAME2), {Conf}))
/\ not (contains(neg(parent(NAME1, NAME2)), {Conf}))
/\ not (contains(plQuery("parent(" + NAME1 + "," + NAME2 + ")\n", "localhost", 3055), {Conf}))
/\ not (contains(plMsg("parent(" + NAME1 + "," + NAME2 + ")\n"), {Conf}))
/\ not (contains(pendingMsg("parent(" + NAME1 + "," + NAME2 + ")"), {Conf})) .
ops test test2 test3 : Nat -> Configuration .
eq test(PORT) =
<> < aClient : Client | none >
male("thomas") female("anna")
relationshipStatus("thomas", single)
relationshipStatus("anna", single) .
eq test2(PORT) =
<> < aClient : Client | none >
male("rolf") female("maria") female("anna")
relationshipStatus("rolf", single)
relationshipStatus("anna", single)
relationshipStatus("maria", single) .
eq test3(PORT) =
<> < aClient : Client | none >
male("thomas") male("rolf") female("anna") female("maria")
relationshipStatus("thomas", single)
relationshipStatus("rolf", single)
relationshipStatus("anna", single)
relationshipStatus("maria", single) .
endm
--- search =>* female("Hera") C:Configuration
--- funker ikke fordi spesifikasjonen ikke er terminerende. bruk heller:
--- search [1] =>* female("Hera") C:Configuration