/* ========================================================================== */ 
/*                        LEA to TILDE TRANSLATOR	                          */
/*         (ici ne traite que le cas particulier des multitables a et not_a)  */
/* ========================================================================== */


   /* create a file containing positive and negative incomplete examples in a tilde format */
convert(
        File_train,   % file containing train examples
        File_test,    % file contaiing test examples
        Concept       % name of the concept
       ) :- 
                          
   
   use_module(library(random)), 

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

   atom_concat(Lear, '/counter.pl', Counter),
   use_module(Counter),  

   atom_concat(Lear, '/list.pl', List),
   use_module(List),


   		% build name of the input files
   atom_concat(File_train, '.pl', File_train1),
   atom_concat(File_test, '.pl', File_test1),
   
   train:consult(File_train1),
   test:consult(File_test1),
   
   atom_concat(File_train, '.kb', File_tilde), 

   lea_to_tilde(File_tilde, Concept).   
   
/* ========================= CREATING AMBIGUOUS EXAMPLES ==================== */  

lea_to_tilde(File, Concept) :-

   get_test_examples(LPos_test, LNeg_test),
   get_train_examples(LPos_train, LNeg_train),
   
   convert_examples(LPos_test, LPos_test_tilde),
   convert_examples(LNeg_test, LNeg_test_tilde),
   convert_examples(LPos_train, LPos_train_tilde),
   convert_examples(LNeg_train, LNeg_train_tilde),
   
   write_tilde_examples(File, Concept,
                        LPos_test_tilde, LNeg_test_tilde, 
                        LPos_train_tilde, LNeg_train_tilde).
  
  
convert_examples([], []) :-
   !.
   
convert_examples([Ex|Lex], [Ex1|Lex1]) :-
   convert_one_example(Ex, Ex1),
   convert_examples(Lex, Lex1).
   
convert_one_example([], []) :-
   !.
   
convert_one_example([Table|Ex], Ex_out) :-
   convert_table(Table, Table1),
   convert_one_example(Ex, Ex1),
   append(Table1, Ex1, Ex_out).
   
   
convert_table([P], P) :-   % we keep the single possibility
   !.  
   
convert_table([_, [A2]], [A2]) :-  % two possibilities : "a" or "not_a"
   atom_concat('not_', _, A2),
   !.
   
convert_table([[A1], _], [A1]).


write_tilde_examples(
                     File,
                     Concept,
                     LPos_test_tilde, 
                     LNeg_test_tilde,
                     LPos_train_tilde,
                     LNeg_train_tilde
                     ) :- 
   
   open(File, write, Stream),
   reset_count(nb_examples),
   
   write_examples(yes, test, LPos_test_tilde, Concept, Stream),
   nl(Stream),
   nl(Stream),
   write_examples(no, test, LNeg_test_tilde, Concept, Stream),
   nl(Stream),
   nl(Stream),
   write_examples(yes, train, LPos_train_tilde, Concept, Stream),
   nl(Stream),
   nl(Stream),
   write_examples(no, train, LNeg_train_tilde, Concept, Stream),
   
   close(Stream).
   
  
	/* get test exemples */
	  
get_test_examples(LPos, LNeg) :-
   test:examplesP_test(LPos),
   test:examplesN_test(LNeg).  
   
get_train_examples(LPos, LNeg) :-
   train:examplesP(LPos),
   train:examplesN(LNeg).  


	/* write test examples  */
write_examples(_Signe, _Train_test, [], _Concept, _Stream) :-
   !.

write_examples(Signe, Train_test, [Ex|Lex], Concept, Stream) :-
   write_one_example(Signe, Train_test, Ex, Concept, Stream),
   write_examples(Signe, Train_test, Lex, Concept, Stream).



/* write the current example in Tilde format */ 
 
write_one_example(Signe, Train_test, Ex, Concept, Stream) :-
  get_count(nb_examples, N),
  fwrite_point_ln(Stream, begin(model(N))),
  write_concept(Concept, Signe, Stream),
  write_testid(Train_test, Stream),
  write_atoms(Ex, Stream),
  fwrite_point_ln(Stream, end(model(N))),
  nl(Stream).
  
write_concept(Concept, Signe, Stream) :-
   Head_concept=..[Concept,Signe],
   fwrite_point_ln(Stream, Head_concept).
     
write_atoms([], _Stream) :-
   !.
   
write_atoms([A|L], Stream) :-   
  fwrite_point_ln(Stream, A),
  write_atoms(L, Stream).
  
write_testid(test, Stream) :-
   !,
   fwrite_point_ln(Stream, testid(1)).
   
write_testid(_, _Stream).   
  
     
   
/* ============================== END OF LISTING ============================ */   
   
   
        
   


    
    
