如何将规则应用于会话中对象的所有属性?
我曾经使用专有的规则引擎,现在尝试使用 Drools,我对 Drools 还很陌生。
使用以前的规则引擎,会对现有对象的所有实例触发规则,即使它们是其他对象的属性。
我有一组应用于给定类型的对象 O 的规则。我将 O 作为属性添加到我的会话和对象 O' 中。但由于规则不适用于 O',因此它们不会应用于 O' 的属性 O。
我将用一个 HEllo 单词示例来说明这一点:
我在基本 drool 示例中添加了以下类:
public static class Email {
private Message message ;
public void setMessage(Message message) {
this.message = message;
}
public Message getMessage() {
return message;
}
}
电子邮件是 O',消息是 O。
我的会话“工作”如下:
...
Message message = new Message();
message.setMessage("Hello World");
message.setStatus(Message.HELLO);
Email email = new Email();
email.setMessage(message);
ksession.insert(email);
ksession.fireAllRules();
logger.close();
...
并且我有一个示例 drool 文件:
rule "Hello World"
when
m : Message( status == Message.HELLO, myMessage : message )
then
System.out.println( myMessage );
m.setMessage( "Goodbye cruel world" );
m.setStatus( Message.GOODBYE );
update( m );
end
规则仅应用于 Message 。
如果我按原样启动会话,则不会触发任何规则。 要触发规则,我需要添加规则:
rule "email"
when
e : Email( message != null)
then
insert(e.getMessage());
end
它工作正常,但我的问题是:是否有一种更简单的方法可以在对象的每个实例上触发规则,即使它是另一个对象的属性?
I was using a proprietary rule engine, and I am now trying to use Drools, i'm pretty new to Drools.
With the previous rule engine, the rule were fired for all instances of existing object even if they were attribute of an other object.
I have a set of rules that are applied to a given type of object O. I add to my session and object O' having O as attribute. But as the rules are not applied to O' they won't be applied to the attribute O of O'.
I will illustrate this with an HEllo word example :
I added to the basic drool Example the following class :
public static class Email {
private Message message ;
public void setMessage(Message message) {
this.message = message;
}
public Message getMessage() {
return message;
}
}
Email is O' and Message is O.
My session "works" as follow :
...
Message message = new Message();
message.setMessage("Hello World");
message.setStatus(Message.HELLO);
Email email = new Email();
email.setMessage(message);
ksession.insert(email);
ksession.fireAllRules();
logger.close();
...
and I have a Sample drool file :
rule "Hello World"
when
m : Message( status == Message.HELLO, myMessage : message )
then
System.out.println( myMessage );
m.setMessage( "Goodbye cruel world" );
m.setStatus( Message.GOODBYE );
update( m );
end
The rule is only applied on Message .
If I launch my session the way it is, no rule will be fired .
To have rules fired I need to add the rule :
rule "email"
when
e : Email( message != null)
then
insert(e.getMessage());
end
It works fine, but my question is : is there an easier way to have the rule fired on each instance of an object even if it's an attribute of an other object ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,您可以执行以下操作:
或者您可以使用规则将消息插入工作内存使用
这种方法,您可以仅使用消息类型编写规则。
干杯
Yes you can do something like:
or you can just insert the message to the working memory using a rule
Using this approach then you can write rules only using the Message type.
Cheers