/*
    The contents of this file are subject to the Mozilla Public License
    Version  1.1  (the "License"); you may not use this file except in
    compliance with the License. You may obtain a copy of the License at:

    http://www.mozilla.org/MPL/

    Software  distributed  under  the License is distributed on an "AS
    IS"  basis,  WITHOUT  WARRANTY  OF  ANY  KIND,  either  express or
    implied.  See  the  License  for  the  specific language governing
    rights and limitations under the License.
    The Original Code is the contents of this file.
    The  Initial  Developer  of  the  Original  Code is SICS, Swedish
    Institute of Computer Science AB (SICS).
    Portions  created  by the Initial Developer are Copyright (C) 2007
    of the Initial Developer. All Rights Reserved.
    Contributor(s):
    _____Mats Carlsson <matsc@sics.se>
    _____Nicolas Beldiceanu <Nicolas.Beldiceanu@emn.fr>

    Alternatively, if the contents of this file is included as a part of
    SICStus Prolog distribution by SICS, it may be used under the terms of
    an appropriate SICStus Prolog License Agreement (the "SICStus Prolog
    License"), in which case the provisions of the SICStus Prolog License
    are applicable instead of those above.
*/

:- multifile
    ctr_predefined/1,
    ctr_date/2,
    ctr_persons/2,
    ctr_origin/3,
    ctr_usual_name/2,
    ctr_synonyms/2,
    ctr_types/2,
    ctr_arguments/2,
    ctr_exchangeable/2,
    ctr_restrictions/2,
    ctr_typical/2,
    ctr_pure_functional_dependency/2,
    ctr_functional_dependency/3,
    ctr_contractible/4,
    ctr_extensible/4,
    ctr_aggregate/3,
    ctr_example/2,
    ctr_draw_example/9,
    ctr_cond_imply/5,
    ctr_see_also/2,
    ctr_key_words/2,
    ctr_derived_collections/2,
    ctr_graph/7,
    ctr_graph/9,
    ctr_eval/2,
    ctr_sol/6,
    ctr_logic/3,
    ctr_application/2.

ctr_date(elem,['20030820','20040530','20060808']).

ctr_origin(elem, 'Derived from %c.', [element]).

ctr_usual_name(elem, element).

ctr_arguments(elem,
              ['ITEM'-collection(index-dvar,value-dvar),
               'TABLE'-collection(index-int,value-dvar)]).

ctr_exchangeable(elem,
                 [items('TABLE',all),
                  vals(['ITEM'^value,'TABLE'^value],int,=\=,all,dontcare)]).

ctr_synonyms(elem,[nth, array]).

ctr_restrictions(elem,
                 [required('ITEM',[index,value]) ,
                  'ITEM'^index >= 1              ,
                  'ITEM'^index =< size('TABLE')  ,
                  size('ITEM') = 1               ,
                  size('TABLE') > 0              ,
                  required('TABLE',[index,value]),
                  'TABLE'^index >= 1             ,
                  'TABLE'^index =< size('TABLE') ,
                  distinct('TABLE',index)        ]).

ctr_typical(elem,
            [size('TABLE')        > 1,
             range('TABLE'^value) > 1]).

ctr_pure_functional_dependency(elem, []).
ctr_functional_dependency(elem, 1-2, [1-1,2]).

ctr_graph(elem,
          ['ITEM','TABLE'],
          2,
          ['PRODUCT'>>collection(item,table)],
          [item^index = table^index,
           item^value = table^value],
          ['NARC' = 1],
          []).

ctr_example(elem,
            elem([[index-3, value-2]],
                 [[index-1, value-6],
                  [index-2, value-9],
                  [index-3, value-2],
                  [index-4, value-9]])).

ctr_draw_example(elem,
                 ['ITEM','TABLE'],
                 [[[index-3, value-2]],
                  [[index-1, value-6],
                   [index-2, value-9],
                   [index-3, value-2],
                   [index-4, value-9]]],
                 ['PRODUCT'],
                 [1-3],
                 ['NARC'],
                 '','NARC=1',
                 [2.145,2.145,1,1]).

