问题是从单个插槽填充多字段插槽。为了指定这个想法,有一组同一模板的事实,其中一些单个插槽已经全部填充,另一个模板,其中还没有填充多字段插槽。需要用属于另一个模板的单个时隙事实的单个时隙值的值来填充后者。 对于Instance,这里声明了模板:
(deftemplate Connexion
(slot ID-BUS)
(multislot CB-CLOSED (default nil))
(multislot CB-OPEN (default nil)))
(deftemplate CB
(slot ID-BUS)
(slot NUM-CB)
(slot STATE-CB (allowed-integers -1 0 1)(default -1)))
(deftemplate State-Vector
(multislot CB-CLOSED (default nil))
(multislot CB-OPEN (default nil)))
假设以下事实:
(deffacts Some-Facts
(State-Vector (CB-CLOSED 1 4 5 6 7)(CB-OPEN 2 3 8 9))
(CB (ID-BUS B1)(NUM-CB 1)(STATE-CB 1))
(CB (ID-BUS B1)(NUM-CB 4)(STATE-CB 1))
(CB (ID-BUS B1)(NUM-CB 6)(STATE-CB 1))
(CB (ID-BUS B2)(NUM-CB 2)(STATE-CB 0))
(CB (ID-BUS B2)(NUM-CB 5)(STATE-CB 1))
(CB (ID-BUS B2)(NUM-CB 8)(STATE-CB 0))
(CB (ID-BUS B3)(NUM-CB 3)(STATE-CB 0))
(CB (ID-BUS B3)(NUM-CB 7)(STATE-CB 1))
(CB (ID-BUS B3)(NUM-CB 9)(STATE-CB 0))
(Connexion (ID-BUS B1)(CB-CLOSED nil)(CB-OPEN nil))
(Connexion (ID-BUS B2)(CB-CLOSED nil)(CB-OPEN nil))
(Connexion (ID-BUS B3)(CB-CLOSED nil)(CB-OPEN nil)))
所需要的是从属于模板CB的事实中填充属于模板Connexion的多字段槽CB-CLOSED和CB-OPEN。预期的结果将是这样的:
(Connexion (ID-BUS B1)(CB-CLOSED 1 4 6)(CB-OPEN nil))
(Connexion (ID-BUS B2)(CB-CLOSED 5)(CB-OPEN 2 8)))
(Connexion (ID-BUS B3)(CB-CLOSED 7)(CB-OPEN 3 9)))
问题:我如何继续获得此结果?
答案 0 :(得分:0)
CLIPS>
(deftemplate Connexion
(slot ID-BUS)
(multislot CB-CLOSED (default nil))
(multislot CB-OPEN (default nil)))
CLIPS>
(deftemplate CB
(slot ID-BUS)
(slot NUM-CB)
(slot STATE-CB (allowed-integers -1 0 1)(default -1)))
CLIPS>
(deftemplate State-Vector
(multislot CB-CLOSED (default nil))
(multislot CB-OPEN (default nil)))
CLIPS>
(deffacts Some-Facts
(State-Vector (CB-CLOSED 1 4 5 6 7)(CB-OPEN 2 3 8 9))
(CB (ID-BUS B1)(NUM-CB 1)(STATE-CB 1))
(CB (ID-BUS B1)(NUM-CB 4)(STATE-CB 1))
(CB (ID-BUS B1)(NUM-CB 6)(STATE-CB 1))
(CB (ID-BUS B2)(NUM-CB 2)(STATE-CB 0))
(CB (ID-BUS B2)(NUM-CB 5)(STATE-CB 1))
(CB (ID-BUS B2)(NUM-CB 8)(STATE-CB 0))
(CB (ID-BUS B3)(NUM-CB 3)(STATE-CB 0))
(CB (ID-BUS B3)(NUM-CB 7)(STATE-CB 1))
(CB (ID-BUS B3)(NUM-CB 9)(STATE-CB 0))
(Connexion (ID-BUS B1)(CB-CLOSED nil)(CB-OPEN nil))
(Connexion (ID-BUS B2)(CB-CLOSED nil)(CB-OPEN nil))
(Connexion (ID-BUS B3)(CB-CLOSED nil)(CB-OPEN nil)))
CLIPS>
(defrule populate-connexion
?c <- (Connexion (ID-BUS ?name) (CB-CLOSED nil) (CB-OPEN nil))
=>
(bind ?closed (create$))
(bind ?open (create$))
(do-for-all-facts ((?cb CB)) (eq ?cb:ID-BUS ?name)
(if (eq ?cb:STATE-CB 1)
then
(bind ?closed (create$ ?closed ?cb:NUM-CB))
else
(bind ?open (create$ ?open ?cb:NUM-CB))))
(modify ?c (CB-CLOSED ?closed) (CB-OPEN ?open)))
CLIPS> (reset)
CLIPS> (run)
CLIPS> (facts)
f-0 (initial-fact)
f-1 (State-Vector (CB-CLOSED 1 4 5 6 7) (CB-OPEN 2 3 8 9))
f-2 (CB (ID-BUS B1) (NUM-CB 1) (STATE-CB 1))
f-3 (CB (ID-BUS B1) (NUM-CB 4) (STATE-CB 1))
f-4 (CB (ID-BUS B1) (NUM-CB 6) (STATE-CB 1))
f-5 (CB (ID-BUS B2) (NUM-CB 2) (STATE-CB 0))
f-6 (CB (ID-BUS B2) (NUM-CB 5) (STATE-CB 1))
f-7 (CB (ID-BUS B2) (NUM-CB 8) (STATE-CB 0))
f-8 (CB (ID-BUS B3) (NUM-CB 3) (STATE-CB 0))
f-9 (CB (ID-BUS B3) (NUM-CB 7) (STATE-CB 1))
f-10 (CB (ID-BUS B3) (NUM-CB 9) (STATE-CB 0))
f-14 (Connexion (ID-BUS B3) (CB-CLOSED 7) (CB-OPEN 3 9))
f-15 (Connexion (ID-BUS B2) (CB-CLOSED 5) (CB-OPEN 2 8))
f-16 (Connexion (ID-BUS B1) (CB-CLOSED 1 4 6) (CB-OPEN))
For a total of 14 facts.
CLIPS>
或者,您可以在单个规则中填充Connexion事实:
(defrule populate-connexion
=>
(do-for-all-facts ((?cn Connexion))
(and (eq ?cn:CB-CLOSED (create$ nil)) (eq ?cn:CB-OPEN (create$ nil)))
(bind ?closed (create$))
(bind ?open (create$))
(do-for-all-facts ((?cb CB)) (eq ?cb:ID-BUS ?cn:ID-BUS)
(if (eq ?cb:STATE-CB 1)
then
(bind ?closed (create$ ?closed ?cb:NUM-CB))
else
(bind ?open (create$ ?open ?cb:NUM-CB))))
(modify ?cn (CB-CLOSED ?closed) (CB-OPEN ?open)))))