Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: typo

...

One of the main ways to start code on creation of a pico is by executing code on ruleset installation. Wrangler raises the wrangler:ruleset_added installed event whenever you install a ruleset on a pico using wrangler (it uses the wrangler:install_rulesets_requested event). One of the attributes that wrangler:child_creation takes is a "rids"/"rid" parameter. When creating a new pico, Wrangler will install the list of rulesets given in this attribute in the new pico as it is created. Wrangler will then raise wrangler:ruleset_added installed as normal, which you can select on and run code with.

...

  • All the valid rulesets given to "rids" will be guaranteed to be installed by the time wrangler:ruleset_added installed is raised in a new pico
  • Any attributes given in the wrangler:child_creation event will be in the attributes for the wrangler:ruleset_added installed event(s). This is a useful way to give parameters to a new child pico's rulesets.
  • Listing initialization rules first in rulesets is good practice, as it is the first thing a ruleset will ever do.

...

Code Block
languagejs
titleCreating a Child then Running Code Inside the New Child
linenumberstrue
// Creates a new child with the "example" ruleset installed and passes an attribute named "testParam"
rule newExampleChild {
  select when example new_child
  always {
    raise wrangler event "child_creation" attributes {
      "rids":"example",
      "testParam":"hello"
    }
  }
}

// In the newly created child this rule will run
rule inNewChild {
  select when wrangler ruleset_addedinstalled where rids >< meta:rid
  pre {
    paramFromParent = event:attr("testParam").klog("Should be 'hello'")
  }
  always {
    ent:initial_state := {} // Set a desired initial state of entity variables
  }
}

...

Code Block
languagejs
titleInitialization Example
linenumberstrue
rule create_wellKnown_Rx{
  select when wrangler ruleset_addedinstalled where rids >< meta:rid
  pre{ channel = wellKnown_Rx() }
  if(channel.isnull() || channel{"type"} != "Tx_Rx") then
    engine:newChannel(meta:picoId, "wellKnown_Rx", "Tx_Rx")
  fired{
    raise Tx_Rx event "wellKnown_Rx_created" attributes event:attrs;
  }
  else{
    raise Tx_Rx event "wellKnown_Rx_not_created" attributes event:attrs; //exists
  }
}

Create well known Rx rule selects on ruleset_added installed event where rids matches its own ruleset rid. Checks for a well known channel, and creates it if one does not exist. Then raises an event detailing the creation. This rule guarantees a well known channel exist, which is a dependency of subscriptions.

...

Code Block
languagejs
titleRegister for Cleanup
linenumberstrue
rule register_for_cleanup {
  select when wrangler ruleset_addedinstalled where event:attr("rids") >< meta:rid // On this ruleset being installed
  always {
    raise wrangler event "ruleset_needs_cleanup_period" attributes { // raise the event
      "domain":meta:rid // the event requires a domain, and meta:rid is a natural choice for this
    }
  }
}

...