从文件读取到数据库序言

时间:2019-01-14 11:44:32

标签: database file 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谓词,我尝试了很多方法,但是没有用。

谢谢

1 个答案:

答案 0 :(得分:0)

因此,对于your other question,我认为您可以使用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])).