首页 > 解决方案 > 如何在 CLIPS 的循环计数中自动添加许多变量?

问题描述

我想使用循环计数来循环许多不同的变量,让用户输入并让 CLIPS 读取变量。

例如:

问题:“您希望添加多少受抚养人?”

答案:5。

然后 CLIPS 应该创建如下变量:

名称1

名称2

名称3

名称4

名称5

这是我的代码:

(printout t "How many dependent you wish to add? (Must have atleast 1): ")
    (bind ?DepNo (read))
    (assert (DepNo ?DepNo))
    (loop-for-count (?DepNo 1 ?DepNo) do 
        (printout t "Name: ")
        (bind $?DepName (explode$ (readline)))
        (assert (DepName $?DepName))
        
    )

标签: clips

解决方案


创建一个多字段值来保存所有从属名称:

         CLIPS (6.31 6/12/19)
CLIPS> 
(defrule get-dependents
   =>
   (printout t "How many dependent you wish to add? (Must have at least 1): ")
   (bind ?DepNo (read))
   (bind ?depNames (create$))
   (loop-for-count ?DepNo 
      (printout t "Name: ")
      (bind ?depNames (create$ ?depNames (readline))))
   (assert (DepNames ?depNames)))
CLIPS>    
(defrule print-names
   (DepNames $?depNames)
   =>
   (printout t "Dependents are: ")
   (foreach ?d ?depNames
      (printout t "   " ?d crlf)))
CLIPS> (reset)
CLIPS> (run)
How many dependent you wish to add? (Must have at least 1): 3
Name: Sally Jones
Name: Fred Jones
Name: David Jones
Dependents are:  
   Sally Jones
   Fred Jones
   David Jones
CLIPS> (facts)
f-0     (initial-fact)
f-1     (DepNames "Sally Jones" "Fred Jones" "David Jones")
For a total of 2 facts.
CLIPS>  

推荐阅读