繁体   English   中英

从文件读取到数据库序言

[英]Reading from file to database prolog

大家好

我的研究项目有问题。 我的任务是在序言中编写一个程序,该程序可以根据用户的输入告诉您您有哪些疾病。 必须从文件格式读取数据库。

施工:

我决定有2条动态规则;

:- dynamic (illness/2).
:- dynamic (symptoms/4).

哪里:

illnes(name_of_illness, symptoms(symptom1, symptom2, symptom3, symptom4)

文件:example.txt:

flu,cough,fever,head_acke, runny_nose.
measles, rash, fever, sore_throat, inflamed_eyes.

问题:

我的主要问题是将数据格式化为使用asserta谓词,我尝试了多种方法,但没有用。

谢谢

因此,对于您的其他问题 ,我想您可以使用split_string/4解析这些字符串,您的问题是,结果不是原子,那么您需要正确地构建结构。 我认为这是您所缺少的:

?- split_string("this,that,the,other,thing", ",", " ", X), 
   maplist(atom_string, [Condition,Sym1,Sym2,Sym3,Sym4], X), 
   Result = illness(Condition, symptoms(Sym1,Sym2,Sym3,Sym4)).
X = ["this", "that", "the", "other", "thing"],
Condition = this,
Sym1 = that,
Sym2 = the,
Sym3 = other,
Sym4 = thing,
Result = illness(this, symptoms(that, the, other, thing)).

如果您只是简单地asserta(Result)那么您已将正确的内容添加到数据库中。

如果您有多种症状,则应在其中保留一个列表,这将大大简化您的处理过程(并且可能是我们的下游代码,因为连续执行四次操作都有些重复):

?- split_string("this,that,the,other,thing", ",", " ", X), 
   maplist(atom_string, [Condition|Symptoms], X), 
   Result = illness(Condition, symptoms(Symptoms)).
X = ["this", "that", "the", "other", "thing"],
Condition = this,
Symptoms = [that, the, other, thing],
Result = illness(this, symptoms([that, the, other, thing])).

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM