HungryCoder
HungryCoder

Reputation: 1139

Prolog - How to assert/make a database only once

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

Upvotes: 3

Views: 1802

Answers (2)

Ihmahr
Ihmahr

Reputation: 1120

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

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

Upvotes: 3

Kaarel
Kaarel

Reputation: 10672

Maybe do retractall/1 before you rerun your program.

Upvotes: 3

Related Questions