我正在尝试从多槽中获取值并将其动态分配给变量。
假设我们有一个如下模板:
(fact (slot name)
(multislot field)
(multislot value))
(fact2 (slot field)
(slot value))
然后可以有一条规则,例如:
(rule
(fact (name ?name)
(field $?field)
(value $?value))
;if length of field and value is greater than 0
(fact2 (field ?field1)
(value ?value1))
;if length of field and value is greater than 1
(fact2 (field ?field2)
(value ?value2))
;if length of field and value is greater than 2
(fact2 (field ?field3)
(value ?value3))
;if length of field and value is greater than 3
(fact2 (field ?field4)
(value ?value4))
=>
(assert (all field:values found))
在这里,事实只能有一个字段值对,或最多为4个。我想要一个通用的单一规则,该规则可用于从多时隙动态提取所有可用值。
我想在上述规则中,我们不仅需要提取值,还需要实现if length > 0
,else-if
种语句。
因此,如果输入为:
(fact (name Employee)
(field Name Role Department Company)
(value Bob Admin Security ABC))
(fact2 (field Name)
(value Bob))
(fact2 (field Role)
(value Admin))
(fact2 (field Department)
(value Security))
(fact2 (field Company)
(value ABC))
那么预期的输出将是:
(all field:value pairs found)
同时
(fact (name Employee)
(field Name Role)
(value Bob Admin))
(fact2 (field Name)
(value Bob))
(fact2 (field Role)
(value Admin))
虽然相同的规则也应符合上述事实。这可能吗?还是有其他替代解决方案?
先谢谢您。
答案 0 :(得分:1)
实际上,您可以使用forall条件元素遍历每个字段/值。如果名称广告位不包含唯一值,并且您有多个事实事实,则要添加一个包含唯一值的广告位即可规则操作中的事实,以便 forall 条件元素外部和内部的事实模式与相同的事实。
CLIPS (6.31 4/1/19)
CLIPS>
(deftemplate fact
(slot name)
(slot id (default-dynamic (gensym*)))
(multislot field)
(multislot value))
CLIPS>
(deftemplate fact2
(slot field)
(slot value))
CLIPS>
(deffacts initial
(fact (name Employee)
(field Name Role Department Company)
(value Bob Admin Security ABC))
(fact (name Employee)
(field Name Age)
(value Bob 38))
(fact2 (field Name)
(value Bob))
(fact2 (field Role)
(value Admin))
(fact2 (field Department)
(value Security))
(fact2 (field Company)
(value ABC)))
CLIPS>
(defrule reasoning
(fact (id ?id))
(forall
(fact (id ?id)
(field $?f ?field $?)
(value $?v&:(= (length$ ?f) (length$ ?v)) ?value $?))
(fact2 (field ?field)
(value ?value)))
=>
(assert (all field:values found)))
CLIPS> (reset)
CLIPS> (agenda)
0 reasoning: f-1,*
For a total of 1 activation.
CLIPS> (ppfact 1)
(fact
(name Employee)
(id gen1)
(field Name Role Department Company)
(value Bob Admin Security ABC))
CLIPS>