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(from_utils, [from_to_file/2,
   36                       from_to_line/2,
   37                       from_to_module/2,
   38                       from_to_file_line_pos/5,
   39                       file_termpos_line/4,
   40                       update_fact_from/2,
   41                       subsumes_from/2]).   42
   43:- use_module(library(prolog_clause),   []).   44:- use_module(library(extra_messages),  []).   45:- use_module(library(assertions)).   46:- use_module(library(plprops)).   47:- use_module(library(extend_args)).   48:- use_module(library(filepos_line)).   49:- init_expansors.
 from_to_file_line_pos(+Loc, -File, -CLine, ?TLine, ?Pos) is semidet
   53from_to_file_line_pos(clause_term_position(ClauseRef, TermPos),
   54                      File, CLine, TLine, Pos) :-
   55    clause_property(ClauseRef, file(File)),
   56    clause_property(ClauseRef, line_count(CLine)),
   57    file_termpos_line(File, TermPos, TLine, Pos).
   58from_to_file_line_pos(clause(ClauseRef), File, CLine, _, _) :-
   59    clause_property(ClauseRef, file(File)),
   60    clause_property(ClauseRef, line_count(CLine)).
   61from_to_file_line_pos(file(File, Line, Pos, _), File, _, Line, Pos).
   62from_to_file_line_pos(file_term_position(File, TermPos), File, _, Line, Pos) :-
   63    file_termpos_line(File, TermPos, Line, Pos).
   64
   65file_termpos_line(File, TermPos, Line, Pos) :-
   66    ( compound(TermPos),
   67      arg(1, TermPos, CharCount),
   68      integer(CharCount)
   69    ->filepos_line(File, CharCount, Line, Pos)
   70    ; true
   71    ).
   72
   73subsumes_from(From1, From2) :-
   74    from_to_file_line_pos(From1, File1, CLine1, TLine1, Pos1),
   75    from_to_file_line_pos(From2, File2, CLine2, TLine2, Pos2),
   76    subsumes_term(flp(File1, CLine1, TLine1, Pos1),
   77                  flp(File2, CLine2, TLine2, Pos2)).
   78
   79from_to_file(clause_term_position(ClauseRef, _), File) :-
   80    clause_property(ClauseRef, file(File)).
   81from_to_file(clause(ClauseRef), File) :-
   82    clause_property(ClauseRef, file(File)).
   83from_to_file(file_term_position(File, _), File).
   84from_to_file(file(File, _, _, _), File).
   85
   86from_to_module(clause_term_position(ClauseRef, _), Module) :-
   87    clause_property(ClauseRef, module(Module)).
   88from_to_module(clause(ClauseRef), Module) :-
   89    clause_property(ClauseRef, module(Module)).
   90from_to_module(file_term_position(File, _), Module) :-
   91    module_property(Module, file(File)).
   92from_to_module(file(File, _, _, _), Module) :-
   93    module_property(Module, file(File)).
   94
   95from_to_line(clause_term_position(ClauseRef, _), Line) :-
   96    clause_property(ClauseRef, line_count(Line)).
   97from_to_line(clause(ClauseRef), Line) :-
   98    clause_property(ClauseRef, line_count(Line)).
   99from_to_line(file_term_position(File, TermPos), Line) :-
  100    file_termpos_line(File, TermPos, Line, _).
  101from_to_line(file(_, Line, _, _), Line).
  102
  103:- meta_predicate update_fact_from(1, ?).  104:- pred update_fact_from/2 + database.
  105update_fact_from(Fact, From) :-
  106    extend_args(Fact, [From1], FactFrom),
  107    functor(Fact, F, A),
  108    atomic_list_concat(['__mutex_', F, '/', A], Mutex),
  109    with_mutex(Mutex, update_fact_from(FactFrom, From1, From)).
  110
  111update_fact_from(FactFrom, From1, From) :-
  112    forall(( clause(FactFrom, _, Ref),
  113             subsumes_from(From1, From)
  114           ),
  115           erase(Ref)),
  116    ( \+ ( call(FactFrom),
  117           subsumes_from(From, From1)
  118         )
  119    ->From = From1,
  120      assertz(FactFrom)
  121    ; true
  122    )