pico-engine 1.2.0 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,473 +1,484 @@
1
- /* Rules with a public facing point of entry are camelCase, internal rules are snake_case
2
- For skyQuery and parent-child trees:
3
- wrangler is built so that the parent can skyQuery its children for info at any time,
4
- but children only get information from their direct parent through event-passing to prevent race conditions
5
- */
6
- ruleset io.picolabs.wrangler {
7
- meta {
8
- name "Wrangler Core"
9
- description <<
10
- Wrangler Core Module,
11
- use example: "use module io.picolabs.wrangler alias wrangler".
12
- This Ruleset/Module provides a developer interface to the pico (persistent compute object).
13
- When a pico is created this ruleset will be installed to provide essential services.
14
- >>
15
- author "BYU Pico Lab"
16
-
17
- provides skyQuery, picoQuery,
18
- channels, createChannel, deleteChannel, //channel
19
- rulesetConfig, rulesetMeta, installedRIDs, //ruleset
20
- children, parent_eci, name, myself //pico
21
-
22
- shares skyQuery, picoQuery,
23
- channels, //channel
24
- rulesetConfig, rulesetMeta,installedRIDs, //ruleset
25
- children, parent_eci, name, myself, id //pico
26
- }
27
- global {
28
- __testing = { "queries": [ {"name": "name"},
29
- {"name": "myself"},
30
- { "name": "channels", "args":["tags"] },
31
- { "name": "installedRIDs"},
32
- {"name":"skyQuery", "args":["eci", "mod", "func", "params","_host","_path","_root_url"]},
33
- {"name":"children", "args":[]}
34
- ],
35
- "events": [
36
- { "domain": "wrangler", "name": "new_child_request",
37
- "attrs": [ "name", "backgroundColor"] },
38
- { "domain": "wrangler", "name": "child_deletion_request",
39
- "attrs": [ "eci"] },
40
- { "domain": "wrangler", "name": "new_channel_request",
41
- "attrs": [ "tags", "eventPolicy", "queryPolicy" ] },
42
- { "domain": "wrangler", "name": "channel_deletion_request",
43
- "attrs": [ "eci" ] },
44
- { "domain": "wrangler", "name": "install_ruleset_request",
45
- "attrs": [ "url" ] },
46
- { "domain": "wrangler", "name": "install_ruleset_request",
47
- "attrs": [ "absoluteURL","rid" ] },
48
- { "domain": "wrangler", "name": "uninstall_ruleset_request",
49
- "attrs": [ "rid" ] },
50
- ] }
51
-
52
-
53
- // ********************************************************************************************
54
- // *** ***
55
- // *** FUNCTIONS ***
56
- // *** ***
57
- // ********************************************************************************************
58
-
59
- /*
60
- picoQuery is used to programmatically call function inside of other picos from inside a rule.
61
- parameters;
62
- eci - The eci of the pico which contains the function to be called
63
- mod - The ruleset ID or alias of the module
64
- func - The name of the function in the module
65
- params - The parameters to be passed to function being called
66
- optional parameters
67
- _host - The host of the pico-engine being queried.
68
- Note this must include protocol (http:// or https://) being used and port number if not 80.
69
- For example "http://localhost:8080", which also is the default.
70
- _path - The sub path of the url which does not include mod or func.
71
- For example "/sky/cloud/", which also is the default.
72
- _root_url - The entire url except eci, mod, func.
73
- For example, dependent on _host and _path is
74
- "http://localhost:8080/sky/cloud/", which also is the default.
75
- picoQuery on success (if status code of request is 200) returns results of the called function.
76
- picoQuery on failure (if status code of request is not 200) returns a Map of error information which contains;
77
- error - general error message.
78
- httpStatus - status code returned from http get command.
79
- picoQueryError - The value of the "error key", if it exist, aof the function results.
80
- picoQueryErrorMsg - The value of the "error_str", if it exist, of the function results.
81
- picoQueryReturnValue - The function call results.
82
- */
83
- QUERY_SELF_INVALID_HTTP_MAP = {"status_code": 400,
84
- "status_line":"HTTP/1.1 400 Pico should not query itself",
85
- "content": "{\"error\":\"Pico should not query itself\"}"};
86
-
87
-
88
- buildWebHook = function(eci, mod, func, _host, _path, _root_url) {
89
- createRootUrl = function (_host,_path){
90
- host = _host || meta:host;
91
- path = _path || "/sky/cloud/";
92
- root_url = host+path;
93
- root_url
94
- };
95
- root_url = _root_url || createRootUrl(_host,_path);
96
- root_url + eci + "/"+mod+"/" + func;
97
- }
98
-
99
- processHTTPResponse = function(response) {
100
- status = response{"status_code"};// pass along the status
101
- error_info = {
102
- "error": "sky query request was unsuccesful.",
103
- "httpStatus": {
104
- "code": status,
105
- "message": response{"status_line"}
106
- }
107
- };
108
- // clean up http return
109
- response_content = response{"content"}.decode();
110
- response_error = (response_content.typeof() == "Map" && (not response_content{"error"}.isnull())) => response_content{"error"} | 0;
111
- response_error_str = (response_content.typeof() == "Map" && (not response_content{"error_str"}.isnull())) => response_content{"error_str"} | 0;
112
- error = error_info.put({"picoQueryError": response_error,
113
- "picoQueryErrorMsg": response_error_str,
114
- "picoQueryReturnValue": response_content});
115
- is_bad_response = (response_content.isnull() || (response_content == "null") || response_error || response_error_str);
116
- // if HTTP status was OK & the response was not null and there were no errors...
117
- (status == 200 && not is_bad_response) => response_content | error.klog("error: ")
118
- }
119
-
120
- skyQuery = function(eci, mod, func, params,_host,_path,_root_url) { // path must start with "/"", _host must include protocol(http:// or https://)
121
- //.../sky/cloud/<eci>/<rid>/<name>?name0=value0&...&namen=valuen
122
- thisPico = ctx:channels.any(function(c){c{"id"}==eci})
123
- web_hook = buildWebHook(eci, mod, func, _host, _path, _root_url)
124
-
125
- response = (not thisPico) => http:get(web_hook, {}.put(params)) | QUERY_SELF_INVALID_HTTP_MAP;
126
- processHTTPResponse(response)
127
- }
128
-
129
- /*
130
- picoQuery() is an improved skyQuery() that automatically queries picos on the same engine using ctx:query() to avoid
131
- unnecessary HTTP calls and channel policy issues (family channels can't be accessed over HTTP).
132
-
133
- The parameters are the same.
134
- */
135
-
136
- // this does nothing now, but it may be that we need to return error codes.
137
- processQueryResponse = function(response) {
138
- response
139
- }
140
-
141
- picoQuery = function(eci, mod, func, params,_host,_path,_root_url) {
142
- thisPico = ctx:channels.any(function(c){c{"id"}==eci})
143
- thisHost = _host.isnull() || _host == meta:host;
144
- web_hook = buildWebHook(eci, mod, func, _host, _path, _root_url)
145
-
146
- response = (not thisPico) => ((not thisHost) => processHTTPResponse(http:get(web_hook, {}.put(params)))
147
- | processQueryResponse(ctx:query(eci, mod, func, {}.put(params))))
148
- | QUERY_SELF_INVALID_HTTP_MAP;
149
-
150
- response
151
- }
152
-
153
-
154
-
155
- // ********************************************************************************************
156
- // *** Rulesets ***
157
- // ********************************************************************************************
158
-
159
- rulesetByRID = function(rid) {
160
- ctx:rulesets.filter(function(rs){rs{"rid"}==rid}).head()
161
- }
162
-
163
- rulesetConfig = function(rid) {
164
- rulesetByRID(rid){"config"}
165
- }
166
-
167
- rulesetMeta = function(rid) {
168
- rulesetByRID(rid){["meta","krlMeta"]}
169
- }
170
-
171
- installedRIDs = function() {
172
- ctx:rulesets.map(function(rs){rs{"rid"}})
173
- }
174
-
175
- // ********************************************************************************************
176
- // *** Channels ***
177
- // ********************************************************************************************
178
-
179
- channels = function(tags){
180
- all_channels = ctx:channels
181
- str_tags = tags.typeof()=="Array" => tags.join(",") | tags
182
- cf_tags = tags => str_tags.lc().split(",").sort().join(",") | null
183
- cf_tags.isnull() => all_channels
184
- | all_channels.filter(function(c){c{"tags"}.sort().join(",")==cf_tags})
185
- }
186
-
187
- deleteChannel = defaction(eci) {
188
- ctx:delChannel(eci)
189
- }
190
-
191
- createChannel = defaction(tags,eventPolicy,queryPolicy){
192
- ctx:newChannel(
193
- tags=tags,
194
- eventPolicy=eventPolicy,
195
- queryPolicy=queryPolicy
196
- ) setting(channel)
197
- return channel
198
- }
199
-
200
- // ********************************************************************************************
201
- // *** Picos ***
202
- // ********************************************************************************************
203
-
204
- myself = function(){
205
- {
206
- "name":ent:name,
207
- "id":ent:id,
208
- "eci":ent:eci
209
- }
210
- }
211
-
212
- children = function() {
213
- convert = function(eci){
214
- pico = {}
215
- .put("eci",eci)
216
- .put("name",ctx:query(eci,ctx:rid,"name"))
217
- .put("parent_eci",ctx:query(eci,ctx:rid,"parent_eci"))
218
- pico
219
- }
220
- wrangler_children = ctx:children
221
- .map(convert)
222
- wrangler_children
223
- }
224
-
225
- parent_eci = function() {
226
- ent:parent_eci
227
- }
228
-
229
- name = function() {
230
- ent:name
231
- }
232
-
233
- id = function() {
234
- meta:picoId
235
- }
236
-
237
- }
238
-
239
- // ********************************************************************************************
240
- // *** ***
241
- // *** System ***
242
- // *** ***
243
- // ********************************************************************************************
244
- /* NOT USED IN 1.0.0 */
245
- rule systemOnLine {
246
- select when system online
247
- foreach children() setting(child)
248
- event:send({ "eci" : child{"eci"},
249
- "domain": "system", "type": "online",
250
- "attrs" : event:attrs })
251
- }
252
- // ********************************************************************************************
253
- // *** ***
254
- // *** Rulesets ***
255
- // *** ***
256
- // ********************************************************************************************
257
-
258
-
259
- rule install_ruleset_relatively {
260
- select when wrangler install_ruleset_request
261
- absoluteURL re#(.+)# // required
262
- rid re#(.+)# // required
263
- setting(absoluteURL,rid)
264
- pre {
265
- parts = absoluteURL.split("/")
266
- url = parts.splice(parts.length()-1,1,rid+".krl").join("/")
267
- }
268
- fired {
269
- raise wrangler event "install_ruleset_request" attributes
270
- event:attrs
271
- .delete("absoluteURL") // so this rule won't select again!
272
- .put({"url":url})
273
- }
274
- }
275
-
276
- rule install_ruleset_absolutely {
277
- select when wrangler install_ruleset_request
278
- url re#(.+)# // required
279
- setting(url)
280
- pre {
281
- config = event:attr("config") || {}
282
- }
283
- every {
284
- ctx:flush(url=url)
285
- ctx:install(url=url, config=config) setting(rid)
286
- }
287
- fired {
288
- raise wrangler event "ruleset_installed" attributes
289
- event:attrs.put({"rids": [rid]})
290
- }
291
- }
292
-
293
- rule uninstall_one_ruleset {
294
- select when wrangler uninstall_ruleset_request
295
- rid re#(.+)# setting(rid)
296
- ctx:uninstall(rid)
297
- fired {
298
- raise wrangler event "ruleset_uninstalled" attributes event:attrs
299
- }
300
- }
301
-
302
- rule flush_all_rulesets {
303
- select when wrangler rulesets_need_flushing
304
- foreach ctx:rulesets setting(rs)
305
- ctx:flush(rs{"url"})
306
- fired {
307
- raise wrangler event "rulesets_flushed" on final
308
- }
309
- }
310
-
311
- // ********************************************************************************************
312
- // *** Channels ***
313
- // ********************************************************************************************
314
-
315
- rule createChannel {
316
- select when wrangler new_channel_request
317
- pre {
318
- tags = event:attrs{"tags"}
319
- eventPolicy = event:attrs{"eventPolicy"}
320
- queryPolicy = event:attrs{"queryPolicy"}
321
- }
322
- if tags && eventPolicy && queryPolicy then
323
- createChannel(tags,eventPolicy,queryPolicy) setting(channel)
324
- fired {
325
- raise wrangler event "channel_created"
326
- attributes event:attrs.put({"channel":channel})
327
- }
328
- }
329
-
330
- rule deleteChannel {
331
- select when wrangler channel_deletion_request
332
- eci re#^(.+)$# // required
333
- setting(eci)
334
- deleteChannel(eci)
335
- fired {
336
- raise wrangler event "channel_deleted" attributes event:attrs
337
- }
338
- }
339
-
340
- // ********************************************************************************************
341
- // *** Picos ***
342
- // ********************************************************************************************
343
- //-------------------- Picos initializing ----------------------
344
-
345
- rule createChild {
346
- select when wrangler new_child_request
347
- name re#.+# // required
348
- backgroundColor re#.*# // optional
349
- pre {
350
- name = event:attrs{"name"}
351
- backgroundColor = event:attrs{"backgroundColor"} || "#87CEFA"
352
- engine_ui_rid = "io.picolabs.pico-engine-ui"
353
- engine_ui_ruleset = function(){
354
- the_ruleset = ctx:rulesets
355
- .filter(function(r){r.get("rid")==engine_ui_rid})
356
- .head();
357
- { "url":the_ruleset.get("url"),
358
- "config":the_ruleset.get("config")
359
- }
360
- }
361
- }
362
- every {
363
- ctx:newPico(rulesets=[
364
- engine_ui_ruleset(),
365
- { "url": ctx:rid_url, "config": {} }
366
- ]) setting(newEci)
367
- ctx:eventQuery(
368
- eci=newEci,
369
- domain="engine_ui",
370
- name="setup",
371
- rid=engine_ui_rid,
372
- queryName="uiECI"
373
- ) setting(newUiECI)
374
- ctx:event(
375
- eci=newEci,
376
- domain="wrangler",
377
- name="pico_created",
378
- attrs=event:attrs
379
- )
380
- ctx:event(
381
- eci=newUiECI,
382
- domain="engine_ui",
383
- name="box",
384
- attrs={
385
- "name": name,
386
- "backgroundColor": backgroundColor
387
- }
388
- )
389
- }
390
- fired {
391
- raise wrangler event "new_child_created"
392
- attributes event:attrs.put({"eci":newEci})
393
- }
394
- }
395
-
396
- //
397
- // NOTE: the next two rules run in a newly created child pico
398
- //
399
- rule initialize_child_after_creation {
400
- select when wrangler pico_created
401
- pre {
402
- subscription_ruleset_url = function(){
403
- subscription_rid = "io.picolabs.subscription"
404
- parts = ctx:rid_url.split("/")
405
- parts.splice(parts.length()-1,1,subscription_rid+".krl").join("/")
406
- }
407
- subs_url = subscription_ruleset_url()
408
- }
409
- fired {
410
- raise wrangler event "install_ruleset_request"
411
- attributes {"url":subs_url}
412
- ent:parent_eci := ctx:parent
413
- ent:name := event:attr("name")
414
- ent:id := ctx:picoId
415
- ent:eci := event:eci
416
- raise wrangler event "pico_initialized"
417
- attributes event:attrs.put("eci",event:eci)
418
- }
419
- }
420
-
421
- rule finish_child_initialization {
422
- select when wrangler pico_initialized
423
- event:send({ "eci" : ent:parent_eci,
424
- "domain": "wrangler", "type": "child_initialized",
425
- "attrs" : event:attrs })
426
- }
427
-
428
- // this pico is the primary pico
429
-
430
- rule pico_root_created {
431
- select when engine_ui setup
432
- if ent:id.isnull() && ent:parent_eci.isnull() then noop()
433
- fired {
434
- ent:id := ctx:picoId
435
- ent:eci := channels("system,self").head(){"id"}
436
- ent:name := "Pico"
437
- }
438
- }
439
-
440
- rule take_note_of_name_change {
441
- select when wrangler name_changed
442
- name re#(.+)# // required
443
- setting(name)
444
- fired {
445
- ent:name := name
446
- }
447
- }
448
-
449
- //-------------------- PARENT PERSPECTIVE ----------------------
450
- rule deleteOneChild {
451
- select when wrangler child_deletion_request
452
- eci re#^(.+)$# setting(eci)
453
- ctx:delPico(eci)
454
- fired {
455
- raise wrangler event "child_deleted"
456
- attributes event:attrs
457
- }
458
- }
459
-
460
- //-------------------- CHILD PERSPECTIVE ----------------------
461
-
462
- rule child_initiates_deletion {
463
- select when wrangler ready_for_deletion
464
- pre {
465
- my_eci = channels("system,child").head().get("id")
466
- }
467
- if my_eci then
468
- event:send({"eci":ent:parent_eci,
469
- "domain":"wrangler", "type":"child_deletion_request",
470
- "attrs":{"eci":my_eci}})
471
- }
472
-
473
- }//end ruleset
1
+ /* Rules with a public facing point of entry are camelCase, internal rules are snake_case
2
+ For skyQuery and parent-child trees:
3
+ wrangler is built so that the parent can skyQuery its children for info at any time,
4
+ but children only get information from their direct parent through event-passing to prevent race conditions
5
+ */
6
+ ruleset io.picolabs.wrangler {
7
+ meta {
8
+ name "Wrangler Core"
9
+ description <<
10
+ Wrangler Core Module,
11
+ use example: "use module io.picolabs.wrangler alias wrangler".
12
+ This Ruleset/Module provides a developer interface to the pico (persistent compute object).
13
+ When a pico is created this ruleset will be installed to provide essential services.
14
+ >>
15
+ author "BYU Pico Lab"
16
+
17
+ provides skyQuery, picoQuery,
18
+ channels, createChannel, deleteChannel, //channel
19
+ rulesetConfig, rulesetMeta, installedRIDs, //ruleset
20
+ children, parent_eci, name, myself //pico
21
+
22
+ shares skyQuery, picoQuery,
23
+ channels, //channel
24
+ rulesetConfig, rulesetMeta,installedRIDs, //ruleset
25
+ children, parent_eci, name, myself, id //pico
26
+ }
27
+ global {
28
+ __testing = { "queries": [ {"name": "name"},
29
+ {"name": "myself"},
30
+ { "name": "channels", "args":["tags"] },
31
+ { "name": "installedRIDs"},
32
+ {"name":"skyQuery", "args":["eci", "mod", "func", "params","_host","_path","_root_url"]},
33
+ {"name":"children", "args":[]}
34
+ ],
35
+ "events": [
36
+ { "domain": "wrangler", "name": "new_child_request",
37
+ "attrs": [ "name", "backgroundColor"] },
38
+ { "domain": "wrangler", "name": "child_deletion_request",
39
+ "attrs": [ "eci"] },
40
+ { "domain": "wrangler", "name": "new_channel_request",
41
+ "attrs": [ "tags", "eventPolicy", "queryPolicy" ] },
42
+ { "domain": "wrangler", "name": "channel_deletion_request",
43
+ "attrs": [ "eci" ] },
44
+ { "domain": "wrangler", "name": "install_ruleset_request",
45
+ "attrs": [ "url" ] },
46
+ { "domain": "wrangler", "name": "install_ruleset_request",
47
+ "attrs": [ "absoluteURL","rid" ] },
48
+ { "domain": "wrangler", "name": "uninstall_ruleset_request",
49
+ "attrs": [ "rid" ] },
50
+ ] }
51
+
52
+
53
+ // ********************************************************************************************
54
+ // *** ***
55
+ // *** FUNCTIONS ***
56
+ // *** ***
57
+ // ********************************************************************************************
58
+
59
+ /*
60
+ picoQuery is used to programmatically call function inside of other picos from inside a rule.
61
+ parameters;
62
+ eci - The eci of the pico which contains the function to be called
63
+ mod - The ruleset ID or alias of the module
64
+ func - The name of the function in the module
65
+ params - The parameters to be passed to function being called
66
+ optional parameters
67
+ _host - The host of the pico-engine being queried.
68
+ Note this must include protocol (http:// or https://) being used and port number if not 80.
69
+ For example "http://localhost:8080", which also is the default.
70
+ _path - The sub path of the url which does not include mod or func.
71
+ For example "/sky/cloud/", which also is the default.
72
+ _root_url - The entire url except eci, mod, func.
73
+ For example, dependent on _host and _path is
74
+ "http://localhost:8080/sky/cloud/", which also is the default.
75
+ picoQuery on success (if status code of request is 200) returns results of the called function.
76
+ picoQuery on failure (if status code of request is not 200) returns a Map of error information which contains;
77
+ error - general error message.
78
+ httpStatus - status code returned from http get command.
79
+ picoQueryError - The value of the "error key", if it exist, aof the function results.
80
+ picoQueryErrorMsg - The value of the "error_str", if it exist, of the function results.
81
+ picoQueryReturnValue - The function call results.
82
+ */
83
+ QUERY_SELF_INVALID_HTTP_MAP = {"status_code": 400,
84
+ "status_line":"HTTP/1.1 400 Pico should not query itself",
85
+ "content": "{\"error\":\"Pico should not query itself\"}"};
86
+
87
+
88
+ buildWebHook = function(eci, mod, func, _host, _path, _root_url) {
89
+ createRootUrl = function (_host,_path){
90
+ host = _host || meta:host;
91
+ path = _path || "/sky/cloud/";
92
+ root_url = host+path;
93
+ root_url
94
+ };
95
+ root_url = _root_url || createRootUrl(_host,_path);
96
+ root_url + eci + "/"+mod+"/" + func;
97
+ }
98
+
99
+ processHTTPResponse = function(response) {
100
+ status = response{"status_code"};// pass along the status
101
+ error_info = {
102
+ "error": "sky query request was unsuccesful.",
103
+ "httpStatus": {
104
+ "code": status,
105
+ "message": response{"status_line"}
106
+ }
107
+ };
108
+ // clean up http return
109
+ response_content = response{"content"}.decode();
110
+ response_error = (response_content.typeof() == "Map" && (not response_content{"error"}.isnull())) => response_content{"error"} | 0;
111
+ response_error_str = (response_content.typeof() == "Map" && (not response_content{"error_str"}.isnull())) => response_content{"error_str"} | 0;
112
+ error = error_info.put({"picoQueryError": response_error,
113
+ "picoQueryErrorMsg": response_error_str,
114
+ "picoQueryReturnValue": response_content});
115
+ is_bad_response = (response_content.isnull() || (response_content == "null") || response_error || response_error_str);
116
+ // if HTTP status was OK & the response was not null and there were no errors...
117
+ (status == 200 && not is_bad_response) => response_content | error.klog("error: ")
118
+ }
119
+
120
+ skyQuery = function(eci, mod, func, params,_host,_path,_root_url) { // path must start with "/"", _host must include protocol(http:// or https://)
121
+ //.../sky/cloud/<eci>/<rid>/<name>?name0=value0&...&namen=valuen
122
+ thisPico = ctx:channels.any(function(c){c{"id"}==eci})
123
+ web_hook = buildWebHook(eci, mod, func, _host, _path, _root_url)
124
+
125
+ response = (not thisPico) => http:get(web_hook, {}.put(params)) | QUERY_SELF_INVALID_HTTP_MAP;
126
+ processHTTPResponse(response)
127
+ }
128
+
129
+ /*
130
+ picoQuery() is an improved skyQuery() that automatically queries picos on the same engine using ctx:query() to avoid
131
+ unnecessary HTTP calls and channel policy issues (family channels can't be accessed over HTTP).
132
+
133
+ The parameters are the same.
134
+ */
135
+
136
+ // this does nothing now, but it may be that we need to return error codes.
137
+ processQueryResponse = function(response) {
138
+ response
139
+ }
140
+
141
+ picoQuery = function(eci, mod, func, params,_host,_path,_root_url) {
142
+ isDid = eci.match(re#^did:#)
143
+ thisPico = ctx:channels.any(function(c){c{"id"}==eci})
144
+ thisHost = _host.isnull() || _host == meta:host;
145
+ web_hook = buildWebHook(eci, mod, func, _host, _path, _root_url)
146
+
147
+ response = thisPico => QUERY_SELF_INVALID_HTTP_MAP |
148
+ thisHost => processQueryResponse(ctx:query(eci, mod, func, {}.put(params))) |
149
+ isDid => dido:prepareQuery(eci, {"rid": mod, "name": func, "args": params}) |
150
+ processHTTPResponse(http:get(web_hook, {}.put(params)))
151
+
152
+ response
153
+ }
154
+
155
+
156
+
157
+ // ********************************************************************************************
158
+ // *** Rulesets ***
159
+ // ********************************************************************************************
160
+
161
+ rulesetByRID = function(rid) {
162
+ ctx:rulesets.filter(function(rs){rs{"rid"}==rid}).head()
163
+ }
164
+
165
+ rulesetConfig = function(rid) {
166
+ rulesetByRID(rid){"config"}
167
+ }
168
+
169
+ rulesetMeta = function(rid) {
170
+ rulesetByRID(rid){["meta","krlMeta"]}
171
+ }
172
+
173
+ installedRIDs = function() {
174
+ ctx:rulesets.map(function(rs){rs{"rid"}})
175
+ }
176
+
177
+ // ********************************************************************************************
178
+ // *** Channels ***
179
+ // ********************************************************************************************
180
+
181
+ channels = function(tags){
182
+ all_channels = ctx:channels
183
+ str_tags = tags.typeof()=="Array" => tags.join(",") | tags
184
+ cf_tags = tags => str_tags.lc().split(",").sort().join(",") | null
185
+ cf_tags.isnull() => all_channels
186
+ | all_channels.filter(function(c){c{"tags"}.sort().join(",")==cf_tags})
187
+ }
188
+
189
+ deleteChannel = defaction(eci) {
190
+ ctx:delChannel(eci)
191
+ }
192
+
193
+ createChannel = defaction(tags,eventPolicy,queryPolicy){
194
+ ctx:newChannel(
195
+ tags=tags,
196
+ eventPolicy=eventPolicy,
197
+ queryPolicy=queryPolicy
198
+ ) setting(channel)
199
+ return channel
200
+ }
201
+
202
+ // ********************************************************************************************
203
+ // *** Picos ***
204
+ // ********************************************************************************************
205
+
206
+ myself = function(){
207
+ {
208
+ "name":ent:name,
209
+ "id":ent:id,
210
+ "eci":ent:eci
211
+ }
212
+ }
213
+
214
+ children = function() {
215
+ convert = function(eci){
216
+ pico = {}
217
+ .put("eci",eci)
218
+ .put("name",ctx:query(eci,ctx:rid,"name"))
219
+ .put("parent_eci",ctx:query(eci,ctx:rid,"parent_eci"))
220
+ pico
221
+ }
222
+ wrangler_children = ctx:children
223
+ .map(convert)
224
+ wrangler_children
225
+ }
226
+
227
+ parent_eci = function() {
228
+ ent:parent_eci
229
+ }
230
+
231
+ name = function() {
232
+ ent:name
233
+ }
234
+
235
+ id = function() {
236
+ meta:picoId
237
+ }
238
+
239
+ }
240
+
241
+ // ********************************************************************************************
242
+ // *** ***
243
+ // *** System ***
244
+ // *** ***
245
+ // ********************************************************************************************
246
+ /* NOT USED IN 1.0.0 */
247
+ rule systemOnLine {
248
+ select when system online
249
+ foreach children() setting(child)
250
+ event:send({ "eci" : child{"eci"},
251
+ "domain": "system", "type": "online",
252
+ "attrs" : event:attrs })
253
+ }
254
+ // ********************************************************************************************
255
+ // *** ***
256
+ // *** Rulesets ***
257
+ // *** ***
258
+ // ********************************************************************************************
259
+
260
+
261
+ rule install_ruleset_relatively {
262
+ select when wrangler install_ruleset_request
263
+ absoluteURL re#(.+)# // required
264
+ rid re#(.+)# // required
265
+ setting(absoluteURL,rid)
266
+ pre {
267
+ parts = absoluteURL.split("/")
268
+ url = parts.splice(parts.length()-1,1,rid+".krl").join("/")
269
+ }
270
+ fired {
271
+ raise wrangler event "install_ruleset_request" attributes
272
+ event:attrs
273
+ .delete("absoluteURL") // so this rule won't select again!
274
+ .put({"url":url})
275
+ }
276
+ }
277
+
278
+ rule install_ruleset_absolutely {
279
+ select when wrangler install_ruleset_request
280
+ url re#(.+)# // required
281
+ setting(url)
282
+ pre {
283
+ config = event:attr("config") || {}
284
+ }
285
+ every {
286
+ ctx:flush(url=url)
287
+ ctx:install(url=url, config=config) setting(rid)
288
+ }
289
+ fired {
290
+ raise wrangler event "ruleset_installed" attributes
291
+ event:attrs.put({"rids": [rid]})
292
+ }
293
+ }
294
+
295
+ rule uninstall_one_ruleset {
296
+ select when wrangler uninstall_ruleset_request
297
+ rid re#(.+)# setting(rid)
298
+ ctx:uninstall(rid)
299
+ fired {
300
+ raise wrangler event "ruleset_uninstalled" attributes event:attrs
301
+ }
302
+ }
303
+
304
+ rule flush_all_rulesets {
305
+ select when wrangler rulesets_need_flushing
306
+ foreach ctx:rulesets setting(rs)
307
+ ctx:flush(rs{"url"})
308
+ fired {
309
+ raise wrangler event "rulesets_flushed" on final
310
+ }
311
+ }
312
+
313
+ // ********************************************************************************************
314
+ // *** Channels ***
315
+ // ********************************************************************************************
316
+
317
+ rule createChannel {
318
+ select when wrangler new_channel_request
319
+ pre {
320
+ tags = event:attrs{"tags"}
321
+ eventPolicy = event:attrs{"eventPolicy"}
322
+ queryPolicy = event:attrs{"queryPolicy"}
323
+ }
324
+ if tags && eventPolicy && queryPolicy then
325
+ createChannel(tags,eventPolicy,queryPolicy) setting(channel)
326
+ fired {
327
+ raise wrangler event "channel_created"
328
+ attributes event:attrs.put({"channel":channel})
329
+ }
330
+ }
331
+
332
+ rule deleteChannel {
333
+ select when wrangler channel_deletion_request
334
+ eci re#^(.+)$# // required
335
+ setting(eci)
336
+ deleteChannel(eci)
337
+ fired {
338
+ raise wrangler event "channel_deleted" attributes event:attrs
339
+ }
340
+ }
341
+
342
+ // ********************************************************************************************
343
+ // *** Picos ***
344
+ // ********************************************************************************************
345
+ //-------------------- Picos initializing ----------------------
346
+
347
+ rule createChild {
348
+ select when wrangler new_child_request
349
+ name re#.+# // required
350
+ backgroundColor re#.*# // optional
351
+ pre {
352
+ name = event:attrs{"name"}
353
+ backgroundColor = event:attrs{"backgroundColor"} || "#87CEFA"
354
+ engine_ui_rid = "io.picolabs.pico-engine-ui"
355
+ engine_ui_ruleset = function(){
356
+ the_ruleset = ctx:rulesets
357
+ .filter(function(r){r.get("rid")==engine_ui_rid})
358
+ .head();
359
+ { "url":the_ruleset.get("url"),
360
+ "config":the_ruleset.get("config")
361
+ }
362
+ }
363
+ }
364
+ every {
365
+ ctx:newPico(rulesets=[
366
+ engine_ui_ruleset(),
367
+ { "url": ctx:rid_url, "config": {} }
368
+ ]) setting(newEci)
369
+ ctx:eventQuery(
370
+ eci=newEci,
371
+ domain="engine_ui",
372
+ name="setup",
373
+ rid=engine_ui_rid,
374
+ queryName="uiECI"
375
+ ) setting(newUiECI)
376
+ ctx:event(
377
+ eci=newEci,
378
+ domain="wrangler",
379
+ name="pico_created",
380
+ attrs=event:attrs
381
+ )
382
+ ctx:event(
383
+ eci=newUiECI,
384
+ domain="engine_ui",
385
+ name="box",
386
+ attrs={
387
+ "name": name,
388
+ "backgroundColor": backgroundColor
389
+ }
390
+ )
391
+ }
392
+ fired {
393
+ raise wrangler event "new_child_created"
394
+ attributes event:attrs.put({"eci":newEci})
395
+ }
396
+ }
397
+
398
+ //
399
+ // NOTE: the next two rules run in a newly created child pico
400
+ //
401
+ rule initialize_child_after_creation {
402
+ select when wrangler pico_created
403
+ pre {
404
+ subscription_ruleset_url = function(){
405
+ subscription_rid = "io.picolabs.subscription"
406
+ parts = ctx:rid_url.split("/")
407
+ parts.splice(parts.length()-1,1,subscription_rid+".krl").join("/")
408
+ }
409
+ subs_url = subscription_ruleset_url()
410
+
411
+ did_o_ruleset_url = function(){
412
+ did_o_rid = "io.picolabs.did-o"
413
+ parts = ctx:rid_url.split("/")
414
+ parts.splice(parts.length()-1,1,did_o_rid+".krl").join("/")
415
+ }
416
+ did_o_url = did_o_ruleset_url()
417
+ }
418
+ fired {
419
+ raise wrangler event "install_ruleset_request"
420
+ attributes {"url":subs_url}
421
+ raise wrangler event "install_ruleset_request"
422
+ attributes {"url":did_o_url}
423
+ ent:parent_eci := ctx:parent
424
+ ent:name := event:attr("name")
425
+ ent:id := ctx:picoId
426
+ ent:eci := event:eci
427
+ raise wrangler event "pico_initialized"
428
+ attributes event:attrs.put("eci",event:eci)
429
+ }
430
+ }
431
+
432
+ rule finish_child_initialization {
433
+ select when wrangler pico_initialized
434
+ event:send({ "eci" : ent:parent_eci,
435
+ "domain": "wrangler", "type": "child_initialized",
436
+ "attrs" : event:attrs })
437
+ }
438
+
439
+ // this pico is the primary pico
440
+
441
+ rule pico_root_created {
442
+ select when engine_ui setup
443
+ if ent:id.isnull() && ent:parent_eci.isnull() then noop()
444
+ fired {
445
+ ent:id := ctx:picoId
446
+ ent:eci := channels("system,self").head(){"id"}
447
+ ent:name := "Pico"
448
+ }
449
+ }
450
+
451
+ rule take_note_of_name_change {
452
+ select when wrangler name_changed
453
+ name re#(.+)# // required
454
+ setting(name)
455
+ fired {
456
+ ent:name := name
457
+ }
458
+ }
459
+
460
+ //-------------------- PARENT PERSPECTIVE ----------------------
461
+ rule deleteOneChild {
462
+ select when wrangler child_deletion_request
463
+ eci re#^(.+)$# setting(eci)
464
+ ctx:delPico(eci)
465
+ fired {
466
+ raise wrangler event "child_deleted"
467
+ attributes event:attrs
468
+ }
469
+ }
470
+
471
+ //-------------------- CHILD PERSPECTIVE ----------------------
472
+
473
+ rule child_initiates_deletion {
474
+ select when wrangler ready_for_deletion
475
+ pre {
476
+ my_eci = channels("system,child").head().get("id")
477
+ }
478
+ if my_eci then
479
+ event:send({"eci":ent:parent_eci,
480
+ "domain":"wrangler", "type":"child_deletion_request",
481
+ "attrs":{"eci":my_eci}})
482
+ }
483
+
484
+ }//end ruleset