1/*  Part of Extended Libraries for SWI-Prolog
    2
    3    Author:        Edison Mera
    4    E-mail:        efmera@gmail.com
    5    WWW:           https://github.com/edisonm/xlibrary
    6    Copyright (C): 2014, 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(context_values,
   36          [context_name/2,
   37           with_value/3,
   38           with_value/4,
   39           get_context_value/2,
   40           set_context_value/2,
   41           nb_set_context_value/2,
   42           current_context_value/2,
   43           with_context_values/3,
   44           with_context_values/4,
   45           with_context_value/3,
   46           with_context_value/4,
   47           without_context_value/2,
   48           without_context_value/3
   49          ]).   50
   51context_name(M:Name, ContextName) :-
   52    context_name(M, Name, ContextName).
   53
   54context_name(M, Name, ContextName) :-
   55    atomic_list_concat([M, Name], ':', ContextName).
   56
   57:- meta_predicate get_context_value(:, ?).   58get_context_value(Name, Value) :-
   59    context_name(Name, ContextName),
   60    b_getval(ContextName, Value).
   61
   62:- meta_predicate set_context_value(:, ?).   63set_context_value(Name, Value) :-
   64    context_name(Name, ContextName),
   65    b_setval(ContextName, Value).
   66
   67:- meta_predicate nb_set_context_value(:, ?).   68nb_set_context_value(Name, Value) :-
   69    context_name(Name, ContextName),
   70    nb_setval(ContextName, Value).
   71
   72:- meta_predicate current_context_value(:, ?).   73current_context_value(Name, Value) :-
   74    context_name(Name, ContextName),
   75    nb_current(ContextName, Value).
   76
   77:- meta_predicate with_value(0, +, +).   78with_value(Goal, Name, NewValue) :-
   79    with_value(Goal, Name, _, NewValue).
   80
   81:- meta_predicate with_context_value(0, :, +).   82with_context_value(Goal, Name, Value) :-
   83    context_name(Name, ContextName),
   84    with_value(Goal, ContextName, Value).
   85
   86:- meta_predicate with_context_value(0, :, ?, +).   87with_context_value(Goal, Name, OldValue, NewValue) :-
   88    context_name(Name, ContextName),
   89    with_value(Goal, ContextName, OldValue, NewValue).
   90
   91:- meta_predicate with_value(0, +, ?, +).   92with_value(Goal, Name, OldValue1, NewValue) :-
   93    ( nb_current(Name, OldValue)
   94    ->OldValue1 = OldValue,
   95      b_setval(Name, NewValue),
   96      Goal,
   97      b_setval(Name, OldValue)
   98    ; b_setval(Name, NewValue),
   99      Goal,
  100      nb_delete(Name)
  101    ).
  102
  103:- meta_predicate without_context_value(0, :).  104without_context_value(Goal, Name) :-
  105    without_context_value(Goal, Name, _).
  106
  107:- meta_predicate without_context_value(0, :, ?).  108without_context_value(Goal, Name, Value) :-
  109    context_name(Name, ContextName),
  110    without_value(Goal, ContextName, Value).
  111
  112without_value(Goal, Name, Value) :-
  113    ( nb_current(Name, Value)
  114    ->setup_call_cleanup(nb_delete(Name),
  115                         ( Goal,
  116                           b_setval(Name, Value)
  117                         ),
  118                         nb_setval(Name, Value))
  119    ; Goal
  120    ).
  121
  122update_value(Name, OldValue1, NewValue, Cleanup) :-
  123    ( nb_current(Name, OldValue)
  124    ->OldValue1 = OldValue,
  125      Cleanup = set(Name, OldValue)
  126    ; Cleanup = del(Name)
  127    ),
  128    b_setval(Name, NewValue).
  129
  130cleanup(set(Name, OldValue)) :- b_setval(Name, OldValue).
  131cleanup(del(Name)) :- nb_delete(Name).
  132
  133:- meta_predicate with_values(0, +, ?, +).  134with_values(Goal, Names, OldValues, NewValues) :-
  135    maplist(update_value, Names, OldValues, NewValues, Cleanups),
  136    Goal,
  137    maplist(cleanup, Cleanups).
  138
  139:- meta_predicate with_context_values(0, :, ?, +).  140with_context_values(Goal, M:Names, OldValues, NewValues) :-
  141    maplist(context_name(M), Names, ContextNames),
  142    with_values(Goal, ContextNames, OldValues, NewValues).
  143
  144with_values(Goal, Names, Values) :- with_values_(Names, Values, Goal).
  145
  146with_values_([], [], Goal) :- Goal.
  147with_values_([Name|Names], [Value|Values], Goal) :-
  148    with_values_(Names, Values, with_value(Goal, Name, Value)).
  149
  150:- meta_predicate with_context_values(0, :, +).  151with_context_values(Goal, M:Names, Values) :-
  152    maplist(context_name(M), Names, ContextNames),
  153    with_values(Goal, ContextNames, Values)