This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
yap-6.3/pl/dbload.yap

189 lines
4.0 KiB
Plaintext
Raw Permalink Normal View History

2017-04-13 21:42:34 +01:00
/*************************************************************************
2011-04-30 01:16:40 +01:00
* *
* YAP Prolog *
* *
* Yap Prolog was developed at NCCUP - Universidade do Porto *
* *
* Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997 *
* *
**************************************************************************
* *
* File: dbload.yap *
* Last rev: 8/2/88 *
* mods: *
* comments: Compact Loading of Facts in YAP *
* *
*************************************************************************/
2017-05-19 09:54:35 +01:00
%% @file dbload.yap
2011-04-30 01:16:40 +01:00
:- module('$db_load',
[]).
2014-04-09 12:39:29 +01:00
:- use_system_module( '$_errors', ['$do_error'/2]).
:- use_system_module( attributes, [get_module_atts/2,
put_module_atts/2]).
2017-05-19 09:54:35 +01:00
%%
% @defgroup YAPBigLoad Loading Large Tables
2017-10-27 13:50:40 +01:00
% @ingroup YAPConsulting
2018-06-05 11:20:39 +01:00
% @{
2017-05-19 09:54:35 +01:00
% @brief Fast and Exo Loading
%
2017-05-02 04:07:23 +01:00
/*!
* @pred load_mega_clause( +Stream ) is detail
* Load a single predicare composed of facts with the same size.
*/
load_mega_clause( Stream ) :-
2016-12-16 09:54:16 +00:00
% line_spec( Stream, Line),
repeat,
( fact( Stream ), fail ;
2016-12-16 09:54:16 +00:00
stream_property(Stream, at_end_of_file( on )) ).
2016-10-20 04:44:59 +01:00
2016-12-16 09:54:16 +00:00
'$input_lines'(R, csv, Lines ) :-
'$process_lines'(R, Lines, _Type ),
2016-10-20 04:44:59 +01:00
close(R).
2013-01-15 11:18:09 +00:00
2017-05-02 04:07:23 +01:00
/*!
* @pred load_db( +Files ) is det
* Load files each one containing as single predicare composed of facts with the same size.
*/
2011-04-30 01:16:40 +01:00
prolog:load_db(Fs) :-
2016-10-20 04:44:59 +01:00
'$current_module'(M0),
2018-09-13 17:08:26 +01:00
retractall(dbloading(_Na,_Arity,_M,_T,_NaAr,_)),
2011-04-30 01:16:40 +01:00
prolog_flag(agc_margin,Old,0),
dbload(Fs,M0,load_db(Fs)),
load_facts,
prolog_flag(agc_margin,_,Old),
clean_up.
dbload(Fs, _, G) :-
var(Fs),
2016-10-20 04:44:59 +01:00
'$do_error'(instantiation_error,G).
dbload([], _, _) :- !.
2013-01-07 09:47:14 +00:00
dbload([F|Fs], M0, G) :- !,
2011-04-30 01:16:40 +01:00
dbload(F, M0, G),
dbload(Fs, M0, G).
2013-01-07 09:47:14 +00:00
dbload(M:F, _M0, G) :- !,
2011-04-30 01:16:40 +01:00
dbload(F, M, G).
dbload(F, M0, G) :-
atom(F), !,
do_dbload(F, M0, G).
dbload(F, _, G) :-
'$do_error'(type_error(atom,F),G).
2018-10-10 12:33:05 +01:00
do_dbload(F0, M0, _G) :-
2018-09-13 17:08:26 +01:00
'$full_filename'(F0, F),
2011-04-30 01:16:40 +01:00
assert(dbprocess(F, M0)),
open(F, read, R),
check_dbload_stream(R, M0),
close(R).
2013-01-15 11:18:09 +00:00
2011-04-30 01:16:40 +01:00
check_dbload_stream(R, M0) :-
repeat,
2013-01-09 09:21:07 +00:00
catch(read(R,T), _, fail),
2011-04-30 01:16:40 +01:00
( T = end_of_file -> !;
dbload_count(T, M0),
2016-10-20 04:44:59 +01:00
fail
2011-04-30 01:16:40 +01:00
).
dbload_count(T0, M0) :-
2019-04-20 12:48:33 +01:00
'$yap_strip_module'(M0:T0,M,T),
2011-04-30 01:16:40 +01:00
functor(T,Na,Arity),
% dbload_check_term(T),
(
dbloading(Na,Arity,M,_,NaAr,_) ->
nb_getval(NaAr,I0),
I is I0+1,
nb_setval(NaAr,I)
;
atomic_concat([Na,'__',Arity,'__',M],NaAr),
assert(dbloading(Na,Arity,M,T,NaAr,0)),
nb_setval(NaAr,1)
).
2016-10-20 04:44:59 +01:00
2013-01-07 09:47:14 +00:00
load_facts :-
2013-01-09 09:21:07 +00:00
!, % yap_flag(exo_compilation, on), !.
2013-01-07 09:47:14 +00:00
load_exofacts.
2011-04-30 01:16:40 +01:00
load_facts :-
retract(dbloading(Na,Arity,M,T,NaAr,_)),
nb_getval(NaAr,Size),
2019-04-20 12:48:33 +01:00
prolog:'$dbload_get_space'(T, M, Size, Handle),
2011-04-30 01:16:40 +01:00
assertz(dbloading(Na,Arity,M,T,NaAr,Handle)),
nb_setval(NaAr,0),
fail.
load_facts :-
dbprocess(F, M),
open(F, read, R),
dbload_add_facts(R, M),
close(R),
fail.
load_facts.
dbload_add_facts(R, M) :-
repeat,
2013-01-09 09:21:07 +00:00
catch(read(R,T), _, fail),
2011-04-30 01:16:40 +01:00
( T = end_of_file -> !;
dbload_add_fact(T, M),
2016-10-20 04:44:59 +01:00
fail
2011-04-30 01:16:40 +01:00
).
dbload_add_fact(T0, M0) :-
2019-04-20 12:48:33 +01:00
'$yap_strip_module'(M0:T0,M,T),
2011-04-30 01:16:40 +01:00
functor(T,Na,Arity),
dbloading(Na,Arity,M,_,NaAr,Handle),
nb_getval(NaAr,I0),
I is I0+1,
nb_setval(NaAr,I),
2019-04-20 12:48:33 +01:00
prolog:'$dbassert'(T,Handle,I0).
2013-01-07 09:47:14 +00:00
load_exofacts :-
retract(dbloading(Na,Arity,M,T,NaAr,_)),
nb_getval(NaAr,Size),
exo_db_get_space(T, M, Size, Handle),
assertz(dbloading(Na,Arity,M,T,NaAr,Handle)),
nb_setval(NaAr,0),
fail.
2013-01-09 09:21:07 +00:00
load_exofacts :-
2013-01-07 09:47:14 +00:00
dbprocess(F, M),
open(F, read, R),
exodb_add_facts(R, M),
close(R),
fail.
2013-01-09 09:21:07 +00:00
load_exofacts.
2013-01-07 09:47:14 +00:00
exodb_add_facts(R, M) :-
repeat,
2013-04-30 21:23:01 +01:00
catch(protected_exodb_add_fact(R, M), _, fail),
!.
protected_exodb_add_fact(R, M) :-
repeat,
read(R,T),
( T == end_of_file -> !;
2013-01-07 09:47:14 +00:00
exodb_add_fact(T, M),
2016-10-20 04:44:59 +01:00
fail
2013-01-07 09:47:14 +00:00
).
exodb_add_fact(T0, M0) :-
2019-04-20 12:48:33 +01:00
'$yap_strip_module'(T0,M0,T,M),
2013-01-07 09:47:14 +00:00
functor(T,Na,Arity),
dbloading(Na,Arity,M,_,NaAr,Handle),
nb_getval(NaAr,I0),
I is I0+1,
nb_setval(NaAr,I),
exoassert(T,Handle,I0).
2011-04-30 01:16:40 +01:00
clean_up :-
retractall(dbloading(_,_,_,_,_,_)),
retractall(dbprocess(_,_)),
fail.
2017-04-07 23:10:59 +01:00
2011-04-30 01:16:40 +01:00
clean_up.
2017-04-07 23:10:59 +01:00
2017-04-13 21:42:34 +01:00
%% @}