%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% XMI WRITING
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% XMI-WRITE  
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

xmi_write(File):-xmi(XMI),
write_xml(File,[element('xmi:XMI', ['xmi:version'='2.1', 'xmlns:uml'='http://schema.omg.org/spec/UML/2.0', 'xmlns:xmi'=
		'http://schema.omg.org/spec/XMI/2.1'],[ 
		element('xmi:Documentation', ['xmi:Exporter'='Visual Paradigm for UML', 'xmi:ExporterVersion'='6.2.1'],[]), 
		element('uml:Model', [name='mt-prolog'],[
		element(ownedMember, [name='Float', visibility=public, 'xmi:id'='float_id', 'xmi:type'='uml:DataType'],[]),
		element(ownedMember, [name='String', visibility=public, 'xmi:id'='String_id', 'xmi:type'='uml:DataType'],[]),
		element(ownedMember, [name='Int', visibility=public, 'xmi:id'='int_id', 'xmi:type'='uml:DataType'],[])|XMI])])]).


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% WRITE-XML
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

write_xml(File,Term):-open(File,write,F),
			xml_write(F,Term,[]),
			close(F).

%%%%%%%%%%%%%%%%%%%%%%%%
% XMI  
%%%%%%%%%%%%%%%%%%%%%%%%

xmi(XMI):-retrieve_all(XMI).

%%%%%%%%%%%%%%%%%%%%%%%%
% RETRIEVE-ALL  
%%%%%%%%%%%%%%%%%%%%%%%%


retrieve_all(XMI):-findall(Id2,element2(ownedMember,0,Id2),IdList),
			retrieve_each_one(IdList,XMI).
 
%%%%%%%%%%%%%%%%%%%%%%%%%%%
% RETRIEVE-EACH-ONE  
%%%%%%%%%%%%%%%%%%%%%%%%%%%%


retrieve_each_one([],[]).

retrieve_each_one([Id|RId],[element(El,Atts,Nested)|Relement]):-
							element2(El,_,Id),!,
							findall(Name=Value,att2(Name,Value,Id,_),Atts),
							findall(IdN,element2(_,Id,IdN),NIds),  
							retrieve_each_one(NIds,Nested),
							retrieve_each_one(RId,Relement).

