 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% MODEL TRANSFORMATION TOOL
% Jesus Almendros and Luis Iribarne (September 2011)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%DYNAMIC_PRED%%%%%%%%%%%%%%%%


:-      dynamic (element)/3.
:-      dynamic  (element2)/3.
:-      dynamic  att2/4.
:-      dynamic  att/4.
:-      dynamic  identifier/1.
:-      dynamic  (rule)/1.
:-      dynamic  from/2. 
:-      dynamic (metamodel)/2.
:-      dynamic objectM/6.
:-      dynamic associationObjectsM/4.
:-      dynamic class/3.
:-      dynamic property/3.
:-      dynamic stereotype/3.
:-      dynamic association/3.
:-      dynamic associationEnds/4.
:-      dynamic input/2.
:-      dynamic output/2.
:-      dynamic vrule/1.

 

load_op:-op(1040,xfy,[from]),
	op(1030,xfy,[to]),
	op(1010,xfy,[and]),
	op(885,xfy,[@]),
	op(1020,fx,[rule]),
	op(890,xfy,[#]),
	op(895,xfy,[::]),
	op(1000,xfy,[==]),
	op(1000,xfy,[=\=]),
	op(890,xfy,[<-]),
	op(1020,xfy,[where]).

:-load_op.
 

:- multifile (input)/2. 
:- multifile (output)/2.
:- multifile (metamodel)/2.
:- multifile (rule)/1.
:- multifile (vrule)/1.

:-style_check(-discontiguous).
:-include('loading.pl').
:-include('writing.pl').
 



%%%%%%%LOAD_OP%%%%%%%%%%%%%%%%

load_op:-op(1040,xfy,[from]),
	op(1030,xfy,[to]),
	op(1010,xfy,[and]),
	op(885,xfy,[@]),
	op(1020,fx,[rule]),
	op(890,xfy,[#]),
	op(895,xfy,[::]),
	op(1000,xfy,[==]),
	op(1000,xfy,[=\=]),
	op(890,xfy,[<-]),
	op(1020,xfy,[where]).

%%%%%%%%%%%%%%INTERFACE%%%%%%%%%%%%%%

transform(Files):-clean_atl,tell('transformation.log'),
		transform_files(Files).

debugging(Files):-clean_atl,tell('debugging.log'),
		debugging_files(Files).

tracing(Files,Id):-clean_atl,tell('tracing.log'),
		tracing_files(Files,Id).



transform_files([]):-!.
transform_files([F|RF]):-
		   
		  atl(F),
		  transform_files(RF).		   

debugging_files([]):-!.
debugging_files([F|RF]):-
		   
		  debug_atl(F),
		  debugging_files(RF).		   

tracing_files([F],Id):-!,trace_atl(F,Id).
tracing_files([F|RF],Id):-
		   
		  atl(F),
		  tracing_files(RF,Id).		   


%%%%%%%%%%%%%MAIN CALLS%%%%%%%%%%

atl(Program):-rewrite(Program,Program2),[Program2],
		generate_metamodels,
		generate_rules,
		catch(load_models,_,(write('Compiler: Syntax error'),nl,fail)),
		clean_transformation.


debug_atl(Program):-nl,
		write('DEBUGGER...........................'),nl,
		rewrite(Program,Program2),[Program2],
		generate_metamodels,
		generate_rules,
		catch(load_models_deb,_,(write('Compiler: Syntax error'),nl,fail)),
		clean_transformation.

trace_atl(Program,Id):-nl,
		write('TRACER............................'),nl,
		write('Tracing the element: '),write(Id),nl,
		rewrite(Program,Program2),[Program2],
		generate_metamodels,
		generate_rules,
		catch(load_models,_,(write('Compiler: Syntax error'),nl,fail)),		
		catch(trace_elem(object(_,_,Id,_,_,_)),_,(write('Compiler: Syntax error'),nl,fail)),	
		clean_transformation.

%%%%%%%%GENERATE METAMODELS%%%%%%%%%%%%%%%%%%%%%%

generate_metamodels:-generate_input_metamodel,generate_output_metamodel.


generate_input_metamodel:-input(_,memory),!. 

generate_input_metamodel:-input(Metamodel,_),generate_metamodel(Metamodel).

generate_output_metamodel:-output(Metamodel,_),generate_metamodel(Metamodel).


%%%%%%%%%LOAD MODELS%%%%%%%%%%%%%%%%%

load_models:-load_input_model,load_output_model.


load_input_model:-input(_,memory),!.

load_input_model:-input(_,File),[xmi2mm],xmi_load(File).



load_output_model:-output(Metamodel,memory),!,load_model(Metamodel).

load_output_model:-output(Metamodel,File),load_model(Metamodel),[mm2xmi],xmi_write(File).


%%%%%%%%%LOAD MODELS DEB%%%%%%%%%%%%%%%%%

load_models_deb:-load_input_model_deb,load_output_model_deb.


load_input_model_deb:-input(_,memory),!.

load_input_model_deb:-input(_,File),[xmi2mm],xmi_load(File).



load_output_model_deb:-output(Metamodel,memory),!,load_model_deb(Metamodel).

load_output_model_deb:-output(Metamodel,File),load_model_deb(Metamodel),[mm2xmi],xmi_write(File).

 


%%%%%%%LOAD_MODEL%%%%%%%%%%%%%%%%

load_model(Name):-retractall(objectM(Name,_,_,_,_,_)),fail.

load_model(Name):-retractall(associationObjectsM(Name,_,_,_)),fail.

load_model(Name):- object(Name,A,B,C,D,E),assert(objectM(Name,A,B,C,D,E)),fail.

load_model(Name):-associationObjects(Name,A,B,C),assert(associationObjectsM(Name,A,B,C)),fail.

load_model(_).


%%%%%%%LOAD_MODEL_DEB%%%%%%%%%%%%%%%%

load_model_deb(Name):-retractall(objectM(Name,_,_,_,_,_)),fail.

load_model_deb(Name):-retractall(associationObjectsM(Name,_,_,_)),fail.

load_model_deb(Name):-debug_term(object(Name,A,B,C,D,E)),assert(objectM(Name,A,B,C,D,E)),fail.

load_model_deb(Name):-debug_term(associationObjects(Name,A,B,C)),assert(associationObjectsM(Name,A,B,C)),fail.

load_model_deb(_).

%%%%%%%DEBUG%%%%%%%%%%%%%%%%

debug_term(Head):-clause(Head,(C,Condition)),C=..[Rule,A], 
			(((findall(A,C,L),L=[])->(clause(C,Cond),debug_condition(Rule,Cond,A),!);
			(findall(A,(C,Condition),L),L=[])->(C,debug_object(Rule,Condition,A),!);(C,Condition))).

%%%%%%%DEBUG_CONDITION%%%%%%%%%%%%%%%%			

debug_condition(Rule,Cond,A):-write('Debugger: Rule Condition of: '),write(Rule),write(' cannot be satisfied.'),nl,
				debug_sequence(Cond,A).

%%%%%%%DEBUG_OBJECT%%%%%%%%%%%%%%%%

debug_object(Rule,Cond,A):-write('Debugger: Objects of: '),write(Rule),write(' cannot be created.'),nl,
				debug_sequence(Cond,A).

%%%%%%%DEBUG_SEQUENCE%%%%%%%%%%%%%%%%

debug_sequence((O,RO),A):-!,
		((findall(A,O,L),L=[])->write('Found error in: '),O=..[Name|_],write(Name),nl;(O,debug_sequence(RO,A))).

debug_sequence(O,A):-((findall(A,O,L),L=[])->write('Found error in: '),O=..[Name|_],write(Name),nl;true).


 

%%%%%%%%%%%TRACE_ELEM%%%%%%%%%%%%%%%

trace_elem(O):-clause(O,Condition),call(Condition),trace_atoms(Condition),!.


%%%%%%%%%%%%TRACE_ATOMS%%%%%%%%%%%%%
 
trace_atoms((O,RO)):-!,(recorded(pointer,O)->write('Traced Execution....'),nl,
			write('Rule: '),O=..[Name|_],write(Name),nl,trace_elem(O);
			(recorded(head,O)->print_trace(O),nl,trace_elem(O);
			(O=..[Name|[_,A|_]],class_def(Name,_,_)->
			print_trace(O),write('xmi:id is '),att('xmi:id',B,A,_),write(B),nl,nl,trace_elem(O);
			(recorded(access,O)->trace_elem(O);
			(recorded(metamodel,O)->true;
			(O=objectM(A,B,C,D,E,F)->trace_elem(object(A,B,C,D,E,F));true)))))),
			trace_atoms(RO).

trace_atoms(O):-(recorded(pointer,O)->write('Traced Execution....'),nl,nl,
		write('Rule: '),O=..[Name|_],write(Name),nl,trace_elem(O);
		(recorded(head,O)->print_trace(O),nl,trace_elem(O);
		(O=..[Name|[_,A|_]],class_def(Name,_,_)->
		print_trace(O),write('xmi:id is '),att('xmi:id',B,A,_),write(B),nl,nl,trace_elem(O);
		(recorded(access,O)->trace_elem(O);
		(recorded(metamodel,O)->true;
		(O=objectM(A,B,C,D,E,F)->trace_elem(object(A,B,C,D,E,F));true)))))).

%%%%%%%PRINT_TRACE%%%%%%%%%%%%%

print_trace(Term):-write('Traced Execution....'),nl,
		Term=..[Name|[Metamodel,_|_]],
		write('Element: '),write(Name),nl,
		write('Metamodel: '),write(Metamodel),nl.
		 
%%%%%%%%%%%%%VALIDATION%%%%%%%%%%%%%%%%

validate(Programs,Rules):-
		clean_atl, 
		transform_files(Programs),
		tell('validation.log'),
		write('Validation................'),nl,
		validate_rules(Rules).
		
validate(_,_):-clean_validation.
		

validate_rules(Rules):-[Rules],validate_rules_one.

validate_rules_one:-Head=..[vrule,N],
		clause(Head,Body),
		validate_body(N,Body),fail.

validate_body(N,Body):- call(Body),!,write('Validation failure on rule: '),write(N),nl.

clean_validation:-retractall(vrule(_)).

 

%%%%%%%GENERATE_METAMODEL%%%%%%%%%%%%%%%%
 



generate_metamodel(Metamodel):- 
		metamodel(Metamodel,MetaElems),!,
		generate_elems(MetaElems,Metamodel).

 

%%%%%%%GENERATE_ELEMS%%%%%%%%%%%%%%%%

generate_elems([],_):-!.

generate_elems([class(Name,Atts)|RMet],Metamodel):-!,
		assert(class_def(Name,Atts,Metamodel)),
		generate_vars(Atts,Elems),
		AtomHead=..[Name,Metamodel,Identifier,Elems],
		(output(Metamodel)->generate_heads(Name,Identifier,AtomHead,Elems,Metamodel);true),
		generate_access(Name,AtomHead,Elems,Identifier,Metamodel),
		generate_elems(RMet,Metamodel).

generate_elems([role(Name,Class,Class2,Low,Up)|RMet],Metamodel):-!,
		assert(role_def(Name,Class,Class2,Low,Up,Metamodel)),
		atom_concat(Class,'_',ClassS),
		atom_concat(ClassS,Name,Head),
		AtomHead=..[Head,Metamodel,IdO1,IdO2],
		assert(AtomHead:-associationEnds(Metamodel,Name,IdO1,IdO2)),recorda(metamodel,AtomHead),	
		generate_elems(RMet,Metamodel).



%%%%%%%GENERATE_HEADS%%%%%%%%%%%%%%%%

generate_heads(Name,Identifier,AtomHead,Elems,Metamodel):-
		AtomTail=..[objectM,Metamodel,Name,Identifier,_,Elems,_],
		assert(AtomHead:-AtomTail),recorda(head,AtomHead).

%%%%%%%GENERATE_ACCESS%%%%%%%%%%%%%%%%

generate_access(Name,AtomTail,[],Identifier,Metamodel):-!,
		atom_concat(Name,'_',NameS),
		atom_concat(NameS,id,Head),
		AtomHead=..[Head,Metamodel,Identifier],
		assert(AtomHead:-AtomTail),recorda(access,AtomHead).

generate_access(Name,AtomTail,[Elem|Elems],Identifier,Metamodel):-
		Elem=..[Att,Var],
		atom_concat(Name,'_',NameS),
		atom_concat(NameS,Att,Head),
		AtomHead=..[Head,Metamodel,Identifier,Var],
		assert(AtomHead:-AtomTail),recorda(metamodel,AtomHead),
		generate_access(Name,AtomTail,Elems,Identifier,Metamodel).	




%%%%%%%GENERATE_RULES%%%%%%%%%%%%%%%%

generate_rules:-generate_rule(_),fail.

generate_rules.


  
%%%%%%%GENERATE_RULE%%%%%%%%%%%%%%%%

generate_rule(NameRule):- (rule NameRule from E to Body), E = (From where Cond), 
				generate_pointers(From,Vars,Atom),
				generate_conditions(Cond,Conds,Vars),
				clean_vars(Vars,VarsClean),
				Head=..[NameRule,VarsClean], 
				generate_head(Body,Vars,Head), 
				Right=..[',',Atom,Conds],
				assert(Head:-Right),recorda(pointer,Head).

generate_rule(NameRule):- (rule NameRule from From to Body),From \= (_ where _),
				generate_pointers(From,Vars,Atom),
				clean_vars(Vars,VarsClean),
				Head=..[NameRule,VarsClean],
				generate_head(Body,Vars,Head), 
				assert(Head:-Atom),recorda(pointer,Head).


%%%%%%%GENERATE_HEAD%%%%%%%%%%%%%%%%

generate_head(((Object :: Metamodel # G), ROb),Vars,Head):- !,
				G =.. [Class|Bindings],
				class_def(Class,Atts,Metamodel),
				clean_vars(Vars,VarsClean),
				generate_vars(Atts,Elems),
				generate_bindings(Metamodel,Head,Object,Class,Bindings,Vars,Elems,Conds),
				assert(object(Metamodel,Class,Id,VarsClean,Elems,Object):-	
				(Head,Conds,generate_ids(VarsClean,Object,Id))), 
 				generate_head(ROb,Vars,Head).



generate_head((Object :: Metamodel # G),Vars,Head):-
				G =.. [Class|Bindings],
				class_def(Class,Atts,Metamodel),
				clean_vars(Vars,VarsClean),
				generate_vars(Atts,Elems),
				generate_bindings(Metamodel,Head,Object,Class,Bindings,Vars,Elems,Conds),
				assert(object(Metamodel,Class,Id,VarsClean,Elems,Object):-
				(Head,Conds,generate_ids(VarsClean,Object,Id))).


%%%%%%%GENERATE_POINTERS%%%%%%%%%%%%%%%%

generate_pointers((Object::Metamodel#Class,B),(Pointer,RV),(Atom,RAtom)):-!,
				atom_concat(Class,'_',ClassS),
				atom_concat(ClassS,'id',Head),
				Atom=..[Head,Metamodel,Var],
				Pointer=..[Object,Var,Class,Metamodel],
				generate_pointers(B,RV,RAtom).

generate_pointers((Object::Metamodel#Class),Pointer,Atom):-
				atom_concat(Class,'_',ClassS),
				atom_concat(ClassS,'id',Head),
				Atom=..[Head,Metamodel,Var],
				Pointer=..[Object,Var,Class,Metamodel].




%%%%%%%GENERATE_BINDINGS%%%%%%%%%%%%%%%%

generate_bindings(_,_,_,_,[],_,_,true).

generate_bindings(Metamodel,Head,Object,Class,[_ <- sequence([])|RBindings],Vars,Elems,Conds):-!,
		generate_bindings(Metamodel,Head,Object,Class,RBindings,Vars,Elems,Conds).

generate_bindings(Metamodel,Head,Object,Class,[Att <- sequence([Expr|RExpr])|RBindings],Vars,Elems,Conds):-!,
				generate_bind(Metamodel,Head,Object,Class,Att,Expr,Vars,Elems,Cond),
				generate_bindings(Metamodel,Head,Object,Class,RBindings,Vars,Elems,RCond),
				(Cond\=true->Conds=(RCond,Cond);Conds=RCond),
				generate_bindings(Metamodel,Head,Object,Class,[Att <- sequence(RExpr)|RBindings],Vars,Elems,Conds).

generate_bindings(Metamodel,Head,Object,Class,[Att <- Expr|RBindings],Vars,Elems,Conds):-
				generate_bind(Metamodel,Head,Object,Class,Att,Expr,Vars,Elems,Cond),
				generate_bindings(Metamodel,Head,Object,Class,RBindings,Vars,Elems,RCond),
				(Cond\=true->Conds=(RCond,Cond);Conds=RCond).


%%%%%%%GENERATE_BIND%%%%%%%%%%%%%%%%

generate_bind(_,_,_,_,Name,Expr,Vars,Elems,Cond):- pointer_to2(Name,Elems,Var),!,
					encode(Expr,Vars,Id,Enc),(Enc\=true->Cond=(Enc,Var=Id);Cond=(Var=Id)).

generate_bind(Metamodel,Head,Object,Class,Role,Expr,Vars,_,true):-
					role_def(Role,Class,_,_,_,Metamodel), 
					encode(Expr,Vars,Id,Enc),
					clean_vars(Vars,VarsClean),
					assert(associationObjects(Metamodel,Role,IdClass,Id):-
					(Head,Enc,object(Metamodel,Class,IdClass,VarsClean,_,Object))).
					



%%%%%%%GENERATE_CONDITIONS%%%%%%%%%%%%%%%%

generate_conditions((Boolean and RB),(EncBoolean,RE),Vars):-!,
				encode_boolean(Boolean,EncBoolean,Vars),
				generate_conditions(RB,RE,Vars).

generate_conditions(Boolean,EncBoolean,Vars):-
				encode_boolean(Boolean,EncBoolean,Vars).

%%%%%%%GENERATE_ID%%%%%%%%%%%%%%%%

generate_id([],Object,Object).

generate_id([Id|RId],Object,IdG):-generate_id(RId,Object,IdRId),atom_concat(Id,IdRId,IdG).


%%%%%%%GENERATE_IDS%%%%%%%%%%%%%%%%

generate_ids((Id,RId),Object,IdG):-!,generate_ids(RId,Object,IdRId),atom_concat(Id,IdRId,IdG).

generate_ids(Id,Object,NewId):-atom_concat(Id,Object,NewId).

%%%%%%%GENERATE_VARS%%%%%%%%%%%%%%%%

generate_vars([],[]):-!.

generate_vars([Name|L],[Atom|V]):-Atom=..[Name,_],generate_vars(L,V).


%%%%%%%ENCODE_BOOLEAN%%%%%%%%%%%%%%%%

encode_boolean(Expr1==Expr2,(Id1=Id2,Atom1,Atom2),Vars):-encode(Expr1,Vars,Id1,Atom1),encode(Expr2,Vars,Id2,Atom2).
					 
encode_boolean(Expr1=\=Expr2,(Id1\=Id2,Atom1,Atom2),Vars):-encode(Expr1,Vars,Id1,Atom1),encode(Expr2,Vars,Id2,Atom2).


%%%%%%%ENCODE%%%%%%%%%%%%%%%%

encode(Number,_,Number,true):-number(Number),!.

encode(true,_,true,true):-!.

encode(false,_,false,true):-!.

encode(String,_,Atom,true):-is_list(String),!,string_to_atom(String,Atom).


encode((Object@Role@Att),Vars,Id2,(Atom,Atom2)):-!,
			pointer_to(Object,Vars,Var,Class,Metamodel),
			atom_concat(Class,'_',ClassS),
			atom_concat(ClassS,Role,Head),
			Atom=..[Head,Metamodel,Var,Id],
			role_def(Role,Class,Class2,_,_,Metamodel),
			atom_concat(Class2,'_',ClassS2),
			atom_concat(ClassS2,Att,Head2),
			Atom2=..[Head2,Metamodel,Id,Id2].

 
encode((Object@Att),Vars,Id,Atom):-!,
			pointer_to(Object,Vars,Var,Class,Metamodel),
			atom_concat(Class,'_',ClassS),
			atom_concat(ClassS,Att,Head),
			Atom=..[Head,Metamodel,Var,Id].

encode(resolveTemp(List,Pointer),Vars,Id,Atom):-!,
				encode_seq(List,Vars,Ids,Atoms),
				Atom=(Atoms,resolveTemp(Ids,Pointer,Id)).

encode(Compound,Vars,Id,Atom):-compound(Compound),Compound=..[Helper|List],atomic(Helper),!,
				encode_list(List,Vars,Ids,Atoms),
				append(Ids,[Id],Args),
				Call=..[Helper|Args],
				Atom=(Atoms,Call).

encode(Object,Vars,Var,Atom):-pointer_to(Object,Vars,Var,Class,Metamodel),!,
			atom_concat(Class,'_',ClassS),
			atom_concat(ClassS,'id',Head),
			Atom=..[Head,Metamodel,Var].

encode(Object,Vars,Var,Atom):-atomic(Object),!,
			clean_vars(Vars,VarsClean),
			Atom=resolveTemp(VarsClean,Object,Var).

 

%%%%%%%ENCODE_LIST%%%%%%%%%%%%%%%%


encode_list([],_,[],true).

encode_list([Ob|ROb],Vars,[Id|RId],Atoms):-encode(Ob,Vars,Id,Atom),encode_list(ROb,Vars,RId,RAtoms),
				Atoms=(RAtoms,Atom).

%%%%%%%ENCODE_SEQ%%%%%%%%%%%%%%%%

encode_seq((Ob,ROb),Vars,(Id,RId),Atoms):-!,encode(Ob,Vars,Id,Atom),encode_seq(ROb,Vars,RId,RAtoms),
				Atoms=(RAtoms,Atom).

encode_seq(Ob,Vars,Id,Atom):-encode(Ob,Vars,Id,Atom).


%%%%%%%POINTERS_TO%%%%%%%%%%%%%%%%

pointer_to(Object,Pointer,Var,Class,Metamodel):-Pointer=..[Object,Var,Class,Metamodel],!.

pointer_to(Object,(Pointer,_),Var,Class,Metamodel):-Pointer=..[Object,Var,Class,Metamodel],!.

pointer_to(Object,(_,RPointers),Var,Class,Metamodel):-pointer_to(Object,RPointers,Var,Class,Metamodel).

%%%%%%%POINTERS_TO2%%%%%%%%%%%%%%%%

pointer_to2(Object,[Pointer|_],Var):-Pointer=..[Object,Var].

pointer_to2(Object,[_|RPointers],Var):-pointer_to2(Object,RPointers,Var).

%%%%%%%CLEAN_VARS%%%%%%%%%%%%%%%%
 
clean_vars((Var,RVar),(Name,RName)):-!,Var=..[_,Name,_,_],clean_vars(RVar,RName).

clean_vars(Var,Name):-Var=..[_,Name,_,_].



%%%%%%%ASSOCIATIONENDS%%%%%%%%%%%%%%%%

associationEnds(Model,Name,Id1,Id2):-associationObjectsM(Model,Name,Id1,Id2).


%%%%%%%RESOLVETEMP%%%%%%%%%%%%%%%%

resolveTemp(Pointers,Name,Id):-object(_,_,Id,Pointers,_,Name).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

output(Model):-output(Model,_).

%%%%%%%CLEAN_TRANSFORMATION%%%%%%%%%%%

clean_transformation:-retractall((rule _ from _)),fail.
clean_transformation:-retractall(input(_,_)),fail.
clean_transformation:-retractall(output(_,_)),fail.
clean_transformation.

%%%%%%%CLEAN_ATL%%%%%%%%%%%

clean_atl:-retractall((rule _ from _)),fail.
clean_atl:-retractall(metamodel(_,_)),fail.
clean_atl:-retractall(class_def(_,_,_)),fail.
clean_atl:-retractall(role_def(_,_,_,_,_,_)),fail.
clean_atl:-recorded(metamodel,Term,Key),retractall(Term),erase(Key),fail.
clean_atl:-recorded(head,Term,Key),retractall(Term),erase(Key),fail.
clean_atl:-recorded(access,Term,Key),retractall(Term),erase(Key),fail.
clean_atl:-recorded(pointer,Term,Key),retractall(Term),erase(Key),fail.
clean_atl:-retractall(input(_,_)),fail.
clean_atl:-retractall(output(_,_)),fail.
clean_atl:-retractall(associationObjects(_,_,_,_)),fail.
clean_atl:-retractall(object(_,_,_,_,_,_)),fail.
clean_atl:-clause(associationObjectsM(_,_,_,_),true),retract(associationObjectsM(_,_,_,_):-true),fail.
clean_atl:-retractall(objectM(_,_,_,_,_,_)),fail.
clean_atl:-retractall(att(_,_,_,_)),fail.
clean_atl:-retractall(element(_,_,_)),fail.
clean_atl.

%%%%%%%%%%REWRITE%%%%%%%%%%%%%%%

rewrite(File,File2):-open(File,read,Stream),
		atom_concat(NameFile,'.ptl',File),
		atom_concat(NameFile,'.pl',File2),
		open(File2,write,Stream2),
		rewrite_file(Stream,Stream2),
		close(Stream2),
		close(Stream).

rewrite_file(Stream,_):-at_end_of_stream(Stream),!.
rewrite_file(Stream,Stream2):-
		get_char(Stream,C), 
		 
		(C='!'->put(Stream2,'#');
		(C=':'->(
			 get_char(Stream,D),
			 (D='-'->
			 (put(Stream2,C),put(Stream2,D));	
			 (put(Stream2,':'),put(Stream2,':'),put(Stream2,D)) 
			 )
			);
		put(Stream2,C) 
		)),
		rewrite_file(Stream,Stream2).

 
 