Creating a channel for a ruleset

In version 1.X of the pico-engine, you will need channels for testing rulesets.

The policy for a testing channel should allow for the events that the rules in your ruleset select on, and all of the shared functions from the global block of your ruleset.

You can create this manually in the developer UI, or programmatically by adding a rule that selects on the wrangler:ruleset_installed event, and having that rule create a permissive channel specifically tailored to the rules and functions of the ruleset.

Instructions

Add an initialization rule to your ruleset, or add code like this to such a rule if you already have one.

  1. You will need to use wrangler as a module, so be sure to specify this in the meta block of your ruleset:

    meta { use module io.picolabs.wrangler alias wrangler }
  2. You will need a rule that selects on the wrangler:ruleset_installed event for this ruleset:

    rule initialize { select when wrangler ruleset_installed where event:attr("rids") >< meta:rid pre { tags = [meta:rid] domains = __testing{"events"} .map(function(e){e.get("domain")}) .unique() .sort() eventPolicy = { "allow":domains.map(function(d){{"domain":d,"name":"*"}}), "deny":[] } queryPolicy = {"allow":[{"rid":meta:rid,"name":"*"}],"deny":[]} } wrangler:createChannel(tags,eventPolicy,queryPolicy) }
  3. Having made this change, you will need to re-install the ruleset in your pico. Flushing is not sufficient. If you are using the developer UI, go to the “Rulesets” tab, then copy the ruleset’s URL, click on the “uninstall” button, paste the saved URL into the URL box and click on the “Install” button.

  4. Now go to the “Channels” tab and your channel should be there, tagged by the RID of your ruleset.

 How it works

It is based off of the __testing map which is automatically generated by the compiler, and which contains an “events” element listing all of the events selected for by the rules of your ruleset, and a “queries” element listing all of your shared global function names.

Lines 6-13 build up an array of allowed events, and line 14 allows any query for this ruleset. Line 16 creates the new channel, using as the only tag the RID of your ruleset (from line 5).

Line 6 starts with all of the events handled by your ruleset (an array of maps), and line 7 then maps each map to the domain of the event (so now you have an array of strings). Line 8 uses the Set Operator unique() to remove any duplicates from the array of event domains, and finally line 9 puts the list into alphabetic order (using the Array Operator sort()).

Line 11 uses the Array Operator map() to make a new array, an array of maps suitable for use in the “allow” element of an event policy.

Variation

If you don’t want the channel to allow any wrangler events, then you could add this Array Operator filter() to remove those. Insert this operator into the chain of operators after line 7:

.filter(function(d){d != "wrangler"})

Keeping channels clean

In the instructions given, Wrangler will tag your channel with the RID of your ruleset.

Note that any dots in the RID will be changed to hyphens in the creation process, so while you can use meta:rid to create a channel, you would have to use this to find it later when you call the wrangler:channels(meta:rid.replace(re#[.]#,"-")) function.

Especially during the ruleset writing and debugging process, but sometimes also in operation, you may need to install the ruleset multiple times. Each time you do so, yet another channel with those tags will be created and added to the pico. Since the usual idiom for finding the channel id is wrangler:channels(tags).head().get("id") (alternatively, wrangler:channels(tags).head(){"id"}), you only really want your pico to have one such channel. This is particularly true if during debugging you find you need to make changes to the event/query policies!

The suggested way to do this is to add a terminal event to the initialize rule, and a rule to select on that event, like so:

This gets all of the channels with the same tags, except for the latest one created, and deletes them.

Related pages

 

Copyright Picolabs | Licensed under Creative Commons.