Drools 查詢學習

Drools 查詢學習
查詢以 query 關鍵字開始,以 end 關鍵字結束,在 package 當中一個查詢要有惟一的名稱,查詢的內容就是查詢的條件部分,條件部份內容的寫法與規則的 LHS 部分寫法徹底相同。java

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;
}
}數組

相關文章
相關標籤/搜索