首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法运行nools

无法运行nools
EN

Stack Overflow用户
提问于 2016-11-07 09:46:48
回答 1查看 223关注 0票数 1

嗨,我正在node.js的nools上工作。当我运行这个程序时,发生了一个错误:

抛出新错误(“无效表达式”+“表达式+ '”)无效表达式'm.text = ~/^hello(\s*world)?$/‘请帮助解决此问题。

在这里,我的守则:

Server.js

代码语言:javascript
复制
var express        =         require("express");
var bodyParser     =         require("body-parser");
var app            =         express();

app.use(bodyParser.urlencoded({
    extended: true
}));

var index = 0;

var nools = require("nools");

var flow = nools.compile(__dirname + "/server/rules.nools");
var Message = flow.getDefined("message");
var session = flow.getSession();

session.matchUntilHalt().then(
    function() {
        //all done!
        console.log("All done!");
    },
    function(err) {
        console.log("Error matchUntilHalt()", err.stack);
    }
);

app.post('/fact', function(req, res) {
    var key = req.body.key;

    console.log("\n" + ++index + " New fact", key);

    var newMsg = new Message(key);

    session.assert(newMsg);

    res.end("All OK");
});

app.get('/', function(req, res) {
    res.end("Watsup! Its " + new Date());
});

app.listen(4000, function() {
    console.log("Started up!");
});

rools.nools

代码语言:javascript
复制
define Message {
    text: '',
    constructor: function(message) {
        this.text = message;
    }
}

//find any message that starts with hello
rule Hello {
    when {
        m: Message m.text = ~/^hello(\s*world)?$/;
    }
    then {
        console.log("Hello rule fired.");
    }
}

//find all messages then end in goodbye
rule Goodbye {
    when {
        m: Message m.text = ~/.*goodbye$/;
    }
    then {
        console.log("Goodbye rule fired.");
    }
}

define Client {
    age: 0,
    constructor: function(age) {
        this.age = age;
    }
}

rule CheckAge {
    when {
        // Multiple conditions in same rule
        c: Client c.age > 30 && c.age < 65
    }
    then {
        console.log("Eligible for loan");
    }
}
EN

回答 1

Stack Overflow用户

发布于 2017-05-12 21:39:54

您的错误在于:

代码语言:javascript
复制
//find any message that starts with hello
rule Hello {
    when {
        m: Message m.text = ~/^hello(\s*world)?$/;
    }
    then {
        console.log("Hello rule fired.");
    }
}

//find all messages then end in goodbye
rule Goodbye {
    when {
        m: Message m.text = ~/.*goodbye$/;
    }
    then {
        console.log("Goodbye rule fired.");
    }
}

要使用的表达式在=和~之间有一个空格

代码语言:javascript
复制
m: Message m.text = ~/.*goodbye$/;
m: Message m.text = ~/^hello(\s*world)?$/;

改为:

代码语言:javascript
复制
m: Message m.text =~ /^hello(\s*world)?$/;

代码语言:javascript
复制
m: Message m.text =~ /.*goodbye$/;

它会成功的。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40462096

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档