%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Call Functions
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
:-consult(auxiliar).
:-consult(parser).
:-consult(tv).
:-consult(debug).
:-consult(fuzzyXPath).
:-consult(similarity).


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% EXECUTE-FUZZYXPATH, Examples
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% execute_fuzzyXPath('example.xml', 'salida.xml', '[DEEP=1.0;DOWN=1.0]/bib/book[@price<30 and @year<2006]').
% execute_fuzzyXPath('example.xml', 'salida.xml', '[DEEP=1.0;DOWN=1.0]/bib/book[(@price>25 and @price<30) avg{5,2} (@year>2000 and @year<2006)]').
% execute_fuzzyXPath('example.xml', 'salida.xml', '[DEEP=1.0;DOWN=1.0]/bib/book[@price<30 avg @year<2006]/publications/[DEEP=0.3;DOWN=0.5]book').
% execute_fuzzyXPath('example.xml', 'salida.xml', '[DEEP=0.9;DOWN=0.8]/bib/book[@price<30 avg @year<2006]/publications/[DEEP=0.7;DOWN=0.6]book[@year<1998 and @price<30]').
%
% execute_fuzzyXPath('example.xml', 'salida.xml', '[DEEP=0.9;DOWN=0.8]//book').
% execute_fuzzyXPath('example.xml', 'salida.xml', '[DEEP=0.9;DOWN=0.8]//publications/book/title').
% execute_fuzzyXPath('example.xml', 'salida.xml', '[DEEP=0.9;DOWN=0.8]//publications/[DEEP=0.7;DOWN=0.6]book/title').
%
% execute_fuzzyXPath('example.xml',  'salida.xml', '[DEEP=0.9;DOWN=0.8]/bib/book/title[text="Don Quijote de la Mancha"]')
% execute_fuzzyXPath('example2.xml', 'salida.xml', '[DEEP=0.9;DOWN=0.8]/bib/book[author/text="Miguel de Cervantes Saavedra"]/title').
% execute_fuzzyXPath('example2.xml', 'salida.xml', '[DEEP=0.9;DOWN=0.8]/bib/book[author/text="Miguel de Cervantes Saavedra" avg @year<2000]/title').
% execute_fuzzyXPath('example2.xml', 'salida.xml', '/bib/book[author/text="Miguel de Cervantes Saavedra"]').
% execute_fuzzyXPath('example2.xml', 'salida.xml', '/bib/book[author/text="Miguel de Cervantes Saavedra"]/title').
% execute_fuzzyXPath('ex.xml', 'salida.xml', '[DEEP=0.9;DOWN=0.8]/bib/book/title[text="La Galatea"]').
%
% execute_fuzzyXPath('example.xml', 'salida.xml', '[DEEP=0.9;DOWN=0.8]/bib/book[( @year=1994 or @year>1994 ) and @year<2000]').

