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      do_with_value(Goal, Name, OldValue, NewValue)
   96    ; call_cleanup(
   97          do_with_value(Goal, Name, [], NewValue),
   98          nb_delete(Name))
   99    ).
  100
  101do_with_value(Goal, Name, OldValue, NewValue) :-
  102    b_setval(Name, NewValue),
  103    Goal,
  104    b_setval(Name, OldValue).
  105
  106:- meta_predicate without_context_value(0, :).  107without_context_value(Goal, Name) :-
  108    without_context_value(Goal, Name, _).
  109
  110:- meta_predicate without_context_value(0, :, ?).  111without_context_value(Goal, Name, Value) :-
  112    context_name(Name, ContextName),
  113    without_value(Goal, ContextName, Value).
  114
  115without_value(Goal, Name, Value) :-
  116    ( nb_current(Name, Value)
  117    ->setup_call_cleanup(nb_delete(Name),
  118                         ( Goal,
  119                           b_setval(Name, Value)
  120                         ),
  121                         nb_setval(Name, Value))
  122    ; Goal
  123    ).
  124
  125update_value(Name, OldValue1, NewValue, Cleanup) :-
  126    ( nb_current(Name, OldValue)
  127    ->OldValue1 = OldValue,
  128      Cleanup = set(Name, OldValue)
  129    ; Cleanup = del(Name)
  130    ),
  131    b_setval(Name, NewValue).
  132
  133cleanup(set(Name, OldValue)) :- b_setval(Name, OldValue).
  134cleanup(del(Name)) :- nb_delete(Name).
  135
  136:- meta_predicate with_values(0, +, ?, +).  137with_values(Goal, Names, OldValues, NewValues) :-
  138    maplist(update_value, Names, OldValues, NewValues, Cleanups),
  139    Goal,
  140    maplist(cleanup, Cleanups).
  141
  142:- meta_predicate with_context_values(0, :, ?, +).  143with_context_values(Goal, M:Names, OldValues, NewValues) :-
  144    maplist(context_name(M), Names, ContextNames),
  145    with_values(Goal, ContextNames, OldValues, NewValues).
  146
  147with_values(Goal, Names, Values) :- with_values_(Names, Values, Goal).
  148
  149with_values_([], [], Goal) :- Goal.
  150with_values_([Name|Names], [Value|Values], Goal) :-
  151    with_values_(Names, Values, with_value(Goal, Name, Value)).
  152
  153:- meta_predicate with_context_values(0, :, +).  154with_context_values(Goal, M:Names, Values) :-
  155    maplist(context_name(M), Names, ContextNames),
  156    with_values(Goal, ContextNames, Values)