1/*  Part of Extended libraries for Prolog
    2
    3    Author:        Edison Mera Menendez
    4    E-mail:        efmera@gmail.com
    5    WWW:           https://github.com/edisonm/xlibrary
    6    Copyright (C): 2014, Process Design Center, Breda, The Netherlands.
    7
    8    This program is free software; you can redistribute it and/or
    9    modify it under the terms of the GNU General Public License
   10    as published by the Free Software Foundation; either version 2
   11    of the License, or (at your option) any later version.
   12
   13    This program is distributed in the hope that it will be useful,
   14    but WITHOUT ANY WARRANTY; without even the implied warranty of
   15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   16    GNU General Public License for more details.
   17
   18    You should have received a copy of the GNU General Public
   19    License along with this library; if not, write to the Free Software
   20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
   21
   22    As a special exception, if you link this library with other files,
   23    compiled with a Free Software compiler, to produce an executable, this
   24    library does not by itself cause the resulting executable to be covered
   25    by the GNU General Public License. This exception does not however
   26    invalidate any other reasons why the executable file might be covered by
   27    the GNU General Public License.
   28*/
   29
   30:- module(context_values, [context_name/2,
   31			   with_value/3,
   32			   with_value/4,
   33			   get_context_value/2,
   34			   set_context_value/2,
   35			   current_context_value/2,
   36			   with_context_values/3,
   37			   with_context_values/4,
   38			   with_context_value/3,
   39			   with_context_value/4,
   40			   without_context_value/2,
   41			   without_context_value/3
   42			   ]).   43
   44context_name(M:Name, ContextName) :-
   45    context_name(M, Name, ContextName).
   46
   47context_name(M, Name, ContextName) :-
   48    atomic_list_concat([M, Name], ':', ContextName).
   49
   50:- meta_predicate get_context_value(:, ?).   51get_context_value(Name, Value) :-
   52    context_name(Name, ContextName),
   53    b_getval(ContextName, Value).
   54
   55:- meta_predicate set_context_value(:, ?).   56set_context_value(Name, Value) :-
   57    context_name(Name, ContextName),
   58    b_setval(ContextName, Value).
   59
   60:- meta_predicate current_context_value(:, ?).   61current_context_value(Name, Value) :-
   62    context_name(Name, ContextName),
   63    nb_current(ContextName, Value).
   64
   65:- meta_predicate with_value(0, +, +).   66with_value(Goal, Name, NewValue) :-
   67    with_value(Goal, Name, _, NewValue).
   68
   69:- meta_predicate with_context_value(0, :, +).   70with_context_value(Goal, Name, Value) :-
   71    context_name(Name, ContextName),
   72    with_value(Goal, ContextName, Value).
   73
   74:- meta_predicate with_context_value(0, :, ?, +).   75with_context_value(Goal, Name, OldValue, NewValue) :-
   76    context_name(Name, ContextName),
   77    with_value(Goal, ContextName, OldValue, NewValue).
   78
   79:- meta_predicate with_value(0, +, ?, +).   80with_value(Goal, Name, OldValue0, NewValue) :-
   81    ( nb_current(Name, OldValue) ->
   82      OldValue0 = OldValue,
   83      b_setval(Name, NewValue),
   84      Goal,
   85      b_setval(Name, OldValue)
   86    ; b_setval(Name, NewValue),
   87      Goal,
   88      nb_delete(Name)
   89    ).
   90
   91:- meta_predicate without_context_value(0, :).   92without_context_value(Goal, Name) :-
   93    without_context_value(Goal, Name, _).
   94
   95:- meta_predicate without_context_value(0, :, ?).   96without_context_value(Goal, Name, Value) :-
   97    context_name(Name, ContextName),
   98    without_value(Goal, ContextName, Value).
   99
  100without_value(Goal, Name, Value) :-
  101    ( nb_current(Name, Value) ->
  102      setup_call_cleanup(nb_delete(Name),
  103			 (Goal, nb_setval(Name, Value)),
  104			 nb_setval(Name, Value))
  105    ; Goal
  106    ).
  107
  108:- meta_predicate with_values(0, +, ?, +).  109with_values(Goal, Names, OldValues, NewValues) :-
  110    maplist(b_getval, Names, OldValues),
  111    maplist(b_setval, Names, NewValues),
  112    Goal,
  113    maplist(b_setval, Names, OldValues).
  114
  115:- meta_predicate with_context_values(0, :, ?, +).  116with_context_values(Goal, M:Names, OldValues, NewValues) :-
  117    maplist(context_name(M), Names, ContextNames),
  118    with_values(Goal, ContextNames, OldValues, NewValues).
  119
  120with_values(Goal, Names, Values) :- with_values_(Names, Values, Goal).
  121
  122with_values_([], [], Goal) :- Goal.
  123with_values_([Name|Names], [Value|Values], Goal) :-
  124    with_values_(Names, Values, with_value(Goal, Name, Value)).
  125
  126:- meta_predicate with_context_values(0, :, +).  127with_context_values(Goal, M:Names, Values) :-
  128    maplist(context_name(M), Names, ContextNames),
  129    with_values(Goal, ContextNames, Values)