Wczytując plik wyprodukowany przez Varde „ard.xml” load_xml_file('ard.xml',X),A=..X,assertz(A),write(A) i otrzymuję:
element(hml, [], [
, element(attribute_set, [], [
, element(att, [name=Thermostat], []),
, element(att, [name=Time], []),
, element(att, [name=Temperature], []),
, element(att, [name=Date], []),
, element(att, [name=Hour], []),
, element(att, [name=season], []),
, element(att, [name=operation], []),
, element(att, [name=thermostat_settings], []),
, element(att, [name=day], []),
, element(att, [name=month], []),
, element(att, [name=today], []),
, element(att, [name=hour], []),
]),
, element(property_set, [], [
, element(property, [pid=1], [
, element(attref, [name=Thermostat], []),
]),
, element(property, [pid=2], [
, element(attref, [name=Time], []),
, element(attref, [name=Temperature], []),
]),
, element(property, [pid=3], [
, element(attref, [name=Time], []),
]),
, element(property, [pid=4], [
, element(attref, [name=Temperature], []),
]),
, element(property, [pid=5], [
, element(attref, [name=Date], []),
, element(attref, [name=Hour], []),
, element(attref, [name=season], []),
, element(attref, [name=operation], []),
]),
, element(property, [pid=6], [
, element(attref, [name=Date], []),
, element(attref, [name=Hour], []),
]),
, element(property, [pid=7], [
, element(attref, [name=season], []),
, element(attref, [name=operation], []),
]),
, element(property, [pid=8], [
, element(attref, [name=thermostat_settings], []),
]),
, element(property, [pid=9], [
, element(attref, [name=season], []),
]),
, element(property, [pid=10], [
, element(attref, [name=operation], []),
]),
, element(property, [pid=11], [
, element(attref, [name=Date], []),
]),
, element(property, [pid=12], [
, element(attref, [name=Hour], []),
]),
, element(property, [pid=13], [
, element(attref, [name=day], []),
, element(attref, [name=month], []),
, element(attref, [name=today], []),
]),
, element(property, [pid=14], [
, element(attref, [name=month], []),
]),
, element(property, [pid=15], [
, element(attref, [name=day], []),
, element(attref, [name=today], []),
]),
, element(property, [pid=16], [
, element(attref, [name=day], []),
]),
, element(property, [pid=17], [
, element(attref, [name=today], []),
]),
, element(property, [pid=18], [
, element(attref, [name=hour], []),
]),
]),
, element(tph, [], [
, element(trans, [src=1, dst=2], []),
, element(trans, [src=2, dst=3], []),
, element(trans, [src=2, dst=4], []),
, element(trans, [src=3, dst=5], []),
, element(trans, [src=5, dst=6], []),
, element(trans, [src=5, dst=7], []),
, element(trans, [src=4, dst=8], []),
, element(trans, [src=7, dst=9], []),
, element(trans, [src=7, dst=10], []),
, element(trans, [src=6, dst=11], []),
, element(trans, [src=6, dst=12], []),
, element(trans, [src=11, dst=13], []),
, element(trans, [src=13, dst=14], []),
, element(trans, [src=13, dst=15], []),
, element(trans, [src=15, dst=16], []),
, element(trans, [src=15, dst=17], []),
, element(trans, [src=12, dst=18], []),
]),
, element(ard, [], [
, element(dep, [independent=9, dependent=8], []),
, element(dep, [independent=10, dependent=8], []),
, element(dep, [independent=14, dependent=9], []),
, element(dep, [independent=16, dependent=17], []),
, element(dep, [independent=17, dependent=10], []),
, element(dep, [independent=18, dependent=10], []),
]),
])
Natomiast za pomocą element(hml,B,X) otrzymuję:
B = [],
X = ['\n ', element(attribute_set, [], ['\n ',
element(att, [name='Thermostat'], []), '\n ', element(att, [... =...], []),
'\n ', element(..., ..., ...)|...]), '\n ', element(property_set, [],
['\n ', element(property, [... =...], ['\n '|...]), '\n ',
element(..., ..., ...)|...]), '\n ', element(tph, [], ['\n ',
element(..., ..., ...)|...]), '\n ', element(ard, [], [...|...]), '\n']
Powyższe przykladu są generowane dla wersji zerowej Vardy.
W poniższym kodzie rozkładam liste oraz tworze odpowiednik w prologu:
%%%%%%%%%%%%%%%%%%%%% Load%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
a:-
load_structure('ard.xml',X,[space(remove),dialect(xml)]),Z=..X,assertz(Z),element(hml,_,A),
checkAtt(A),checkProp(A),checkHist(A),checkDepend(A),show,zapisz.
%%%%%%%%%%%% Wyswietl%%%%%%%%%%%%%%%
show:-
listing(ard_att),listing(ard_property),listing(ard_hist),listing(ard_depend).
%%%%%%%%%%%%%%%%%%%%%%%%%%% Zapis%%%%%%%%%%%%%%%%%%%%%%
zapisz:-
tell('ardII.xml'),write('<?xml version="1.0" encoding="utf-8"?>'),
listing(ard_att),listing(ard_property),listing(ard_hist),
listing(ard_depend),told.
%%%%%%%%%%%%%%%%%%%%% Attribute_set%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
checkAtt(A):-
nalezy(element(attribute_set,_,L),A),
dlugosc(L,Dl1),wyswietlAtt(L,Dl1,0).
wyswietlAtt(Ll1,0,Dl).
wyswietlAtt(Ll1,D1,Dl):-
D1>0,
NewDlu is D1-1,
usun1(Dl,Ll1,element(att,[name=ZX],[])),
assert(ard_att(ZX)),
DlNew is Dl+1,
wyswietlAtt(Ll1,NewDlu,DlNew).
%%%%%%%%%%%%%%%%%%%%% Property_set%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
checkProp(A):-
nalezy(element(property_set,_,L2),A),
dlugosc(L2,D2),
assert(zobacz(ListaGlob)),
wyswietlProp(L2,D2,0).
wyswietlProp(L2,0,Dl2).
wyswietlProp(L2,D2,Dl2):-
D2>0,
NewDlu2 is D2-1,
usun1(Dl2,L2,element(property,_,C)),
dlugosc(C,CA),
wypiszProp(CA,C,[]),pamiec(Lista),assert(ard_property(Lista)),
zobacz(ListaGlob),dodaj(ListaGlob,Lista,ListaGlob2),
retractall(zobacz(_)),
assert(zobacz(ListaGlob2)),
retractall(pamiec(_)),
DlNew2 is Dl2+1,
wyswietlProp(L2,NewDlu2,DlNew2).
wypiszProp(0,L,Lista).
wypiszProp(CA,[J1|L1],Lel):-
CA>0,
CNew is CA-1,
pokazPierwszy(J1,element(attref, [name=Bb], [])),
dodaj(Lel,Bb,Lista1),
retractall(pamiec(_)),
assert(pamiec(Lista1)),
wypiszProp(CNew,L1,Lista1).
pokazPierwszy(L,L).
%%%%%%%%%%%%%%%%%%%%% Hist_set%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
checkHist(A):-
nalezy(element(tph,_,L1),A),
dlugosc(L1,D1),wyswietlHist(L1,D1,0).
wyswietlHist(L1,0,Dl).
wyswietlHist(L1,D1,Dl):-
D1>0,
NewDlu is D1-1,
usun1(Dl,L1,element(trans,[src=Zrodlo,dst=Destination],[])),
sprawdz(Zrodlo,Destination,Z,D),assert(ard_hist(Z,D)),
DlNew is Dl+1,
wyswietlHist(L1,NewDlu,DlNew).
%%%%%%%%%%%%%% pobrac src i dest%%%%%%%%%%
sprawdz(Zr,De,Zrod,Dest):-
atom_number(Zr,Zrr),
atom_number(De,Dee),
zobacz(Wszystkie),
wyswietlPropert(Wszystkie,Zrr,Zrod),
wyswietlPropert(Wszystkie,Dee,Dest).
wyswietlPropert(Wszystkie,1,Wyjscie):-
usun1(0,Wszystkie,Wyjscie).
wyswietlPropert(Wszystkie,Miejsce,Wyjscie):-
usun1(Miejsce-1,Wszystkie,Wyjscie).
%%%%%%%%%%%%%%%%%%%%% Depend_set%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
checkDepend(A):-
nalezy(element(ard,_,L1),A),
dlugosc(L1,D1),wyswietlDepe(L1,D1,0).
wyswietlDepe(L1,0,Dl).
wyswietlDepe(L1,D1,Dl):-
D1>0,
NewDlu is D1-1,
usun1(Dl,L1,element(dep,[independent=Ind,dependent=Dep],[])),
sprawdzDep(Ind,Dep,Indep,Depen),assert(ard_depend(Indep,Depen)),
DlNew is Dl+1,
wyswietlDepe(L1,NewDlu,DlNew).
%%%%%%%%%%%%%% pobrac independent i dependent%%%%%%%%%%%%%%
sprawdzDep(In,De,InOut,DeOut):-
atom_number(In,Inn),
atom_number(De,Depe),
zobacz(Wszystkie),
wyswietlDependent(Wszystkie,Inn,InOut),
wyswietlDependent(Wszystkie,Depe,DeOut).
wyswietlDependent(Wszystkie,1,Wyjscie):-
usun1(0,Wszystkie,Wyjscie).
wyswietlDependent(Wszystkie,Miejsce,Wyjscie):-
usun1(Miejsce-1,Wszystkie,Wyjscie).
%%%%%%%%%%%%%%%%%%%%%%%%%%% Pozostałe %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
nalezy(X,[X|_]).
nalezy(X,[_|Yogon]) :-
nalezy(X,Yogon).
dlugosc([],0).
dlugosc([_|Ogon],Dlug) :-
dlugosc(Ogon,X),
Dlug is X+1.
usun1(0,[F|_],F).
usun1(Poz,[_|Xx],W):-
Poz>0,
PozNew is Poz-1,
usun1(PozNew,Xx,W).
dodaj([],X,[X]).
dodaj([G|O],X,[G|R]):-
dodaj(O,X,R).
Plik do parsowania: :pl:miw:ard-vardy.xml
Można połączyć translator bezpośrednio z vardą umieszczając odpowiedni wpis w pliku „VARDA.pl”,
„:-ensure_loaded('varda_pl2xml').”, dzięki temu no załadowaniu vardy i wygenerowaniu pliku przy pomocy axg,
możemy bezpośrednio translatować xml.
Poniżej znajdujesię kod translatora.
%%%%%%%%%%%%%%%%%%%%% Load %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
translate(Source,Dest):-
load_structure(Source,X,[space(remove),dialect(xml)]), %załadowanie pliku
Z=..X,assertz(Z),element(hml,_,A), %wyciągnięcie elementu hml
checkAtt(A), %analiza elementu "attribute_set"
checkProperty(A), %analiza elementu "property_set"
checkHistory(A), %analiza elementu "tph"
checkDepend(A), %analiza elementu "ard"
show, % wyświetlenie wyników
save(Dest). %zapis wyniku
translate(Source):-
load_structure(Source,X,[space(remove),dialect(xml)]),Z=..X,assertz(Z),element(hml,_,A),
checkAtt(A),checkProperty(A),checkHistory(A),checkDepend(A),show.
%%%%%%%%%%%% Wyswietl%%%%%%%%%%%%%%%
show:-
listing(ard_att),listing(ard_property),listing(ard_hist),listing(ard_depend).
%%%%%%%%%%%%%%%%%%%%%%%%%%% Save to file %%%%%%%%%%%%%%%%%%%%%%
save(Dest):-
tell(Dest),write('<?xml version="1.0" encoding="utf-8"?>'),
listing(ard_att),listing(ard_property),listing(ard_hist),
listing(ard_depend),told.
%%%%%%%%%%%%%%%%%%%%% Attribute_set%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
checkAtt(A):-
include(element(attribute_set,_,Li),A), % sprawdzanie istnienia elementu
leng(Li,Dl1), % wylicznie dlugości listy
assert(viewList(_)), % deklarowanie pamięci
showAtt(Li,Dl1,0).
showAtt(_,0,_).
showAtt(L,D1,Dl):-
D1>0,
NewDlu is D1-1,
deleteOne(Dl,L,element(att,[name=ZX,_,_,_],[])),
assert(memory(ZX)),
memory(ZX),
assert(ard_att(ZX)),
viewList(LisGlob),add(LisGlob,ZX,LisGlob2),
retractall(viewList(_)),
assert(viewList(LisGlob2)),
retractall(memory(_)),
DlNew is Dl+1,
showAtt(L,NewDlu,DlNew).
%%%%%%%%%%%%%%%%%%%%% Property_set %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
checkProperty(A):-
include(element(property_set,_,L),A),
leng(L,D),
assert(view(_)),
showProp(L,D,0).
showProp(_,0,_).
showProp(L2,D2,Dl2):-
D2>0,
NewDlu2 is D2-1,
deleteOne(Dl2,L2,element(property,_,C)),
leng(C,CA),
writeProp(CA,C,[]),memoryList(Lista),assert(ard_property(Lista)),
view(ListaGlob),add(ListaGlob,Lista,ListaGlob2),
retractall(view(_)),
assert(view(ListaGlob2)),
retractall(memoryList(_)),
DlNew2 is Dl2+1,
showProp(L2,NewDlu2,DlNew2).
writeProp(0,_,_).
writeProp(CA,[J1|L1],Lel):-
CA>0,
CNew is CA-1,
showFirst(J1,element(attref, [ref=Bb], [])),
string_length(Bb,L),
LEEE is L-4,
sub_string(Bb,4,LEEE,_,WYJ),
checkProp(WYJ,Insert),
add(Lel,Insert,Lista1),
retractall(memoryList(_)),
assert(memoryList(Lista1)),
writeProp(CNew,L1,Lista1).
showFirst(L,L).
%%%%%%%%%
checkProp(Output,Name):-
atom_number(Output,Wyj),
viewList(All),
NWyj is Wyj+1,
showName(All,NWyj,Name).
showName(All,1,Name):-
deleteOne(0,All,Name).
showName(All,Wyj,Name):-
deleteOne(Wyj-1,All,Name).
%%%%%%%%%%%%%%%%%%%%% Hist_set %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
checkHistory(A):-
include(element(tph,_,L1),A),
leng(L1,D1),showlHist(L1,D1,0).
showlHist(_,0,_).
showlHist(L1,D1,Dl):-
D1>0,
NewDlu is D1-1,
deleteOne(Dl,L1,element(trans,[src=Source,dst=Destination],[])),
string_length(Source,L),
LEEE is L-4,
sub_string(Source,4,LEEE,_,WYJ),
string_length(Destination,LG),
LEEEE is LG-4,
sub_string(Destination,4,LEEEE,_,Output),
checkHist(WYJ,Output,Z,D),assert(ard_hist(Z,D)),
DlNew is Dl+1,
showlHist(L1,NewDlu,DlNew).
%%%%%%%%%%%%%% Get src i des t%%%%%%%%%%
checkHist(Zr,De,Zrod,Dest):-
atom_number(Zr,Zrr),
atom_number(De,Dee),
view(All),
NZrr is Zrr+1,
NDee is Dee+1,
showPropert(All,NZrr,Zrod),
showPropert(All,NDee,Dest).
showPropert(All,1,Output):-
deleteOne(0,All,Output).
showPropert(All,Position,Output):-
deleteOne(Position-1,All,Output).
%%%%%%%%%%%%%%%%%%%%% Depend_set %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
checkDepend(A):-
include(element(ard,_,LisDep),A),
leng(LisDep,DlDep),wyswietlDepe(LisDep,DlDep,0).
wyswietlDepe(_,0,_).
wyswietlDepe(L1,D1,Dl):-
D1>0,
NewDlu is D1-1,
deleteOne(Dl,L1,element(dep,[independent=Ind,dependent=Dep],[])),
string_length(Ind,L),
LEEE is L-4,
sub_string(Ind,4,LEEE,_,WYJ),
string_length(Dep,LENG),
LEE is LENG-4,
sub_string(Dep,4,LEE,_,Output),
checkDep(WYJ,Output,Indep,Depen),assert(ard_depend(Indep,Depen)),
DlNew is Dl+1,
wyswietlDepe(L1,NewDlu,DlNew).
%%%%%%%%%%%%%% Get independent i dependent %%%%%%%%%%%%%%
checkDep(In,De,InOut,DeOut):-
atom_number(In,Inn),
atom_number(De,Depe),
view(All),
NInn is Inn+1,
NDepe is Depe+1,
showDependent(All,NInn,InOut),
showDependent(All,NDepe,DeOut).
showDependent(All,1,Output):-
deleteOne(0,All,Output).
showDependent(All,Position,Output):-
deleteOne(Position-1,All,Output).
%%%%%%%%%%%%%%%%%%%%%%%%%%% Others %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% sprawdzanie istnienia elementów w liście
include(X,[X|_]).
include(X,[_|Tail]) :-
include(X,Tail).
% obliczanie długości listy
leng([],0).
leng([_|Tail],Dlug) :-
leng(Tail,X),
Dlug is X+1.
% wyciąganie konkretnego elementu listy
deleteOne(0,[F|_],F).
deleteOne(Poz,[_|Xx],W):-
Poz>0,
PozNew is Poz-1,
deleteOne(PozNew,Xx,W).
% dodanie do listy
add([],X,[X]).
add([G|O],X,[G|R]):-
add(O,X,R).
Translator Plik wejściowy Plik wyjściowy
Dla potrzeb wyświetlenia wyników plik wyjściowy został powyżej dołączony z rozszerzeniem pl.