Skip to content

Instantly share code, notes, and snippets.

@snay2
Created February 25, 2012 22:50
Show Gist options
  • Save snay2/1911296 to your computer and use it in GitHub Desktop.
Save snay2/1911296 to your computer and use it in GitHub Desktop.
Simple example for using maps in KRL
ruleset a163x153 {
meta {
name "Maps"
description <<
Simple example of creating and using maps
>>
author "Steve Nay"
logging off
}
dispatch { }
global { }
// A simple rule to fetch the value from an entity variable map
// and allow the user to modify it.
rule display_value {
select when web pageview ".*"
pre {
val = ent:mymap{"mykey"};
msg = <<
The current value is #{val}.<br />
Set it here:
<form id="myform">
<input name="val" />
<input type="submit" />
</form>
>>;
}
{
notify("Maps", msg) with sticky=true;
watch("#myform", "submit");
}
}
// When the user clicks the Submit button on the form, save that value to
// the entity variable map
rule assign_key {
select when web submit
pre {
val = event:param("val");
}
notify("Maps", "I'm assigning the value '#{val}' to the key 'mykey' in the entity variable 'mymap'.");
fired {
set ent:mymap{"mykey"} val;
}
}
///////////////////////////////////////////////////
// A simpler rule to demonstrate only getting. Test this and the following
// rule by deploying this app and installing it your personal event network.
// Then use the Kynetx Event Console to raise the value:fetch and value:store events.
rule fetch_value {
select when value fetch
pre {
val = ent:mymap{"mykey"};
}
send_directive("text") with body="#{val}";
}
// A simpler rule to demonstrate only setting
rule set_value {
select when value store
pre {
val = event:param("val");
}
noop();
fired {
set ent:mymap{"mykey"} val;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment