我的规则文件如下,
import com.springapp.mvc.model.Person;
dialect "java"
rule "4"
when
$person:Person(((date > "20-Jan-2015") && (date < "20-Dec-2015")) && (call_count >= "299"))
then
System.out.println("Beep");
end
我添加了以下人物对象和消防规则,如下所示
Person person = new Person();
person.date = "20-Feb-2015";
person.call_count = 400;
kSession.insert(person);
int fires = kSession.fireAllRules();
但它没有打印“Beep”。我觉得条件不匹配,但我不明白为什么会发生这种情况。如何比较流氓中的日期?
我的实际规则集
package Customer_Loyalty_Categorization;
import com.springapp.mvc.model.Person;
dialect "java"
rule "4"
when
$person:Person(((date > "10-Nov-2015") && (date < "10-Dec-2015")) && (call_count >= "299"))
$person:Person(((date > "10-Nov-2015")&&(date < "30-Dec-2015")) && (call_count >= "299"))
then
System.out.println("Point rule runs.");
$person.points = ($person.call_count)*0.2;
end
rule "6"
when
$person:Person(call_count >= "599")
then
System.out.println("Category rule runs.");
$person.setCategory('PLATINUM');
end
在更改了我遇到异常后的人的日期变量的类型后,
java.lang.RuntimeException: Unable to Analyse Expression date > "20-Nov-2015":
[Error: Comparison operation requires compatible types. Found class java.util.Date and class java.lang.String]
[Near : {... date > "20-Nov-2015" ....}]
^
[Line: 8, Column: 8] : [Rule name='4']
Unable to Analyse Expression date < "20-Dec-2015":
[Error: Comparison operation requires compatible types. Found class java.util.Date and class java.lang.String]
[Near : {... date < "20-Dec-2015" ....}]
^
[Line: 8, Column: 8] : [Rule name='4']
Unable to Analyse Expression date > "01-Jan-2015":
[Error: Comparison operation requires compatible types. Found class java.util.Date and class java.lang.String]
[Near : {... date > "01-Jan-2015" ....}]
^
[Line: 40, Column: 8] : [Rule name='1']
Unable to Analyse Expression date < "07-Jan-2015":
[Error: Comparison operation requires compatible types. Found class java.util.Date and class java.lang.String]
[Near : {... date < "07-Jan-2015" ....}]
^
[Line: 40, Column: 8] : [Rule name='1']
Unable to Analyse Expression date > "01-Jan-2015":
[Error: Comparison operation requires compatible types. Found class java.util.Date and class java.lang.String]
[Near : {... date > "01-Jan-2015" ....}]
^
[Line: 48, Column: 8] : [Rule name='2']
Unable to Analyse Expression date < "07-Jan-2015":
[Error: Comparison operation requires compatible types. Found class java.util.Date and class java.lang.String]
[Near : {... date < "07-Jan-2015" ....}]
^
[Line: 48, Column: 8] : [Rule name='2']
Unable to Analyse Expression date > "05-Jan-2015":
[Error: Comparison operation requires compatible types. Found class java.util.Date and class java.lang.String]
[Near : {... date > "05-Jan-2015" ....}]
^
[Line: 48, Column: 8] : [Rule name='2']
Unable to Analyse Expression date < "10-Jan-2015":
[Error: Comparison operation requires compatible types. Found class java.util.Date and class java.lang.String]
[Near : {... date < "10-Jan-2015" ....}]
^
[Line: 48, Column: 8] : [Rule name='2']
我正在生成规则作为字符串集,并使用以下函数将它们转换为知识库,
public void createKnowledgeBase(){
String ruleSet = loadRuleSet();//generate rules as strings.
try {
System.out.println(ruleSet);
long start = System.currentTimeMillis();
if(ruleSet!=null){
KnowledgeBuilder knowledgeBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
Resource myResource = ResourceFactory.newReaderResource(new StringReader(ruleSet));
knowledgeBuilder.add(myResource, ResourceType.DRL);
if (knowledgeBuilder.hasErrors()) {
throw new RuntimeException(knowledgeBuilder.getErrors().toString());
}
knowledgeBase = KnowledgeBaseFactory.newKnowledgeBase();
knowledgeBase.addKnowledgePackages(knowledgeBuilder.getKnowledgePackages());
}
long finish = System.currentTimeMillis();
System.out.println("Execution time = " + (finish-start) + " milliseconds.");
}
catch (Exception e) {
e.printStackTrace();
}
}
答案 0 :(得分:2)
显然你有
class Person {
String date;
// ...
}
那样
when
$person:Person(((date > "20-Jan-2015") ...
导致
的字符串(!)比较"20-Feb-2015" > "20-Jan-2015" && "20-Feb-2015" < "20-Dec-2015"
有时甚至可能有效,但大多数情况下都不会。你应该使用
class Person {
java.util.Date date;
// ...
}
您需要更改
person.date = new Date( 115, 1, 20 ); // or, preferably, parse a string
但你可以保留原则; Drools会将字符串转换为Date值(前提是它对应于您的语言环境设置)。
稍后经过一些实验,我发现在编译java.util.Date与String的比较时,6.3.0(可能还有早期版本)有一个相当奇怪的行为。
rule x1 when
Person(date > "10-Jan-2000") // OK
Person($date:date, date > "10-Jan-2000") // OK
Person($date:date, $date > "10-Jan-2000") // Error (types incompatible)
当程序员可能不依赖于绑定变量的行为类似于绑定它的属性时,这绝对令人困惑。
<强>最后:强> 不要在您的事实类中使用公共字段。继续使用Java Beans模型并声明getter和setter。事实证明,当访问(公共)实例变量本身时,Drools不使用从String到java.util.Date的自动转换缺乏吸气剂。
答案 1 :(得分:1)
您可以尝试使用org.apache.commons.lang.time.DateUtils
。我遇到了同样的问题,这个问题对我有用,可能对你有用。
import org.apache.commons.lang.time.DateUtils;
$person: Person((date > DateUtils.parseDate("20-01-2015", "dd-MM-yyyy") && date < DateUtils.parseDate("20-12-2015", "dd-MM-yyyy")) && (call_count >= "299"));