gpt4 book ai didi

java - 比较流口水中的日期

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:40:59 25 4
gpt4 key购买 nike

我的规则文件如下,

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();
}
}

最佳答案

显然你有

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)

当程序员可能不依赖绑定(bind)变量的行为与其绑定(bind)到的属性这一事实时,这绝对是令人困惑的。

最后:不要在事实类中使用公共(public)字段。继续使用 Java Beans 模型并声明 getter 和 setter。事实证明,由于缺少访问(公共(public))实例变量本身,Drools 没有使用从 String 到 java.util.Date 的自动转换一个 setter/getter 。

关于java - 比较流口水中的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33494628/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com