/* ========================================================================== */ 
/*                        DISCARD BIGGEST EXAMPLES                            */
/*                        (to prevent cost time problem)                      */
/* ========================================================================== */

   /* create a file containing positive and negative examples */
discard_examples(
                       File_in,   % file containing examples
                       File_out,  % file containing remaining examples
                       Percent    % we accept examples less than percent of 
                                  % Max_length
                 ) :-           
              
   writeln('discard biggest examples.'),
   nl,
   getenv('LEAR_DIR', Lear),
   
   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(Lear, '/graph_examples.pl', Graph),
   use_module(Graph),
   
   atom_concat(File_in, '.pl', File_int),
   ex:consult(File_int),

   writeln('discarding biggest examples...'),
   discard_the_examples(Percent, LPos, LTab, LNeg),

   write_files(File_out, LPos, LTab, LNeg),
   writeln('finished').
   

/* ========================= DISCARD THE EXAMPLES =========================== */  

discard_the_examples(Percent, LPos_out, LTab_out, LNeg_out) :-

   ex:examplesP(LPos),
   ex:tabs(pos,LTab),   
   get_max_length_pos(LPos, LTab, 0, Max_length_pos, LPos1),

   Acceptable_length_pos is (Percent/100.0) * Max_length_pos, 
   discard_pos(LPos1, LTab, Acceptable_length_pos, LPos_out, LTab_out),

   ex:examplesN(LNeg),
   get_max_length_neg(LNeg, 0, Max_length_neg, LNeg1),

   Acceptable_length_neg is (Percent/100.0) * Max_length_neg, 
   discard_neg(LNeg1, Acceptable_length_neg, LNeg_out).



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

discard_pos([ex(Length, Ex)|LEx], [Tab|LTab], Max_length, 
            [Ex|LEx1], [Tab|LTab1]) :-
   Length =< Max_length,
   !,
   discard_pos(LEx, LTab, Max_length, LEx1, LTab1).


discard_pos([_|LEx], [_|LTab], Max_length, LEx1, LTab1) :-
   discard_pos(LEx, LTab, Max_length, LEx1, LTab1).
  


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

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


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


 
	/* get the length of the biggest possibility of each example and the
        length of the biggest possibility among all examples */
get_max_length_pos([], [], Max_length, Max_length, []) :-
   !.

get_max_length_pos([Ex|LEx], [Tab|LTab], Max_length_so_far, Max_length, 
                      [ex(Length_ex, Ex)|LEx1]) :-
   get_length_ex_pos(Ex, Tab, Length_ex),
   Max_length_so_far1 is max(Max_length_so_far, Length_ex),
   get_max_length_pos(LEx, LTab, Max_length_so_far1, Max_length, LEx1).


get_length_ex_pos((Id, Head, Graph), (Id, Tab), Length) :-
     build_tables_from_graph(
                                 graph(Id, Head, Graph), 
                                 tabs(Id, Tab), 
                                 tables(_Id, Head, Seed)
                                ),	
    get_length_ex(Seed, 0, Length).


	/* idem for the negative examples */
get_max_length_neg([], Max_length, Max_length, []) :-
   !.

get_max_length_neg([(Id, Head, Ex)|LEx],Max_length_so_far, Max_length, 
                      [ex(Length_ex, (Id, Head, Ex))|LEx1]) :-
   get_length_ex(Ex, 0, Length_ex),
   Max_length_so_far1 is max(Max_length_so_far, Length_ex),
   get_max_length_neg(LEx, Max_length_so_far1, Max_length, LEx1).



	/* the the length of the biggest possibility of the current example */
get_length_ex([], Length, Length) :-
   !.

get_length_ex([Table|LTable], Length_so_far, Length) :-
   get_length_table(Table, 0, Length_table),
   Length_so_far1 is Length_so_far + Length_table,
   get_length_ex(LTable, Length_so_far1, Length).


get_length_table([], Length, Length) :-
   !.

get_length_table([Possi|LPossi], Length_so_far, Length) :-
   length(Possi, Length_possi),
   Length_so_far1 is max(Length_so_far, Length_possi),
   get_length_table(LPossi, Length_so_far1, Length).


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


write_files(File, LPos, LTab, LNeg) :-

   atom_concat(File, '.pl', File_out),
   open(File_out, write, Out),
   
   fwrite_ln(Out, 'loaded_examples.'),
   nl(Out),
    
   fwriteln(Out, 'examplesP('),
   fwriteln(Out, '['),
   write_examples(LPos, Out),
   fwriteln(Out,']'),
   fwriteln(Out,').'),

   nl(Out),
   nl(Out),

   fwriteln(Out, 'tabs(pos,'),
   fwriteln(Out, '['),
   write_tabs(LTab, Out),
   fwriteln(Out,']'),
   fwriteln(Out,').'),
 
   nl(Out),
   nl(Out),

   fwriteln(Out, 'examplesN('),
   fwriteln(Out, '['),
   write_examples(LNeg, Out),
   fwriteln(Out, ']'),
   fwriteln(Out,').'),

   nl(Out),
   nl(Out),
 
   write_targets(LPos, pos, Out),
   nl(Out),
   write_targets(LNeg, neg, Out),

   nl(Out),
   nl(Out),

   write_domains(LPos, pos, Out),
   nl(Out),
   write_domains(LNeg, neg, Out),

   close(Out).

 
  /* write the targets on file */
write_targets([], _, _) :-
   !.

write_targets([(Num, _Head, _Ex)|LL], Signe, Out) :-
   ex:target(Num, Signe, T),
   fwrite(Out, 'target('),
   fwrite(Out, Num),
   fwrite(Out, ','),
   fwrite(Out, Signe),
   fwrite(Out,','),
   fwrite(Out, T),
   fwriteln(Out,').'),
   write_targets(LL, Signe, Out).

 
  /* write the domains on file */
write_domains([], _, _) :-
   !.

write_domains([(Num, _Head, _Ex)|LL], Signe, Out) :-
   ex:domains(Num, Signe, L),
   fwrite(Out, 'domains('),
   fwrite(Out, Num),
   fwriteln(Out,','),
   fwrite(Out, Signe),
   fwrite(Out,','),
   fwrite_list(Out, L),
   fwriteln(Out,').'),
   write_domains(LL, Signe, Out).
                

         

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