...
Next, learn about persistence, the "P" of pico, by working through Pico State Lesson.
Exercises (Beginner)
Do the following:
- Create a new channel (pick any name and type you like).
- Send an event to your pico using the new channel.
- Do you get the same result? Why or why not?
- Delete the channel.
- Resend the event using the deleted channel.
- What happens? Why?
- Send the event
ecco/hello
to your pico. What do you observe? Why? - Add a new rule that selects on
echo/monkey
and responds with "Hello Monkey" unless an event attribute namedname
is given, in which case it will respond with "Hello " followed by the given name. Hint: use the.defaultsTo()
operator. Use the.klog()
operator to log the value that is used. - Repeat the previous exercise using the ternary conditional instead of the
.defaultsTo()
operator.
Exercises (Intermediate, presumes completion of Modules and External APIs Lesson)
Do the following:
Write and register a ruleset named
track_trips
that contains a rule calledprocess_trips
that responds to thecar:new_trip
event with an attributemileage
. This rule should return a directive namedtrip
with the optiontrip_length
set to the value of themileage
attribute.- Using the Pico Engine UI, create a pico that represents a car and install your
track_trips
ruleset in it. Using one of the techniques in Raising Events, test your ruleset. Try it with and without the mileage attribute.
Modify the select statement in the
process_trips
rule so that it will not fire unless there is a mileage attribute with the string value of a positive number. Hint: read the docs for the universal operatoras()
.Modify the
process_trips
rule to raise an event.with domainexplicit
and typetrip_processed
. The explicit event should include any attributes that were in thecar:new_trip
event. Hint:event:attrs()
returns all attributes of the current event.In the same ruleset, write a new rule named
find_long_trips
that selects on theexplicit:trip_processed
event. It should read themileage
attribute and, if it contains a value greater than the numeric value of the global variable namedlong_trip
, raises another explicit event with domainexplicit
, typefound_long_trip
, and any atrributes passed along. Note, there are multiple ways to accomplish this. You can pick any positive value forlong_trip
that you like.In the same ruleset, write another rule called
trip_fuel_usage
that selects on theexplicit:trip_processed
event. The rule should use the VIN event attribute as input to the vehicle information function you created in Modules and External APIs Lesson to retrieve information about this vehicle. Using the number in the mileage attribute, calculate the gallons of gas used for this trip (assume it was highway). Your rule action should return a directive that contains the miles driven and gallons of gas used for the trip.Use ruleset logging and debugging tools to convince yourself that your rules work.