/* ========================================================================== */ 
/*                        DISCARD BIGGEST INTENTIONAL EXAMPLES                */
/*                        (to prevent cost time problem)                      */
/*                                                                            */
/*                        Dominique BOUTHINON (db@lipn.univ-paris13.fr)       */
/*                        24/11/12                                            */  
/* ========================================================================== */

   /* create a file containing positive and negative examples */
discard_examples(
                       File_in,      % file containing intensional examples
                       File_out,     % file containing remaining examples
                       Percent_pos,  % we accept pos examples less than 
                                     %percent of Max_length
                       Percent_neg
                 ) :-           
    
   getenv('LEAR_DIR', Lear),
   
   atom_concat(Lear, '/read_write_examples.pl', Read_write),
   use_module(Read_write),

   atom_concat(Lear, '/file.pl', File),
   use_module(File),

   atom_concat(Lear, '/utilities_lear10.pl', Utils),
   use_module(Utils),

   atom_concat(Lear, '/formated_writing.pl', Format),
   use_module(Format),

   atom_concat(File_in, '.int', File_int),
   ex:consult(File_int),

   writeln('discarding biggest intensional examples...'),
   discard_the_examples(Percent_pos, Percent_neg, LPos, LNeg),

   write_files(File_out, LPos, LNeg),
   writeln('finished').
   
/* ========================= DISCARD THE EXAMPLES =========================== */  

discard_the_examples(Percent_pos, Percent_neg, LPos_out, LNeg_out) :-

   get_positive_examples(LPos),
   get_negative_examples(LNeg),

   get_max_length(LPos, 0, Max_length_pos, LPos1),
	Acceptable_length_pos is (Percent_pos/100.0) * Max_length_pos,
   discard(LPos1, Acceptable_length_pos, LPos_out),


   get_max_length(LNeg, 0, Max_length_neg, LNeg1),
   Acceptable_length_neg is (Percent_neg/100.0) * Max_length_neg,
   discard(LNeg1, Acceptable_length_neg, LNeg_out).


	/* discard the too long negative examples */
discard([], _Max_length, []) :-
   !.

discard([ex(Length, Ex)|LEx], Max_length, [Ex|LEx1]) :-
   Length =< Max_length,
   !,
   discard(LEx, Max_length, LEx1).


discard([_|LEx], Max_length, LEx1) :-
   discard(LEx, Max_length, LEx1). 

/* ======================== GET THE LENGTH OF THE EXAMPLES ================== */
 
	/* idem for the negative examples */
get_max_length([], Max_length, Max_length, []) :-
   !.

get_max_length([(N, Ex, Domains, Target)|LEx],Max_length_so_far, Max_length, 
               [ex(Length_ex, (N, Ex, Domains, Target))|LEx1]) :-

   discard_neg_atoms(Ex, Ex1),
   length(Ex1, Length_ex),
   Max_length_so_far1 is max(Max_length_so_far, Length_ex),
   get_max_length(LEx, Max_length_so_far1, Max_length, LEx1).


	/* discard atoms of the forme neg(A) */
discard_neg_atoms([], []) :-
   !.

discard_neg_atoms([neg(_A)|L], L1) :-
   !,
   discard_neg_atoms(L, L1).

discard_neg_atoms([A|L], [A|L1]) :-
   discard_neg_atoms(L, L1).

/* =========================== GET THE EXAMPLES ============================= */

get_positive_examples(LPos) :-
   findall(
           (N, Ex, Domains, Target), 
           (
             ex:pos(N,Ex),  
             ex:domains(N, Domains),
             ex:target(N, Target)
           ),
           LPos
           ).
   
get_negative_examples(LNeg) :-
  findall(
           (N, Ex, Domains, Target), 
           (
             ex:neg(N,Ex),  
             ex:domains(N, Domains),
             ex:target(N, Target)
           ),
           LNeg
           ).

/* ========================= WRITE THE RESULTING FILE ======================= */

write_files(File, LPos, LNeg) :-

   atom_concat(File, '.int', File_out),
   open(File_out, write, Out),

   fwrite_ln(Out, ':- style_check(-discontiguous).'),
   nl(Out),
    
   write_examples(LPos, pos, Out),
   write_examples(LNeg, neg, Out),

   close(Out).


write_examples([], _Signe, _Out) :-
   !.

write_examples([(N, Ex, Domains, Target)|LEx], Signe, Out) :-
   write_intentional_example_domain_target((Ex, Domains, Target), 
                                            Signe, N, Out),
   write_examples(LEx, Signe, Out).

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