pico-engine 1.9.4 → 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 +14 -0
- package/krl/io.picolabs.xmlui.krl +3 -2
- package/mcp/docs/CLAUDE.md +3 -2
- package/mcp/docs/pico-engine.md +3 -0
- package/package.json +6 -3
- 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 +15 -0
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Try It! bootstrap for the Guard rules demo. Install it on a new pico: it
|
|
3
|
+
* installs io.picolabs.xmlui and com.example.archive. Then, in one go, it asks
|
|
4
|
+
* for the archive five times and tries to add a note before the archive exists.
|
|
5
|
+
* The guard lets one request through and the gate refuses the early note. When
|
|
6
|
+
* the archive is created, it adds a note that is stored. tryIt() reports
|
|
7
|
+
* progress and links to the Archive page.
|
|
8
|
+
*
|
|
9
|
+
* This ruleset does not use io.picolabs.xmlui as a module, because it is the
|
|
10
|
+
* one that installs it.
|
|
11
|
+
*/
|
|
12
|
+
ruleset com.example.archive.bootstrap {
|
|
13
|
+
meta {
|
|
14
|
+
name "Guard rules"
|
|
15
|
+
description "Shows guard rules for idempotent requests, a gate rule that stops later rules with last, and why rule order within a ruleset matters."
|
|
16
|
+
author "PICOLABS"
|
|
17
|
+
|
|
18
|
+
use module io.picolabs.wrangler alias wrangler
|
|
19
|
+
|
|
20
|
+
shares tryIt
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
global {
|
|
24
|
+
app_rid = "com.example.archive"
|
|
25
|
+
xmlui_rid = "io.picolabs.xmlui"
|
|
26
|
+
|
|
27
|
+
host = function() {
|
|
28
|
+
meta:host.defaultsTo("").replace(re#/$#, "")
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Progress of this demo's setup, and the links to open when it is ready.
|
|
33
|
+
* @returns Map
|
|
34
|
+
*/
|
|
35
|
+
tryIt = function() {
|
|
36
|
+
app = wrangler:channels(["archive", "app"]).head()
|
|
37
|
+
ready = (ent:stage == "ready" && app) => true | false
|
|
38
|
+
status = {
|
|
39
|
+
"ready": ready,
|
|
40
|
+
"stage": ent:stage.defaultsTo("starting"),
|
|
41
|
+
"links": ready => [
|
|
42
|
+
{"label": "Archive", "url": host() + "/ui/" + app{"id"} + "/" + app_rid + "/"}
|
|
43
|
+
] | []
|
|
44
|
+
}
|
|
45
|
+
status
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
rule start {
|
|
50
|
+
select when wrangler ruleset_installed where event:attr("rids") >< meta:rid
|
|
51
|
+
pre {
|
|
52
|
+
has_xmlui = wrangler:installedRIDs() >< xmlui_rid
|
|
53
|
+
}
|
|
54
|
+
always {
|
|
55
|
+
ent:stage := "installing"
|
|
56
|
+
raise wrangler event "new_channel_request" attributes {
|
|
57
|
+
"tags": ["try_it"],
|
|
58
|
+
"eventPolicy": {"allow": [], "deny": []},
|
|
59
|
+
"queryPolicy": {"allow": [{"rid": meta:rid, "name": "tryIt"}], "deny": []}
|
|
60
|
+
}
|
|
61
|
+
raise wrangler event "install_ruleset_request" attributes
|
|
62
|
+
{"url": host() + "/krl/" + xmlui_rid + ".krl"}
|
|
63
|
+
if not has_xmlui
|
|
64
|
+
raise wrangler event "install_ruleset_request" attributes
|
|
65
|
+
{"absoluteURL": meta:rulesetURI, "rid": app_rid}
|
|
66
|
+
if has_xmlui
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
rule install_app {
|
|
71
|
+
select when wrangler ruleset_installed where event:attr("rids") >< xmlui_rid
|
|
72
|
+
always {
|
|
73
|
+
raise wrangler event "install_ruleset_request" attributes
|
|
74
|
+
{"absoluteURL": meta:rulesetURI, "rid": app_rid}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
rule ask_five_times {
|
|
79
|
+
select when wrangler ruleset_installed where event:attr("rids") >< app_rid
|
|
80
|
+
foreach [1, 2, 3, 4, 5] setting(n)
|
|
81
|
+
always {
|
|
82
|
+
ent:stage := "asking for the archive"
|
|
83
|
+
raise archive event "needed" attributes {"try": n}
|
|
84
|
+
raise archive event "note_added" attributes {"text": "Too early"} on final
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
rule add_a_note {
|
|
89
|
+
select when archive archive_created where ent:stage != "ready"
|
|
90
|
+
always {
|
|
91
|
+
raise archive event "note_added" attributes {"text": "Written after the archive was created"}
|
|
92
|
+
ent:stage := "ready"
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Guard rules: this pico needs exactly one Archive child pico, however many
|
|
3
|
+
* times archive:needed arrives. A guard rule decides whether to go ahead and
|
|
4
|
+
* raises an event; a second rule does the work. Asking again changes nothing,
|
|
5
|
+
* so the request is idempotent.
|
|
6
|
+
*
|
|
7
|
+
* Notes are stored in the archive. A gate rule stops archive:note_added with
|
|
8
|
+
* `last` until the archive exists. Rules in one ruleset that select the same
|
|
9
|
+
* event run in the order they are written, and `last` skips the rest of this
|
|
10
|
+
* ruleset's rules for the event, so the gate comes first and lives in the same
|
|
11
|
+
* ruleset as the rule it guards.
|
|
12
|
+
*/
|
|
13
|
+
ruleset com.example.archive {
|
|
14
|
+
meta {
|
|
15
|
+
name "Archive keeper"
|
|
16
|
+
description "Creates one Archive child however often it is asked, and stores notes in it once it exists."
|
|
17
|
+
author "PICOLABS"
|
|
18
|
+
|
|
19
|
+
use module io.picolabs.wrangler alias wrangler
|
|
20
|
+
use module io.picolabs.xmlui alias ui
|
|
21
|
+
|
|
22
|
+
shares status, notes, archivePage
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
global {
|
|
26
|
+
channel_tags = ["archive", "app"]
|
|
27
|
+
store_rid = "com.example.archive_store"
|
|
28
|
+
|
|
29
|
+
archives = function() {
|
|
30
|
+
wrangler:children().filter(function(child) { child{"name"} == "Archive" })
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* The archive's state and the counts the page shows.
|
|
35
|
+
* @returns Map
|
|
36
|
+
*/
|
|
37
|
+
status = function() {
|
|
38
|
+
state = ent:archive_eci => "ready" | ent:creating => "being created" | "none"
|
|
39
|
+
counts = {
|
|
40
|
+
"archive": state,
|
|
41
|
+
"archive_picos": archives().length(),
|
|
42
|
+
"requests": ent:requests.defaultsTo(0),
|
|
43
|
+
"ignored": ent:ignored.defaultsTo(0),
|
|
44
|
+
"gated": ent:gated.defaultsTo(0)
|
|
45
|
+
}
|
|
46
|
+
counts
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* The notes stored in the archive, oldest first.
|
|
51
|
+
* @returns Array of { text, time }
|
|
52
|
+
*/
|
|
53
|
+
notes = function() {
|
|
54
|
+
stored = ent:archive_eci => wrangler:picoQuery(ent:archive_eci, store_rid, "notes") | []
|
|
55
|
+
stored.typeof() == "Array" => stored | []
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Ask for the archive as often as you like, and add notes.
|
|
60
|
+
* @view "Archive"
|
|
61
|
+
*/
|
|
62
|
+
archivePage = function() {
|
|
63
|
+
status_url = ui:query_url("status", meta:rid, meta:eci)
|
|
64
|
+
notes_url = ui:query_url("notes", meta:rid, meta:eci)
|
|
65
|
+
needed_url = ui:event_url("archive", "needed")
|
|
66
|
+
note_url = ui:event_url("archive", "note_added")
|
|
67
|
+
reset_url = ui:event_url("archive", "reset")
|
|
68
|
+
<<
|
|
69
|
+
<Component name="ArchivePage" var.said="">
|
|
70
|
+
<DataSource id="status" url="#{status_url}" headers="{appGlobals.auth}" pollIntervalInSeconds="1" />
|
|
71
|
+
<DataSource id="notes" url="#{notes_url}" headers="{appGlobals.auth}" pollIntervalInSeconds="2" />
|
|
72
|
+
<APICall id="needed" method="post" url="#{needed_url}" headers="{appGlobals.auth}"
|
|
73
|
+
onSuccess="(result) => { said = result.directives.length ? 'Guard: go ahead' : 'Guard: already have one, ignored'; status.refetch(); }" />
|
|
74
|
+
<APICall id="note" method="post" url="#{note_url}" headers="{appGlobals.auth}" body="{$param}"
|
|
75
|
+
onSuccess="(result) => { said = result.directives.length ? 'Note: ' + result.directives[0].options.result : ''; status.refetch(); notes.refetch(); }" />
|
|
76
|
+
<APICall id="reset" method="post" url="#{reset_url}" headers="{appGlobals.auth}"
|
|
77
|
+
onSuccess="said = ''; status.refetch(); notes.refetch();" />
|
|
78
|
+
<PicoTheme>
|
|
79
|
+
<Card title="Archive"
|
|
80
|
+
subtitle="Click Request the archive as often as you like. The guard lets one request through; the rest change nothing." />
|
|
81
|
+
<HStack>
|
|
82
|
+
<Button label="Request the archive" onClick="needed.execute({})" />
|
|
83
|
+
<Button label="Start over" variant="outlined" onClick="reset.execute({})" />
|
|
84
|
+
</HStack>
|
|
85
|
+
<Text when="{said}">{said}</Text>
|
|
86
|
+
<Table data="{[status.value]}">
|
|
87
|
+
<Column bindTo="archive" header="Archive" />
|
|
88
|
+
<Column bindTo="archive_picos" header="Archive picos" />
|
|
89
|
+
<Column bindTo="requests" header="Requests" />
|
|
90
|
+
<Column bindTo="ignored" header="Ignored" />
|
|
91
|
+
<Column bindTo="gated" header="Notes refused" />
|
|
92
|
+
</Table>
|
|
93
|
+
<Form saveLabel="Add a note" onSubmit="(data) => note.execute(data)">
|
|
94
|
+
<FormItem bindTo="text" label="Note" initialValue="Remember the milk" />
|
|
95
|
+
</Form>
|
|
96
|
+
<Table data="{notes}">
|
|
97
|
+
<Column bindTo="text" header="Notes in the archive" />
|
|
98
|
+
<Column bindTo="time" header="Stored" />
|
|
99
|
+
</Table>
|
|
100
|
+
</PicoTheme>
|
|
101
|
+
</Component>
|
|
102
|
+
>>
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Guard: go ahead only when there is no archive and none is being created.
|
|
108
|
+
* The flag ent:creating matters: the child does not exist until
|
|
109
|
+
* child_initialized arrives, and requests can come in before then.
|
|
110
|
+
*/
|
|
111
|
+
rule archive_needed {
|
|
112
|
+
select when archive needed
|
|
113
|
+
pre {
|
|
114
|
+
allowed = not ent:archive_eci && not ent:creating
|
|
115
|
+
}
|
|
116
|
+
if allowed then send_directive("archive", {"result": "creating"})
|
|
117
|
+
fired {
|
|
118
|
+
ent:creating := true
|
|
119
|
+
raise archive event "creation_approved"
|
|
120
|
+
}
|
|
121
|
+
else {
|
|
122
|
+
ent:ignored := ent:ignored.defaultsTo(0) + 1
|
|
123
|
+
}
|
|
124
|
+
finally {
|
|
125
|
+
ent:requests := ent:requests.defaultsTo(0) + 1
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// The work: create the archive. Only the guard raises this event.
|
|
130
|
+
rule create_archive {
|
|
131
|
+
select when archive creation_approved
|
|
132
|
+
always {
|
|
133
|
+
raise wrangler event "new_child_request" attributes {"name": "Archive", "archive_role": "store"}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// The child is set up: give it the store ruleset, and tell anyone listening.
|
|
138
|
+
rule archive_initialized {
|
|
139
|
+
select when wrangler child_initialized where event:attr("archive_role") == "store"
|
|
140
|
+
event:send({
|
|
141
|
+
"eci": event:attr("eci"),
|
|
142
|
+
"domain": "wrangler",
|
|
143
|
+
"type": "install_ruleset_request",
|
|
144
|
+
"attrs": {"absoluteURL": meta:rulesetURI, "rid": store_rid}
|
|
145
|
+
})
|
|
146
|
+
always {
|
|
147
|
+
ent:archive_eci := event:attr("eci")
|
|
148
|
+
ent:creating := false
|
|
149
|
+
raise archive event "archive_created" attributes {"eci": event:attr("eci")}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Gate: no archive, no notes. It comes before store_note, and `last` skips
|
|
155
|
+
* store_note for this event.
|
|
156
|
+
*/
|
|
157
|
+
rule require_archive {
|
|
158
|
+
select when archive note_added
|
|
159
|
+
if not ent:archive_eci then send_directive("note", {"result": "refused, no archive yet"})
|
|
160
|
+
fired {
|
|
161
|
+
ent:gated := ent:gated.defaultsTo(0) + 1
|
|
162
|
+
last
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Store a note in the archive.
|
|
168
|
+
* @attr text String "the note"
|
|
169
|
+
* @example "milk" { "text": "Remember the milk" }
|
|
170
|
+
*/
|
|
171
|
+
rule store_note {
|
|
172
|
+
select when archive note_added
|
|
173
|
+
text re#(.+)#
|
|
174
|
+
setting(text)
|
|
175
|
+
every {
|
|
176
|
+
event:send({
|
|
177
|
+
"eci": ent:archive_eci,
|
|
178
|
+
"domain": "archive",
|
|
179
|
+
"type": "store",
|
|
180
|
+
"attrs": {"text": text}
|
|
181
|
+
})
|
|
182
|
+
send_directive("note", {"result": "stored"})
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Delete the archive and the counts, to try again.
|
|
188
|
+
*/
|
|
189
|
+
rule reset {
|
|
190
|
+
select when archive reset
|
|
191
|
+
always {
|
|
192
|
+
raise archive event "archives_to_delete" attributes {"archives": archives()}
|
|
193
|
+
ent:archive_eci := null
|
|
194
|
+
ent:creating := false
|
|
195
|
+
ent:requests := 0
|
|
196
|
+
ent:ignored := 0
|
|
197
|
+
ent:gated := 0
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
rule delete_archives {
|
|
202
|
+
select when archive archives_to_delete
|
|
203
|
+
foreach event:attr("archives") setting(child)
|
|
204
|
+
always {
|
|
205
|
+
raise wrangler event "child_deletion_request" attributes {"eci": child{"eci"}}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
rule create_app_channel {
|
|
210
|
+
select when wrangler ruleset_installed where event:attr("rids") >< meta:rid
|
|
211
|
+
ui:create_app_channel(channel_tags, ["archive"], ["status", "notes", "archivePage"], meta:rid)
|
|
212
|
+
}
|
|
213
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Archive store: installed on the Archive child pico. Keeps the notes its
|
|
3
|
+
* parent sends and shares them back.
|
|
4
|
+
*/
|
|
5
|
+
ruleset com.example.archive_store {
|
|
6
|
+
meta {
|
|
7
|
+
name "Archive store"
|
|
8
|
+
description "Keeps the notes the archive keeper sends."
|
|
9
|
+
author "PICOLABS"
|
|
10
|
+
|
|
11
|
+
shares notes
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
global {
|
|
15
|
+
/**
|
|
16
|
+
* Every note, oldest first.
|
|
17
|
+
* @returns Array of { text, time }
|
|
18
|
+
*/
|
|
19
|
+
notes = function() {
|
|
20
|
+
ent:notes.defaultsTo([])
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Keep a note.
|
|
26
|
+
* @attr text String "the note"
|
|
27
|
+
*/
|
|
28
|
+
rule store {
|
|
29
|
+
select when archive store
|
|
30
|
+
text re#(.+)#
|
|
31
|
+
setting(text)
|
|
32
|
+
always {
|
|
33
|
+
ent:notes := notes().append({"text": text, "time": time:now()})
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
- Open **Archive**. The bootstrap asked for the archive five times: 1 archive pico, 5 requests, 4 ignored. It also added a note too early, so 1 note was refused.
|
|
2
|
+
- Click **Request the archive** several times. **Ignored** goes up, and **Archive picos** stays at 1.
|
|
3
|
+
- Leave the **Try It!** tab and close the pico. The demo's child has exactly one child, Archive.
|
|
4
|
+
- Click **Start over**, then **Add a note** before requesting the archive. The gate rule refuses it.
|
|
5
|
+
- Look at `archive_needed` in `com.example.archive.krl`. Why does it check `ent:creating` as well as `ent:archive_eci`? Remove `&& not ent:creating`, click **Flush**, and run the demo again from this window to find out.
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# KBlog demo
|
|
2
|
+
|
|
3
|
+
A blog written in KRL: four rulesets on one pico that work together by raising events, with a reader channel and an author channel. A new version of the [2011 KBlog](https://github.com/windley/KBlog).
|
|
4
|
+
|
|
5
|
+
- Overview: [`docs/cookbook/kblog/README.md`](../../docs/cookbook/kblog/README.md) (on the docs site: https://docs.picolabs.io/#/cookbook/kblog/)
|
|
6
|
+
- Instructions: [`docs/cookbook/kblog/instructions.md`](../../docs/cookbook/kblog/instructions.md)
|
|
7
|
+
|
|
8
|
+
| File | Shows |
|
|
9
|
+
|------|-------|
|
|
10
|
+
| `com.example.kblog.krl` | The pages (Home, an article page, Contact, Post) and the reader channel |
|
|
11
|
+
| `com.example.kblog.data.krl` | The articles: adds one on `kblog:new_article_available`, clears them, and provides them |
|
|
12
|
+
| `com.example.kblog.posting.krl` | Posting: checks each submitted post, provides the post form, and creates the author channel |
|
|
13
|
+
| `com.example.kblog.config.krl` | The title and the about and contact text, as a module |
|
|
14
|
+
| `com.example.kblog.bootstrap.krl` | Installs them in order on a new pico and submits three posts; the **Try It!** tab runs it |
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Try It! bootstrap for KBlog. Install it on a new pico: it installs
|
|
3
|
+
* io.picolabs.xmlui and the four KBlog rulesets, one after another, each when
|
|
4
|
+
* the one it needs is in place. Then it submits three posts the way an author
|
|
5
|
+
* would. tryIt() reports progress and links to the blog on the reader and
|
|
6
|
+
* author channels.
|
|
7
|
+
*
|
|
8
|
+
* This ruleset does not use io.picolabs.xmlui as a module, because it is the
|
|
9
|
+
* one that installs it.
|
|
10
|
+
*/
|
|
11
|
+
ruleset com.example.kblog.bootstrap {
|
|
12
|
+
meta {
|
|
13
|
+
name "KBlog"
|
|
14
|
+
description "Shows an application built from cooperating rulesets: an event hierarchy across rulesets, writes as events and reads as queries, multi-page views with routes, a component from another ruleset, and reader and author channels."
|
|
15
|
+
author "PICOLABS"
|
|
16
|
+
|
|
17
|
+
use module io.picolabs.wrangler alias wrangler
|
|
18
|
+
|
|
19
|
+
shares tryIt
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
global {
|
|
23
|
+
xmlui_rid = "io.picolabs.xmlui"
|
|
24
|
+
config_rid = "com.example.kblog.config"
|
|
25
|
+
data_rid = "com.example.kblog.data"
|
|
26
|
+
kblog_rid = "com.example.kblog"
|
|
27
|
+
posting_rid = "com.example.kblog.posting"
|
|
28
|
+
posts = [
|
|
29
|
+
{
|
|
30
|
+
"author": "Phil Windley",
|
|
31
|
+
"title": "Event hierarchies",
|
|
32
|
+
"body": "Events are nouns and rules are verbs. Each rule in KBlog does one thing and raises an event saying what is now true. Post something and watch the **event path**."
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
"author": "Phil Windley",
|
|
36
|
+
"title": "Rulesets in layers",
|
|
37
|
+
"body": "Posting is its own ruleset. Its install creates the author channel, the only channel that can submit a post."
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
"author": "Code Monkey",
|
|
41
|
+
"title": "Code Monkey was here",
|
|
42
|
+
"body": ":)"
|
|
43
|
+
}
|
|
44
|
+
]
|
|
45
|
+
|
|
46
|
+
host = function() {
|
|
47
|
+
meta:host.defaultsTo("").replace(re#/$#, "")
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
link = function(tags, label, path) {
|
|
51
|
+
channel = wrangler:channels(tags).head()
|
|
52
|
+
channel => {"label": label, "url": host() + "/ui/" + channel{"id"} + "/" + kblog_rid + "/" + path} | 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
|
+
links = [
|
|
61
|
+
link(["kblog", "reader"], "Read the blog (reader channel)", ""),
|
|
62
|
+
link(["kblog", "author"], "Write a post (author channel)", "#/new")
|
|
63
|
+
].filter(function(l) { l })
|
|
64
|
+
ready = (ent:stage == "ready" && links.length() == 2) => true | false
|
|
65
|
+
status = {"ready": ready, "stage": ent:stage.defaultsTo("starting"), "links": ready => links | []}
|
|
66
|
+
status
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
rule start {
|
|
71
|
+
select when wrangler ruleset_installed where event:attr("rids") >< meta:rid
|
|
72
|
+
pre {
|
|
73
|
+
has_xmlui = wrangler:installedRIDs() >< xmlui_rid
|
|
74
|
+
}
|
|
75
|
+
always {
|
|
76
|
+
ent:stage := "installing"
|
|
77
|
+
raise wrangler event "new_channel_request" attributes {
|
|
78
|
+
"tags": ["try_it"],
|
|
79
|
+
"eventPolicy": {"allow": [], "deny": []},
|
|
80
|
+
"queryPolicy": {"allow": [{"rid": meta:rid, "name": "tryIt"}], "deny": []}
|
|
81
|
+
}
|
|
82
|
+
raise wrangler event "install_ruleset_request" attributes
|
|
83
|
+
{"url": host() + "/krl/" + xmlui_rid + ".krl"}
|
|
84
|
+
if not has_xmlui
|
|
85
|
+
raise wrangler event "install_ruleset_request" attributes
|
|
86
|
+
{"absoluteURL": meta:rulesetURI, "rid": config_rid}
|
|
87
|
+
if has_xmlui
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
rule install_config {
|
|
92
|
+
select when wrangler ruleset_installed where event:attr("rids") >< xmlui_rid
|
|
93
|
+
always {
|
|
94
|
+
raise wrangler event "install_ruleset_request" attributes
|
|
95
|
+
{"absoluteURL": meta:rulesetURI, "rid": config_rid}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
rule install_data {
|
|
100
|
+
select when wrangler ruleset_installed where event:attr("rids") >< config_rid
|
|
101
|
+
always {
|
|
102
|
+
raise wrangler event "install_ruleset_request" attributes
|
|
103
|
+
{"absoluteURL": meta:rulesetURI, "rid": data_rid}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// The pages use the config and data rulesets as modules, so they come next.
|
|
108
|
+
rule install_pages {
|
|
109
|
+
select when wrangler ruleset_installed where event:attr("rids") >< data_rid
|
|
110
|
+
always {
|
|
111
|
+
raise wrangler event "install_ruleset_request" attributes
|
|
112
|
+
{"absoluteURL": meta:rulesetURI, "rid": kblog_rid}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
rule install_posting {
|
|
117
|
+
select when wrangler ruleset_installed where event:attr("rids") >< kblog_rid
|
|
118
|
+
always {
|
|
119
|
+
raise wrangler event "install_ruleset_request" attributes
|
|
120
|
+
{"absoluteURL": meta:rulesetURI, "rid": posting_rid}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
rule submit_posts {
|
|
125
|
+
select when wrangler ruleset_installed where event:attr("rids") >< posting_rid
|
|
126
|
+
foreach posts setting(post)
|
|
127
|
+
always {
|
|
128
|
+
raise kblog event "post_submitted" attributes post
|
|
129
|
+
ent:stage := "ready" on final
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* KBlog configuration: the blog's title and the text of its fixed pages, in one
|
|
3
|
+
* place. The other KBlog rulesets use it as a module.
|
|
4
|
+
*/
|
|
5
|
+
ruleset com.example.kblog.config {
|
|
6
|
+
meta {
|
|
7
|
+
name "KBlog configuration"
|
|
8
|
+
description "The blog's title, about text, and contact text."
|
|
9
|
+
author "PICOLABS"
|
|
10
|
+
|
|
11
|
+
provides blogtitle, about_text, contact_text
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
global {
|
|
15
|
+
// Modules provide functions; a provided plain value can't be called.
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* The blog's title.
|
|
19
|
+
* @returns String
|
|
20
|
+
*/
|
|
21
|
+
blogtitle = function() {
|
|
22
|
+
"KBlog"
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* The about box, in Markdown.
|
|
27
|
+
* @returns String
|
|
28
|
+
*/
|
|
29
|
+
about_text = function() {
|
|
30
|
+
<<
|
|
31
|
+
KBlog is a blog written in KRL. Four rulesets on one pico run it: one for the
|
|
32
|
+
pages, one for the articles, one for posting, and one for this text. Rules raise
|
|
33
|
+
events for each other; none of them knows how the others work.
|
|
34
|
+
|
|
35
|
+
It is a new version of the [2011 KBlog](https://www.windley.com/archives/2011/04/event_hierarchies_a_blog_written_in_krl.shtml).
|
|
36
|
+
>>
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* The Contact page, in Markdown.
|
|
41
|
+
* @returns String
|
|
42
|
+
*/
|
|
43
|
+
contact_text = function() {
|
|
44
|
+
<<
|
|
45
|
+
Pico Labs builds the pico engine and KRL. Find us at [picolabs.io](https://picolabs.io)
|
|
46
|
+
and the code at [github.com/Picolab](https://github.com/Picolab).
|
|
47
|
+
>>
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* KBlog data: keeps the articles. It adds an article when
|
|
3
|
+
* kblog:new_article_available arrives and raises kblog:new_article_added, and
|
|
4
|
+
* provides the articles to the other KBlog rulesets. Nothing else stores or
|
|
5
|
+
* changes them.
|
|
6
|
+
*/
|
|
7
|
+
ruleset com.example.kblog.data {
|
|
8
|
+
meta {
|
|
9
|
+
name "KBlog data"
|
|
10
|
+
description "Stores KBlog's articles and provides them to the other rulesets."
|
|
11
|
+
author "PICOLABS"
|
|
12
|
+
|
|
13
|
+
provides articles, article
|
|
14
|
+
shares articles, article
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
global {
|
|
18
|
+
/**
|
|
19
|
+
* Every article, newest first.
|
|
20
|
+
* @returns Array of { id, author, title, body, time }
|
|
21
|
+
*/
|
|
22
|
+
articles = function() {
|
|
23
|
+
ent:articles.defaultsTo({}).values()
|
|
24
|
+
.sort(function(a, b) { b{"id"} <=> a{"id"} })
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* One article.
|
|
29
|
+
* @param id String "the article's id"
|
|
30
|
+
* @returns Map
|
|
31
|
+
*/
|
|
32
|
+
article = function(id) {
|
|
33
|
+
ent:articles.defaultsTo({}){id.as("String")}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Store a new article. The posting ruleset raises this after it checks a
|
|
39
|
+
* submitted post.
|
|
40
|
+
* @attr author String "who wrote it"
|
|
41
|
+
* @attr title String "the title"
|
|
42
|
+
* @attr body String optional "the text, in Markdown"
|
|
43
|
+
*/
|
|
44
|
+
rule add_article {
|
|
45
|
+
select when kblog new_article_available
|
|
46
|
+
pre {
|
|
47
|
+
id = ent:next_id.defaultsTo(1)
|
|
48
|
+
entry = {
|
|
49
|
+
"id": id,
|
|
50
|
+
"author": event:attr("author"),
|
|
51
|
+
"title": event:attr("title"),
|
|
52
|
+
"body": event:attr("body") || "",
|
|
53
|
+
"time": time:now()
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
send_directive("add_article", {"id": id})
|
|
57
|
+
fired {
|
|
58
|
+
ent:next_id := id + 1
|
|
59
|
+
ent:articles{id.as("String")} := entry
|
|
60
|
+
raise kblog event "new_article_added" attributes entry
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Delete every article.
|
|
66
|
+
*/
|
|
67
|
+
rule clear_articles {
|
|
68
|
+
select when kblog clear_requested
|
|
69
|
+
send_directive("clear_articles")
|
|
70
|
+
fired {
|
|
71
|
+
ent:articles := {}
|
|
72
|
+
raise kblog event "articles_cleared"
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|