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