pico-engine 1.9.3 → 1.9.5
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.
- package/README.md +2 -0
- package/demos/bootstrap/README.md +12 -0
- package/demos/bootstrap/com.example.visitors.bootstrap.krl +97 -0
- package/demos/bootstrap/com.example.visitors.format.krl +25 -0
- package/demos/bootstrap/com.example.visitors.krl +228 -0
- package/demos/bootstrap/try-it.md +5 -0
- package/demos/child_tasks/README.md +12 -0
- package/demos/child_tasks/com.example.task_runner.bootstrap.krl +85 -0
- package/demos/child_tasks/com.example.task_runner.krl +139 -0
- package/demos/child_tasks/com.example.task_worker.krl +45 -0
- package/demos/child_tasks/try-it.md +5 -0
- package/demos/collection/README.md +13 -0
- package/demos/collection/com.example.collection.bootstrap.krl +148 -0
- package/demos/collection/com.example.collection.krl +95 -0
- package/demos/collection/com.example.collection.report.krl +93 -0
- package/demos/collection/com.example.collection.room.krl +101 -0
- package/demos/collection/try-it.md +6 -0
- package/demos/echo/README.md +11 -0
- package/demos/echo/com.example.echo.bootstrap.krl +81 -0
- package/demos/echo/com.example.echo.krl +73 -0
- package/demos/echo/try-it.md +5 -0
- package/demos/guard_rules/README.md +12 -0
- package/demos/guard_rules/com.example.archive.bootstrap.krl +95 -0
- package/demos/guard_rules/com.example.archive.krl +213 -0
- package/demos/guard_rules/com.example.archive_store.krl +36 -0
- package/demos/guard_rules/try-it.md +5 -0
- package/demos/kblog/README.md +14 -0
- package/demos/kblog/com.example.kblog.bootstrap.krl +132 -0
- package/demos/kblog/com.example.kblog.config.krl +50 -0
- package/demos/kblog/com.example.kblog.data.krl +75 -0
- package/demos/kblog/com.example.kblog.krl +184 -0
- package/demos/kblog/com.example.kblog.posting.krl +109 -0
- package/demos/kblog/try-it.md +6 -0
- package/demos/layered_rulesets/README.md +13 -0
- package/demos/layered_rulesets/com.example.invitations.bootstrap.krl +84 -0
- package/demos/layered_rulesets/com.example.invitations.krl +164 -0
- package/demos/layered_rulesets/com.example.invitations.short_links.krl +45 -0
- package/demos/layered_rulesets/com.example.invitations.welcome.krl +39 -0
- package/demos/layered_rulesets/try-it.md +5 -0
- package/demos/moving_picos/README.md +15 -0
- package/demos/moving_picos/io.picolabs.club_gear_demo.krl +650 -0
- package/demos/moving_picos/io.picolabs.ride_coordinator_demo.krl +331 -0
- package/demos/score_tracker/README.md +11 -0
- package/demos/score_tracker/com.example.score_tracker.bootstrap.krl +104 -0
- package/demos/score_tracker/com.example.score_tracker.krl +162 -0
- package/demos/score_tracker/try-it.md +5 -0
- package/demos/secrets/README.md +12 -0
- package/demos/secrets/com.example.hook_service.krl +120 -0
- package/demos/secrets/com.example.notifier.bootstrap.krl +127 -0
- package/demos/secrets/com.example.notifier.krl +138 -0
- package/demos/secrets/try-it.md +5 -0
- package/demos/try-it-order.json +1 -0
- package/demos/two_picos/README.md +12 -0
- package/demos/two_picos/com.example.two_picos.answerer.krl +125 -0
- package/demos/two_picos/com.example.two_picos.asker.krl +124 -0
- package/demos/two_picos/com.example.two_picos.bootstrap.krl +131 -0
- package/demos/two_picos/try-it.md +5 -0
- package/demos/xmlui/README.md +17 -0
- package/demos/xmlui/com.example.contacts.krl +132 -0
- package/demos/xmlui/com.example.fav_color.krl +39 -0
- package/demos/xmlui/com.example.guestbook.krl +61 -0
- package/demos/xmlui/com.example.hello.krl +19 -0
- package/demos/xmlui/com.example.temperature_sensors_view.krl +105 -0
- package/demos/xmlui/com.example.thermostat.bootstrap.krl +92 -0
- package/demos/xmlui/com.example.thermostat.krl +124 -0
- package/demos/xmlui/temperature_sensors.krl +56 -0
- package/demos/xmlui/try-it.md +5 -0
- package/dist/bundledKrl.js +97 -12
- package/dist/cli.js +2 -0
- package/dist/engineConfig.js +19 -0
- package/dist/index.js +1 -0
- package/dist/server.js +18 -4
- package/dist/xmluiRuntime.js +28 -9
- package/krl/io.picolabs.xmlui.krl +149 -1
- package/mcp/docs/CLAUDE.md +3 -2
- package/mcp/docs/pico-engine.md +3 -0
- package/package.json +9 -6
- package/public/.DS_Store +0 -0
- package/public/pico-engine-ui.css +1 -1
- package/public/pico-engine-ui.js +23 -22
- package/src/bundledKrl.ts +118 -12
- package/src/cli.ts +2 -0
- package/src/engineConfig.ts +28 -0
- package/src/index.ts +8 -0
- package/src/server.ts +20 -5
- package/src/xmluiRuntime.ts +32 -9
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# Collection demo
|
|
2
|
+
|
|
3
|
+
A pico that keeps a collection of other picos over relationships, accepts members by role, and reports on all of them.
|
|
4
|
+
|
|
5
|
+
- Overview: [`docs/cookbook/collection/README.md`](../../docs/cookbook/collection/README.md) (on the docs site: https://docs.picolabs.io/#/cookbook/collection/)
|
|
6
|
+
- Instructions: [`docs/cookbook/collection/instructions.md`](../../docs/cookbook/collection/instructions.md)
|
|
7
|
+
|
|
8
|
+
| File | Shows |
|
|
9
|
+
|------|-------|
|
|
10
|
+
| `com.example.collection.krl` | The collection: accepts members by role, turns away other roles, removes members, and provides `members()` |
|
|
11
|
+
| `com.example.collection.room.krl` | A member: has a temperature, joins, leaves, and rejoins |
|
|
12
|
+
| `com.example.collection.report.krl` | Uses the collection as a module, asks every member for its reading, and has the Rooms page |
|
|
13
|
+
| `com.example.collection.bootstrap.krl` | Creates the collection and five rooms on a new pico (one asks in the wrong role); the **Try It!** tab runs it |
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Try It! bootstrap for the Collection demo. Install it on a new pico: it
|
|
3
|
+
* creates a Rooms child with the collection and report rulesets, then five more
|
|
4
|
+
* children with the room ruleset. Four ask to join as members; the fifth,
|
|
5
|
+
* Stranger, asks in another role and is turned away. tryIt() reports how many
|
|
6
|
+
* have joined and links to the report.
|
|
7
|
+
*
|
|
8
|
+
* Each child gets its install requests and then the event that uses them. A
|
|
9
|
+
* pico handles events one at a time, in the order they arrive, so the rulesets
|
|
10
|
+
* are installed before that event is handled.
|
|
11
|
+
*/
|
|
12
|
+
ruleset com.example.collection.bootstrap {
|
|
13
|
+
meta {
|
|
14
|
+
name "Collection"
|
|
15
|
+
description "Shows the collection pattern: members as relationships, accepting and rejecting by role, a ruleset used as a module, and querying every member to report on all of them."
|
|
16
|
+
author "PICOLABS"
|
|
17
|
+
|
|
18
|
+
use module io.picolabs.wrangler alias wrangler
|
|
19
|
+
|
|
20
|
+
shares tryIt
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
global {
|
|
24
|
+
collection_rid = "com.example.collection"
|
|
25
|
+
report_rid = "com.example.collection.report"
|
|
26
|
+
room_rid = "com.example.collection.room"
|
|
27
|
+
xmlui_rid = "io.picolabs.xmlui"
|
|
28
|
+
rooms = [
|
|
29
|
+
{"name": "Kitchen", "temperature": 68},
|
|
30
|
+
{"name": "Office", "temperature": 71},
|
|
31
|
+
{"name": "Garage", "temperature": 64},
|
|
32
|
+
{"name": "Bedroom", "temperature": 75},
|
|
33
|
+
{"name": "Stranger", "temperature": 99, "as": "visitor"}
|
|
34
|
+
]
|
|
35
|
+
|
|
36
|
+
host = function() {
|
|
37
|
+
meta:host.defaultsTo("").replace(re#/$#, "")
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
collectionQuery = function(func) {
|
|
41
|
+
installed = ent:collection
|
|
42
|
+
=> wrangler:picoQuery(ent:collection, "io.picolabs.wrangler", "installedRIDs") | []
|
|
43
|
+
answer = (installed.typeof() == "Array" && installed >< collection_rid)
|
|
44
|
+
=> wrangler:picoQuery(ent:collection, collection_rid, func) | []
|
|
45
|
+
answer.typeof() == "Array" => answer | []
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
reportLink = function() {
|
|
49
|
+
channel = ent:collection
|
|
50
|
+
=> wrangler:picoQuery(ent:collection, "io.picolabs.wrangler", "channels", {"tags": "collection,app"}).head()
|
|
51
|
+
| null
|
|
52
|
+
channel => {"label": "Rooms", "url": host() + "/ui/" + channel{"id"} + "/" + report_rid + "/"} | null
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Progress of this demo's setup, and the links to open when it is ready.
|
|
57
|
+
* @returns Map
|
|
58
|
+
*/
|
|
59
|
+
tryIt = function() {
|
|
60
|
+
joined = collectionQuery("members").length()
|
|
61
|
+
turned_away = collectionQuery("rejections").length()
|
|
62
|
+
ready = joined >= 4 && turned_away >= 1
|
|
63
|
+
link = ready => reportLink() | null
|
|
64
|
+
status = {
|
|
65
|
+
"ready": (ready && link) => true | false,
|
|
66
|
+
"stage": ready => "ready" | ent:collection => joined.as("String") + " of 4 rooms joined" | "starting",
|
|
67
|
+
"links": link => [link] | []
|
|
68
|
+
}
|
|
69
|
+
status
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
rule start {
|
|
74
|
+
select when wrangler ruleset_installed where event:attr("rids") >< meta:rid
|
|
75
|
+
always {
|
|
76
|
+
raise wrangler event "new_channel_request" attributes {
|
|
77
|
+
"tags": ["try_it"],
|
|
78
|
+
"eventPolicy": {"allow": [], "deny": []},
|
|
79
|
+
"queryPolicy": {"allow": [{"rid": meta:rid, "name": "tryIt"}], "deny": []}
|
|
80
|
+
}
|
|
81
|
+
raise wrangler event "new_child_request" attributes
|
|
82
|
+
{"name": "Rooms", "collection_demo": "collection", "backgroundColor": "#c8e6c9"}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// The collection pico is set up: install its rulesets, then create the rooms.
|
|
87
|
+
rule set_up_collection {
|
|
88
|
+
select when wrangler child_initialized where event:attr("collection_demo") == "collection"
|
|
89
|
+
pre {
|
|
90
|
+
child = event:attr("eci")
|
|
91
|
+
}
|
|
92
|
+
every {
|
|
93
|
+
event:send({
|
|
94
|
+
"eci": child,
|
|
95
|
+
"domain": "wrangler",
|
|
96
|
+
"type": "install_ruleset_request",
|
|
97
|
+
"attrs": {"url": host() + "/krl/" + xmlui_rid + ".krl"}
|
|
98
|
+
})
|
|
99
|
+
event:send({
|
|
100
|
+
"eci": child,
|
|
101
|
+
"domain": "wrangler",
|
|
102
|
+
"type": "install_ruleset_request",
|
|
103
|
+
"attrs": {"absoluteURL": meta:rulesetURI, "rid": collection_rid}
|
|
104
|
+
})
|
|
105
|
+
event:send({
|
|
106
|
+
"eci": child,
|
|
107
|
+
"domain": "wrangler",
|
|
108
|
+
"type": "install_ruleset_request",
|
|
109
|
+
"attrs": {"absoluteURL": meta:rulesetURI, "rid": report_rid}
|
|
110
|
+
})
|
|
111
|
+
}
|
|
112
|
+
always {
|
|
113
|
+
ent:collection := child
|
|
114
|
+
raise collection_demo event "rooms_needed"
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
rule create_rooms {
|
|
119
|
+
select when collection_demo rooms_needed
|
|
120
|
+
foreach rooms setting(room)
|
|
121
|
+
always {
|
|
122
|
+
raise wrangler event "new_child_request" attributes room.put("collection_demo", "room")
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// A room is set up: install the room ruleset, then ask it to join.
|
|
127
|
+
rule set_up_room {
|
|
128
|
+
select when wrangler child_initialized where event:attr("collection_demo") == "room"
|
|
129
|
+
pre {
|
|
130
|
+
child = event:attr("eci")
|
|
131
|
+
well_known = wrangler:picoQuery(ent:collection, "io.picolabs.subscription", "wellKnown_Rx"){"id"}
|
|
132
|
+
}
|
|
133
|
+
every {
|
|
134
|
+
event:send({
|
|
135
|
+
"eci": child,
|
|
136
|
+
"domain": "wrangler",
|
|
137
|
+
"type": "install_ruleset_request",
|
|
138
|
+
"attrs": {"absoluteURL": meta:rulesetURI, "rid": room_rid, "temperature": event:attr("temperature")}
|
|
139
|
+
})
|
|
140
|
+
event:send({
|
|
141
|
+
"eci": child,
|
|
142
|
+
"domain": "collection",
|
|
143
|
+
"type": "join_requested",
|
|
144
|
+
"attrs": {"wellKnown_Tx": well_known, "as": event:attr("as") || "member"}
|
|
145
|
+
})
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Collection: keeps a collection of other picos. Each member is a relationship
|
|
3
|
+
* in which this pico plays the role "collection" and the other pico plays
|
|
4
|
+
* "member". This ruleset only manages the collection: it accepts members,
|
|
5
|
+
* turns away requests in any other role, removes members, and raises
|
|
6
|
+
* collection:member_added and collection:member_removed. Other rulesets on this
|
|
7
|
+
* pico use the members; com.example.collection.report is one.
|
|
8
|
+
*/
|
|
9
|
+
ruleset com.example.collection {
|
|
10
|
+
meta {
|
|
11
|
+
name "Collection"
|
|
12
|
+
description "Accepts member picos by relationship role and provides them to other rulesets."
|
|
13
|
+
author "PICOLABS"
|
|
14
|
+
|
|
15
|
+
use module io.picolabs.subscription alias subscription
|
|
16
|
+
|
|
17
|
+
provides members, rejections
|
|
18
|
+
shares members, rejections
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
global {
|
|
22
|
+
/**
|
|
23
|
+
* Every member: the relationships in which the other pico is a member.
|
|
24
|
+
* @returns Array of relationships (Id, Tx, Rx, Tx_role, Rx_role, ...)
|
|
25
|
+
*/
|
|
26
|
+
members = function() {
|
|
27
|
+
subscription:established("Tx_role", "member")
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Requests this collection turned away, oldest first.
|
|
32
|
+
* @returns Array of { name, role, time }
|
|
33
|
+
*/
|
|
34
|
+
rejections = function() {
|
|
35
|
+
ent:rejections.defaultsTo([])
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Accept a request to join as a member. Reject a request in any other role,
|
|
41
|
+
* and remember it.
|
|
42
|
+
*/
|
|
43
|
+
rule accept_member {
|
|
44
|
+
select when wrangler inbound_pending_subscription_added
|
|
45
|
+
pre {
|
|
46
|
+
acceptable = event:attr("Rx_role") == "collection" && event:attr("Tx_role") == "member"
|
|
47
|
+
}
|
|
48
|
+
if acceptable then noop()
|
|
49
|
+
fired {
|
|
50
|
+
raise wrangler event "pending_subscription_approval" attributes event:attrs
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
ent:rejections := rejections().append({
|
|
54
|
+
"name": event:attr("name"),
|
|
55
|
+
"role": event:attr("Tx_role"),
|
|
56
|
+
"time": time:now()
|
|
57
|
+
})
|
|
58
|
+
raise wrangler event "inbound_rejection" attributes event:attrs
|
|
59
|
+
raise collection event "member_rejected" attributes event:attrs
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Remove a member. Only relationships with members can be removed this way.
|
|
65
|
+
* @attr Id String "the member's relationship id"
|
|
66
|
+
*/
|
|
67
|
+
rule remove_member {
|
|
68
|
+
select when collection member_removal_requested
|
|
69
|
+
Id re#(.+)#
|
|
70
|
+
setting(id)
|
|
71
|
+
pre {
|
|
72
|
+
member = members().filter(function(bus) { bus{"Id"} == id }).head()
|
|
73
|
+
}
|
|
74
|
+
if member then noop()
|
|
75
|
+
fired {
|
|
76
|
+
raise wrangler event "subscription_cancellation" attributes {"Id": id}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
rule member_added {
|
|
81
|
+
select when wrangler subscription_added
|
|
82
|
+
where event:attr("bus"){"Tx_role"} == "member"
|
|
83
|
+
always {
|
|
84
|
+
raise collection event "member_added" attributes event:attrs
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
rule member_removed {
|
|
89
|
+
select when wrangler subscription_removed
|
|
90
|
+
where event:attr("bus"){"Tx_role"} == "member"
|
|
91
|
+
always {
|
|
92
|
+
raise collection event "member_removed" attributes event:attrs
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Room report: uses com.example.collection as a module. It asks every member
|
|
3
|
+
* for its reading over the relationship, averages the temperatures, and shows
|
|
4
|
+
* them on a page. It knows nothing about how members join; that is the
|
|
5
|
+
* collection ruleset's job.
|
|
6
|
+
*/
|
|
7
|
+
ruleset com.example.collection.report {
|
|
8
|
+
meta {
|
|
9
|
+
name "Room report"
|
|
10
|
+
description "Reports the temperature of every room in the collection."
|
|
11
|
+
author "PICOLABS"
|
|
12
|
+
|
|
13
|
+
use module com.example.collection alias collection
|
|
14
|
+
use module io.picolabs.subscription alias subscription
|
|
15
|
+
use module io.picolabs.xmlui alias ui
|
|
16
|
+
|
|
17
|
+
shares report, rejections, reportPage
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
global {
|
|
21
|
+
channel_tags = ["collection", "app"]
|
|
22
|
+
room_rid = "com.example.collection.room"
|
|
23
|
+
|
|
24
|
+
readingOf = function(bus) {
|
|
25
|
+
reading = subscription:queryOnSub(bus{"Id"}, room_rid, "reading")
|
|
26
|
+
row = {"Id": bus{"Id"}, "name": reading{"name"}, "temperature": reading{"temperature"}}
|
|
27
|
+
row
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Every member's reading, and the average temperature.
|
|
32
|
+
* @returns Map { members, count, average }
|
|
33
|
+
*/
|
|
34
|
+
report = function() {
|
|
35
|
+
rows = collection:members().map(readingOf)
|
|
36
|
+
temperatures = rows
|
|
37
|
+
.map(function(row) { row{"temperature"} })
|
|
38
|
+
.filter(function(t) { t.typeof() == "Number" })
|
|
39
|
+
average = temperatures.length() > 0
|
|
40
|
+
=> math:round(temperatures.reduce(function(a, b) { a + b }, 0) / temperatures.length(), 1)
|
|
41
|
+
| null
|
|
42
|
+
result = {"members": rows, "count": rows.length(), "average": average}
|
|
43
|
+
result
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Requests the collection turned away.
|
|
48
|
+
* @returns Array of { name, role, time }
|
|
49
|
+
*/
|
|
50
|
+
rejections = function() {
|
|
51
|
+
collection:rejections()
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* The rooms in the collection and their temperatures.
|
|
56
|
+
* @view "Rooms"
|
|
57
|
+
*/
|
|
58
|
+
reportPage = function() {
|
|
59
|
+
report_url = ui:query_url("report", meta:rid, meta:eci)
|
|
60
|
+
rejections_url = ui:query_url("rejections", meta:rid, meta:eci)
|
|
61
|
+
remove_url = ui:event_url("collection", "member_removal_requested")
|
|
62
|
+
<<
|
|
63
|
+
<Component name="ReportPage">
|
|
64
|
+
<DataSource id="report" url="#{report_url}" headers="{appGlobals.auth}" pollIntervalInSeconds="3" />
|
|
65
|
+
<DataSource id="rejected" url="#{rejections_url}" headers="{appGlobals.auth}" pollIntervalInSeconds="3" />
|
|
66
|
+
<APICall id="remove" method="post" url="#{remove_url}" headers="{appGlobals.auth}" body="{$param}"
|
|
67
|
+
onSuccess="report.refetch()" />
|
|
68
|
+
<PicoTheme>
|
|
69
|
+
<Card title="Rooms"
|
|
70
|
+
subtitle="{report.value.count + ' rooms in the collection' + (report.value.average === null ? '' : ', averaging ' + report.value.average + ' F')}" />
|
|
71
|
+
<Table data="{report.value.members}">
|
|
72
|
+
<Column bindTo="name" header="Room" />
|
|
73
|
+
<Column bindTo="temperature" header="Temperature (F)" />
|
|
74
|
+
<Column header="">
|
|
75
|
+
<Button label="Remove" variant="outlined" onClick="remove.execute({Id: $item.Id})" />
|
|
76
|
+
</Column>
|
|
77
|
+
</Table>
|
|
78
|
+
<Card title="Turned away" subtitle="Requests to join in a role other than member." />
|
|
79
|
+
<Table data="{rejected}">
|
|
80
|
+
<Column bindTo="name" header="Pico" />
|
|
81
|
+
<Column bindTo="role" header="Asked to be" />
|
|
82
|
+
</Table>
|
|
83
|
+
</PicoTheme>
|
|
84
|
+
</Component>
|
|
85
|
+
>>
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
rule create_app_channel {
|
|
90
|
+
select when wrangler ruleset_installed where event:attr("rids") >< meta:rid
|
|
91
|
+
ui:create_app_channel(channel_tags, ["collection"], ["report", "rejections", "reportPage"], meta:rid)
|
|
92
|
+
}
|
|
93
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Room: a member pico. It has a temperature, joins a collection through the
|
|
3
|
+
* collection's well-known channel, and can leave and rejoin.
|
|
4
|
+
*/
|
|
5
|
+
ruleset com.example.collection.room {
|
|
6
|
+
meta {
|
|
7
|
+
name "Room"
|
|
8
|
+
description "A pico with a temperature that joins a collection as a member."
|
|
9
|
+
author "PICOLABS"
|
|
10
|
+
|
|
11
|
+
use module io.picolabs.wrangler alias wrangler
|
|
12
|
+
use module io.picolabs.subscription alias subscription
|
|
13
|
+
|
|
14
|
+
shares reading, membership
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
global {
|
|
18
|
+
/**
|
|
19
|
+
* This room's name and temperature.
|
|
20
|
+
* @returns Map
|
|
21
|
+
*/
|
|
22
|
+
reading = function() {
|
|
23
|
+
result = {"name": wrangler:name(), "temperature": ent:temperature}
|
|
24
|
+
result
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* The relationship with the collection, or null when this room isn't in one.
|
|
29
|
+
* @returns Map
|
|
30
|
+
*/
|
|
31
|
+
membership = function() {
|
|
32
|
+
subscription:established("Tx_role", "collection").head()
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
rule installed {
|
|
37
|
+
select when wrangler ruleset_installed where event:attr("rids") >< meta:rid
|
|
38
|
+
if event:attr("temperature") then noop()
|
|
39
|
+
fired {
|
|
40
|
+
ent:temperature := event:attr("temperature").as("Number")
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Set this room's temperature.
|
|
46
|
+
* @attr temperature Number "degrees F"
|
|
47
|
+
* @example "70" { "temperature": "70" }
|
|
48
|
+
*/
|
|
49
|
+
rule new_temperature {
|
|
50
|
+
select when room new_temperature
|
|
51
|
+
temperature re#^(-?\d+(?:[.]\d+)?)$#
|
|
52
|
+
setting(temperature)
|
|
53
|
+
always {
|
|
54
|
+
ent:temperature := temperature.as("Number")
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Ask to join a collection.
|
|
60
|
+
* @attr wellKnown_Tx String "the collection's well-known channel id"
|
|
61
|
+
* @attr as String optional "the role to ask for: member, unless you want to see a request turned away"
|
|
62
|
+
*/
|
|
63
|
+
rule join {
|
|
64
|
+
select when collection join_requested
|
|
65
|
+
wellKnown_Tx re#(.+)#
|
|
66
|
+
setting(well_known)
|
|
67
|
+
always {
|
|
68
|
+
ent:collection_wellKnown := well_known
|
|
69
|
+
raise wrangler event "subscription" attributes {
|
|
70
|
+
"wellKnown_Tx": well_known,
|
|
71
|
+
"name": wrangler:name(),
|
|
72
|
+
"Rx_role": event:attr("as") || "member",
|
|
73
|
+
"Tx_role": "collection"
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Leave the collection.
|
|
80
|
+
*/
|
|
81
|
+
rule leave {
|
|
82
|
+
select when collection leave_requested
|
|
83
|
+
pre {
|
|
84
|
+
bus = membership()
|
|
85
|
+
}
|
|
86
|
+
if bus then noop()
|
|
87
|
+
fired {
|
|
88
|
+
raise wrangler event "subscription_cancellation" attributes {"Id": bus{"Id"}}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Join the last collection this room asked to join, again.
|
|
94
|
+
*/
|
|
95
|
+
rule rejoin {
|
|
96
|
+
select when collection rejoin_requested where ent:collection_wellKnown
|
|
97
|
+
always {
|
|
98
|
+
raise collection event "join_requested" attributes {"wellKnown_Tx": ent:collection_wellKnown}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
- Open **Rooms**: four rooms averaging 69.5 F, and Stranger under **Turned away**.
|
|
2
|
+
- To see the mesh the demo built, leave the **Try It!** tab and close the pico. The demo's child has six children: Rooms, Kitchen, Office, Garage, Bedroom, and Stranger.
|
|
3
|
+
- Click into each one and open its **Relationships** tab. Rooms has a relationship with each room, with Rooms in the role `collection` and the room in the role `member`. Each room has the same relationship, seen from its side. Stranger has none.
|
|
4
|
+
- Why was Stranger turned away? Read `accept_member` in `com.example.collection.krl`, then see how the bootstrap set Stranger up. (The cookbook has the answer.)
|
|
5
|
+
- On **Rooms**, click **Remove** next to a room, then look at that room's **Relationships** tab. The relationship is gone on both sides.
|
|
6
|
+
- On a room's **Testing** tab, raise `collection` `leave_requested`, then `collection` `rejoin_requested`, and watch the Rooms page.
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# Echo demo
|
|
2
|
+
|
|
3
|
+
The hello world of events: send an event to a pico and get back the directive a rule answers with.
|
|
4
|
+
|
|
5
|
+
- Overview: [`docs/cookbook/echo/README.md`](../../docs/cookbook/echo/README.md) (on the docs site: https://docs.picolabs.io/#/cookbook/echo/)
|
|
6
|
+
- Instructions: [`docs/cookbook/echo/instructions.md`](../../docs/cookbook/echo/instructions.md)
|
|
7
|
+
|
|
8
|
+
| File | Shows |
|
|
9
|
+
|------|-------|
|
|
10
|
+
| `com.example.echo.krl` | Two rules that answer `echo:hello` and `echo:message` with a `say` directive, and a page that sends both |
|
|
11
|
+
| `com.example.echo.bootstrap.krl` | Sets up the demo on a new pico; the **Try It!** tab runs it |
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Try It! bootstrap for the Echo demo. Install it on a new pico: it installs
|
|
3
|
+
* io.picolabs.xmlui and com.example.echo. tryIt() reports progress and links
|
|
4
|
+
* to the Echo page.
|
|
5
|
+
*
|
|
6
|
+
* This ruleset does not use io.picolabs.xmlui as a module, because it is the
|
|
7
|
+
* one that installs it.
|
|
8
|
+
*/
|
|
9
|
+
ruleset com.example.echo.bootstrap {
|
|
10
|
+
meta {
|
|
11
|
+
name "Echo"
|
|
12
|
+
description "Shows the basics of events: selecting an event with select when, matching an attribute with a regex, answering with send_directive, and sending the same event from a page and from curl."
|
|
13
|
+
author "PICOLABS"
|
|
14
|
+
|
|
15
|
+
use module io.picolabs.wrangler alias wrangler
|
|
16
|
+
|
|
17
|
+
shares tryIt
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
global {
|
|
21
|
+
app_rid = "com.example.echo"
|
|
22
|
+
xmlui_rid = "io.picolabs.xmlui"
|
|
23
|
+
|
|
24
|
+
host = function() {
|
|
25
|
+
meta:host.defaultsTo("").replace(re#/$#, "")
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Progress of this demo's setup, and the links to open when it is ready.
|
|
30
|
+
* @returns Map
|
|
31
|
+
*/
|
|
32
|
+
tryIt = function() {
|
|
33
|
+
app = wrangler:channels(["echo", "app"]).head()
|
|
34
|
+
ready = (ent:stage == "ready" && app) => true | false
|
|
35
|
+
status = {
|
|
36
|
+
"ready": ready,
|
|
37
|
+
"stage": ent:stage.defaultsTo("starting"),
|
|
38
|
+
"links": ready => [
|
|
39
|
+
{"label": "Echo page", "url": host() + "/ui/" + app{"id"} + "/" + app_rid + "/"}
|
|
40
|
+
] | []
|
|
41
|
+
}
|
|
42
|
+
status
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
rule start {
|
|
47
|
+
select when wrangler ruleset_installed where event:attr("rids") >< meta:rid
|
|
48
|
+
pre {
|
|
49
|
+
has_xmlui = wrangler:installedRIDs() >< xmlui_rid
|
|
50
|
+
}
|
|
51
|
+
always {
|
|
52
|
+
ent:stage := "installing"
|
|
53
|
+
raise wrangler event "new_channel_request" attributes {
|
|
54
|
+
"tags": ["try_it"],
|
|
55
|
+
"eventPolicy": {"allow": [], "deny": []},
|
|
56
|
+
"queryPolicy": {"allow": [{"rid": meta:rid, "name": "tryIt"}], "deny": []}
|
|
57
|
+
}
|
|
58
|
+
raise wrangler event "install_ruleset_request" attributes
|
|
59
|
+
{"url": host() + "/krl/" + xmlui_rid + ".krl"}
|
|
60
|
+
if not has_xmlui
|
|
61
|
+
raise wrangler event "install_ruleset_request" attributes
|
|
62
|
+
{"absoluteURL": meta:rulesetURI, "rid": app_rid}
|
|
63
|
+
if has_xmlui
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
rule install_app {
|
|
68
|
+
select when wrangler ruleset_installed where event:attr("rids") >< xmlui_rid
|
|
69
|
+
always {
|
|
70
|
+
raise wrangler event "install_ruleset_request" attributes
|
|
71
|
+
{"absoluteURL": meta:rulesetURI, "rid": app_rid}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
rule app_installed {
|
|
76
|
+
select when wrangler ruleset_installed where event:attr("rids") >< app_rid
|
|
77
|
+
always {
|
|
78
|
+
ent:stage := "ready"
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Echo: the hello world of events and directives. An event comes in; a rule
|
|
3
|
+
* selects on it and answers with a directive, which the sender gets back in the
|
|
4
|
+
* response. The view sends the events and shows the directive.
|
|
5
|
+
*/
|
|
6
|
+
ruleset com.example.echo {
|
|
7
|
+
meta {
|
|
8
|
+
name "Echo"
|
|
9
|
+
description "Answers echo:hello and echo:message events with a say directive."
|
|
10
|
+
author "PICOLABS"
|
|
11
|
+
|
|
12
|
+
use module io.picolabs.xmlui alias ui
|
|
13
|
+
|
|
14
|
+
shares echoPage
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
global {
|
|
18
|
+
channel_tags = ["echo", "app"]
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Send an echo event and see the directive that comes back.
|
|
22
|
+
* @view "Echo"
|
|
23
|
+
*/
|
|
24
|
+
echoPage = function() {
|
|
25
|
+
hello_url = ui:event_url("echo", "hello")
|
|
26
|
+
message_url = ui:event_url("echo", "message")
|
|
27
|
+
<<
|
|
28
|
+
<Component name="EchoPage" var.reply="">
|
|
29
|
+
<APICall id="hello" method="post" url="#{hello_url}" headers="{appGlobals.auth}"
|
|
30
|
+
onSuccess="(result) => reply = result.directives[0].options.something" />
|
|
31
|
+
<APICall id="say" method="post" url="#{message_url}" headers="{appGlobals.auth}" body="{$param}"
|
|
32
|
+
onSuccess="(result) => reply = result.directives[0].options.something" />
|
|
33
|
+
<PicoTheme>
|
|
34
|
+
<Card title="Echo"
|
|
35
|
+
subtitle="Each button raises an event on this pico. A rule answers with a say directive, shown below." />
|
|
36
|
+
<HStack>
|
|
37
|
+
<Button label="Say hello" onClick="hello.execute({})" />
|
|
38
|
+
</HStack>
|
|
39
|
+
<Form saveLabel="Echo it" onSubmit="(data) => say.execute(data)">
|
|
40
|
+
<FormItem bindTo="input" label="Message" initialValue="KRL programs the Internet!" />
|
|
41
|
+
</Form>
|
|
42
|
+
<Card title="The pico said" subtitle="{reply || '(nothing yet)'}" />
|
|
43
|
+
<Text>The same message event from a terminal:</Text>
|
|
44
|
+
<Text variant="code">curl "#{meta:host}/sky/event/#{meta:eci}/curl/echo/message?input=hello"</Text>
|
|
45
|
+
</PicoTheme>
|
|
46
|
+
</Component>
|
|
47
|
+
>>
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Answer with "Hello World".
|
|
53
|
+
*/
|
|
54
|
+
rule hello_world {
|
|
55
|
+
select when echo hello
|
|
56
|
+
send_directive("say", {"something": "Hello World"})
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Answer with the message that came in.
|
|
61
|
+
* @attr input String "what to echo"
|
|
62
|
+
* @example "hi" { "input": "hi" }
|
|
63
|
+
*/
|
|
64
|
+
rule echo {
|
|
65
|
+
select when echo message input re#(.*)# setting(m)
|
|
66
|
+
send_directive("say", {"something": m})
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
rule create_app_channel {
|
|
70
|
+
select when wrangler ruleset_installed where event:attr("rids") >< meta:rid
|
|
71
|
+
ui:create_app_channel(channel_tags, ["echo"], ["echoPage"], meta:rid)
|
|
72
|
+
}
|
|
73
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
- Open **Echo page**. Click **Say hello**, then **Echo it**. Each answer is the `say` directive a rule sent back.
|
|
2
|
+
- Copy the `curl` line at the bottom of the page into a terminal. The JSON has the same directive, plus `ruleOutcomes`, which names the rule that fired.
|
|
3
|
+
- Change `echo/message` in that URL to `wrangler/new_child_request?name=x`. The pico refuses it: `Not allowed by channel policy`.
|
|
4
|
+
- Open the child pico and look at its **Channels** tab. The `echo, app` channel allows `echo` events and nothing else. That's why the last step failed.
|
|
5
|
+
- On its **Logging** tab, find the `echo:message` events you sent and the rule each one fired.
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
# Guard rules demo
|
|
2
|
+
|
|
3
|
+
Ask for something five times and get it once. A guard rule makes a request idempotent, and a gate rule with `last` refuses work until it can be done.
|
|
4
|
+
|
|
5
|
+
- Overview: [`docs/cookbook/guard_rules/README.md`](../../docs/cookbook/guard_rules/README.md) (on the docs site: https://docs.picolabs.io/#/cookbook/guard_rules/)
|
|
6
|
+
- Instructions: [`docs/cookbook/guard_rules/instructions.md`](../../docs/cookbook/guard_rules/instructions.md)
|
|
7
|
+
|
|
8
|
+
| File | Shows |
|
|
9
|
+
|------|-------|
|
|
10
|
+
| `com.example.archive.krl` | A guard rule and the rule it starts, a gate rule with `last`, and the Archive page |
|
|
11
|
+
| `com.example.archive_store.krl` | Installed on the Archive child pico; keeps the notes |
|
|
12
|
+
| `com.example.archive.bootstrap.krl` | Sets up the demo on a new pico, asks for the archive five times, and tries a note too early; the **Try It!** tab runs it |
|