Drools 查询学习

Drools 查询学习
查询以 query 关键字开始,以 end 关键字结束,在 package 当中一个查询要有唯一的名称,查询的内容就是查询的条件部分,条件部分内容的写法与规则的 LHS 部分写法完全相同。

1.无参数查询
代码语法如下:
query "query name"
#conditions
end
示例代码:
query "testQuery"
customer:Customer(age>30,orders.size >10)
end
查询调用的过程:
查 询 的 调 用 是 由 StatefulSession 完 成 的 , 通 过 调 用 StatefulSession对象的getQueryResults(String queryName)方法实现对查询的调用,
该方法的调用会返回一个QueryResults 对象,QueryResults 是一个类似于 Collection 接口的集合对象,在它当中存放在若干个 QueryResultsRow 对象,
通过 QueryResultsRow 可以得到对应的 Fact 对象,从而实现根据条件对当前 WorkingMemory 当中 Fact 对象的查询。


java代码如下:
package test;
import java.util.ArrayList;
import java.util.List;
import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseFactory;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.io.impl.ClassPathResource;
import org.drools.runtime.StatefulKnowledgeSession;
import org.drools.runtime.rule.QueryResults;
import org.drools.runtime.rule.QueryResultsRow;
public class Test {
public static void main(String[] args) {
KnowledgeBuilder kb = KnowledgeBuilderFactory.newKnowledgeBuilder();
kb.add(new ClassPathResource("test/test.drl"),ResourceType.DRL);
KnowledgeBase knowledgeBase = KnowledgeBaseFactory.newKnowledgeBase();
knowledgeBase.addKnowledgePackages(kb.getKnowledgePackages());
StatefulKnowledgeSession statefulSession = knowledgeBase.newStatefulKnowledgeSession();

//向当前WorkingMemory当中插入Customer对象
statefulSession.insert(generateCustomer("张三",20,21));
statefulSession.insert(generateCustomer("李四",33,11));
statefulSession.insert(generateCustomer("王二",43,12));

//调用查询
QueryResults queryResults=statefulSession.getQueryResults("testQuery");
for(QueryResultsRow qr:queryResults){
Customer cus=(Customer)qr.get("customer");

//打印查询结果
System.out.println("customer name :"+cus.getName());
}
statefulSession.dispose();
}

/**
* 产生包括指定数量Order的Customer
*
*/
public static Customer generateCustomer(String name,int age,int orderSize){
Customer cus=new Customer();
cus.setName(name);
cus.setAge(age);
List ls=new ArrayList();
for (int i = 0; i < orderSize; i++) {
ls.add(new Order());
}
cus.setOrders(ls);
return cus;
}
}

2.参数查询
代码语法如下:
query "query name" (Object obj,...)
#conditions
end
示例代码:
query "testQuery"(int $age,String $gender)
customer:Customer(age>$age,gender==$gender)
end
调用过程:
有两个外部参数需要传入,一个是类型为 int 的 $age;一个是类型为String 的$gender(这里传入参数变量名前添加前缀“$”符号,是为了和条件表达式中相关对于带参数的查询,
可以采用 StatefulSession 提供的 getQueryResults(String queryName,new Object[]{})方法来实现, 这个方法中第一个参数为查询的名称, 第二个 Object对象数组既为要输入的参数集合。
java代码如下:
package test;
import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseFactory;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.io.impl.ClassPathResource;
import org.drools.runtime.StatefulKnowledgeSession;
import org.drools.runtime.rule.QueryResults;
import org.drools.runtime.rule.QueryResultsRow;
public class Test {
public static void main(String[] args) {
KnowledgeBuilder kb =KnowledgeBuilderFactory.newKnowledgeBuilder();
kb.add(new ClassPathResource("test/test.drl"),ResourceType.DRL);
KnowledgeBase knowledgeBase = KnowledgeBaseFactory.newKnowledgeBase();
knowledgeBase.addKnowledgePackages(kb.getKnowledgePackages());
StatefulKnowledgeSession statefulSession = knowledgeBase.newStatefulKnowledgeSession();

//向当前WorkingMemory当中插入Customer对象
statefulSession.insert(generateCustomer("张三",20,"F"));
statefulSession.insert(generateCustomer("李四",33,"M"));
statefulSession.insert(generateCustomer("王二",43,"F"));

//调用查询
QueryResults queryResults=statefulSession.getQueryResults("testQuery", new Object[]{new Integer(20),"F"}); //函数名 和参数
for(QueryResultsRow qr:queryResults){
Customer cus=(Customer)qr.get("customer");
System.out.println("customer name :"+cus.getName());
}
statefulSession.dispose();
}


/**
* 产生Customer对象
*/
public static Customer generateCustomer(String name,int age,String gender){
Customer cus=new Customer();
cus.setAge(age);
cus.setName(name);
cus.setGender(gender);
return cus;
}
}

 

posted @ 2016-03-02 14:17  ciade  阅读(1541)  评论(0编辑  收藏  举报