% execute_fuzzyXPath('example.xml', 'salida.xml', '[DEEP=1.0;DOWN=1.0]/bib/book[@year=1994 or @year<1999]').
% execute_fuzzyXPath('example.xml', 'salida.xml', '[DEEP=0.9;DOWN=0.8]/bib/book[@year<2010]').
% execute_fuzzyXPath('example.xml', 'salida.xml', '[DEEP=0.9;DOWN=0.8]/bib/book/@year').
%
% execute_fuzzyXPath('example.xml', 'salida.xml', '[DEEP=0.9;DOWN=0.8]/bib/book[@year=1994 avg @year<1999]').
%
% execute_fuzzyXPath('example.xml', 'salida.xml', '[DEEP=1.0;DOWN=1.0]/bib/book[@year=1994 avg{3,1} @year<1999]').
% execute_fuzzyXPath('example.xml', 'salida.xml', '[DEEP=0.9;DOWN=0.8]//book[(@year=1994 avg{1,2} @year>1994 ) and @year<2007]').
%
% load_fuzzyXPath(A, B, C, "[DEEP=0.9;DOWN=0.8]/bib/book/title",[])
%
% Eamples
% 1.
% execute_fuzzyXPath('example.xml', 'salida.xml', '[DEEP=0.9;DOWN=0.8]//title').
% 2.
% execute_fuzzyXPath('example.xml', 'salida.xml', '[DEEP=1;DOWN=0.8]/bib/book/title').
% 3.
% execute_fuzzyXPath('example.xml', 'salida.xml', '/bib/book[@price<30 avg @year<2006]').
% 4.
% execute_fuzzyXPath('example.xml', 'salida.xml', '[DEEP=0.9;DOWN=0.8]//book[(@price>25 and @price<30) avg (@year<2000 or @year>2006)]/title').
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%55
% Nuevas consultas?
% execute_fuzzyXPath('example.xml', 'salida.xml', '[DEEP=0.9;DOWN=0.8]//book[author/text=\"Miguel de Cervantes Saavedra\"]/publications[DEEP=0.7;DOWN=0.6]//title').
% execute_fuzzyXPath('hotels.xml', 'salida.xml', '/bib/book[@price<30 avg{3,1} @year<2006]').
%
% aca otra...
%
execute_fuzzyXPathTV(In, _, FuzzyXPath):-
                        load_fuzzyXPath(FuzzyXPath, ListFXPath),
			load_xml(In,InputDoc),
                        fuzzyXPath(ListFXPath,InputDoc, tv(1.0,[]), tv(1.0,[]), OutputDoc),
                        tv_to_print(OutputDoc,''), !.

execute_fuzzyXPath(In, _, FuzzyXPath):-
                        load_fuzzyXPath(FuzzyXPath, ListFXPath), % this is the parser
			load_xml(In,InputDoc),
                        fuzzyXPath(ListFXPath,InputDoc, tv(1.0,[]), tv(1.0,[]), OutputDoc),
%			tv_to_print(OutputDoc,''), write('\n'), write('\n'),
			tv_to_elem(OutputDoc, 1, OutputElement),
			elemen_orderRSV(OutputElement,OutputElement2), Result=[element(result,[],OutputElement2)],
%			Result=[element(result,[],OutputElement)],
                        xml_write(Result,[]), !.
%			xml_write(Result,[]), tv_to_print(OutputDoc,''),!.

%	 execute_fuzzyXPath("hotels.xml", "salida.xml", "//hotel/close_to/[text=Sol]")



isDebug([], 0, []):- !.
isDebug([debug(IsD)|B], IsD, B):- !.
isDebug([A|B], IsD, [A|B1]):- !, isDebug(B, IsD, B1).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% DEBUG-FUZZYXPATH, Examples
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% debug_fuzzyXPath('library.xml', 'salida.xml', '[DEEP=0.9;DOWN=0.8]/bib/book/@year').
% debug_fuzzyXPath('library.xml','salida.xml', '/bib/book/writer').

debug_fuzzyXPath(In, _, FuzzyXPath):-
                        load_fuzzyXPath(FuzzyXPath, ListFXPath),
			load_xml(In,InputDoc),
			Similarity =0.5, % Le dice que utifffflice la similaridad
                        debugQuery(ListFXPath,InputDoc, Similarity, OutputDoc),
%                        tv_to_print(OutputDoc,''),
			tv_to_tag(OutputDoc, ResultE),
			tag_to_element(ResultE, OutputElement,_), write('\n'), write('\n'), write('\n'),
%			Result=[element(result,[],OutputElement)],


%			tv_to_elem(OutputDoc, 1, OutputElement),
			elemen_order(OutputElement,OutputElement2), Result=[element(result,[],OutputElement2)],
%			Result=[element(result,[],OutputElement)],
                        xml_write(Result,[]), !.
%			xml_write(Result,[]), tv_to_print(OutputDoc,''),!.

debug_fuzzyXPathTV(In, _, FuzzyXPath):-
                        load_fuzzyXPath(FuzzyXPath, ListFXPath),
			load_xml(In,InputDoc),
			Similarity =0.5, % Le dice que utilice la similaridad
                        debugQuery(ListFXPath,InputDoc, Similarity, OutputDoc),
                        tv_to_print(OutputDoc,'').










