HELP!

The Rules Engine "Replay Stack"

This advanced topic is only relevant to Viabl.ai Platform developers wishing to use JavaScript code in their applications.

The Viabl.ai Platform Rules Engine supports a synchronous inference environment whereby each inference operation is completed before the next operation is started. In order for the this inference to facilitate access to (external) asynchronous functions, such as making direct AJAX calls to web servers using the XMLHttpRequest or jQuery AJAX methods, it employs an internal mechanism called the "Replay Stack" whereby the Object inference may get re-executed multiple times. Generally, the Viabl Platform Rules Engine shields the developer from any side effects of this internal mechanism except for the following two scenarios:

1. Making Asynchronous Calls

When the developer uses JavaScript Asynchronous calls (such as XMLHttpRequest or jQuery AJAX methods) to invoke external REST web services then they must "protect" these calls from being executed more than once using the xpertrule.onceAsync method. Otherwise the unnecessary extra calls may, at best, place unnecessary load on the server hosting the web service or, at worst, cause the engine to appear to be behaving erratically (if the later calls return different values)

Build Tools provided by the Viabl Platform that utilise Asynchronous functions will automatically generate code that protects against the effects of the "Replay Stack". For example, the JavaScript code generated below by Add REST Web Service Tool uses the xpertrule.onceAsync method to protect the "Replay Stack" against being fired multiple times.

let input = {
};
let outputJSON = xpertrule.onceAsync(function(completeCB) {
     $.ajax({
         url: 'https://www.bbc.co.uk/radio4/programmes/schedules/fm/today.json',
         headers: {},
         type: 'GET',
         data: input,
         dataType: 'json',
         success: function(d) {
             completeCB(d);
         },
         error: function(e, text, error) {
             console.log('API error: ' + text +': '+ error + ' ['+ e.status +']');
             completeCB(null);
         }
     });
});

Using Viabl Platform's own Asynchronous calls such as the xpertrule.urlFetch method and Database Access Objects are automatically protected from the side effects of the Replay Stack.

2. Using Certain JavaScript Functions

When calling any of the below JavaScript functions, the developer must "protect" these calls from being executed multiple times using the xpertrule.once method:

  • All JavaScript Date and Time get methods
  • The JavaScript Math.random() method

  • JavaScript windows Popup boxes: alert(), confirm() and prompt()
  • Console methods: e.g. console.log()

The first and second group of functions in the above list can cause the engine to appear to be behaving erratically (as extras calls may return different values). The second group, which are typically used by developers for testing & debugging, can confuse the developer by displaying extra messages.

For example, the following JavaScript for getting the current time should use the "xpertrule.once" as follows:

var aData = xpertrule.once(function() {
    var dt = new Date();
    return dt.getTime();
});
The Viabl.ai Platform xpertrule.message & xpertrule.messageJSON methods (which can be used during debugging) are automatically protected from the side effects of the Replay Stack.

On This Page