2015-10-13 31 views
0

如何在DRL文件中定義變量並向其添加值,DRL文件可以在規則之間用作靜態資源。如何在側面drools中定義變量並向它們添加值

我試圖用全球關鍵字,但是當我添加值添加到它,我也不會影響工作記憶裏面,因爲它在文檔中提到的。在我的情況下,我不能從應用程序端添加它。

實施例:

global java.util.List myList; 

function java.util.List initMyList() { 
List list = new ArrayList(); 
list.add(1); 
return list; 
} 

rule "initListRule" 
salience 1001 
when 
    eval(myList == null) 
then 
myList = initMyList(); 
end 

rule "checkIfListIsStillEmptyRule" 
salience 1000 
when 
eval(myList != null) 
then 
System.out.println("MyList is Not null"); 
end 

全球不存儲在存儲器沃然後,因爲它沒有從應用側設置myList中會始終爲空。有沒有其他方法來定義變量並將其填入DRL中?

回答

0

一個DRL全球沒有動態評估,因此您的規則「checkIfListIsStillEmptyRule」將不會觸發。

你可以做

rule "initListFact" 
when 
    not List() 
then 
    insert(new ArrayList()); 
end 
rule "checkThatThereIsAnEmptyList" 
when 
    $list: List(size == 0) 
then 
    modify($list){ add("something") } 
end 

如果不需要觀察,你可以在規則中具有非常高的顯着性初始化一個全球性的變化。它將作爲資源可用,但不能將規則條件基於其狀態。

global list myList 
rule "initListRule" 
salience 9999999999 
when 
then 
    myList = new ArrayList(); 
end 

您可以使用多個全局:

global List myListOne 
global List myListTwo 
rule "initListsRule" 
salience 9999999999 
when 
then 
    myListOne = new ArrayList(); 
    myListTwo = new ArrayList(); 
end 

如果需要對變化做出反應,有周圍的事實沒有辦法。

declare NamedList 
    name: String 
    list: ArrayList 
end 
rule createListOne 
when 
    not NamedList(name == "one") 
then 
    insert(new NamedList("one", new ArrayList())); 
end 
rule "checkIfListOneIsStillEmpty" 
when 
    $nl: NamedList(name == "one", eval(list.size() == 0)) 
then 
    modify($nl){vgetList().add("something") } 
end 
+0

感謝您的回答,如果假設我們定義了一個列表,通常我們有多個列表,沒有變量就不能區分兩個列表。 – Rami

+0

只要您不需要對其動態更改做出反應,就可以使用多個全局。 – laune

相關問題