Prolog - How to assert/make a database only once

五迷三道 提交于 2019-12-02 05:24:36

问题


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(_).

I want to populate a fact database named 'listofQuestions'. Everything works fine, but the stuffs that I am asserting stays in the memory. So, if I run my program again, I get the same bunch of facts added to the 'listofQuestions'.

I only want to have one set of data.

Thankyou


回答1:


Maybe do retractall/1 before you rerun your program.




回答2:


Make a separate predicate for assertion that checks if fact is not yet asserted:

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


来源:https://stackoverflow.com/questions/10437395/prolog-how-to-assert-make-a-database-only-once

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!