Warning | ||
---|---|---|
| ||
This page has not been adjusted for version 1.0 of the pico-engine |
Having a pico that maintains a collection of other picos is a pattern that is often useful.
...
The complete code for this ruleset can be found at https://raw.githubusercontent.com/Picolab/pico_lessons-engine/master/collections/edu.byu.enMotion.packages/pico-engine/krl/io.picolabs.collection.krl
Creating a collection pico
...
Code Block | ||
---|---|---|
| ||
rule collection_needed { select when grouper creation pre { name_prefix = event:attr("name_prefix"); group_name = name_prefix + " Rooms"; rids = "eduio.byu.enMotionpicolabs.collection;io.picolabs.subscription"; child_specs = { "name": group_name, "name_prefix": name_prefix, "rids": rids, "color": "#002e5d" }; } fired { raise wrangler event "new_child_request" attributes child_specs; } } |
...
Code Block | ||
---|---|---|
| ||
rule collection_subscriptions { select when wrangler child_initialized pre { eci = event:attr("eci"); collection_eci = get_wellKnown_eci(eci); } if collection_eci then noop(); fired { raise grouper event "need_subscriptions" attributes event:attrattrs("rs_attrs").put("collection_eci",collection_eci); } } |
...
This KRL code will respond to the internal event, expecting the collection_eci
and the child_specs
given to wrangler earlier (wrangler renames it rs_attrs
for some reason).
...
.
Code Block | ||
---|---|---|
| ||
rule collection_subscription_requests { select when grouper need_subscriptions foreach by_prefix(event:attr("name_prefix")) setting(room_name,tag_id) pre { eci = building:eci(tag_id); wellKnown_Tx = get_wellKnown_eci(eci).klog("wellKnown_Tx"); } if wellKnown_Tx then every { event:send({"eci":eci,"domain":"wrangler","type":"subscription", "attrs": { "wellKnown_Tx": event:attr("collection_eci"), "Rx_role": "member", "Tx_role": "collection", "name": tag_id, "channel_type": "subscription" } }) } } |
...
Code Block | ||
---|---|---|
| ||
meta { use module eduio.byupicolabs.enMotion.collection alias collection shares some_function } global { some_function = function() { collection:members()// some further processing } } rule some_rule { select when something required foreach collection:members() setting(subs) // some action/postludes for each subs } |
and specify additional processing in some_function
(defined in lines 6-8) that it shares (in line 3) to endpoints, and/or specify rules like some_rule
(defined in lines 10-14) that take action on the collection's members when certain events occur.
When a member is added
Since members of the collection are determined by subscriptions, your ruleset can listen for wrangler:subscription_added
events to do something when a new member joins your collection. Since your pico might participate in other subscriptions, only those in which the roles are "collection"
and "member"
would be applicable.
Code Block | ||
---|---|---|
| ||
rule new_member {
select when wrangler subscription_added
pre {
pertinent = event:attr("Rx_role")=="collection"
&& event:attr("Tx_role")=="member";
}
if pertinent then noop();
fired {
raise collection event "new_member" attributes event:attrs
}
} |
The event attributes available are those surrounding a subscription in general. Some of these might be useful when a new member arrives into your collection
Tip | ||
---|---|---|
| ||
|
When a member is removed
Your ruleset might wish to listen to the event wrangler:subscription_removed
which will be raised when a subscription is removed from your pico. Such events which are applicable to membership in your collection could then be handled appropriately.
Code Block | ||
---|---|---|
| ||
rule new_member {
select when wrangler subscription_removed
pre {
pertinent = event:attr("Rx_role")=="collection"
&& event:attr("Tx_role")=="member";
}
if pertinent then noop();
fired {
raise collection event "member_removed" attributes event:attrs
}
} |