Through the following examples we will try to illustrate the two mains ways of declaring and using persistent predicates: statically (the preferred method) and dynamically (necessary when the new persistent predicates have to be defined at run-time). The final example is a small application implementing a simple persistent queue.
:- include(library(persdb)). %% Required declarations for persistent_dir/2. :- multifile persistent_dir/2. :- data persistent_dir/2. %% Declare the directory associated to the key "db" where the %% persistence sets of the persistent predicates are stored: persistent_dir(db,'./'). %% Declare a persistent predicate: :- persistent(bar/1, db). %% Read a term, storing it in a new fact of the persistent predicate %% and list all the current facts of that predicate main:- read(X), passertz_fact(bar(X)), findall(Y,bar(Y),L), write(L).
:- include(library(persdb)). %% Required declarations for persistent_dir/2. :- multifile persistent_dir/2. :- data persistent_dir/2. main([X]):- %% Declare the directory associated to the key "db" asserta_fact(persistent_dir(db,'./')), %% Declare the predicate bar/1 as dynamic (and data) at run-time data(bar/1), %% Declare the predicate bar/1 as persistent at run-time make_persistent(bar/1, db), passertz_fact(bar(X)), findall(Y, bar(Y), L), write(L).
:- module(queue, [main/0],[]). :- use_module(library(read)). :- use_module(library(write)). :- use_module(library(aggregates)). :- include(library(persdb)). :- multifile persistent_dir/2. :- data persistent_dir/2. :- persistent(queue/1, queue_dir). persistent_dir(queue_dir,'./pers'). main:- write('Action ( in(Term). | out. | list. | halt. ): '), read(A), ( handle_action(A) -> true ; write('Unknown command.'), nl ), main. handle_action(halt) :- halt. handle_action(in(Term)) :- passertz_fact(queue(Term)), main. handle_action(out) :- ( pretract_fact(queue(Term)) -> write('Out '), write(Term) ; write('FIFO empty.') ), nl, main. handle_action(list) :- findall(Term,pcurrent_fact(queue(Term)),Terms), write('Contents: '), write(Terms), nl, main.
Go to the first, previous, next, last section, table of contents.