The safari continues with a tour of persistent application variables. This is the second article in the series to chronicle the investigation of scope and extent of persistent variables in the Kynetx KRL platform. In the first KRL Scope Safari we explored persistent Entity Variables. Application variables in KRL are used to record persistent data within a ruleset. The value of an application variable is retained between execution of the ruleset. So far application variables seem to be the same as entity variables. The difference is that application variables are shared across all sessions of the ruleset. They are not restrained to a single browser session. A truly global application variable!

Note: For another perspective on KRL persistent variables I would highly recommend the Kynetx App A Day article Day 12 - Persistent Variables by Michael Grace.

We need some KRL code for this safari! Starting with the KRL from the last safari we replace all occurrences of ent:safari_entity with app:safari_app. There are three rules in the demo application. The first rule, Safari_Initialize displays a growl notification which includes a title, the current value of the application variable and two html forms which permit the application variable to mutated and cleared. The second rule, Safari_Respond_Submit processes the form submission to mutate (change) the application variable. The last rule, Safari_Respond_Clear processes the form submission to clear the application variable.

The value of the application variable can be accessed inside prelude expressions. In the prelude section of Safari_Initalize the value of safari_app is accessed. Application variables are mutated in the postlude section of a rule. In the postlude section of Safari_Respond_Submit the value of the application variable safari_app is set to the value of newValue. And in the postlude section of Safari_Respond_Clear the application variable safari_app is cleared.

It's demo time! You can follow along with the demo by running the application in two separate browsers at http://kynetx.aculis.net/safari/app.html The screen shots below are taking from Mozilla Firefox and Google Chrome. Starting in Firefox, the very first time the Kynetx ruleset is run the application variable does not exist, so it's value is set to "Just Born!". Note: Your mileage may vary since the application variable will retain it's value from the last user.


The value of the application variable can be changed by entering a new value into the text field.


After the Mutate button is clicked the current value of the application variable is set.


Switching over to the Google Chrome browser, when the Kynetx ruleset fires the value of the application variable is the same as it was in Firefox. This is our proof that the application variable is accessible to all running sessions of the ruleset.


Still in Chrome a new value is entered into the text field.


After clicking the Mutate button the current value is updated.


Back to the Firefox browser, the page is reloaded and we can see that updated value of the application variable is the same as it was in Chrome.


Clicking the Clear button will destory the application variable.


Finally we reload the page in Chrome after it has been cleared.


In the next KRL Scope Safari we will add Modules to the adventure! Will we be able to share an persistent application variable between to different rulesets by using the same module? Stay Tuned!



Enjoy!