/* ========================================================================== */ 
/* 		Transforme le format .pl de Lear au format .pl de Lear	             */
/* ========================================================================== */


:- dynamic loaded_examples, loaded_examples_test.

   /* Transforme un fichier .pl Lea en fichier .pl Lear */
lea_to_lear(File_Lea) :-
   
   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 file
   atom_concat(File_Lea, '.pl', File_Lea_pl),
   bk:consult(File_Lea_pl),
   
   make_file(File_Lea).   
   
   

/* ========================= CREATE LEAR FILE ==================== */  

make_file(FileName) :-

   get_mode(Line, Mode),

   atom_concat('lear_', FileName, File),   
   build_output_name(Mode, File, File_Lear),    
  
   open(File_Lear, write, Stream),

   fwrite_point_ln(Stream, Line),


   get_examples(pos, Mode, LPos),			% positive examples
   length(LPos, NPos),
   write_examples_head(pos, Mode, Stream),
   fwrite_ln(Stream,'['),
   deal_with_examples(1, LPos, Stream),
   fwrite_ln(Stream, ']'),
   fwrite_ln(Stream, ').'),
   nl(Stream),
   nl(Stream),
   
   get_examples(neg, Mode, LNeg),			% negative examples
   length(LNeg, NNeg),
   write_examples_head(neg, Mode, Stream),
   fwrite_ln(Stream,'['),
   deal_with_examples(1, LNeg, Stream),
   fwrite_ln(Stream, ']'),
   fwrite_ln(Stream, ').'),
   
   deal_with_heads(Mode, NPos, NNeg, Stream),

   nl(Stream),
   nl(Stream),
 
   close(Stream). 


build_output_name(train, File, File_Lear) :-
    atom_concat(File, '.pl', File_Lear),
    !.
    
build_output_name(test, File, File_Lear) :-
    atom_concat(File, '.tst', File_Lear).
    

deal_with_heads(train, NPos, NNeg, Stream) :-
   !,
   deal_with_heads(target, pos, 1, NPos, Stream, concept),
   nl(Stream),
   deal_with_heads(target, neg, 1, NNeg, Stream, concept),
   nl(Stream),

   deal_with_heads(domains, pos, 1, NPos, Stream, []),
   nl(Stream),
   deal_with_heads(domains, neg, 1, NNeg, Stream, []).
   
deal_with_heads(test, _NPos, _NNeg, _Stream).  


write_examples_head(pos, train, Stream) :-
   fwrite_ln(Stream,'examplesP('),
   !.

write_examples_head(neg, train, Stream) :-
   fwrite_ln(Stream,'examplesN('),
   !.
 
write_examples_head(pos, test, Stream) :-
   fwrite_ln(Stream,'examplesP_test('),
   !.

write_examples_head(neg, test, Stream) :-
   fwrite_ln(Stream,'examplesN_test(').


	/* get the first line of the file */
get_mode(loaded_test, test) :-
   bk:loaded_examples_test,
   !.

get_mode(loaded_examples, train) :-
   bk:loaded_examples.

	/* read the examples */
get_examples(pos, test, LEx) :-
   bk:examplesP_test(LEx),
   !.

get_examples(pos, train, LEx) :-
   bk:examplesP(LEx),
   !.

get_examples(neg, test, LEx) :-
   bk:examplesN_test(LEx),
   !.

get_examples(neg, train, LEx) :-
   bk:examplesN(LEx).


	/* translate Lea examples into Lear examples */
deal_with_examples(N, [Ex], Stream) :-
   write_example(Ex, N, Stream),
   nl(Stream),
   !.

deal_with_examples(N, [Ex|Lex], Stream) :-
   write_example(Ex, N, Stream),
   fwrite_ln(Stream,','),
   N1 is N+1,
   deal_with_examples(N1, Lex, Stream). 


/* write the current example */ 
write_example(Ex, N, Stream) :-
  fwrite(Stream,'('),
  fwrite(Stream, (N, [[[concept]]|Ex])),
  fwrite(Stream,')').
 

deal_with_heads(_Pred, _Sign, K, N, _Stream, _Term) :-
   K > N,
   !.
 
deal_with_heads(Pred, Sign, K, N, Stream, Term) :-
    Head =..[Pred, K, Sign, Term], 
    fwrite_point_ln(Stream, Head),
    K1 is K+1,
    deal_with_heads(Pred, Sign, K1, N, Stream, Term).
    

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


    
    
