Note: inspired by and often copied verbatim from (Classic) Lesson: Pico State the original second lesson.
After completing this lesson, you will be able to do the following:
You should have completed the following lessons:
hello_worldruleset from Pico Engine Quickstart.
Let's change the
hello_world ruleset to know your name by default.
To do this we will need:
Before starting, visit the "Rulesets" tab for your pico, and notice that your pico has no entity variables for the "hello_world" ruleset. Click on the ruleset name to see any entity variables stored for that ruleset by this pico.
You will create a rule and name it
store_name. Have it select on event domain
hello and event type
Have the rule expect an event attribute named "name".
Include an entry in the array of "events" in your
__testing shared name:
In the prelude, bind a variable for the name event attribute (also named
name) and log it. When the rule
store_name fires, have it send a directive named
store_name with option
name giving the name event attribute we are going to set. The directive will allow us to know that the rule fired without having to look at the logs.
Entity variables can only be set in the postlude. We use the
always form of the postlude so that this postlude is always evaluated. Just mentioning the name of the entity variable (yet another "name") will cause it to be created when the rule fires. Notice that mutating an entity variable requires the ":=" operator.
Notice that the identifier "name" appears in several different place in this rule, and plays different roles.
With an entry for the
hello/name event in place, when you refresh your pico's "Testing" tab, there will be a UI for sending the event which will trigger the new rule. Use this UI to send the event. Notice the directive returned by the rule, named "store_name".
Refresh the "Rulesets" tab for your pico, and you'll see the entity variable named "ent:name", which was created the first time it was used.
Now change the
hello_world rule to default to our persistent name.
Test the persistence by raising an
echo/hello event to h
ello_world without providing a value. This will cause the
defaultsTo operator to provide the persisted name, which will be mentioned in the directive.
You have now created and used a simple entity variable to give your pico persistent state.
We used an entity variable to store a simple string, but you can also store complex objects in your entity variables. To access deep branches of your object you use a hash path.
Let's change the
hello_world ruleset to know multiple users.
To do this we will need to do the following:
store_nameto store multiple users (one at a time).
hello_worldto greet from a user id.
Modify the entity variable in
store_name to be a hash of user id's and names.
Start by adding a rule to initialize a structure to contain the map of user names. We'll bind a name
clear_name to a structure with one, default, name. Be sure to put this declaration in the globals section of the ruleset.
If you add the following to the "events" array in the "__testing" structure, then you'll have a UI in the "Testing" tab of your Pico to initialize the entity variable "name".
Validate your code changes, and refresh the "Testing" tab. Click on the "hello/clear" button to trigger the event.
Refresh the "Rulesets" tab to verify that the entity variable has been set to the default name structure.
Now, let's modify the
hello_world rule to obtain the name for the greeting from the structure. All we need to change is the code in the rule prelude.
You'll notice that where this rule used to expect the name as an event attribute, it now expects the id. So, adjust the entry accordingly in the "__testing" object.
Validate and install the modified ruleset. Refresh the "Testing" tab and click on the "echo/hello" button. Notice the directive now suggests a greeting to the default name.
Now, we need to modify the
store_name rule to add another name to our entity variable structure.
Update the entry for this rule in the
__testing structure, to reflect the fact that it now expects three event attributes.
Refresh the "Testing" tab and add a new name, filling in the three fields and clicking on the "hello/name" button.
Notice also, in the web server console, these messages:
Now use the new "id" value and click the "echo/hello" button. Note the suggested greeting in the directive sent by this rule.
Splendid! You have learned to use complex entity variables.
We could use our entity variable directly, but the code will quickly become un-readable.
Let's create some functions to access our entity variable.
To do this we will need to:
usersfunction to retrieve all users
namefunction to retrieve a user from id.
hello_worldto use the new
Create two functions in the global block that returns all users and that returns the user name given an id. You can read more about functions here. A function returns the value of its last expression.
With the functions defined in the global block, we can simplify the prelude of the "hello_world" rule:
After saving your changes to the ruleset, refresh the "Testing" tab and verify that all is working as before.
Sweet, you now can add as many users as you want and have your ruleset say hello to each user.
We have multiple users our ruleset can recognize with an id, but it would be so much cooler if we knew how many times our ruleset says hello to a user.
Let's make our ruleset smarter.
To do this we will need to use the structure in our entity variable to count visits.
After adding some users, and sending some "hello_world" events, your entity variable would look something like this:
You have learned how to use complex entity variables and how your Picos can maintain state.