1/*  Part of Extended Tools for SWI-Prolog
    2
    3    Author:        Edison Mera Menendez
    4    E-mail:        efmera@gmail.com
    5    WWW:           https://github.com/edisonm/xtools
    6    Copyright (C): 2015, Process Design Center, Breda, The Netherlands.
    7    All rights reserved.
    8
    9    Redistribution and use in source and binary forms, with or without
   10    modification, are permitted provided that the following conditions
   11    are met:
   12
   13    1. Redistributions of source code must retain the above copyright
   14       notice, this list of conditions and the following disclaimer.
   15
   16    2. Redistributions in binary form must reproduce the above copyright
   17       notice, this list of conditions and the following disclaimer in
   18       the documentation and/or other materials provided with the
   19       distribution.
   20
   21    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   22    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   23    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   24    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   25    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   26    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   27    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   28    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   29    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   30    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   31    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   32    POSSIBILITY OF SUCH DAMAGE.
   33*/
   34
   35:- module(database_fact,
   36          [database_fact/1,
   37           database_fact/3,
   38           database_fact_ort/4,
   39           database_def_fact/2,
   40           database_mod_fact/2,
   41           database_use_fact/2,
   42           clause_head/2,
   43           fa_to_head/3
   44          ]).   45
   46:- use_module(library(lists)).   47:- use_module(library(assertions)).   48:- use_module(library(plprops)).   49:- use_module(library(extend_args)).   50:- use_module(library(static_strip_module)).   51:- use_module(library(persistency), []).   52
   53:- create_prolog_flag(check_database_preds, false, [type(boolean)]).   54
   55% help analyzers to track indirect calls via prolog database manipulation:
   56%
   57
   58prolog:called_by(H, IM, CM, [F]) :-
   59    current_prolog_flag(check_database_preds, true),
   60    \+ is_meta(IM:H),
   61    database_use_fact(IM:H, F),
   62    static_strip_module(F, CM, C, M),
   63    callable(C),
   64    nonvar(M).
   65
   66is_meta(G) :-
   67    predicate_property(G, meta_predicate(Meta)),
   68    arg(_, Meta, S),
   69    integer(S).
   70
   71:- multifile
   72    database_def_fact/3,
   73    database_dec_fact/3,
   74    database_retract_fact/3,
   75    database_query_fact/3.   76
   77:- meta_predicate
   78    database_fact(0).   79
   80database_fact(M:G) :-
   81    predicate_property(M:G, implementation_module(IM)),
   82    database_fact(IM:G, _).
   83database_fact(MG) :-
   84    prop_asr(head, MG, _, Asr),
   85    prop_asr(glob, database(_), _, Asr).
   86
   87database_mod_fact(M:G, F) :- database_def_fact(    G, M, F).
   88database_mod_fact(M:G, F) :- database_dec_fact(    G, M, F).
   89database_mod_fact(M:G, F) :- database_retract_fact(G, M, F).
   90
   91database_use_fact(M:G, F) :- database_query_fact(  G, M, F).
   92database_use_fact(M:G, F) :- database_retract_fact(G, M, F).
   93
   94clause_head(A,          A) :- var(A), !.
   95clause_head(M:A,        M:A) :- var(A), !.
   96clause_head((A :- _),   A) :- !.
   97clause_head(M:(A :- _), M:A) :- !.
   98clause_head(A,          A).
   99
  100database_fact(def, Goal, Fact) :- database_def_fact(Goal, Fact).
  101database_fact(dec, Goal, Fact) :- database_dec_fact(Goal, Fact).
  102database_fact(use, Goal, Fact) :- database_use_fact(Goal, Fact).
  103database_fact(mod, Goal, Fact) :- database_mod_fact(Goal, Fact).
  104
  105% ortogonal operations:
  106database_fact_ort(def,     G, M, F) :- database_def_fact(G, M, F).
  107database_fact_ort(dec,     G, M, F) :- database_dec_fact(G, M, F).
  108database_fact_ort(retract, G, M, F) :- database_retract_fact(G, M, F).
  109database_fact_ort(query,   G, M, F) :- database_query_fact(G, M, F).
  110
  111database_fact(M:G, F) :- database_fact_ort(_, G, M, F).
  112
  113database_def_fact(M:H, F) :- database_def_fact(H, M, F).
  114
  115database_def_fact(bind_interface(Intf, Impl), interface, Intf:H) :-
  116    interface:'$interface'(Intf, DIL),
  117    interface:'$implementation'(Impl, Intf),
  118    member(F/A, DIL),
  119    functor(H, F, A).
  120
  121database_def_fact(asserta_with_names(A, _),  ifprolog,   F) :- clause_head(A, F).
  122database_def_fact(assertz_with_names(A, _),  ifprolog,   F) :- clause_head(A, F).
  123database_def_fact(lasserta(A),               pce_config, F) :- clause_head(A, F).
  124database_def_fact(assert_cyclic(A),          plunit,     F) :- clause_head(A, F).
  125database_def_fact(assert(A),                 system,     F) :- clause_head(A, F).
  126database_def_fact(assert(A, _),              system,     F) :- clause_head(A, F).
  127database_def_fact(asserta(A),                system,     F) :- clause_head(A, F).
  128database_def_fact(asserta(A, _),             system,     F) :- clause_head(A, F).
  129database_def_fact(assertz(A),                system,     F) :- clause_head(A, F).
  130database_def_fact(assertz(A, _),             system,     F) :- clause_head(A, F).
  131database_def_fact(update_fact_from(A, From), from_utils, F) :-
  132    nonvar(A),
  133    extend_args(A, [From], H),
  134    clause_head(H, F).
  135database_def_fact(PAssert, M, Fact) :-
  136    persistency:persistent(M, Fact, _),
  137    functor(Fact, Name, Arity),
  138    member(Prefix, [assert_, asserta_]),
  139    atom_concat(Prefix, Name, PName),
  140    functor(PAssert, PName, Arity).
  141
  142database_dec_fact(M:H, F) :- database_dec_fact(H, M, F).
  143
  144database_dec_fact(abolish(F, A),             system,     H) :- fa_to_head(F, A, H).
  145database_dec_fact(abolish(PI),               system,     H) :- pi_to_head(PI, H).
  146database_dec_fact(retractall(F),             system,     F).
  147database_dec_fact(retractall_near(F),        near_utils, F).
  148database_dec_fact(forall(A, B),              system,     F) :-
  149    subsumes_term(forall(retract(F), true), forall(A, B)),
  150    A=retract(F).
  151database_dec_fact(\+ A,  system,     F) :-
  152    subsumes_term((retract(F), \+ true), A),
  153    A = (retract(F), \+ true).
  154database_dec_fact(PRetractall, M, Fact) :-
  155    persistency:persistent(M, Fact, _),
  156    functor(Fact, Name, Arity),
  157    atom_concat(retractall_, Name, PName),
  158    functor(PRetractall, PName, Arity).
  159
  160database_retract_fact(retract(A),  system,     F) :- clause_head(A, F).
  161database_retract_fact(lretract(A), pce_config, F) :- clause_head(A, F).
  162database_retract_fact(PRetract, M, Fact) :-
  163    persistency:persistent(M, Fact, _),
  164    functor(Fact, Name, Arity),
  165    atom_concat(retract_, Name, PName),
  166    functor(PRetract, PName, Arity).
  167
  168database_query_fact(clause(A, _),       system,     F) :- clause_head(A, F).
  169database_query_fact(clause(A, _, _),    system,     F) :- clause_head(A, F).
  170database_query_fact(unfold_goal(_,A,_), refactor,   F) :- clause_head(A, F).
  171database_query_fact(fact_near(A, _),    near_utils, F) :- clause_head(A, F).
  172
  173pi_to_head(PI, H) :- nonvar(PI) -> PI=F/A, fa_to_head(F, A, H) ; true.
  174
  175fa_to_head(M:F, A, M:H) :- atomic(M) -> fa_to_head_(F, A, H), !.
  176fa_to_head(F,   A, H) :- fa_to_head_(F, A, H).
  177
  178fa_to_head_(F, A, H) :- atomic(F), integer(A) -> functor(H, F, A) ; true