简体   繁体   English

从文件读取到数据库序言

[英]Reading from file to database prolog

Hello everyone 大家好

I have problem with part of my project for studies. 我的研究项目有问题。 My task is to write a program in prolog that can tell you what illnes do you have based on input from user. 我的任务是在序言中编写一个程序,该程序可以根据用户的输入告诉您您有哪些疾病。 Data base must be read from a file which format is up to me. 必须从文件格式读取数据库。

Construction: 施工:

I decide to have 2 dynamic rules; 我决定有2条动态规则;

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

where: 哪里:

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

File: example.txt: 文件:example.txt:

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

Problem: 问题:

My major problem is to format this data to use asserta predicat, I tried many ways but it didn't work. 我的主要问题是将数据格式化为使用asserta谓词,我尝试了多种方法,但没有用。

Thank you 谢谢

So, per your other question , I think you can parse these strings with split_string/4 , your problem is that the result of that is not atoms, and then you need to build the structure properly. 因此,对于您的其他问题 ,我想您可以使用split_string/4解析这些字符串,您的问题是,结果不是原子,那么您需要正确地构建结构。 This I think is the piece you're missing: 我认为这是您所缺少的:

?- 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)).

If you then simply asserta(Result) you have added the right thing to the database. 如果您只是简单地asserta(Result)那么您已将正确的内容添加到数据库中。

If you have a variable number of symptoms, you should keep it a list in there instead, and that would simplify your processing greatly (and probably our downstream code, as doing anything four times in a row is a bit repetitive): 如果您有多种症状,则应在其中保留一个列表,这将大大简化您的处理过程(并且可能是我们的下游代码,因为连续执行四次操作都有些重复):

?- 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