Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Added an intermediate Exercises section in accordance with Lab 3 of 462: https://byu.instructure.com/courses/617/assignments/19190

...

Next, learn about persistence, the "P" of pico, by working through Pico State Lesson.

Exercises (Beginner)

Do the following:

  1. Create a new channel (pick any name and type you like). 
    1. Send an event to your pico using the new channel. 
    2. Do you get the same result? Why or why not? 
  2. Delete the channel.
    1. Resend the event using the deleted channel. 
    2. What happens? Why?
  3. Send the event ecco/hello to your pico. What do you observe? Why? 
  4. Add a new rule that selects on echo/monkey and responds with "Hello Monkey" unless an event attribute named name 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. 
  5. 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:

  1. Write and register a ruleset named track_trips that contains a rule called process_trips that responds to the car:new_trip event with an attribute mileage. This rule should return a directive named trip with the option trip_length set to the value of the mileage attribute.

  2. Using the Pico Engine UI, create a pico that represents a car and install your track_trips ruleset in it.
  3. Using one of the techniques in Raising Events, test your ruleset.  Try it with and without the mileage attribute.

  4. 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 operator as().

  5. Modify the process_trips rule to raise an event.with domain explicit and type trip_processed. The explicit event should include any attributes that were in the car:new_trip event. Hint: event:attrs() returns all attributes of the current event.

  6. In the same ruleset, write a new rule named find_long_trips that selects on the explicit:trip_processed event. It should read the mileage attribute and, if it contains a value greater than the numeric value of the global variable named long_trip, raises another explicit event with domain explicit, type found_long_trip, and any atrributes passed along. Note, there are multiple ways to accomplish this.  You can pick any positive value for long_trip that you like.

  7. In the same ruleset, write another rule called trip_fuel_usage that selects on the explicit: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. 

  8. Use ruleset logging and debugging tools to convince yourself that your rules work.