我的规则文件,比较日期在Drools中,如下
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();
但它并没有打印出“哔”。我认为条件不匹配,但我不明白为什么会发生这种情况。我如何比较口水中的日期?
我的实际规则集,
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();
}
}
我正在使用Drools 6.3.0。如果我删除日期比较部分这个规则工作正常。 – Hasitha
查看我的答案... – laune
啊,最后 - 缺少的信息已经到达。看到我的答案还有一个补充。 – laune