1/*  Part of Extended Tools for SWI-Prolog
    2
    3    Author:        Edison Mera
    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/2,
   38           database_fact/3,
   39           database_fact_ort/4,
   40           database_def_fact/2,
   41           database_mod_fact/2,
   42           database_use_fact/2,
   43           clause_head/2,
   44           fa_to_head/3
   45          ]).   46
   47:- use_module(library(lists)).   48:- use_module(library(assertions)).   49:- use_module(library(plprops)).   50:- use_module(library(extend_args)).   51:- use_module(library(static_strip_module)).   52:- use_module(library(persistency), []).   53:- use_module(library(interface)).   54:- init_expansors.   55
   56:- create_prolog_flag(check_database_preds, false, [type(boolean)]).   57
   58% help analyzers to track indirect calls via prolog database manipulation:
   59%
   60
   61prolog:called_by(H, IM, CM, [F]) :-
   62    current_prolog_flag(check_database_preds, true),
   63    \+ is_meta(IM:H),
   64    database_use_fact(IM:H, F),
   65    static_strip_module(F, CM, C, M),
   66    callable(C),
   67    nonvar(M).
   68
   69is_meta(G) :-
   70    predicate_property(G, meta_predicate(Meta)),
   71    arg(_, Meta, S),
   72    integer(S).
   73
   74:- multifile
   75    database_def_fact/3,
   76    database_dec_fact/3,
   77    database_retract_fact/3,
   78    database_query_fact/3.   79
   80:- meta_predicate
   81    database_fact(0),
   82    database_fact(0, -).   83
   84database_fact(MG) :-
   85    database_fact(MG, _).
   86database_fact(MG) :-
   87    prop_asr(head, MG, _, Asr),
   88    prop_asr(glob, database(_), _, Asr).
   89
   90database_mod_fact(M:G, F) :- database_def_fact(    G, M, F).
   91database_mod_fact(M:G, F) :- database_dec_fact(    G, M, F).
   92database_mod_fact(M:G, F) :- database_retract_fact(G, M, F).
   93
   94database_use_fact(M:G, F) :- database_query_fact(  G, M, F).
   95database_use_fact(M:G, F) :- database_retract_fact(G, M, F).
   96
   97clause_head(A,          A) :- var(A), !.
   98clause_head(M:A,        M:A) :- var(A), !.
   99clause_head((A :- _),   A) :- !.
  100clause_head(M:(A :- _), M:A) :- !.
  101clause_head(A,          A).
  102
  103database_fact(def, Goal, Fact) :- database_def_fact(Goal, Fact).
  104database_fact(dec, Goal, Fact) :- database_dec_fact(Goal, Fact).
  105database_fact(use, Goal, Fact) :- database_use_fact(Goal, Fact).
  106database_fact(mod, Goal, Fact) :- database_mod_fact(Goal, Fact).
  107
  108% ortogonal operations:
  109database_fact_ort(def,     G, M, F) :- database_def_fact(G, M, F).
  110database_fact_ort(dec,     G, M, F) :- database_dec_fact(G, M, F).
  111database_fact_ort(retract, G, M, F) :- database_retract_fact(G, M, F).
  112database_fact_ort(query,   G, M, F) :- database_query_fact(G, M, F).
  113
  114database_fact(M:G, F) :-
  115    predicate_property(M:G, implementation_module(IM)),
  116    database_fact_ort(_, G, IM, F).
  117
  118database_def_fact(M:H, F) :- database_def_fact(H, M, F).
  119
  120database_def_fact(bind_interface(Intf, Impl), interface, Intf:H) :-
  121    interface:'$interface'(Intf, DIL),
  122    interface:'$implementation'(Impl, Intf),
  123    member(F/A, DIL),
  124    functor(H, F, A).
  125
  126database_def_fact(asserta_with_names(A, _),  ifprolog,   F) :- clause_head(A, F).
  127database_def_fact(assertz_with_names(A, _),  ifprolog,   F) :- clause_head(A, F).
  128database_def_fact(lasserta(A),               pce_config, F) :- clause_head(A, F).
  129database_def_fact(assert_cyclic(A),          plunit,     F) :- clause_head(A, F).
  130database_def_fact(assert(A),                 system,     F) :- clause_head(A, F).
  131database_def_fact(assert(A, _),              system,     F) :- clause_head(A, F).
  132database_def_fact(asserta(A),                system,     F) :- clause_head(A, F).
  133database_def_fact(asserta(A, _),             system,     F) :- clause_head(A, F).
  134database_def_fact(assertz(A),                system,     F) :- clause_head(A, F).
  135database_def_fact(assertz(A, _),             system,     F) :- clause_head(A, F).
  136database_def_fact(update_fact_from(A, From), from_utils, F) :-
  137    nonvar(A),
  138    extend_args(A, [From], H),
  139    clause_head(H, F).
  140database_def_fact(PAssert, M, Fact) :-
  141    persistency:persistent(M, Fact, _),
  142    functor(Fact, Name, Arity),
  143    member(Prefix, [assert_, asserta_]),
  144    atom_concat(Prefix, Name, PName),
  145    functor(PAssert, PName, Arity).
  146
  147database_dec_fact(M:H, F) :- database_dec_fact(H, M, F).
  148
  149database_dec_fact(abolish(F, A),             system,     H) :- fa_to_head(F, A, H).
  150database_dec_fact(abolish(PI),               system,     H) :- pi_to_head(PI, H).
  151database_dec_fact(retractall(F),             system,     F).
  152database_dec_fact(retractall_near(F),        near_utils, F).
  153database_dec_fact(forall(A, B),              system,     F) :-
  154    subsumes_term(forall(retract(F), true), forall(A, B)),
  155    A=retract(F).
  156database_dec_fact(\+ A,  system,     F) :-
  157    subsumes_term((retract(F), \+ true), A),
  158    A = (retract(F), \+ true).
  159database_dec_fact(PRetractall, M, Fact) :-
  160    persistency:persistent(M, Fact, _),
  161    functor(Fact, Name, Arity),
  162    atom_concat(retractall_, Name, PName),
  163    functor(PRetractall, PName, Arity).
  164
  165database_retract_fact(retract(A),      system,     F) :- clause_head(A, F).
  166database_retract_fact(retract_near(A), near_utils, F) :- clause_head(A, F).
  167database_retract_fact(lretract(A),     pce_config, F) :- clause_head(A, F).
  168database_retract_fact(PRetract, M, Fact) :-
  169    persistency:persistent(M, Fact, _),
  170    functor(Fact, Name, Arity),
  171    atom_concat(retract_, Name, PName),
  172    functor(PRetract, PName, Arity).
  173
  174database_query_fact(clause(A, _),       system,     F) :- clause_head(A, F).
  175database_query_fact(clause(A, _, _),    system,     F) :- clause_head(A, F).
  176database_query_fact(unfold_goal(_,A,_), refactor,   F) :- clause_head(A, F).
  177database_query_fact(fact_near(A),       near_utils, F) :- clause_head(A, F).
  178database_query_fact(fact_near(A, _),    near_utils, F) :- clause_head(A, F).
  179
  180pi_to_head(PI, H) :- nonvar(PI) -> PI=F/A, fa_to_head(F, A, H) ; true.
  181
  182fa_to_head(M:F, A, M:H) :- atomic(M) -> fa_to_head_(F, A, H), !.
  183fa_to_head(F,   A, H) :- fa_to_head_(F, A, H).
  184
  185fa_to_head_(F, A, H) :- atomic(F), integer(A) -> functor(H, F, A) ; true