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
   51:- use_module(library(apply)).   52
   53context_name(M:Name, ContextName) :-
   54    context_name(M, Name, ContextName).
   55
   56context_name(M, Name, ContextName) :-
   57    atomic_list_concat([M, Name], ':', ContextName).
   58
   59:- meta_predicate get_context_value(:, ?).   60get_context_value(Name, Value) :-
   61    context_name(Name, ContextName),
   62    b_getval(ContextName, Value).
   63
   64:- meta_predicate set_context_value(:, ?).   65set_context_value(Name, Value) :-
   66    context_name(Name, ContextName),
   67    b_setval(ContextName, Value).
   68
   69:- meta_predicate nb_set_context_value(:, ?).   70nb_set_context_value(Name, Value) :-
   71    context_name(Name, ContextName),
   72    nb_setval(ContextName, Value).
   73
   74:- meta_predicate current_context_value(:, ?).   75current_context_value(Name, Value) :-
   76    context_name(Name, ContextName),
   77    nb_current(ContextName, Value).
   78
   79:- meta_predicate with_value(0, +, +).   80with_value(Goal, Name, NewValue) :-
   81    with_value(Goal, Name, _, NewValue).
   82
   83:- meta_predicate with_context_value(0, :, +).   84with_context_value(Goal, Name, Value) :-
   85    context_name(Name, ContextName),
   86    with_value(Goal, ContextName, Value).
   87
   88:- meta_predicate with_context_value(0, :, ?, +).   89with_context_value(Goal, Name, OldValue, NewValue) :-
   90    context_name(Name, ContextName),
   91    with_value(Goal, ContextName, OldValue, NewValue).
   92
   93:- meta_predicate with_value(0, +, ?, +).   94with_value(Goal, Name, OldValue1, NewValue) :-
   95    ( nb_current(Name, OldValue)
   96    ->OldValue1 = OldValue,
   97      do_with_value(Goal, Name, OldValue, NewValue)
   98    ; call_cleanup(
   99          do_with_value(Goal, Name, [], NewValue),
  100          nb_delete(Name))
  101    ).
  102
  103do_with_value(Goal, Name, OldValue, NewValue) :-
  104    b_setval(Name, NewValue),
  105    Goal,
  106    b_setval(Name, OldValue).
  107
  108:- meta_predicate without_context_value(0, :).  109without_context_value(Goal, Name) :-
  110    without_context_value(Goal, Name, _).
  111
  112:- meta_predicate without_context_value(0, :, ?).  113without_context_value(Goal, Name, Value) :-
  114    context_name(Name, ContextName),
  115    without_value(Goal, ContextName, Value).
  116
  117without_value(Goal, Name, Value) :-
  118    ( nb_current(Name, Value)
  119    ->setup_call_cleanup(nb_delete(Name),
  120                         ( Goal,
  121                           b_setval(Name, Value)
  122                         ),
  123                         nb_setval(Name, Value))
  124    ; Goal
  125    ).
  126
  127update_value(Name, OldValue1, NewValue, Cleanup) :-
  128    ( nb_current(Name, OldValue)
  129    ->OldValue1 = OldValue,
  130      Cleanup = set(Name, OldValue)
  131    ; Cleanup = del(Name)
  132    ),
  133    b_setval(Name, NewValue).
  134
  135cleanup(set(Name, OldValue)) :- b_setval(Name, OldValue).
  136cleanup(del(Name)) :- nb_delete(Name).
  137
  138:- meta_predicate with_values(0, +, ?, +).  139with_values(Goal, Names, OldValues, NewValues) :-
  140    maplist(update_value, Names, OldValues, NewValues, Cleanups),
  141    Goal,
  142    maplist(cleanup, Cleanups).
  143
  144:- meta_predicate with_context_values(0, :, ?, +).  145with_context_values(Goal, M:Names, OldValues, NewValues) :-
  146    maplist(context_name(M), Names, ContextNames),
  147    with_values(Goal, ContextNames, OldValues, NewValues).
  148
  149with_values(Goal, Names, Values) :- with_values_(Names, Values, Goal).
  150
  151with_values_([], [], Goal) :- Goal.
  152with_values_([Name|Names], [Value|Values], Goal) :-
  153    with_values_(Names, Values, with_value(Goal, Name, Value)).
  154
  155:- meta_predicate with_context_values(0, :, +).  156with_context_values(Goal, M:Names, Values) :-
  157    maplist(context_name(M), Names, ContextNames),
  158    with_values(Goal, ContextNames, Values)