Olexiy  Pyvovarov
Olexiy Pyvovarov

Reputation: 890

Prolog C# Interface retract from file

I just established a connection to SWI Prolog and want to manipulate facts. e.g. retract and assert them.

I have some function like this:

       String[] param = { "-q" };
       PlEngine.Initialize(param);
            PlQuery.PlCall("consult('tablets.pl').");

            PlQuery.PlCall("assert(tablet(4,newatomic)).");
            PlQuery.PlCall("tell('tablets.pl'), listing(tablet/2), told.");
            PlQuery.PlCall("retractall(tablet/2).");
            PlQuery.PlCall("assert(tablet(1,n1ewatomic)).");
            PlQuery.PlCall("assert(tablet(2,n2ewatomic)).");
            PlQuery.PlCall("tell('tablets.pl'), listing(tablet/2), told.");

As you can see this function, it's working for assertion as expected, but not for retract. The matter is that, I want to delete all tablets facts (they are dynamic) from file before inserting the next ones. PlQuery.PlCall("retractall(tablet/2)."); this query must delete all record that are in the file. and also how to delete a fact for example tablet(4,newatomic), but not to delete another facts.

The resulting file after execution is:

:- dynamic tablet/2.

tablet(4, newatomic).
tablet(1, n1ewatomic).
tablet(2, n2ewatomic).

Upvotes: 5

Views: 368

Answers (1)

Olexiy  Pyvovarov
Olexiy Pyvovarov

Reputation: 890

Well, don't want to delete a question, cause it got two upvotes. Perhaps it'll be useful for someone.

So, the logic was right.

Whenever we want to attach something to database we write:

assert(predicat(var1, var2, ... , varn))

Whenever we want to delete something from database we write:

retract(predicat(var1, var2, ... , varn))
retractall(predicat(var1, var2, ... , varn))

If all the terms are equal it's deleted from db (also it must be dynamic)

If we want to delete all data, we need to specify it by Variable. So...

retract(predicat(_,_,...,_))
retractall(predicat(_,_,...,_))

will delete all data that matches the query. And to save data we just write next:

tell('database_file.txt'), %opening file for writing
listing(ig_node), %writing
told. %closing/saving file

Upvotes: 0

Related Questions