我正在构建一个简单的疾病诊断专家系统。我收集了患者的症状并将其存储在如下列表中:
(patient_symptoms headache)
(patient_symptoms temperature)
(patient_symptoms cough)
我还定义了一个模板:
(deftemplate illness_matching
(multislot symptom_names (type SYMBOL))
)
我正在尝试将值从“病人症状”转换为“疾病匹配”模板的单个实例。
我试过这个:
(defrule convert_to_template
(patient_symptoms $?all)
=>
(assert (illness_matching (symptom_names ?all)))
)
结果:
(illness_matching (symptom_names headache))
(illness_matching (symptom_names temperature))
(illness_matching (symptom_names cough))
我期待的结果:
(illness_matching (symptom_names headache temperature cough))
假设您的症状列表是您一一断言的事实,您将需要在您的规则中保留当前疾病状态的信息,以便您可以附加新症状。
(defrule convert_to_template
; Merge a set of symptoms into a multifield
?illness_fact <- (illness_matching)
?symptom_fact <- (patient_symptom ?symptom)
=>
(modify ?illness_fact (symptom_names (insert$ (fact-slot-value ?illness_fact symptom_names) 1 ?symptom)))
; you want to retract the symptom to avoid a loop
(retract ?symptom_fact))
modify
语句允许您更改现有事实的值。然后,您只需将 symptom_name
插槽更改为新插槽,该新插槽是旧插槽(通过 fact-slot-value
函数获得)的结果,并通过 insert$
函数在开头添加新症状。
示例:
Jupyter console 6.6.3
iCLIPS
In [1]: (deftemplate illness_matching
...: (multislot symptom_names (type SYMBOL)))
In [2]: (defrule convert_to_template
...: ?illness_fact <- (illness_matching)
...: ?symptom_fact <- (patient_symptom ?symptom)
...: =>
...: (modify ?illness_fact (symptom_names (insert$ (fact-slot-value ?illness_fact symptom_names) 1 ?symptom)))
...: (retract ?symptom_fact))
In [3]: (assert (illness_matching))
(illness_matching (symptom_names))
In [4]: (assert (patient_symptom fever))
(patient_symptom fever)
In [5]: (assert (patient_symptom nausea))
(patient_symptom nausea)
In [6]: (run)
In [7]: (facts)
f-1 (illness_matching (symptom_names fever nausea))
For a total of 1 fact.