gpt4 book ai didi

node.js - 无法运行 nools

转载 作者:太空宇宙 更新时间:2023-11-04 00:32:31 25 4
gpt4 key购买 nike

嗨,我正在使用 Node.js 开发 nools。当我运行该程序时发生错误:

throw new Error("Invalid expression '" + expression + "'") Invalid expression 'm.text = ~/^hello(\s*world)?$/' pls help to fix this problem.

这是我的代码:

服务器.js

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

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");
}
}

最佳答案

您的错误在这里:

//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.");
}
}

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

m: Message m.text = ~/.*goodbye$/;
m: Message m.text = ~/^hello(\s*world)?$/;

更改为:

m: Message m.text =~ /^hello(\s*world)?$/;

m: Message m.text =~ /.*goodbye$/;

它会起作用。

关于node.js - 无法运行 nools,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40462096/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com