-
Notifications
You must be signed in to change notification settings - Fork 1
/
if_prop.py
415 lines (324 loc) · 14 KB
/
if_prop.py
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
# SPDX-License-Identifier: AGPL-3.0-or-later
# Copyright (C) 2022-2023 Dyne.org foundation <[email protected]>.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# Let's start with some lovely imports that should have been installed if not available by default
import json
import inspect
from datetime import datetime, timezone
from pdb import set_trace
from if_lib import send_signed
from if_consts import SUPPORTED_ACTIONS, IN_PR_ACTIONS, OUT_PR_ACTIONS, IN_OUT_PR_ACTIONS
from if_consts import EVENT_FRAG, AGENT_FRAG, QUANTITY_FRAG, RESOURCE_FRAG, PROPOSAL_FRAG, \
INTENT_FRAG, PROPINT_FRAG, LOCATION_FRAG, ACTION_FRAG, PROCESS_FRAG, PROCESSSPEC_FRAG, \
UNIT_FRAG, RESSPEC_FRAG, PROCESSGRP_FRAG
DEBUG_create_proposal = False
def create_proposal(proposal, user_data, endpoint):
# ts = datetime.now(timezone.utc).isoformat()
variables = {
"proposal": {
"name": proposal['name'],
"note": proposal['note'],
"unitBased": proposal['unitBased'],
"hasBeginning": proposal['hasBeginning'],
"hasEnd": proposal['hasEnd'],
"eligibleLocation": proposal['eligibleLocation']
}
}
query = """mutation($proposal:ProposalCreateParams!) {
createProposal(proposal:$proposal) {
proposal {
...proposal
}
}
}""" + PROPOSAL_FRAG + UNIT_FRAG + RESSPEC_FRAG + INTENT_FRAG + PROPINT_FRAG + AGENT_FRAG + LOCATION_FRAG + \
QUANTITY_FRAG + RESOURCE_FRAG + ACTION_FRAG + PROCESS_FRAG + PROCESSGRP_FRAG + PROCESSSPEC_FRAG
res_json = send_signed(query, variables, user_data['username'], user_data['keyring']['eddsa'], endpoint)
if 'errors' in res_json:
print("Error message")
print(json.dumps(res_json['errors'], indent=2))
print("Query")
print(query)
print("Variables")
print(variables)
raise Exception(f"Error in function {inspect.stack()[0][3]}")
if DEBUG_create_proposal:
print("Query")
print(query)
print("Variables")
print(variables)
print("Result")
print(json.dumps(res_json, indent=2))
proposal['id'] = res_json['data']['createProposal']['proposal']['id']
# Wrapper for process creation
def get_proposal(name, proposal_data, note, user_data, hasBeginning, hasEnd, eligibleLocation, unitBased, endpoint):
if name in proposal_data:
return
proposal_data[f'{name}'] = {}
cur_proposal = proposal_data[f'{name}']
cur_proposal['name'] = name
cur_proposal['note'] = note
cur_proposal['hasBeginning'] = hasBeginning
cur_proposal['hasEnd'] = hasEnd
cur_proposal['eligibleLocation'] = eligibleLocation
cur_proposal['unitBased'] = unitBased
create_proposal(cur_proposal, user_data, endpoint)
DEBUG_show_proposal = False
def show_proposal(user_data, id, endpoint):
variables = {
"id": id
}
query = """query($id:ID!){
proposal(id:$id){
...proposal
}
}""" + PROPOSAL_FRAG + PROPINT_FRAG + LOCATION_FRAG + UNIT_FRAG + RESSPEC_FRAG + INTENT_FRAG + RESOURCE_FRAG + \
QUANTITY_FRAG + AGENT_FRAG + ACTION_FRAG + PROCESS_FRAG + PROCESSGRP_FRAG + PROCESSSPEC_FRAG
res_json = send_signed(query, variables, user_data['username'], user_data['keyring']['eddsa'], endpoint)
if 'errors' in res_json:
print("Error message")
print(json.dumps(res_json['errors'], indent=2))
print("Query")
print(query)
print("Variables")
print(variables)
raise Exception(f"Error in function {inspect.stack()[0][3]}")
if DEBUG_show_proposal:
print("Query")
print(query)
print("Variables")
print(variables)
print("Result")
print(json.dumps(res_json, indent=2))
return res_json
DEBUG_create_intent = False
def create_intent(intent, user_data, res_spec_data, endpoint):
# ts = datetime.now(timezone.utc).isoformat()
variables = {
"intent": {
"action": intent['action'],
"agreedIn": intent['agreedIn'],
"atLocation": intent['atLocation'],
"availableQuantity": intent['availableQuantity'],
"due": intent['due'],
"effortQuantity": intent['effortQuantity'],
"finished": intent['finished'],
"hasBeginning": intent['hasBeginning'],
"hasEnd": intent['hasEnd'],
"hasPointInTime": intent['hasPointInTime'],
"inputOf": intent['inputOf'],
"name": intent['name'],
"note": intent['note'],
"outputOf": intent['outputOf'],
"provider": intent['provider'],
"receiver": intent['receiver'],
"resourceClassifiedAs": intent['resourceClassifiedAs'],
"resourceConformsTo": intent['resourceConformsTo'],
"resourceInventoriedAs": intent['resourceInventoriedAs'],
"resourceQuantity": {
"hasUnit": [values['defaultUnit'] for key, values in res_spec_data.items() \
if values['id'] == intent['resourceConformsTo']][0],
"hasNumericalValue": intent['amount']
}
}
}
query = """mutation($intent:IntentCreateParams!) {
createIntent(intent:$intent) {
intent {
...intent
}
}
}""" + UNIT_FRAG + RESSPEC_FRAG + INTENT_FRAG + PROPINT_FRAG + AGENT_FRAG + LOCATION_FRAG + \
QUANTITY_FRAG + RESOURCE_FRAG + ACTION_FRAG + PROCESS_FRAG + PROCESSGRP_FRAG + PROCESSSPEC_FRAG
res_json = send_signed(query, variables, user_data['username'], user_data['keyring']['eddsa'], endpoint)
if 'errors' in res_json:
print("Error message")
print(json.dumps(res_json['errors'], indent=2))
print("Query")
print(query)
print("Variables")
print(variables)
raise Exception(f"Error in function {inspect.stack()[0][3]}")
if DEBUG_create_intent:
print("Query")
print(query)
print("Variables")
print(variables)
print("Result")
print(json.dumps(res_json, indent=2))
intent['id'] = res_json['data']['createIntent']['intent']['id']
def get_intent(name, intent_data, note, user_data, res_spec_data, provider, receiver, action, \
resourceClassifiedAs, resourceConformsTo, \
resourceInventoriedAs, amount, agreedIn, \
atLocation, availableQuantity, effortQuantity, \
finished, due, hasBeginning, hasEnd, hasPointInTime, \
inputOf, outputOf, endpoint):
if name in intent_data:
return
intent_data[f'{name}'] = {}
cur_intent = intent_data[f'{name}']
cur_intent['name'] = name
cur_intent['note'] = note
cur_intent['action'] = action
cur_intent['agreedIn'] = agreedIn
cur_intent['amount'] = amount
cur_intent['finished'] = finished
cur_intent['due'] = due
cur_intent['resourceClassifiedAs'] = resourceClassifiedAs
cur_intent['resourceConformsTo'] = resourceConformsTo
cur_intent['resourceInventoriedAs'] = resourceInventoriedAs
cur_intent['availableQuantity'] = availableQuantity
cur_intent['effortQuantity'] = effortQuantity
if provider != None and receiver != None:
raise Exception("At max one of provider and receiver must be specified")
if provider == None and receiver == None:
raise Exception("At least one of provider or receiver must be specified")
cur_intent['provider'] = provider
cur_intent['receiver'] = receiver
if not ((hasPointInTime != None and not (hasBeginning!= None or hasEnd != None)) or \
((hasBeginning!= None and hasEnd != None) and not hasPointInTime != None)):
raise Exception("Specify either hasPointInTime or hasBeginning anf hasEnd")
cur_intent['hasBeginning'] = hasBeginning
cur_intent['hasEnd'] = hasEnd
cur_intent['hasPointInTime'] = hasPointInTime
cur_intent['atLocation'] = atLocation
cur_intent['inputOf'] = inputOf
cur_intent['outputOf'] = outputOf
create_intent(cur_intent, user_data, res_spec_data, endpoint)
DEBUG_create_proposedIntent = False
def create_proposedIntent(cur_propint, user_data, endpoint):
# ts = datetime.now(timezone.utc).isoformat()
variables = {
"publishedIn": cur_propint['publishedIn'],
"publishes": cur_propint['publishes'],
"reciprocal": cur_propint['reciprocal']
}
query = """mutation($publishedIn: ID!, $publishes: ID!, $reciprocal: Boolean){
proposeIntent(publishedIn:$publishedIn, publishes: $publishes, reciprocal:$reciprocal){
proposedIntent{
id
publishedIn {
...proposal
}
publishes {
...intent
}
reciprocal
}
}
}""" + PROPOSAL_FRAG + UNIT_FRAG + RESSPEC_FRAG + INTENT_FRAG + PROPINT_FRAG + AGENT_FRAG + LOCATION_FRAG + QUANTITY_FRAG + \
RESOURCE_FRAG + ACTION_FRAG + PROCESS_FRAG + PROCESSGRP_FRAG + PROCESSSPEC_FRAG
res_json = send_signed(query, variables, user_data['username'], user_data['keyring']['eddsa'], endpoint)
if 'errors' in res_json:
print("Error message")
print(json.dumps(res_json['errors'], indent=2))
print("Query")
print(query)
print("Variables")
print(variables)
raise Exception(f"Error in function {inspect.stack()[0][3]}")
if DEBUG_create_proposedIntent:
print("Query")
print(query)
print("Variables")
print(variables)
print("Result")
print(json.dumps(res_json, indent=2))
cur_propint['id'] = res_json['data']['proposeIntent']['proposedIntent']['id']
def get_proposedIntent(name, prop_int_data, user_data, publishedIn, publishes,reciprocal, endpoint):
if name in prop_int_data:
return
prop_int_data[f'{name}'] = {}
cur_propint = prop_int_data[f'{name}']
cur_propint['publishedIn'] = publishedIn
cur_propint['publishes'] = publishes
cur_propint['reciprocal'] = reciprocal
create_proposedIntent(cur_propint, user_data, endpoint)
DEBUG_create_satisfaction = False
def create_satisfaction(cur_sat, user_data, endpoint):
variables = {
"satisfaction": {
"effortQuantity": cur_sat['effortQuantity'],
"note": cur_sat['note'],
"resourceQuantity": cur_sat['resourceQuantity'],
"satisfiedByEvent": cur_sat['satisfiedByEvent'],
"satisfies": cur_sat['satisfies']
}
}
query = """mutation($satisfaction: SatisfactionCreateParams!){
createSatisfaction(satisfaction:$satisfaction){
satisfaction{
id
effortQuantity {
...quantity
}
note
resourceQuantity{
...quantity
}
satisfiedByEvent {
...event
}
satisfies {
...intent
}
}
}
}""" + EVENT_FRAG + UNIT_FRAG + RESSPEC_FRAG + INTENT_FRAG + PROPINT_FRAG + AGENT_FRAG + LOCATION_FRAG + \
QUANTITY_FRAG + RESOURCE_FRAG + ACTION_FRAG + PROCESS_FRAG + PROCESSGRP_FRAG +PROCESSSPEC_FRAG
res_json = send_signed(query, variables, user_data['username'], user_data['keyring']['eddsa'], endpoint)
if 'errors' in res_json:
print("Error message")
print(json.dumps(res_json['errors'], indent=2))
print("Query")
print(query)
print("Variables")
print(variables)
raise Exception(f"Error in function {inspect.stack()[0][3]}")
if DEBUG_create_satisfaction:
print("Query")
print(query)
print("Variables")
print(variables)
print("Result")
print(json.dumps(res_json, indent=2))
cur_sat['id'] = res_json['data']['createSatisfaction']['satisfaction']['id']
def get_satisfaction(name, user_data, event_id, intent_id, note, satisfaction_data, endpoint, effortQuantity:dict={}, amount=0, cur_res:dict={}, res_spec_data:dict={}):
satisfaction_data[f'{name}'] = {}
cur_sat = satisfaction_data[f'{name}']
cur_sat["effortQuantity"] = {
"hasNumericalValue": effortQuantity['amount'],
"hasUnit": effortQuantity['unit_id'],
} if effortQuantity != {} else None
cur_sat["note"] = note
cur_sat["resourceQuantity"] = {
"hasNumericalValue": amount,
"hasUnit": [specs['defaultUnit'] for name, specs in res_spec_data.items() \
if specs['id'] == cur_res['spec_id']][0],
} if res_spec_data != {} else None
cur_sat["satisfiedByEvent"] = event_id
cur_sat["satisfies"] = intent_id
create_satisfaction(cur_sat, user_data, endpoint)
def check_proposals(user_data, proposal_data:dict, endpoint):
if proposal_data == {}:
print("No proposal to check")
return
for key in proposal_data.keys():
prop_id = proposal_data[key]['id']
data = show_proposal(user_data, prop_id, endpoint)
prop = data['data']['proposal']
print(f"Proposal {prop['name']} has status {prop['status']}")
print("Primary intents")
for intent in prop['primaryIntents']:
print(f"{intent['name']}")