zoukankan      html  css  js  c++  java
  • node(规则引擎)

    本文主要记录node的下的一个开源规则引擎nools,给出简单的实例,github地址为:

    https://github.com/C2FO/nools

    • 定义规则引擎(test.nools)
    define Param { //作为规则的输入消息
        param1 : 0,
        note :'test for nothing',
        constructor : function(p1){
            this.param1 = p1;
        }
    }
    
    rule condition1 { //定义规则condition1
        when {
            p:Param p.param1 <= 10;
        }
        then {
            assert('result1');
        }
    }
    
    rule condition2 { //定义规则condition2
        when {
            p:Param p.param1 > 10 && p.param1 <= 20; 
        }
        then {
    //这些事件的触发都可以是多个 assert('result2'); //触发assert事件 modify(p,function(){ //触发modify事件 p.param1 += 10; }); } }
    • 代码调用
    var nools = require("nools");
    
    var flow = nools.compile("./test.nools"),
    
    Param = flow.getDefined("Param");
    session = flow.getSession(new Param(11)).on('assert',function(result){
        console.log(result);//回调返回assert的内容,输出 result2
    }).on('modify',function(result){
        console.log(result, result.param1);//回调返回修改的值,输出{param1:21} 21(无参数note)
    }).match().then(function(){
        console.log('end');//不管是否有规则满足都执行,输出end
    });
  • 相关阅读:
    【3】网站搭建:分页功能
    mapserv和mapserv.exe的区别
    WMS请求GetCapabilities,变成下载mapserv.exe解决办法
    get和post的区别
    实现ajax异步请求
    Thinkphp3.2 Widget的扩展
    Thinkphp3.2 路由是使用
    Thinkphp3.2 TagLib的使用
    Thinkphp下实现Rbac
    Thinkphp下实现分页
  • 原文地址:https://www.cnblogs.com/Fredric-2013/p/4302985.html
Copyright © 2011-2022 走看看