(Classic) Event Network Exercises: Data Storage Module
Objective
Learn how to create a data storage module.
Data storage modules combine modules with explicite events to allow a personal event network to store data that can be shared between rulesets.
Setup
- Complete the Foursquare Checkin exercise
Exercise
- Create, register, and install a ruleset named
location_data
- Add a rule called
add_location_item
that listens for apds:new_location_data
event. The attributes to this event will bekey
andvalue
. - When the
add_location_item
rule fires, it should store the data in thevalue
attribute in an map entity variable using the value of thekey
attribute as the key for the map. - Add a function called
get_location_data()
that takes a key as it's sole argument and returns the value of that key in the entity variable you created above. - Make sure the
get_location_data()
function is listed in theprovides
pragma in themeta
section of the ruleset.
- Add a rule called
- Modify the
process_fs_checkin
rule from the Foursquare Checkin exercise to raise a pds:new_location_data event thekey
attribute should befs_checkin
and thevalue
attribute should be a map with thevenue
,city
,shout
, andcreatedAt
event attributes. - Create and activate (register and install) a ruleset named
examine_location:
- The ruleset should use the
location_data
ruleset as a module. - Add a rule
show_fs_location
that shows the last Foursquare checkin in SquareTag. - This ruleset could simply show a notify box on ktest.heroku.com or run as an app in Squaretag.com
- The ruleset should use the
You've built a system of three rulesets that use explicit events and modules that looks something like this:
Notes
- Both the Foursquare and Location Data rulesets will have to be installed in your Squaretag account (i.e. your pico) to see events from each other. Obviously the Inspector ruleset will need to be installed if you run it as a Squaretag app.
- Persistent variables are specific to a ruleset. Put another way, rulesets form closures over their persistent variables and only their rules and functions have access to them. That's why you're building the Location Data ruleset. Think of persistent variables like private variables in an object.
- Be careful how you store and access the data inside the Location Data ruleset. If you're not sure things are wired up correcting, you can test the module function by having it return something static (i.e. a string) and ensuring you see it in the Inspector.
Demonstrating Your Code Works
To demonstrate your code works (say for grading purposes), please add a send_directive()
action to the rules add_location_item
and process_fs_checkin
as follows:
- The directive name (i.e. the argument to
send_directive()
) should be venue name forprocess_fs_checkin
The options forprocess_fs_checkin
should include an item with key equal to "checkin" with a value equal to the name of the venue. - The directive name should be the value of
event:attr("key")
foradd_location_item
. The options for add_location_item should include an item with key equal to "location" with a value equal toevent:attr("value")
.
Copyright Picolabs | Licensed under Creative Commons.