ruleset rulesetExample{// "rulesetExample" is the RID, and must be unique in the engine
//The meta block is where module declarations, ruleset names/descriptions, and logging declarations occur
meta{
name "Rule Example"
description <<
An example of how to build a ruleset
>>
author "CS462 TA"
//You can use other modules (a.k.a. rulesets) that have functions or defactions that you want to use in this ruleset.
//To use a function/defaction in another ruleset, call the module: io.picolabs.pico:someProvidedFunction(<pass in params here>)
//The optional alias keyword shortens the above function call to: wrangler:someFunction(<pass in params here>)
use module io.picolabs.pico alias wrangler
//to use more than one module, do not separate with commas. Simply add a new line:
use module someRulesetName
//shares <give function/defaction names separated by commas>
//the "shares" keyword means that anyone can call the shared function on this pico, so long as they have an authorized channel
shares foo
//provides <give function/defaction names separated by commas>
//the "provides" keyword allows other rulesets that use this one (rulesetExample) as a module to call
//the stated functions/defactions.
provides foo, defFoo
//NOTICE: This ruleset provides, but does not share defFoo, which is a defaction. To clarify, this means that any
//ruleset using this ruleset (rulesetExample) as a module may call it, but outside sources querying this pico
//do NOT have access to it.
//historicaly used to enable and disable logs printing to the terminal where pico engine is running. This may be depricated
logging on
} //end meta
//The place to declare global variables and functions
global{
//functions can be used in pre-blocks or called by queries on authorized channels to this pico
foo = function(someValue, anotherValue){//pass any arbitrary number of parameters by separating with commas
aReturnValue = someValue + " " + anotherValue;//just do whatever processing you might want. Put semicolons on every statement execution except the return statement
aReturnValue //the last expression in a function is what will be returned. It can be a map, array, string, number etc, but not a function
}
//defactions can be called in action-blocks, and as such have slightly different syntax
defFoo = defaction(someParam, anotherParam){
//perform any needed pre-processing by simply writing code
aProcessedBinding = someParam + " " + anotherParam;
//then perform all action calls in an every block, including event:send(), send_directive() etc.
every{
//every action has a semicolon at the end of it save for the last action statement
event:send(/* put params here */);
send_directive("An Example Directive", { "processedValue": aProcessedBinding })
}
//You can also return values from a defaction with an optional returns statement
returns
//put the value you want to return here... it could be an array, map, number, string etc.
"A temporary placeholder value."
}
} //end global
//Then begin to declare rules
//Rules are primarily made up of a name, select statement, a pre-block, an action-block, and a post-block
rule exampleRule{//First: give the name
//Second: the select statement tells the engine software when to run the following code. When events (NOT QUERIES)
//are received, the engine matches up the domain name and type name with those given in the select statements of
//rules on installed rulesets and queues them up to run. Rules in the same ruleset that select on the same domain and type
//will be queued up from top going down. This garentees order of exacution within a single ruleset.
select when exampleDomain exampleType
//the pre-block is used for pre-processing, and is where you would want to call functions
pre{
resultOfFunction = foo("Hello,", "World!")
}
//the action-block performs defaction calls and other action calls, like send_directive() or event_send()
//For the following, in place of "true", you can have any valid krl expression or boolean value. It follows
//javascript truthy rules, meaning that any expression that evaluates to 0 will be false, other number values
//will evaluate as true
if true then //alternate ex: could use "if x > 5 then"
every{//perform action calls separated by semicolons save for the last expression, just like in a defaction
defFoo("Hello,", "World!") setting(returnedValue)//grab the result of a defaction using setting
}
//the post-block is used to set entity variables and raise new events on this pico in response to what happened in this event
//A fired block will run if an action was executed (if the conditional statement above was true). A fired block may
//be chained with an else block, which will run if the condition failed. Alternative post-blocks include "always{}".
//A common pitfall includes writing a fired block but leaving out an action block (action blocks are optional). This
//will ALWAYS fail, since no action block is run, and therefore the fired block will not run. Use an always block instead.
fired{
ent:someEntityVar := "Assign an entity variable like this!";
raise exampleDomain event "exampleRuleSucceeded"
attributes {};
}else{
ent:someEntityVar := {};
raise exampleDomain event "exampleRuleFailed"
attributes event:attrs();
}
}//end exampleRule
}