Skip to content

Converting older (JSON) rulesets to JayRule format

JayRule provides a much more straightforward language and structure for defining rules than the older style (Javascript/JSON), which was quite prone to error and crashing.

Switching from a regular JSON ruleset is fairly straightforward.

For non-asynchronous rules

JSON Normal layout of a rule block (non-asynchronous)


// ----------------------------------------------------
// Rule "ABC"
// ----------------------------------------------------
if (
    !ntf.hasRun.ruleABC
    && condition
) {
    ntf.logger.info('In rule "ABC");
    action
    ntf.hasRun.ruleABC = true;
}


to JayRule ruleblock (non-asynchronous):


// ----------------------------------------------------
// Rule "ABC"
// ----------------------------------------------------
ruleABC : {
    ruleCondition : function(ntf) {
        return (condition);
    },

    ruleAction : function(ntf) {
        action
    }
}


Note, the ntf.hasRun.flag does not need to be set, nor does the logger line for the rule starting.

Eg

// ----------------------------------------------------  
// Rule "ABC"  
// ----------------------------------------------------  
if (  
    !ntf.hasRun.ruleABC  
    && ntf.context.fieldChanged = 'buttonABC'  
) {  
    ntf.hasRun.ruleABC = true;  
    ntf.logger.info('In rule "ABC");  
    ft3.ModalService.openModal('ABC', 'Hello from the ABC button'), 'info');  
    ntf.document.abcClicker = ntf.user.email;
}  

converts to

// ----------------------------------------------------  
// Rule "ABC"  
// ----------------------------------------------------  
ruleABC : {  
    ruleCondition : function(ntf) {  
        return (ntf.context.fieldChanged === 'buttonABC');  
    },  

    ruleAction : function(ntf) {  
        ft3.ModalService.openModal('ABC', 'Hello from the ABC button'), 'info');  
        ntf.document.abcClicker = ntf.user.email;
    }
}  

For asynchronous rules


// ----------------------------------------------------
// Rule "ABC"
// ----------------------------------------------------
if (
    !ntf.hasRun.ruleABC
    && condition
) {
    ntf.logger.info('In rule "ABC");
    action(..., function(..) {
        ...
        ntf.hasRun.ruleABC = true;
        callbackContinue(ntf);
    });
    return;
}


to JayRule ruleblock (asynchronous):


// ----------------------------------------------------
// Rule "ABC"
// ----------------------------------------------------
ruleABC : {
    ruleCondition : function(ntf) {
        return (condition);
    },

    ruleAction : function(ntf, callback) {
        action(..., function(..) {
            ...
            callback();
        });
    } }


Note, the ntf.hasRun.flag does not need to be set, and return does not need to be added, nor does the logger line for the rule starting.

Eg

// ----------------------------------------------------  
// Rule "ABC"  
// ----------------------------------------------------  
if (  
    !ntf.hasRun.ruleABC  
    && ntf.context.fieldChanged = 'buttonABC'  
) {  
    ntf.hasRun.ruleABC = true;  
    ntf.logger.info('In rule "ABC");  

    var eqry = {"query": {"bool": {"filter": [
        {"term" : {"documentId" : "1234"}}
    ]}}}; 

    ft3.findDocumentsByElastic(eqry, ntf.user.documentId, function(err, result) {
        if (result && result.data && result.data.hits) {
            ft3.showNotification('# documents found: ' + result.data.hits.total);
        }
        callbackContinue(ntf);
        return;
    });
    return;
}  

converts to

// ----------------------------------------------------  
// Rule "ABC"  
// ----------------------------------------------------  
ruleABC : {  
    ruleCondition : function(ntf) {  
        return (ntf.context.fieldChanged === 'buttonABC');  
    },  

    ruleAction : function(ntf, callback) {  
        var eqry = {"query": {"bool": {"filter": [
            {"term" : {"documentId" : "1234"}}
        ]}}}; 

        ft3.findDocumentsByElastic(eqry, ntf.user.documentId, function(err, result) {
            if (result && result.data && result.data.hits) {
                ft3.showNotification('# documents found: ' + result.data.hits.total);
            }
            callback();
        });
    }
}