/* ========================================================================== */ 
/*                        LEAR to TILDE TRANSLATOR	                          */
/* ========================================================================== */

   /* create a file containing positive and negative ambiguous examples from a lear format
      into a tilde (model) format */
convert(
        File_lear   % file.int containing train examples
       ) :- 

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

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

   		% build name of the input files
   atom_concat(File_lear, '.int', File_lear1),
   atom_concat(File_lear, '.kb', File_tilde1),
   
   train:consult(File_lear1),

   gtrace,
   get_pos_examples(train, LPos_train),
   get_neg_examples(train,LNeg_train),
   append(LPos_train, LNeg_train, LTrain),
   convert_examples(train, LTrain, LTilde),
      
   open(File_tilde1, write, Stream),
   write_examples(LTilde, Stream),
   close(Stream).
   
   
/* ================================ MISCELENAOUS ======================== */   
  
shift_number_negative_examples([], _Number, []) :-
   !.
   
shift_number_negative_examples([(N, Class, Ex)|LEx], Number, [(N1, Class, Ex)|LEx1]) :-
   N1 is N + Number,
   shift_number_negative_examples(LEx, Number, LEx1).
   
/* ================================ GETTING EXAMPLES ======================== */     
  
get_pos_examples(Module, LEx) :-
   findall((N, Target, pos, Ex), (Module:pos(N, Ex), Module:target(N, Target)), LEx).
   
get_neg_examples(Module, LEx) :-
   findall((N, Target, neg, Ex), (Module:neg(N, Ex), Module:target(N, Target)), LEx).  

/* ========================= CONVERTING EXAMPLES ============================ */  

convert_examples(_Train_test, [], []) :-
   !.
   
convert_examples(Train_test, [Ex|Lex], [Ex2|Lex1]) :-   
   convert_one_example(Ex, Ex1),
   add_test(Train_test, Ex1, Ex2),
   convert_examples(Train_test, Lex, Lex1).
   
   
add_test(test, [Atom|Ex], [Atom, testid(1)|Ex]) :-
   !.
   
add_test(_, Ex, Ex).   

   
convert_one_example([], []) :-
   !.
   
convert_one_example((N, Target, Class, Ex), Ex_out) :-
   convert_atoms(Ex, Ex1),
   Target=..[Pred|Args],
   append(Args, [Class], Args1),
   Target1=..[Pred|Args1],
   append([begin(model(N)), Target1|Ex1], [end(model(N))], Ex_out).


convert_atoms([], []) :-
   !.
   
convert_atoms([neg(_)|LAtoms], LAtoms_out) :-
   !,
   convert_atoms(LAtoms, LAtoms_out).
   
convert_atoms([Atom|LAtoms], [Atom|LAtoms_out]) :-
   convert_atoms(LAtoms, LAtoms_out).   

   
	/* write examples  */
write_examples([], _Stream) :-
   !.

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


/* write the current example in Tilde format */  
write_one_example(Ex, Stream) :-
  write_atoms(Ex, Stream),
  nl(Stream).
    
    
write_atoms([], _Stream) :-
   !.
   
write_atoms([A|L], Stream) :-   
  fwrite_point_ln(Stream, A),
  write_atoms(L, Stream).

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