gpt4 book ai didi

Java Drools - 应用哪些规则和事实列表

转载 作者:行者123 更新时间:2023-12-02 02:03:15 24 4
gpt4 key购买 nike

我对 drools 有点陌生,我想对一个项目了解更多,所以我在 eclipse 上用 drools 做了一个简单的程序。工作流程非常简单,我有一个珠宝产品模型,对于每件珠宝,我根据名称应用一个折扣。

效果很好,但我想查看事实列表以及每个触发规则使用哪些事实。我有这个:

Rules.drl

package com.rule

import com.javainuse.model.Product
import com.javainuse.model.Counter

rule "Offer for Diamond"
when
productObject: Product(type=="diamond")
then
productObject.setDiscount(15);
end

rule "Offer for Gold"
when
productObject: Product(type=="gold")
then
productObject.setDiscount(25);
end

model.Product.java

package com.javainuse.model;

public class Product {
private String type;
private int discount;

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public int getDiscount() {
return discount;
}

public void setDiscount(int discount) {
this.discount = discount;
}
}

ma​​in.DroolsTest.java

package com.javainuse.main;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Collection;
import java.util.Iterator;
import org.drools.compiler.compiler.DroolsParserException;
import org.drools.compiler.compiler.PackageBuilder;
import org.drools.core.FactHandle;
import org.drools.core.RuleBase;
import org.drools.core.RuleBaseFactory;
import org.drools.core.WorkingMemory;
import com.javainuse.model.Product;

public class DroolsTest {

public static void main(String[] args) throws DroolsParserException,
IOException {
DroolsTest droolsTest = new DroolsTest();
droolsTest.executeDrools();
}

public void executeDrools() throws DroolsParserException, IOException {

PackageBuilder packageBuilder = new PackageBuilder();

String ruleFile = "/com/rule/Rules.drl";
InputStream resourceAsStream = getClass().getResourceAsStream(ruleFile);

Reader reader = new InputStreamReader(resourceAsStream);
packageBuilder.addPackageFromDrl(reader);
org.drools.core.rule.Package rulesPackage = packageBuilder.getPackage();
RuleBase ruleBase = RuleBaseFactory.newRuleBase();
ruleBase.addPackage(rulesPackage);

WorkingMemory workingMemory = ruleBase.newStatefulSession();

Product product = new Product();
Product product2 = new Product();
product.setType("gold");
product2.setType("diamond");

//List of facts!
workingMemory.insert(product);
workingMemory.insert(product2);
workingMemory.fireAllRules();

System.out.println("Discount for " + product.getType() + " is " + product.getDiscount());

System.out.println("Discount for " + product2.getType() + " is " + product2.getDiscount());

//how many facts
System.out.println("There are " + workingMemory.getFactCount() + " facts");
//facts handles
Collection<org.kie.api.runtime.rule.FactHandle> x = workingMemory.getFactHandles();
System.out.println("Facts Handles: " + x +"\n");

FactHandle fh;
Iterator<org.kie.api.runtime.rule.FactHandle> it = x.iterator();
while(it.hasNext())
{
fh = (FactHandle) it.next();
System.out.println("FactHandle to string "+fh.toExternalForm().toString());
}
}
}

输出:

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Discount for gold is 25
Discount for diamond is 15
There are 2 facts
Facts Handles: [[fact 0:2:1889057031:1889057031:2:DEFAULT:NON_TRAIT:com.javainuse.model.Product@7098b907], [fact 0:1:93199773:93199773:1:DEFAULT:NON_TRAIT:com.javainuse.model.Product@58e1d9d]]

FactHandle to string 0:2:1889057031:1889057031:2:DEFAULT:NON_TRAIT
FactHandle to string 0:1:93199773:93199773:1:DEFAULT:NON_TRAIT

I want something like this:

Discount for gold is 25
Discount for diamond is 15
There are 2 facts: gold, diamond
gold triggered rule Offer for Gold
diamond triggered Offer for Diamond

这可能吗?我检查了WorkingMemory、Endpoint、FactHandle 类,但什么也没检查。

最佳答案

您可以使用 AgendaEventListener 在规则匹配时收到通知(以及使其匹配的事实)。

有关更多详细信息,请参阅此帖子:Drools- how to find out which all rules were matched?

希望对你有帮助

关于Java Drools - 应用哪些规则和事实列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51243945/

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