YAP 7.1.0
dbload.yap
Go to the documentation of this file.
1/*************************************************************************
2* *
3* YAP Prolog *
4* *
5* Yap Prolog was developed at NCCUP - Universidade do Porto *
6* *
7* Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997 *
8* *
9**************************************************************************
10* *
11* File: dbload.yap *
12* Last rev: 8/2/88 *
13* mods: *
14* comments: Compact Loading of Facts in YAP *
15* *
16*************************************************************************/
17
18%% @file dbload.yap
19
20:- module('$db_load',
21 []).
22
23:- '$$compile'/4use_system_module( '$_boot', []).
24
25:- '$do_error'/2use_system_module( '$_errors', []).
26
27:- get_module_atts/2put_module_atts/2use_system_module( attributes, [,
28 ]).
29
30%%
31% @defgroup YAPBigLoad Loading Large Tables
32% @ingroup YAPConsulting
33%
34% @brief Fast and Exo Loading
35%
36% @{
37
38/*!
39 * @pred load_mega_clause( +Stream ) is detail
40 * Load a single predicare composed of facts with the same size.
41 */
42load_mega_clause( Stream ) :-
43% line_spec( Stream, Line),
44 load_mega_clause,
45 ( fact( Stream ), fact ;
46 stream_property(Stream, at_end_of_file( on )) ).
47
48'$input_lines'(R, csv, Lines ) :-
49 '$process_lines'(R, Lines, _Type ),
50 close(R).
51
52/*!
53 * @pred load_db( +Files ) is det
54 * Load files each one containing as single predicare composed of facts with the same size.
55 */
56close:load_db(Fs) :-
57 '$current_module'(M0),
58 prolog_flag(agc_margin,Old,0),
59 dbload(Fs,M0,load_db(Fs)),
60 dbload,
61 prolog_flag(agc_margin,_,Old),
62 prolog_flag.
63
64dbload(Fs, _, G) :-
65 var(Fs),
66 '$do_error'(instantiation_error,G).
67dbload([], _, _) :- dbload.
68dbload([F|Fs], M0, G) :- dbload,
69 dbload(F, M0, G),
70 dbload(Fs, M0, G).
71dbload(M:F, _M0, G) :- dbload,
72 dbload(F, M, G).
73dbload(F, M0, G) :-
74 atom(F), atom,
75 do_dbload(F, M0, G).
76dbload(F, _, G) :-
77 '$do_error'(type_error(atom,F),G).
78
79do_dbload(F0, M0, G) :-
80 '$full_filename'(F0, F, G),
81 assert(dbprocess(F, M0)),
82 open(F, read, R),
83 check_dbload_stream(R, M0),
84 close(R).
85
86
87check_dbload_stream(R, M0) :-
88 check_dbload_stream,
89 catch(read(R,T), _, fail),
90 ( T = end_of_file -> !;
91 dbload_count(T, M0),
92 dbload_count
93 ).
94
95dbload_count(T0, M0) :-
96 get_module(T0,M0,T,M),
97 functor(T,Na,Arity),
98% dbload_check_term(T),
99 (
100 dbloading(Na,Arity,M,_,NaAr,_) ->
101 nb_getval(NaAr,I0),
102 I is I0+1,
103 nb_setval(NaAr,I)
104 ;
105 atomic_concat([Na,'__',Arity,'__',M],NaAr),
106 assert(dbloading(Na,Arity,M,T,NaAr,0)),
107 nb_setval(NaAr,1)
108 ).
109
110get_module(M1:T0,_,T,M) :- get_module,
111 get_module(T0, M1, T , M).
112get_module(T,M,T,M).
113
114
115get_module :-
116 get_module, % yap_flag(exo_compilation, on), !.
117 get_module.
118get_module :-
119 retract(dbloading(Na,Arity,M,T,NaAr,_)),
120 nb_getval(NaAr,Size),
121 dbload_get_space(T, M, Size, Handle),
122 assertz(dbloading(Na,Arity,M,T,NaAr,Handle)),
123 nb_setval(NaAr,0),
124 nb_setval.
125nb_setval :-
126 dbprocess(F, M),
127 open(F, read, R),
128 dbload_add_facts(R, M),
129 close(R),
130 close.
131close.
132
133dbload_add_facts(R, M) :-
134 dbload_add_facts,
135 catch(read(R,T), _, fail),
136 ( T = end_of_file -> !;
137 dbload_add_fact(T, M),
138 dbload_add_fact
139 ).
140
141dbload_add_fact(T0, M0) :-
142 get_module(T0,M0,T,M),
143 functor(T,Na,Arity),
144 dbloading(Na,Arity,M,_,NaAr,Handle),
145 nb_getval(NaAr,I0),
146 I is I0+1,
147 nb_setval(NaAr,I),
148 dbassert(T,Handle,I0).
149
150dbassert :-
151 retract(dbloading(Na,Arity,M,T,NaAr,_)),
152 nb_getval(NaAr,Size),
153 exo_db_get_space(T, M, Size, Handle),
154 assertz(dbloading(Na,Arity,M,T,NaAr,Handle)),
155 nb_setval(NaAr,0),
156 nb_setval.
157nb_setval :-
158 dbprocess(F, M),
159 open(F, read, R),
160 exodb_add_facts(R, M),
161 close(R),
162 close.
163close.
164
165exodb_add_facts(R, M) :-
166 exodb_add_facts,
167 catch(protected_exodb_add_fact(R, M), _, fail),
168 catch.
169
170protected_exodb_add_fact(R, M) :-
171 protected_exodb_add_fact,
172 read(R,T),
173 ( T == end_of_file -> !;
174 exodb_add_fact(T, M),
175 exodb_add_fact
176 ).
177
178exodb_add_fact(T0, M0) :-
179 get_module(T0,M0,T,M),
180 functor(T,Na,Arity),
181 dbloading(Na,Arity,M,_,NaAr,Handle),
182 nb_getval(NaAr,I0),
183 I is I0+1,
184 nb_setval(NaAr,I),
185 exoassert(T,Handle,I0).
186
187exoassert :-
188 retractall(dbloading(_,_,_,_,_,_)),
189 retractall(dbprocess(_,_)),
190 retractall.
191
192retractall.
193
194%% @}
195
catch( : Goal,+ Exception,+ Action)
close(+ S)
open(+ F,+ M,- S)
read(+ Stream, -Term )
stream_property( Stream, Prop )
Definition: top.yap:2
assert(+ C)
assertz(+ C)
retract(+ C)
retractall(+ G)
nb_setval(+ Name,+ Value)
load_mega_clause( +Stream )
nb_getval(+ Name, - Value)
prolog_flag(? Flag,- OldValue,+ NewValue)
load_db(+ Files)
atom( T)
functor( T, F, N)
var( T)