/* ========================================================================== */
/*                          HANDLE NEGATIVE ASSUMPTIONS                       */
/* ========================================================================== */
/*  History                                                                   */
/*   - every example is represented by its sets of maximal and minimal        */
/*	  possibilities						                            */
/*     (8/02/2013)                                                            */
/* ========================================================================== */
   
:- module(
             assumptions2,
             [
               update_negative_examples/2,
               get_negative_assumptions/2,
               initialize_negative_assumptions/1,
               clear_negative_assumptions/1,
               update_negative_one_example/4
             ]
          ).

:- 
   getenv('LEAR_DIR', Lear),
   
   atom_concat(Lear, '/list.pl', List),    
   use_module(List),
   atom_concat(Lear, '/compatibility5.pl', Comp),
   use_module(Comp).
          
/* ============================ UPDATE ASSUMPTIONS ========================== */

 /* discard of the negative examples of LExp the interpretations that are models
    of the conjunction just added to the current set of rules */
update_negative_examples(_, []) :-
        !.

update_negative_examples(Hyp, [(Num_neg, Head_neg, _Body_neg)|LExp]) :-
        get_negative_assumptions(Num_neg, LInters),
        update_negative_one_example(Num_neg, Head_neg, Hyp, LInters),
        update_negative_examples(Hyp, LExp).


	/* discard of the negative example Exp the interpretations that are models
        of the clause Hyp */
update_negative_one_example(Num_neg, Head_neg, Hyp, LInters) :-
   update_assumptions(LInters, Num_neg, Head_neg, Hyp, LInters1),
   set_negative_assumptions(Num_neg, LInters1).
   
    
  /* update assumptions */ 
update_assumptions([], _Num_neg, _Head_neg, _Hyp, []) :- 
   !.						  % no more assumption to update
   
update_assumptions([Inter|LInters], Num_neg, Head_neg, [Hyp, Hypa], 
                   [Inter|LInters1]) :-
   compatible_neg(neg, Hyp, Num_neg, Head_neg, [Inter]),
   !,
   update_assumptions(LInters, Num_neg, Head_neg, [Hyp, Hypa], LInters1).
   
update_assumptions([_Inter|LInters], Num_neg, Head_neg, Hyp, LInters1) :-
   update_assumptions(LInters, Num_neg, Head_neg, Hyp, LInters1).
   
/* ========================== GET/SET ASSUMPTIONS =========================== */
                 
get_negative_assumptions(Number_neg, LAssumptions) :-
   atom_number(AN, Number_neg),
   atom_concat('negative_assumptions_', AN, Key),
   learn:recorded(Key, LAssumptions).
   
   
set_negative_assumptions(Number_neg, LAssumptions) :-
   atom_number(AN, Number_neg),
   atom_concat('negative_assumptions_', AN, Key),
   learn:recorded(Key, _, Reference),
   learn:erase(Reference),
   learn:recorda(Key, LAssumptions).
   
/* ============================ INIT/CLEAR ASSUMPTIONS ====================== */
   
clear_negative_assumptions([]) :-
   !.

clear_negative_assumptions([(Number_neg,_)|LNeg]) :-
   atom_number(AN, Number_neg),
   atom_concat('negative_assumptions_', AN, Key),
   learn:recorded(Key, _, Reference),
   learn:erase(Reference),
   clear_negative_assumptions(LNeg).


initialize_negative_assumptions([]) :-
   !.

initialize_negative_assumptions([(Number_neg, _Head, 
                                 min_max(Exp_min, _Exp_max))|LNeg]) :-
   atom_number(AN, Number_neg),
   atom_concat('negative_assumptions_', AN, Key),
   	/* compute all interpretations of an ambiguous example expressed
           as a cross-product of tables */      
   learn:recorda(Key, Exp_min),
   initialize_negative_assumptions(LNeg).

   
/* ================================ END OF LISTING ========================== */                   
