序言 - 如何断言/使数据库只有一次(Prolog - How to assert/make a d

2019-07-29 06:21发布

resultList(UsersQuery):-
    question(X,H),
    write(H),
    myintersection(H,UsersQuery,Match,TotalQuestionKeywords),
    Percent is Match/TotalQuestionKeywords*100,
    write('Question: '),
    write(X),nl,write('Quality: '), write(Percent),write('%'),nl,

    /* please look at this part
    Percent>=50,
    assert(listofQuestions(Percent,Question)),
    write(Percent),write(Question),nl,
    fail.
resultList(_).

我想填充名为“listofQuestions”事实数据库。 一切工作正常,但东西,我主张在记忆停留。 所以,如果我再次运行我的程序,我得到同样的一堆添加到“listofQuestions”的事实。

我只希望有一组数据。

谢谢

Answer 1:

也许你retractall/1你重新运行程序之前。



Answer 2:

作出断言,检查是否事实尚未认定了独立的谓语:

assertThisFact(Fact):-
    \+( Fact ),!,         % \+ is a NOT operator.
    assert(Fact).
assertThisFact(_).


文章来源: Prolog - How to assert/make a database only once
标签: prolog rule