Question

In my program I have a dynamic clauses, they works fine, but when I closing my program, they are disappears.

I've tryed that

saveState :-
  write_pl_state_file('backup.dat').

loadState :-
  file_exists('backup.dat'),
  read_pl_state_file('backup.dat'); !.

but this is not works.

Is there a way to save this databse to a file?

Was it helpful?

Solution

The predicates write_pl_state_file/1 and read_pl_state_file/1 are connected with the information/state that affects parsing of terms, i.e. operator definitions, character conversion Prolog flags, etc.

So that is part of your solution (perhaps), but more fundamentally you wish to save the dynamic clause definitions, probably in a form that allows you to reinstate them by consulting a file.

The predicate listing/0 does something like this, but it displays the dynamic clauses to the "console", not to a file. Probably you want to use the underlying predicate portray_clause/2, which does allow redirecting output to a file (stream).

The author Daniel Diaz noted a slight change (adding a newline to end of output) for portray_clause/2 in recent release notes for version 1.4.0, so you may want to make sure you've got the latest version for the sake of legibility.

Added:

It appears that starting with version 1.3.2 GNU Prolog supports sending listing/0 output to the current stream (rather than just to the console as in 1.3.1 and earlier).

Here's a test case:

| ?- assertz(whoami(i)).
| ?- assertz(whoami(me)).
| ?- assertz(whoami(myself)).

which creates three clauses (facts) for a dynamic predicate whoami/1.

I then created a file myClauses.pl with the following query:

| ?- open('myClauses.pl',write,S), set_output(S), listing, close(S).

Once the stream S is closed, current output is reset to the console.

You will find that the file myClauses.pl contains a blank line followed by the three clauses, so that the source code is in proper form to be consulted. However I'm having a problem with the consult/1 predicate (and its File -> Consult... menu equivalent) in my newly installed GNU Prolog 1.4.0 under Windows. The compilation works from the command line and produces a byte-code file that load/1 can correctly handle in the console, so there's some small problem in how things are set up. I'll post a further note when I get that squared away, having sent in a bug report. I've not tried it yet under Linux.

OTHER TIPS

You can use current_predicate/1 or predicate_property/2 to access predicates, and clause/2 to access the clauses for a predicate.

Then you can write a save utility by using that information.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top