比较流口水中的日期
Compare dates in 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();
但它没有打印 "Beep"。我认为条件不匹配,但我不明白为什么会这样。我如何比较 drools 中的日期?
我的实际规则集,
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();
}
}
你可以试试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"));
显然你有
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 会将字符串转换为日期值(前提是它与您的语言环境设置相对应)。
后来 经过一些实验,我发现 6.3.0(可能还有更早的版本)在编译 java.util.Date 与 String 的比较时有一个相当奇怪的行为。
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)
当程序员可能不依赖绑定变量的行为与其绑定的 属性 一样时,这绝对是令人困惑的。
最后:
不要在您的事实 类 中使用 public 字段。继续使用 Java Beans 模型并声明 getters 和 setter。 事实证明,当 (public) 由于缺少 getter,实例变量本身被访问。
我的规则文件如下,
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"。我认为条件不匹配,但我不明白为什么会这样。我如何比较 drools 中的日期?
我的实际规则集,
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();
}
}
你可以试试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"));
显然你有
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 会将字符串转换为日期值(前提是它与您的语言环境设置相对应)。
后来 经过一些实验,我发现 6.3.0(可能还有更早的版本)在编译 java.util.Date 与 String 的比较时有一个相当奇怪的行为。
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)
当程序员可能不依赖绑定变量的行为与其绑定的 属性 一样时,这绝对是令人困惑的。
最后: 不要在您的事实 类 中使用 public 字段。继续使用 Java Beans 模型并声明 getters 和 setter。 事实证明,当 (public) 由于缺少 getter,实例变量本身被访问。