Sophie

Sophie

distrib > Mandriva > 2010.0 > i586 > by-pkgid > 2fc07611b08d4a735fd34d5eb60d8e16 > files > 2095

ciao-1.10p8-3mdv2010.0.i586.rpm

<HTML>
<HEAD>
<!-- Created by texi2html 1.56k + clip patches and <A href="http://www.clip.dia.fi.upm.es/Software">lpdoc</A> from ciao.texi on 28 January 2007 -->

<LINK rel="stylesheet" href="ciao.css" type="text/css">
<TITLE>The Ciao Prolog System               - Using the persdb library</TITLE>
</HEAD>
<BODY> 
Go to the <A HREF="ciao_1.html">first</A>, <A HREF="ciao_148.html">previous</A>, <A HREF="ciao_150.html">next</A>, <A HREF="ciao_241.html">last</A> section, <A HREF="ciao_toc.html">table of contents</A>.
<P><HR><P>


<H1><A NAME="SEC622" HREF="ciao_toc.html#TOC622">Using the persdb library</A></H1>

<P>
<STRONG>Author(s):</STRONG> The CLIP Group.


<P>
<STRONG>Version:</STRONG> 1.10#7 (2006/4/26, 19:22:13 CEST)


<P>
<STRONG>Version of last change:</STRONG> 1.9#232 (2003/12/22, 18:4:0 CET)


<P>
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. 



<UL>
<LI><A HREF="ciao_149.html#SEC623">An example of persistent predicates (static version)</A>
<LI><A HREF="ciao_149.html#SEC624">An example of persistent predicates (dynamic version)</A>
<LI><A HREF="ciao_149.html#SEC625">A simple application / a persistent queue</A>
</UL>



<H2><A NAME="SEC623" HREF="ciao_toc.html#TOC623">An example of persistent predicates (static version)</A></H2>


<PRE>
:- use_package(iso).
:- use_package(persdb).

%% 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),
         assertz_fact(bar(X)),
         findall(Y,bar(Y),L),
         write(L).

erase_one :-
        retract_fact(bar(_)).
erase_all :-
        retractall_fact(bar(_)).

u(X) :- update_files(X).

</PRE>



<H2><A NAME="SEC624" HREF="ciao_toc.html#TOC624">An example of persistent predicates (dynamic version)</A></H2>


<PRE>
:- use_package(iso).
:- use_package(persdb).

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),
     assertz_fact(bar(X)),
     findall(Y, bar(Y), L),
     write(L).    

</PRE>



<H2><A NAME="SEC625" HREF="ciao_toc.html#TOC625">A simple application / a persistent queue</A></H2>

<PRE>
:- module(queue, [main/0],[persdb]).

:- use_package(iso).

:- use_module(library(read)).
:- use_module(library(write)).
:- use_module(library(aggregates)).

persistent_dir(queue_dir,'./pers').

:- persistent(queue/1, queue_dir).

queue(first).
queue(second).

main:-
     write('Action ( in(Term). | slip(Term) | out. | list. | halt. ): '),
     read(A),
     (  handle_action(A)
     -&#62; true
     ;  write('Unknown command.'), nl ),
     main.

handle_action(end_of_file) :-
     halt.
handle_action(halt) :-
     halt.
handle_action(in(Term)) :-
     assertz_fact(queue(Term)),
     main.
handle_action(slip(Term)) :-
     asserta_fact(queue(Term)),
     main.
handle_action(out) :-
     (  retract_fact(queue(Term))
     -&#62; write('Out '), write(Term)
     ;  write('FIFO empty.') ),
     nl,
     main.
handle_action(list) :-
     findall(Term,queue(Term),Terms),
     write('Contents: '), write(Terms), nl,
     main.
     

</PRE>

<P><HR><P>
Go to the <A HREF="ciao_1.html">first</A>, <A HREF="ciao_148.html">previous</A>, <A HREF="ciao_150.html">next</A>, <A HREF="ciao_241.html">last</A> section, <A HREF="ciao_toc.html">table of contents</A>.
</BODY>
</HTML>