#! /usr/bin/swipl -q -G1g -L1g -T1g -t main -s
% (home)

% #! /usr/local/bin/lib/swipl-5.11.34/bin/x86_64-linux/swipl -q -G1g -L1g -T1g -t main -s
% (travail)

/* ========================================================================== */ 
/*      CONVERT THE OLD INT FORMAT INTO THE NEW ONE                           */
/*                                                                            */
/*                        Dominique BOUTHINON (db@lipn.univ-paris13.fr)       */
/*                        11/02/13                                            */  
/* ========================================================================== */

main :-
        catch(goal, E, (print_message(error, E), fail)),
        halt.

main :-
        halt(1).

goal :-
        get_arguments(File_int, File_int2),
	     new_format(File_int, File_int2).
 
get_arguments(File_int, File_int2) :-
        current_prolog_flag(argv, Argv),
        append(_, [--|[File_int, File_int2]], Argv).

/* ========================================================================== */

   /* create a file containing positive and negative examples */
new_format(
               File_int,          % file (int) containing noised  
                                  % intensional examples
               File_int2          % name of the output file (int) containing 
                                  % new format 
          ) :- 
    
   gtrace,
   writeln('changing format...'),
   active_modules,
   get_the_examples(File_int, LPos, LNeg),
   write_files(File_int2, LPos, LNeg),
   writeln('finished').


active_modules :-

   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_lear11.pl', Utils),
   use_module(Utils),

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

   atom_concat(Lear, '/domains.pl', Domains),
   use_module(Domains),

   atom_concat(Lear, '/manage_examples.pl', Manage),
   use_module(Manage).


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


get_the_examples(File_int, LPos, LNeg) :-

   atom_concat(File_int, '.int', File_int1),
   train:consult(File_int1),

   get_positive_examples(LPos),
   get_negative_examples(LNeg).


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

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

write_files(File, LPos, LNeg) :-

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

   good_format(LPos, LPos1),
   good_format(LNeg, LNeg1),
 
   write(Out, ':- style_check(-discontiguous).'),
   nl(Out),

         	/* write a list of intentional ambiguous examples */
   write_intentional_examples_domains_targets(1, LPos1, pos, Out),
   write_intentional_examples_domains_targets(1, LNeg1, neg, Out),
                      
   close(Out).


/* translate examples in a good writing format */
good_format([], []) :-
   !.

good_format([ex(_N, Body, Domains, Head, [])|LEx], 
            [(Body, Domains, Head)|LEx1]) :-
   good_format(LEx, LEx1).
                
/* ============================== END OF LISTING ============================ */   