ctr_cond_imply(elem, bin_packing_capa, ['TABLE'^value >= 0], [], ['TABLE','ITEM']).

ctr_see_also(elem,
 [link('implies',                   element,               'single item replaced by two variables', []),
  link('implied by',                element,               '',                                      []),
  link('implies',                   element_greatereq,     '',                                      []),
  link('implies',                   element_lesseq,        '',                                      []),
  link('implies',                   elements,              '',                                      []),
  link('common keyword',            element_sparse,        '%k',                                    ['array constraint']),
  link('common keyword',            element_matrix,        '%k',                                    ['array constraint']),
  link('common keyword',            element_product,       '%k',                                    ['array constraint']),
  link('common keyword',            elem_from_to,          '%k',                                    ['array constraint']),
  link('common keyword',            elements_sparse,       '%k',                                    ['data constraint']),
  link('common keyword',            stage_element,         '%k',                                    ['data constraint']),
  link('system of constraints',     elements,              '',                                      []),
  link('uses in its reformulation', elements_alldifferent, '',                                      [])]).

ctr_key_words(elem,['array constraint'                        ,
                    'data constraint'                         ,
                    'table'                                   ,
                    'functional dependency'                   ,
		    'pure functional dependency'              ,
                    'variable indexing'                       ,
                    'variable subscript'                      ,
                    'disjunction'                             ,
                    'assignment to the same set of values'    ,
                    'sequence dependent set-up'               ,
                    'automaton'                               ,
                    'automaton without counters'              ,
                    'reified automaton constraint'            ,
                    'centered cyclic(2) constraint network(1)',
                    'arc-consistency'                         ,
                    'zebra puzzle'                            ,
                    'labelling by increasing cost'            ,
                    'regret based heuristics'                 ]).

ctr_persons(elem,['Hardy G. H.' ,
                  'Wright E. M.']).

ctr_eval(elem, [builtin(elem_b), automaton(elem_a)]).

elem_b(ITEM, TABLE) :-
    length(ITEM , 1),
    length(TABLE, N),
    collection(ITEM , [dvar(1,N),dvar]),
    collection(TABLE, [ int(1,N),dvar]),
    sort_collection(TABLE, index, SORTED_TABLE),
    get_attr1(SORTED_TABLE, INDEXES),
    increasing_values(INDEXES),
    get_attr2(SORTED_TABLE, VALUES),
    get_attr1(ITEM, [INDEX]),
    get_attr2(ITEM, [VALUE]),
    element(INDEX, VALUES, VALUE).

% 0: ITEM_INDEX=\=TABLE_INDEX or  ITEM_VALUE=\=TABLE_VALUE
% 1: ITEM_INDEX = TABLE_INDEX and ITEM_VALUE = TABLE_VALUE
elem_a(FLAG, ITEM, TABLE) :-
    length(ITEM , 1),
    length(TABLE, N),
    collection(ITEM , [dvar(1,N),dvar]),
    collection(TABLE, [ int(1,N),dvar]),
    get_attr1(TABLE, INDEXES),
    all_different(INDEXES),
    ITEM = [[index-ITEM_INDEX, value-ITEM_VALUE]],
    elem_signature(TABLE, SIGNATURE, ITEM_INDEX, ITEM_VALUE),
    AUTOMATON = automaton(SIGNATURE, _,
                          SIGNATURE, 
                          [source(s),sink(t)],
                          [arc(s,0,s),
                           arc(s,1,t),
                           arc(t,0,t),
                           arc(t,1,t)],
                          [],[],[]),
    automaton_bool(FLAG, [0,1], AUTOMATON).

elem_signature([], [], _, _).
elem_signature([[index-TABLE_INDEX,value-TABLE_VALUE]|TABLEs], [S|Ss], ITEM_INDEX, ITEM_VALUE) :-
    ((ITEM_INDEX #= TABLE_INDEX) #/\ (ITEM_VALUE #= TABLE_VALUE)) #<=> S,
    elem_signature(TABLEs, Ss, ITEM_INDEX, ITEM_VALUE).
