将用户列表追加到数据库原子

时间:2019-03-08 01:29:34

标签: database list loops prolog

我要完成的工作是使用疾病事实数据库

symptom(shingles,headache).
symptom(shingles,fever).
symptom(shingles,malaise).
symptom(shingles,headache).
symptom(smallpox,fever).
symptom(smallpox,rash).

,并将其与用户的症状列表进行比较。我目前可以从用户那里得到症状并将疾病添加到列表中,但是,我无法弄清楚如何遍历整个数据库以添加所有可能的疾病。

start:-
    consult(diseases1),
    getSymptoms(Symptoms),
    write(Symptoms).

welcome:-
        write('Welcome to the Disease Diagnostic Center'),nl,nl.

getSymptoms(Symptoms) :-
    write('Please enter symptoms now, enter "Done" when finished: ' ),
    read_string(user, "\n", "\r", _, Response),
    (
        Response == "Done"
    ->
        Symptoms = []
    ;
        atom_string(Symptom,Response),
        valid_symptom(Symptom,Symptoms)

    ).

valid_symptom(Symptom,Symptoms) :-
(
    symptom(_,Symptom)
->
    getSymptoms(Symptoms0),
    foreach(symptom(Y,Symptom),write(Y))
;
    format('Invalid symptom: `~w''~n',[Symptom]),
    getSymptoms(Symptoms0),
    Symptoms = Symptoms0
).  

因此,例如,用户输入发烧作为症状之一,则该列表中应包含带状疱疹和天花。目前,我可以将每种可能的疾病写到屏幕上,但是我不确定用什么来替代write才能将每种疾病添加到列表中。

1 个答案:

答案 0 :(得分:0)

如果您有所有疾病的清单,则可以通过要求特定症状进行过滤。

所有疾病:

all_diseases(Diseases) :-
    setof(Disease, Symptom^symptom(Disease, Symptom), Diseases).

?- all_diseases(D).
D = [shingles, smallpox].

然后对其进行过滤:

require_symptom(Symptom, Diseases0, Diseases) :-
    setof(Disease,
          ( member(Disease, Diseases0),
            symptom(Disease, Symptom)),
          Diseases)
    *-> true
    ; Diseases = [].

?- all_diseases(Ds), require_symptom(headache, Ds, D1).
Ds = [shingles, smallpox],
D1 = [shingles].

?- all_diseases(Ds), require_symptom(fever, Ds, D1).
Ds = D1, D1 = [shingles, smallpox].

?- all_diseases(Ds), require_symptom(vomiting, Ds, D1).
Ds = [shingles, smallpox],
D1 = [].

在过滤之前和之后输出列表。