Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 3 Current »

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"})

Related pages

  • No labels