Drools中的类型不安全的对象字段访问 [英] Type-unsafe Object field access in Drools

查看:104
本文介绍了Drools中的类型不安全的对象字段访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个系统,在该系统中,插入到Drools引擎中的某些数据遵循以下格式(大大简化):

I'm working with a system where some of the data inserted into a Drools engine follows this (heavily oversimplified) format:

public class Item {
    public String getValueType() { ... }
    public Object getValue() { ... }
}

值可能是几种不同的类型。现在,我想创建一堆Drools规则来检查值的各个字段,类似于:

The values may be several different types. Now, I want to create a bunch of Drools rules to check various fields of the values, something along the lines of:

$a : Item(valueType == "Car", value.owner.location == "At Home")
$b : Item(valueType == "Payments", value.rates.discounts.percent == 1337)
$c : Item(valueType == "Royalty", value.student.occupation == "Librarian")

...等等。现在,我面临的问题是Drools在加载规则时会对其进行静态类型检查,因为不能保证Object不能拥有owner / rates / student字段等,所以禁止使用此方法。

...and so on. Now, the problem I'm facing is that Drools statically type checks the rules as they're loaded, disallowing this use as Object isn't guaranteed to have the owner/rates/student fields and so on.

有没有办法做到这一点,可能是某种类型转换或绕过类型检查?

Is there a way to do this, possibly some kind of typecasting or bypassing the type checking?

(注意:不幸的是,截至目前,不能选择更改要键入的模型,而不是将所有内容都塞入对象。)

(Note: Unfortunately, it is as of right now not an option to change the model to be typed instead of shoving everything into Objects.)

推荐答案

从Drools开始5.5.0您可以投射事实的属性。请阅读本文档第4.1.1.2节: http://docs.jboss.org/drools/release/5.5.0.Final/droolsjbpm-introduction-docs/html_single/

Starting from Drools 5.5.0 you can 'cast' the attributes of your facts. Please read section 4.1.1.2 of this document: http://docs.jboss.org/drools/release/5.5.0.Final/droolsjbpm-introduction-docs/html_single/

基本上,什么您需要做的是:

Basically, what you need to do is:

$a : Item(valueType == "Car", value#Car.owner.location == "At Home")

免责声明:我之前从未尝试过。

这篇关于Drools中的类型不安全的对象字段访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