JESS添加recommandation模块后出现routine call错误求助
看起来你遇到的问题大概率是代码不完整、模块作用域隔离问题,或者自定义函数的语法错误导致的,我帮你一步步排查和修复:
1. 首先补全截断的代码
你提供的代码最后一行是(questions (ident PM_m的内容,明显没有写完,缺少闭合的括号和完整的事实定义。Jess在解析不完整的代码时,会直接抛出语法错误,这很可能是触发「routine call」错误的首要原因。
你需要把deffacts MAIN::question-data块补全,确保所有的(questions ...)事实都有正确的闭合,并且整个deffacts块以)结尾,比如补全后的片段:
(deffacts MAIN::question-data "questions posée par le systeme" (questions (ident poids) (type number)(texte "quel poids a-t-il?")) (questions (ident age) (type number)(texte "quelle age a l'enfant?")) (questions (ident symptome_majeur_1) (type yes-no)(texte "presente t-il des dyspnès sifflantes?")) ; ... 其他已有的问题事实 ... (questions (ident Rx_foyer_pneumo_inhalation) (type yes-no)(texte "la Radio du thorax presente-t-elle des foyers de pneumopathies d'inhalaton?")) ; 补全你未写完的PM_xxx问题 (questions (ident PM_exemple) (type yes-no)(texte "Votre question ici?")) )
2. 修复自定义函数的语法错误
你的is-of-type函数存在嵌套逻辑错误,最后一个else分支没有正确包裹在if结构里,这会导致函数定义失效,调用时触发routine错误。修复后的函数应该是:
(deffunction is-of-type (?answer ?type) "Check that the answer has the right form" (if (eq ?type yes-no) then (return (or (eq ?answer yes) (eq ?answer no))) else (if (eq ?type number) then (return (numberp ?answer)) else (return (> (str-length ?answer) 0)) ) ) )
3. 处理模块间的作用域隔离问题
Jess的模块是命名空间隔离的,默认情况下,一个模块内的规则/函数只能访问当前模块的元素。如果你在recommandation模块中调用了MAIN模块的自定义函数(比如ask-patient、is-of-type),或者引用MAIN模块的模板/事实,必须加上MAIN::前缀。
举个正确的recommandation模块示例:
(defmodule recommandation) (defrule recommandation::generate-treatment ; 引用MAIN模块的diagnostique事实 (MAIN::diagnostique (maladie ?maladie) (explication ?expl)) ; 引用MAIN模块的年龄回答 (MAIN::reponses (ident age) (texte ?age)) => (printout t crlf "--- Diagnostique & Recommandation ---" crlf) (printout t "Maladie: " ?maladie crlf) (printout t "Explication: " ?expl crlf) ; 如果需要调用MAIN模块的自定义函数,必须加前缀 (if (MAIN::is-of-type ?age number) then (printout t "Age valide: " ?age " ans" crlf) ) ; 添加你的推荐逻辑 (printout t "Recommandation: Consultez un pneumologue immédiatement." crlf) )
4. 检查模块规则的定义
确保recommandation模块内的规则都正确使用了模块前缀(比如recommandation::generate-treatment),避免和其他模块的规则重名,同时确保规则中的模式匹配都正确引用了对应模块的事实。
按照这几步修复后,再重新运行你的代码,应该就能解决这个routine call错误了。
内容的提问来源于stack exchange,提问作者Nadh Hely

