Reactive Programming: Multiple Picos
- Due Apr 3, 2017 by 11:59pm
- Points 43
- Submitting a text entry box, a website url, or a file upload
- Available after Jan 9, 2017 at 12am
Objectives
The purpose of this lab is to introduce actor-model programming principles using picos.
Reading
- Using the Scatter-Gather Pattern to Asynchronously Create Fuse Reports Links to an external site.
- Do the Pico-Based Systems Lesson Links to an external site.
- Do the Pico to Pico Subscriptions Lesson Links to an external site.
Implementation Notes
- This lab is less prescriptive than the Reactive Programming: Single Pico Lab. You have more freedom to define event and function names.
- Even though another event may be the primary result of a rule firing, you can use directives to see what's happening for debugging purposes.
Part 1: Building a Fleet
In Reactive Programming: Single Pico you created a ruleset that stores trips. In this lesson, you'll build a fleet pico and program it to create multiple vehicle picos. When you create a vehicle pico, you will install the Subscriptions
, trip_store
, and modified track_trips
rulesets (from Reactive Programming: Single Pico).
Do the following:
- Create a new pico that represents a fleet of vehicles. This pico may be a child of the pico you used in Reactive Programming: Single Pico or a new account. It doesn't matter. Install the
Subscriptions
ruleset in this pico. - Create, register, and install a ruleset called
manage_fleet
in the fleet pico. - Write a rule called
create_vehicle
that responds to acar:new_vehicle
event by - creating a new pico
- creating a subscription (in a new rule) between the fleet pico and the new vehicle pico. Be sure the roles (fleet and vehicle) are correct and that the subscription has a name based on an appropriate event attribute in the
car:new_vehicle
event. - installs the
Subscriptions
,trip_store
, andtrack_trips
rulesets in the new vehicle. - Write a function in the
manage_fleet
ruleset calledvehicles
that returns the fleet's vehicle subscriptions. -
Write a rule called
delete_vehicle
that responds to acar:unneeded_vehicle
event by- deleting the appropriate vehicle pico (identified by an event attribute)
- cleans up the subscription between the fleet pico and the deleted vehicle pico.
- Test the rules and functions you defined by:
- creating vehicles,
- delete at least one vehicle
- you should end up with at least three vehicles
- Test the vehicles by sending them trips. Use the
trips
andlong_trips
functions to ensure they are working. You may want to script something with curl to send vehicles a number of random trips for testing purposes. You'll need a variety of trips in the vehicles for the next two parts.
Part 2: Generating Reports via Functions
In this part of the lab, you'll generate a fleet trip report using the trips
function.
Write a function in the manage_fleet
ruleset that calls the trips
function in each of the vehicles that the fleet knows about. The result should be JSON (note 4).
Be sure that the function continues to work even if vehicles are added or deleted.
Part 3: Generating Reports via Scatter-Gather
In this part of the lab, you'll generate a fleet trip report using the scatter-gather pattern.
- You will need a rule in the
manage_fleet
ruleset that sends an event to each vehicle (and only vehicles) in the fleet notifying them that a new fleet report is needed. - You will need a rule in the
track_trips
ruleset that sees the event sent in (1), calls its owntrips
function (do NOT use skyQuery for this), and sends an event back to the originator of the event with the result. The originator is usually the fleet, but assume only that it is a pico in the same engine. - You will need a rule in the
manage_fleet
ruleset that selects on the event from (2) and stores the results in a fleet trip report. Be sure the report includes a counter of the number of responding vehicles. - Add a function that returns a JSON structure with the five (5) latest fleet trip reports.
Notes:
- Be sure to use a correlation identifier to ensure that you're storing data about the right trips. Note that due to an implementation decision in KRL (some might call it a bug), you should not use a number as the correlation ID. It's OK to use a random number, but prefix it with a string so that it's not a number.
- You don't have to deal with any failures of the vehicles to respond with retries.
- The fleet might have subscriptions to picos other than the vehicles. Be sure you are only asking vehicles for reports.
- A fleet report is simply a JSON structure that includes the reports from each vehicle (you may assume the Subscriptions ruleset's skyQuery never fails), a count of how many vehicles are in the fleet, and a count of how many responded. For example:
{<vehicle_0_id>: {"vehicles" : 4,
"responding" : 3,
"trips" : <trip report from vehicle>
},
...
}
Deliverables
- A diagram showing the relationships (i.e. subscriptions) between the picos.
- The source URLs for your rulesets
- The RIDs for your rulesets
- ECIs for the picos that your rulesets are installed in.
- The ECIs and events for adding trips to the vehicle picos.
- A URL for generating a fleet report via the function method.
- An ECI and event for generating a fleet report via the scatter-gather method
- Answer the following questions:
Questions
- Can a vehicle be in more than one fleet based on the code your wrote? Why or why not? What are the implications of a vehicle being in more than one fleet?
- How did you solve the problem of only notifying vehicles that you needed a report?
- How could you ensure that only certain picos can raise an event that causes a fleet report to be generated?
- How do the debug logs differ for Part 2 (Generating Reports via Functions) and Part 3 (Generating Reports via Scatter-Gather)?
- How can you know a report is done and all the vehicles that are going to respond have reported?
- Given your answer in (3), how would you recover if the number of responding vehicles is less than the total number of vehicles?
Rubric
Criteria | Ratings | Pts | ||
---|---|---|---|---|
part 1
threshold:
pts
|
|
pts
--
|
||
part 2
threshold:
pts
|
|
pts
--
|
||
part 3
threshold:
pts
|
|
pts
--
|
||
Total Points:
43
out of 43
|