如何在CLIPS中正确打开文件?

问题描述 投票:0回答:1

我的任务是编写一个函数,该函数接收一个文件名作为参数,该文件包含以矩形矩阵形式指定的线性方程组的系数。使用可接受的变换,使系统呈现三角形外观。将结果写入文件。

   (bind ?input (open ?input-filename "r"))
   (bind ?output (open ?output-filename "w"))
   (if (not ?input)
      then
      (printout t "Error of opening " ?input-filename crlf)
      (return))
   
   (bind ?matrix (create$))
   (while (not (eq (readline ?input) EOF))
      (bind ?line (readline ?input))
      (if (neq ?line nil)
         then
         (bind ?row (create$))
         (bind ?token "")
         (bind ?len (str-length ?line))
         (loop-for-count (?i 1 (+ 1 ?len)) do
            (bind ?char (str-index ?line ?i))
            (if (str-compare ?char " ")
               then
               (if (neq ?token "")
                  then
                  (bind ?number (string-to-field ?token))
                  (if (numberp ?number)
                     then
                     (bind ?row (create$ ?row ?number))
                     (bind ?token "")))
               else
               (bind ?token (str-cat ?token ?char))))
         (if (neq (length$ ?row) 0)
            then
            (bind ?matrix (create$ ?matrix ?row)))))
   
   (bind ?num-rows (length$ ?matrix))
   (bind ?num-cols (length$ (first$ ?matrix)))

   (loop-for-count (?i 1 (+ 1 ?num-cols)) do
      (loop-for-count (?j ?i (- ?num-rows 1)) do
         (bind ?pivot-row (nth$ ?j ?matrix))
         (bind ?pivot (nth$ ?i ?pivot-row))
         (if (neq ?pivot 0)
            then
            (loop-for-count (?k (+ ?j 1) ?num-rows) do
               (bind ?current-row (nth$ ?k ?matrix))
               (bind ?factor (/ (nth$ ?i ?current-row) ?pivot))
               (bind ?scaled-row (create$))
               (loop-for-count (?l 1 (+ 1 ?num-cols)) do
                  (bind ?new-value (- (* (nth$ ?l ?pivot-row) ?factor) (nth$ ?l ?current-row)))
                  (bind ?scaled-row (create$ ?scaled-row ?new-value)))
               
               (replace$ ?matrix ?k ?k ?scaled-row)
            )
         )
      )
   )

   (foreach ?row ?matrix do
      (bind ?result "")
      (loop-for-count (?i 1 (+ 1 (length$ ?row))) do
         (bind ?element (nth$ ?i ?row))
         (if (neq ?i 1) then (bind ?result (str-cat ?result " "))) 
         (bind ?result (str-cat ?result ?element)) 
      )
      (printout ?output ?result crlf)
   )

   (close ?input) 
   (close ?output) 
)

这是错误:

`CLIPS\> (solve-system "file1" "file3")
\[ROUTER1\] Logical name 'TRUE' was not recognized by any routers.
\[PRCCODE4\] Execution halted during the actions of deffunction 'solve-system'.
FALSE`

我该如何解决这个问题?

我尝试不绑定文件,但我不知道如何解决这个问题。我还认为问题可能出在文件名上,但事实并非如此。

file bind clips
1个回答
0
投票

根据基本编程指南,open 函数的语法是

(open <file-name> <logical-name> [<mode>])

readline 函数的语法是

(readline [<logical-name>])

在您的代码中,您像这样调用 open :

(bind ?input (open ?input-filename "r"))

你认为的论点实际上就是论点,而论点是未指定的。如果文件存在,则它将被打开并与逻辑名称“r”关联。 open 调用的返回值为 TRUE 并分配给变量 ?input.

在您的代码中,您可以像这样调用 readline 函数:

(readline ?input)

变量 ?input 设置为 TRUE,它作为逻辑名称传递给 readline,这就是导致错误消息的原因,因为该名称与打开的文件无关。如果您改为传递“r”,则该调用将会起作用。

您需要做的是在公开调用中指定一个逻辑名称:

(bind ?input (open ?input-filename in "r"))

然后在 readline 调用中使用相同的名称:

(readline in)
© www.soinside.com 2019 - 2024. All rights reserved.