2015-04-23 71 views
0

舉個例子,我有:在Jess中,我將如何通過規則向模板添加一個插槽?

(deftemplate Animal 
(slot has-feathers (default FALSE)) 
(slot name (default "George")) 
) 

,並在規則中我有:

(defrule bird-test 
?a <-(Animal (has-feathers ?)) 
=> 
(printout t ?a.name " is a bird" crlf) 
"Add slot 'bird' to ?a or Animal" 
) 

我將如何做到這一點?並提前感謝您

編輯:謝謝你們!我想我明白我需要做什麼。

+0

請問您可以添加一個2或3個文字描述你正在做什麼?簡單地列出代碼然後「我會怎麼做?」並沒有給我們足夠的信息來幫助你。 –

+0

如果你可以閱讀代碼,問題其實很清楚。 –

回答

0

除了前面提供的插槽歐內斯特的建議,您也可以考慮多時隙可以爲各種屬性的容器行爲的規則可能檢測動物。

(deftemplate Animal 
    (slot name) 
    (slot has-feathers) 
    (multislot props)...) 

你可以寫

(defrule bird-test 
    (declare (no-loop TRUE)) 
    ?a <-(Animal (has-feathers TRUE)(props $?ex)) 
=> 
    (modify ?a (props $?ex isBird)) 
    (printout t ?a.name "'s props: " ?a.props crlf) 
) 

或者,可以使用非常普遍的一種自定義模板來表達各種或屬性動態:

(deftemplate is-a 
    (slot thing) 
    (slot property)) 

但是,這超越了單純的回答。

0

您定義後無法將模板添加到模板;這與您的程序運行時向Java類添加成員變量類似。

但是,您可以設置現有插槽的值;如果你的模板裏有一個kind插槽,你可以說

(modify ?a (kind bird)) 
相關問題