Both sides previous revision
Poprzednia wersja
Nowa wersja
|
Poprzednia wersja
|
pl:miw:miw2008_xtt_xmlpro [2008/06/02 22:24] miw |
pl:miw:miw2008_xtt_xmlpro [2019/06/27 15:50] (aktualna) |
====== Opis ====== | ====== Opis ====== |
| __**Projekt zakończony**__ |
| |
Maksymilian Imioło <maximiolo@o2.pl> | Maksymilian Imioło <maximiolo@o2.pl> |
| |
Translacja XTTML do innych formatów z użyciem Prologu. | Translacja XTTML do innych formatów z użyciem Prologu. |
| |
====== Spotkania ====== | |
===== 08.03.04 ===== | |
* parsowanie xml w prologu | |
* obsługa XSD w prologu | |
* poczytac: graphviz, RIF | |
| |
| |
| |
===== 080318 ===== | |
* poczytać... | |
| |
===== 080408 ===== | |
* translacja [[hekate:hekate_markup_language|HML/ARDML]] (produkowanego przez [[hekate:VARDA]]) | |
* przykład z [[hekate:hekate_case_thermostat]] therm jako p. odn. | |
* podejście: | |
- mamy plik ARDML z Vardy (axg.) | |
- parsujemy go w prologu | |
- generujemy bazę wiedzy prologową dla VARDA (ard_att/1, ard_property/1, ard_depend/2, ard_hist/2) (assert(ard_att('Thermostat'))) (czyli to, co dostaniemy po listing(ard_depend),listing(ard_attribute),listing(ard_property),listing(ard_hist).) | |
- generujemy odpowiadający mu plik DOT | |
| |
http://www.swi-prolog.org/packages/sgml2pl.html | ====== Spotkania====== |
load_xml_file(+File, -ListOfContent) | |
| [[pl:miw:miw2008_xtt_xmlpro:spotkania|Spotkania]] |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
np: | |
<att name="Thermostat"/> -> assert(ard_att('Thermostat')). | |
| |
| |
| |
===== 080415 ===== | |
* parsowanie listy | |
| |
===== 080422 ===== | |
* generowanie prologu | |
| |
| |
| |
===== 080603 ===== | |
* testy kodu dla varda, po ang, upload do wiki | |
* [[pl:miw:miw08_xtt_rif|XTT i RIF]] | |
| |
====== Projekt ====== | ====== Projekt ====== |
| |
| |
====Thermostat==== | |
| |
Wczytuję plik "ard.xml" load_xml_file('ard.xml',X),A=..X,assertz(A),write(A) i otrzymuję: | |
| |
| ====Ogólny opis problemu==== |
| Celem jest przetworzenie pliku xml produkowanego przez Varde [[hekate:hekate_case_thermostat|Thermostat]] do postaci, w której |
| widzimy bezpośrednie zależności pomiedzy elementami. |
| |
| |
| |
| |
| ====Varda wersja 0==== |
| |
| Wczytując plik wyprodukowany przez Varde "ard.xml" load_xml_file('ard.xml',X),A=..X,assertz(A),write(A) i otrzymuję: |
<code prolog> | <code prolog> |
element(hml, [], [ | element(hml, [], [ |
]), | ]), |
]) | ]) |
| |
X = [element(hml, [], ['\n ', element(attribute_set, [], ['\n | |
', element(att, [... =...], []), '\n ', element(..., ..., ...)|...]), | |
'\n ', element(property_set, [], ['\n ', element(..., ..., ...)|...]), '\n | |
', element(tph, [], [...|...]), '\n '|...])], | |
| |
| |
</code> | </code> |
| |
Następnie za pomocą element(hml,B,X) otrzymuję: | Natomiast za pomocą element(hml,B,X) otrzymuję: |
<code prolog> | <code prolog> |
B = [], | B = [], |
element(..., ..., ...)|...]), '\n ', element(ard, [], [...|...]), '\n'] | element(..., ..., ...)|...]), '\n ', element(ard, [], [...|...]), '\n'] |
</code> | </code> |
| Powyższe przykladu są generowane dla wersji zerowej Vardy. |
| |
Można to wyświetlić w inny sposób element(hml,B,X),write(X): | |
<code prolog> | |
[ | |
, 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], []), | |
]), | |
] | |
</code> | |
| |
| |
====Napotkane problemy==== | |
W pliku mamy znaczniki nowej lini np:('\n','\n ','\n ') bez ich usunięcia nie można dotrzeć do wnętrza pliku. | |
Korzystajac z "usun" | |
<code prolog> | |
usun(X,[X|Reszta],Reszta). | |
usun(X,[Y|Ogon],[Y|Reszta]) :- | |
usun(X,Ogon,Reszta). | |
</code> | |
nie daje oczekiwanych efektów. | |
element(hml,X,C),usun('\n',C,B),write(B). nie usuwa wszystkich znaków lecz tylko ostatni. | |
| |
http://www.swi-prolog.org/packages/sgml2pl.html#sec:sec-3.1 | |
http://www.swi-prolog.org/packages/sgml2pl.html#sec:space | |
| |
space(sgml) In SGML, newlines at the start and end of an element are removed.2 This is the default mode for the SGML dialect. | |
space(default) In addition to sgml space-mode, all consequtive white-space is reduced to a single space-character. This mode canonises all white space. | |
| |
| |
Za pomocą poniższego kodu ładuję plik usuwając spacje oraz znaczniki końca lini: | |
<code prolog> | |
load_structure('ard.xml',X,[space(remove),dialect(xml)]),Z=..X,assertz(Z). | |
</code> | |
| |
| |
====Varda wersja 0==== | |
W poniższym kodzie rozkładam liste oraz tworze odpowiednik w prologu: | W poniższym kodzie rozkładam liste oraz tworze odpowiednik w prologu: |
<code prolog> | <code prolog> |
| |
%%%%%%%%%%%%%%%%%%%%% Load%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | %%%%%%%%%%%%%%%%%%%%% Load%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
a:- | a:- |
load_structure('ard.xml',X,[space(remove),dialect(xml)]),Z=..X,assertz(Z),element(hml,_,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. | checkAtt(A),checkProp(A),checkHist(A),checkDepend(A),show,zapisz. |
| |
%%%%%%%%%%%% Wyswietl%%%%%%%%%%%%%%% | %%%%%%%%%%%% Wyswietl%%%%%%%%%%%%%%% |
show:- | show:- |
listing(ard_att),listing(ard_property),listing(ard_hist),listing(ard_depend). | listing(ard_att),listing(ard_property),listing(ard_hist),listing(ard_depend). |
%%%%%%%%%%%%%%%%%%%%%%%%%%% Zapis%%%%%%%%%%%%%%%%%%%%%% | %%%%%%%%%%%%%%%%%%%%%%%%%%% Zapis%%%%%%%%%%%%%%%%%%%%%% |
zapisz:- | zapisz:- |
tell('ardII.xml'),write('<?xml version="1.0" encoding="utf-8"?>'), | tell('ardII.xml'),write('<?xml version="1.0" encoding="utf-8"?>'), |
listing(ard_att),listing(ard_property),listing(ard_hist), | listing(ard_att),listing(ard_property),listing(ard_hist), |
listing(ard_depend),told. | listing(ard_depend),told. |
%%%%%%%%%%%%%%%%%%%%% Attribute_set%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | %%%%%%%%%%%%%%%%%%%%% Attribute_set%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
checkAtt(A):- | checkAtt(A):- |
nalezy(element(attribute_set,_,L),A), | nalezy(element(attribute_set,_,L),A), |
dlugosc(L,Dl1),wyswietlAtt(L,Dl1,0). | dlugosc(L,Dl1),wyswietlAtt(L,Dl1,0). |
| |
wyswietlAtt(Ll1,0,Dl). | wyswietlAtt(Ll1,0,Dl). |
wyswietlAtt(Ll1,D1,Dl):- | wyswietlAtt(Ll1,D1,Dl):- |
D1>0, | D1>0, |
NewDlu is D1-1, | NewDlu is D1-1, |
usun1(Dl,Ll1,element(att,[name=ZX],[])), | usun1(Dl,Ll1,element(att,[name=ZX],[])), |
assert(ard_att(ZX)), | assert(ard_att(ZX)), |
DlNew is Dl+1, | DlNew is Dl+1, |
wyswietlAtt(Ll1,NewDlu,DlNew). | wyswietlAtt(Ll1,NewDlu,DlNew). |
| |
%%%%%%%%%%%%%%%%%%%%% Property_set%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | %%%%%%%%%%%%%%%%%%%%% Property_set%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
checkProp(A):- | checkProp(A):- |
nalezy(element(property_set,_,L2),A), | nalezy(element(property_set,_,L2),A), |
dlugosc(L2,D2), | dlugosc(L2,D2), |
assert(zobacz(ListaGlob)), | assert(zobacz(ListaGlob)), |
wyswietlProp(L2,D2,0). | wyswietlProp(L2,D2,0). |
| |
wyswietlProp(L2,0,Dl2). | wyswietlProp(L2,0,Dl2). |
wyswietlProp(L2,D2,Dl2):- | wyswietlProp(L2,D2,Dl2):- |
D2>0, | D2>0, |
NewDlu2 is D2-1, | NewDlu2 is D2-1, |
usun1(Dl2,L2,element(property,_,C)), | usun1(Dl2,L2,element(property,_,C)), |
dlugosc(C,CA), | dlugosc(C,CA), |
wypiszProp(CA,C,[]),pamiec(Lista),assert(ard_property(Lista)), | wypiszProp(CA,C,[]),pamiec(Lista),assert(ard_property(Lista)), |
zobacz(ListaGlob),dodaj(ListaGlob,Lista,ListaGlob2), | zobacz(ListaGlob),dodaj(ListaGlob,Lista,ListaGlob2), |
retractall(zobacz(_)), | retractall(zobacz(_)), |
assert(zobacz(ListaGlob2)), | assert(zobacz(ListaGlob2)), |
retractall(pamiec(_)), | retractall(pamiec(_)), |
DlNew2 is Dl2+1, | DlNew2 is Dl2+1, |
wyswietlProp(L2,NewDlu2,DlNew2). | wyswietlProp(L2,NewDlu2,DlNew2). |
| |
wypiszProp(0,L,Lista). | wypiszProp(0,L,Lista). |
wypiszProp(CA,[J1|L1],Lel):- | wypiszProp(CA,[J1|L1],Lel):- |
CA>0, | CA>0, |
CNew is CA-1, | CNew is CA-1, |
pokazPierwszy(J1,element(attref, [name=Bb], [])), | pokazPierwszy(J1,element(attref, [name=Bb], [])), |
dodaj(Lel,Bb,Lista1), | dodaj(Lel,Bb,Lista1), |
retractall(pamiec(_)), | retractall(pamiec(_)), |
assert(pamiec(Lista1)), | assert(pamiec(Lista1)), |
wypiszProp(CNew,L1,Lista1). | wypiszProp(CNew,L1,Lista1). |
| |
pokazPierwszy(L,L). | pokazPierwszy(L,L). |
| |
%%%%%%%%%%%%%%%%%%%%% Hist_set%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | %%%%%%%%%%%%%%%%%%%%% Hist_set%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
checkHist(A):- | checkHist(A):- |
nalezy(element(tph,_,L1),A), | nalezy(element(tph,_,L1),A), |
dlugosc(L1,D1),wyswietlHist(L1,D1,0). | dlugosc(L1,D1),wyswietlHist(L1,D1,0). |
| |
wyswietlHist(L1,0,Dl). | wyswietlHist(L1,0,Dl). |
wyswietlHist(L1,D1,Dl):- | wyswietlHist(L1,D1,Dl):- |
D1>0, | D1>0, |
NewDlu is D1-1, | NewDlu is D1-1, |
usun1(Dl,L1,element(trans,[src=Zrodlo,dst=Destination],[])), | usun1(Dl,L1,element(trans,[src=Zrodlo,dst=Destination],[])), |
sprawdz(Zrodlo,Destination,Z,D),assert(ard_hist(Z,D)), | sprawdz(Zrodlo,Destination,Z,D),assert(ard_hist(Z,D)), |
DlNew is Dl+1, | DlNew is Dl+1, |
wyswietlHist(L1,NewDlu,DlNew). | wyswietlHist(L1,NewDlu,DlNew). |
| |
%%%%%%%%%%%%%% pobrac src i dest%%%%%%%%%% | %%%%%%%%%%%%%% pobrac src i dest%%%%%%%%%% |
sprawdz(Zr,De,Zrod,Dest):- | sprawdz(Zr,De,Zrod,Dest):- |
atom_number(Zr,Zrr), | atom_number(Zr,Zrr), |
atom_number(De,Dee), | atom_number(De,Dee), |
zobacz(Wszystkie), | zobacz(Wszystkie), |
wyswietlPropert(Wszystkie,Zrr,Zrod), | wyswietlPropert(Wszystkie,Zrr,Zrod), |
wyswietlPropert(Wszystkie,Dee,Dest). | wyswietlPropert(Wszystkie,Dee,Dest). |
| |
wyswietlPropert(Wszystkie,1,Wyjscie):- | wyswietlPropert(Wszystkie,1,Wyjscie):- |
usun1(0,Wszystkie,Wyjscie). | usun1(0,Wszystkie,Wyjscie). |
| |
wyswietlPropert(Wszystkie,Miejsce,Wyjscie):- | wyswietlPropert(Wszystkie,Miejsce,Wyjscie):- |
usun1(Miejsce-1,Wszystkie,Wyjscie). | usun1(Miejsce-1,Wszystkie,Wyjscie). |
| |
%%%%%%%%%%%%%%%%%%%%% Depend_set%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | %%%%%%%%%%%%%%%%%%%%% Depend_set%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
checkDepend(A):- | checkDepend(A):- |
nalezy(element(ard,_,L1),A), | nalezy(element(ard,_,L1),A), |
dlugosc(L1,D1),wyswietlDepe(L1,D1,0). | dlugosc(L1,D1),wyswietlDepe(L1,D1,0). |
| |
wyswietlDepe(L1,0,Dl). | wyswietlDepe(L1,0,Dl). |
wyswietlDepe(L1,D1,Dl):- | wyswietlDepe(L1,D1,Dl):- |
D1>0, | D1>0, |
NewDlu is D1-1, | NewDlu is D1-1, |
usun1(Dl,L1,element(dep,[independent=Ind,dependent=Dep],[])), | usun1(Dl,L1,element(dep,[independent=Ind,dependent=Dep],[])), |
sprawdzDep(Ind,Dep,Indep,Depen),assert(ard_depend(Indep,Depen)), | sprawdzDep(Ind,Dep,Indep,Depen),assert(ard_depend(Indep,Depen)), |
DlNew is Dl+1, | DlNew is Dl+1, |
wyswietlDepe(L1,NewDlu,DlNew). | wyswietlDepe(L1,NewDlu,DlNew). |
| |
%%%%%%%%%%%%%% pobrac independent i dependent%%%%%%%%%%%%%% | %%%%%%%%%%%%%% pobrac independent i dependent%%%%%%%%%%%%%% |
sprawdzDep(In,De,InOut,DeOut):- | sprawdzDep(In,De,InOut,DeOut):- |
atom_number(In,Inn), | atom_number(In,Inn), |
atom_number(De,Depe), | atom_number(De,Depe), |
zobacz(Wszystkie), | zobacz(Wszystkie), |
wyswietlDependent(Wszystkie,Inn,InOut), | wyswietlDependent(Wszystkie,Inn,InOut), |
wyswietlDependent(Wszystkie,Depe,DeOut). | wyswietlDependent(Wszystkie,Depe,DeOut). |
| |
wyswietlDependent(Wszystkie,1,Wyjscie):- | wyswietlDependent(Wszystkie,1,Wyjscie):- |
usun1(0,Wszystkie,Wyjscie). | usun1(0,Wszystkie,Wyjscie). |
| |
wyswietlDependent(Wszystkie,Miejsce,Wyjscie):- | wyswietlDependent(Wszystkie,Miejsce,Wyjscie):- |
usun1(Miejsce-1,Wszystkie,Wyjscie). | usun1(Miejsce-1,Wszystkie,Wyjscie). |
| |
%%%%%%%%%%%%%%%%%%%%%%%%%%% Pozostałe %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | %%%%%%%%%%%%%%%%%%%%%%%%%%% Pozostałe %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| |
nalezy(X,[X|_]). | nalezy(X,[X|_]). |
nalezy(X,[_|Yogon]) :- | nalezy(X,[_|Yogon]) :- |
nalezy(X,Yogon). | nalezy(X,Yogon). |
| |
dlugosc([],0). | dlugosc([],0). |
dlugosc([_|Ogon],Dlug) :- | dlugosc([_|Ogon],Dlug) :- |
usun1(0,[F|_],F). | usun1(0,[F|_],F). |
usun1(Poz,[_|Xx],W):- | usun1(Poz,[_|Xx],W):- |
Poz>0, | Poz>0, |
PozNew is Poz-1, | PozNew is Poz-1, |
usun1(PozNew,Xx,W). | usun1(PozNew,Xx,W). |
| |
| |
dodaj([],X,[X]). | dodaj([],X,[X]). |
dodaj([G|O],X,[G|R]):- | dodaj([G|O],X,[G|R]):- |
dodaj(O,X,R). | dodaj(O,X,R). |
| |
| |
Plik do parsowania: {{:pl:miw:ard-vardy.xml|:pl:miw:ard-vardy.xml}} | Plik do parsowania: {{:pl:miw:ard-vardy.xml|:pl:miw:ard-vardy.xml}} |
| |
====Otrzymany wynik translacji==== | |
Otrzymujemy następujący efekt: | |
<code xml> | |
<?xml version="1.0" encoding="utf-8"?> | |
:- dynamic ard_att/1. | |
| |
ard_att('Thermostat'). | |
ard_att('Time'). | |
ard_att('Temperature'). | |
ard_att('Date'). | |
ard_att('Hour'). | |
ard_att(season). | |
ard_att(operation). | |
ard_att(thermostat_settings). | |
ard_att(day). | |
ard_att(month). | |
ard_att(today). | |
ard_att(hour). | |
| |
:- dynamic ard_property/1. | |
| |
ard_property(['Thermostat']). | |
ard_property(['Time', 'Temperature']). | |
ard_property(['Time']). | |
ard_property(['Temperature']). | |
ard_property(['Date', 'Hour', season, operation]). | |
ard_property(['Date', 'Hour']). | |
ard_property([season, operation]). | |
ard_property([thermostat_settings]). | |
ard_property([season]). | |
ard_property([operation]). | |
ard_property(['Date']). | |
ard_property(['Hour']). | |
ard_property([day, month, today]). | |
ard_property([month]). | |
ard_property([day, today]). | |
ard_property([day]). | |
ard_property([today]). | |
ard_property([hour]). | |
| |
:- dynamic ard_hist/2. | |
| |
ard_hist(['Thermostat'], ['Time', 'Temperature']). | |
ard_hist(['Time', 'Temperature'], ['Time']). | |
ard_hist(['Time', 'Temperature'], ['Temperature']). | |
ard_hist(['Time'], ['Date', 'Hour', season, operation]). | |
ard_hist(['Date', 'Hour', season, operation], ['Date', 'Hour']). | |
ard_hist(['Date', 'Hour', season, operation], [season, operation]). | |
ard_hist(['Temperature'], [thermostat_settings]). | |
ard_hist([season, operation], [season]). | |
ard_hist([season, operation], [operation]). | |
ard_hist(['Date', 'Hour'], ['Date']). | |
ard_hist(['Date', 'Hour'], ['Hour']). | |
ard_hist(['Date'], [day, month, today]). | |
ard_hist([day, month, today], [month]). | |
ard_hist([day, month, today], [day, today]). | |
ard_hist([day, today], [day]). | |
ard_hist([day, today], [today]). | |
ard_hist(['Hour'], [hour]). | |
| |
:- dynamic ard_depend/2. | |
| |
ard_depend([season], [thermostat_settings]). | |
ard_depend([operation], [thermostat_settings]). | |
ard_depend([month], [season]). | |
ard_depend([day], [today]). | |
ard_depend([today], [operation]). | |
ard_depend([hour], [operation]). | |
| |
| |
</code> | |
| |
Zapis: | |
<code prolog> | |
tell('ardII.xml'),write('<?xml version="1.0" encoding="utf-8"?>'), | |
listing(ard_att),listing(ard_property), | |
listing(ard_hist),listing(ard_depend),told. | |
</code> | |
| |
| |
| |
Translator można uruchomić na kilka sposobów: | Translator można uruchomić na kilka sposobów: |
1 | ===1=== |
Załadować plik źródłowy i wpisać : translate('Source.xml','Destination.xml'). | Załadować plik źródłowy i wpisać : translate('Source.xml','Destination.xml'). |
Otrzymamy wydruk na ekranie monitora oraz, zapis do wskazanego pliku. | Otrzymamy wydruk na ekranie monitora oraz, zapis do wskazanego pliku. |
2 | ===2 === |
Załadować plik źródłowy i wpisać : translate('Source.xml'). | Załadować plik źródłowy i wpisać : translate('Source.xml'). |
Otrzymamy wydruk na ekranie monitora, jeśli będziemy chcieli zapisać do pliku to należy | Otrzymamy wydruk na ekranie monitora, jeśli będziemy chcieli zapisać do pliku to należy |
wpisać: save('Destination.xml'). | wpisać: save('Destination.xml'). |
3 | === 3 === |
Można połączyć translator bezpośrednio z vardą umieszczając odpowiedni wpis w pliku "VARDA.pl", | 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, | ":-ensure_loaded('varda_pl2xml').", dzięki temu no załadowaniu vardy i wygenerowaniu pliku przy pomocy axg, |
| |
| |
| Poniżej znajdujesię kod translatora. |
<code prolog> | <code prolog> |
| %%%%%%%%%%%%%%%%%%%%% Load %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| |
| |
%%%%%%%%%%%%%%%%%%%%% Load%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
translate(Source,Dest):- | translate(Source,Dest):- |
load_structure(Source,X,[space(remove),dialect(xml)]),Z=..X,assertz(Z),element(hml,_,A), | load_structure(Source,X,[space(remove),dialect(xml)]), %załadowanie pliku |
checkAtt(A),checkProperty(A),checkHistory(A),checkDepend(A),show,save(Dest). | 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):- | translate(Source):- |
load_structure(Source,X,[space(remove),dialect(xml)]),Z=..X,assertz(Z),element(hml,_,A), | load_structure(Source,X,[space(remove),dialect(xml)]),Z=..X,assertz(Z),element(hml,_,A), |
checkAtt(A),checkProperty(A),checkHistory(A),checkDepend(A),show. | checkAtt(A),checkProperty(A),checkHistory(A),checkDepend(A),show. |
| |
%%%%%%%%%%%% Wyswietl%%%%%%%%%%%%%%% | %%%%%%%%%%%% Wyswietl%%%%%%%%%%%%%%% |
show:- | show:- |
listing(ard_att),listing(ard_property),listing(ard_hist),listing(ard_depend). | listing(ard_att),listing(ard_property),listing(ard_hist),listing(ard_depend). |
%%%%%%%%%%%%%%%%%%%%%%%%%%% Zapis%%%%%%%%%%%%%%%%%%%%%% | |
| %%%%%%%%%%%%%%%%%%%%%%%%%%% Save to file %%%%%%%%%%%%%%%%%%%%%% |
save(Dest):- | save(Dest):- |
tell(Dest),write('<?xml version="1.0" encoding="utf-8"?>'), | tell(Dest),write('<?xml version="1.0" encoding="utf-8"?>'), |
listing(ard_att),listing(ard_property),listing(ard_hist), | listing(ard_att),listing(ard_property),listing(ard_hist), |
listing(ard_depend),told. | listing(ard_depend),told. |
| |
%%%%%%%%%%%%%%%%%%%%% Attribute_set%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | %%%%%%%%%%%%%%%%%%%%% Attribute_set%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| |
| |
checkAtt(A):- | checkAtt(A):- |
include(element(attribute_set,_,L),A), | include(element(attribute_set,_,Li),A), % sprawdzanie istnienia elementu |
leng(L,Dl1),assert(viewList(List)),showAtt(L,Dl1,0).%,viewList(List),write(List). | leng(Li,Dl1), % wylicznie dlugości listy |
| assert(viewList(_)), % deklarowanie pamięci |
| showAtt(Li,Dl1,0). |
| |
showAtt(L,0,Dl). | |
| showAtt(_,0,_). |
showAtt(L,D1,Dl):- | showAtt(L,D1,Dl):- |
D1>0, | D1>0, |
NewDlu is D1-1, | NewDlu is D1-1, |
deleteOne(Dl,L,element(att,[name=ZX,id=ID,_,_],[])), | deleteOne(Dl,L,element(att,[name=ZX,_,_,_],[])), |
string_length(ID,LENG), | assert(memory(ZX)), |
LEE is LENG-4, | memory(ZX), |
assert(memory(ZX)), | assert(ard_att(ZX)), |
memory(ZX), | viewList(LisGlob),add(LisGlob,ZX,LisGlob2), |
sub_string(ID,4,LEE,_,WY), | retractall(viewList(_)), |
assert(ard_att(ZX)),%nalezy(C,W), | assert(viewList(LisGlob2)), |
viewList(LisGlob),add(LisGlob,ZX,LisGlob2), | retractall(memory(_)), |
retractall(viewList(_)), | DlNew is Dl+1, |
assert(viewList(LisGlob2)), | showAtt(L,NewDlu,DlNew). |
retractall(memory(_)), | |
DlNew is Dl+1, | |
showAtt(L,NewDlu,DlNew). | |
| |
%%%%%%%%%%%%%%%%%%%%% Property_set%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | %%%%%%%%%%%%%%%%%%%%% Property_set %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
checkProperty(A):- | checkProperty(A):- |
include(element(property_set,_,L2),A), | include(element(property_set,_,L),A), |
leng(L2,D2), | leng(L,D), |
assert(view(ListaGl)), | assert(view(_)), |
showProp(L2,D2,0). | showProp(L,D,0). |
| |
showProp(L2,0,Dl2). | showProp(_,0,_). |
showProp(L2,D2,Dl2):- | showProp(L2,D2,Dl2):- |
D2>0, | D2>0, |
NewDlu2 is D2-1, | NewDlu2 is D2-1, |
deleteOne(Dl2,L2,element(property,_,C)), | deleteOne(Dl2,L2,element(property,_,C)), |
leng(C,CA), | leng(C,CA), |
writeProp(CA,C,[]),memoryList(Lista),assert(ard_property(Lista)), | writeProp(CA,C,[]),memoryList(Lista),assert(ard_property(Lista)), |
view(ListaGlob),add(ListaGlob,Lista,ListaGlob2), | view(ListaGlob),add(ListaGlob,Lista,ListaGlob2), |
retractall(view(_)), | retractall(view(_)), |
assert(view(ListaGlob2)), | assert(view(ListaGlob2)), |
retractall(memoryList(_)), | retractall(memoryList(_)), |
DlNew2 is Dl2+1, | DlNew2 is Dl2+1, |
showProp(L2,NewDlu2,DlNew2). | showProp(L2,NewDlu2,DlNew2). |
| |
writeProp(0,L,Lista). | writeProp(0,_,_). |
writeProp(CA,[J1|L1],Lel):- | writeProp(CA,[J1|L1],Lel):- |
CA>0, | CA>0, |
CNew is CA-1, | CNew is CA-1, |
showFirst(J1,element(attref, [ref=Bb], [])), | showFirst(J1,element(attref, [ref=Bb], [])), |
string_length(Bb,L), | string_length(Bb,L), |
LEEE is L-4, | LEEE is L-4, |
sub_string(Bb,4,LEEE,_,WYJ), | sub_string(Bb,4,LEEE,_,WYJ), |
checkProp(WYJ,Insert), | checkProp(WYJ,Insert), |
add(Lel,Insert,Lista1), | add(Lel,Insert,Lista1), |
retractall(memoryList(_)), | retractall(memoryList(_)), |
assert(memoryList(Lista1)), | assert(memoryList(Lista1)), |
writeProp(CNew,L1,Lista1). | writeProp(CNew,L1,Lista1). |
| |
showFirst(L,L). | showFirst(L,L). |
%%%%%%%%% | %%%%%%%%% |
checkProp(Output,Name):- | checkProp(Output,Name):- |
atom_number(Output,Wyj), | atom_number(Output,Wyj), |
viewList(All), | viewList(All), |
NWyj is Wyj+1, | NWyj is Wyj+1, |
showName(All,NWyj,Name).%,write(Name). | showName(All,NWyj,Name). |
| |
showName(All,1,Name):- | showName(All,1,Name):- |
deleteOne(0,All,Name). | deleteOne(0,All,Name). |
| |
showName(All,Wyj,Name):- | showName(All,Wyj,Name):- |
deleteOne(Wyj-1,All,Name). | deleteOne(Wyj-1,All,Name). |
| |
%%%%%%%%%%%%%%%%%%%%% Hist_set%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | %%%%%%%%%%%%%%%%%%%%% Hist_set %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
checkHistory(A):- | checkHistory(A):- |
include(element(tph,_,L1),A), | include(element(tph,_,L1),A), |
leng(L1,D1),showlHist(L1,D1,0). | leng(L1,D1),showlHist(L1,D1,0). |
| |
showlHist(L1,0,Dl). | showlHist(_,0,_). |
showlHist(L1,D1,Dl):- | showlHist(L1,D1,Dl):- |
D1>0, | D1>0, |
NewDlu is D1-1, | NewDlu is D1-1, |
deleteOne(Dl,L1,element(trans,[src=Source,dst=Destination],[])), | deleteOne(Dl,L1,element(trans,[src=Source,dst=Destination],[])), |
string_length(Source,L), | string_length(Source,L), |
LEEE is L-4, | LEEE is L-4, |
sub_string(Source,4,LEEE,_,WYJ), | sub_string(Source,4,LEEE,_,WYJ), |
string_length(Destination,LG), | string_length(Destination,LG), |
LEEEE is LG-4, | LEEEE is LG-4, |
sub_string(Destination,4,LEEEE,_,Output), | sub_string(Destination,4,LEEEE,_,Output), |
checkHist(WYJ,Output,Z,D),assert(ard_hist(Z,D)), | checkHist(WYJ,Output,Z,D),assert(ard_hist(Z,D)), |
DlNew is Dl+1, | DlNew is Dl+1, |
showlHist(L1,NewDlu,DlNew). | showlHist(L1,NewDlu,DlNew). |
| |
%%%%%%%%%%%%%% pobrac src i dest%%%%%%%%%% | %%%%%%%%%%%%%% Get src i des t%%%%%%%%%% |
checkHist(Zr,De,Zrod,Dest):- | checkHist(Zr,De,Zrod,Dest):- |
atom_number(Zr,Zrr), | atom_number(Zr,Zrr), |
atom_number(De,Dee), | atom_number(De,Dee), |
view(All), | view(All), |
NZrr is Zrr+1, | NZrr is Zrr+1, |
NDee is Dee+1, | NDee is Dee+1, |
showPropert(All,NZrr,Zrod), | showPropert(All,NZrr,Zrod), |
showPropert(All,NDee,Dest). | showPropert(All,NDee,Dest). |
| |
showPropert(All,1,Output):- | showPropert(All,1,Output):- |
deleteOne(0,All,Output). | deleteOne(0,All,Output). |
| |
showPropert(All,Position,Output):- | showPropert(All,Position,Output):- |
deleteOne(Position-1,All,Output). | deleteOne(Position-1,All,Output). |
| |
%%%%%%%%%%%%%%%%%%%%% Depend_set%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | %%%%%%%%%%%%%%%%%%%%% Depend_set %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
checkDepend(A):- | checkDepend(A):- |
include(element(ard,_,LisDep),A), | include(element(ard,_,LisDep),A), |
leng(LisDep,DlDep),wyswietlDepe(LisDep,DlDep,0). | leng(LisDep,DlDep),wyswietlDepe(LisDep,DlDep,0). |
| |
wyswietlDepe(L1,0,Dl). | wyswietlDepe(_,0,_). |
wyswietlDepe(L1,D1,Dl):- | wyswietlDepe(L1,D1,Dl):- |
D1>0, | D1>0, |
NewDlu is D1-1, | NewDlu is D1-1, |
deleteOne(Dl,L1,element(dep,[independent=Ind,dependent=Dep],[])), | deleteOne(Dl,L1,element(dep,[independent=Ind,dependent=Dep],[])), |
string_length(Ind,L), | string_length(Ind,L), |
LEEE is L-4, | LEEE is L-4, |
sub_string(Ind,4,LEEE,_,WYJ), | sub_string(Ind,4,LEEE,_,WYJ), |
string_length(Dep,LENG), | string_length(Dep,LENG), |
LEE is LENG-4, | LEE is LENG-4, |
sub_string(Dep,4,LEE,_,Output), | sub_string(Dep,4,LEE,_,Output), |
checkDep(WYJ,Output,Indep,Depen),assert(ard_depend(Indep,Depen)), | checkDep(WYJ,Output,Indep,Depen),assert(ard_depend(Indep,Depen)), |
DlNew is Dl+1, | DlNew is Dl+1, |
wyswietlDepe(L1,NewDlu,DlNew). | wyswietlDepe(L1,NewDlu,DlNew). |
| |
%%%%%%%%%%%%%% pobrac independent i dependent%%%%%%%%%%%%%% | %%%%%%%%%%%%%% Get independent i dependent %%%%%%%%%%%%%% |
checkDep(In,De,InOut,DeOut):- | checkDep(In,De,InOut,DeOut):- |
atom_number(In,Inn), | atom_number(In,Inn), |
atom_number(De,Depe), | atom_number(De,Depe), |
view(All), | view(All), |
NInn is Inn+1, | NInn is Inn+1, |
NDepe is Depe+1, | NDepe is Depe+1, |
showDependent(All,NInn,InOut), | showDependent(All,NInn,InOut), |
showDependent(All,NDepe,DeOut). | showDependent(All,NDepe,DeOut). |
| |
showDependent(All,1,Output):- | showDependent(All,1,Output):- |
deleteOne(0,All,Output). | deleteOne(0,All,Output). |
| |
showDependent(All,Position,Output):- | showDependent(All,Position,Output):- |
deleteOne(Position-1,All,Output). | deleteOne(Position-1,All,Output). |
| |
%%%%%%%%%%%%%%%%%%%%%%%%%%% Pozostałe %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
| |
| %%%%%%%%%%%%%%%%%%%%%%%%%%% Others %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| % sprawdzanie istnienia elementów w liście |
include(X,[X|_]). | include(X,[X|_]). |
include(X,[_|Tail]) :- | include(X,[_|Tail]) :- |
include(X,Tail). | include(X,Tail). |
| % obliczanie długości listy |
leng([],0). | leng([],0). |
leng([_|Tail],Dlug) :- | leng([_|Tail],Dlug) :- |
leng(Tail,X), | leng(Tail,X), |
Dlug is X+1. | Dlug is X+1. |
| % wyciąganie konkretnego elementu listy |
deleteOne(0,[F|_],F). | deleteOne(0,[F|_],F). |
deleteOne(Poz,[_|Xx],W):- | deleteOne(Poz,[_|Xx],W):- |
deleteOne(PozNew,Xx,W). | deleteOne(PozNew,Xx,W). |
| |
| % dodanie do listy |
add([],X,[X]). | add([],X,[X]). |
add([G|O],X,[G|R]):- | add([G|O],X,[G|R]):- |
add(O,X,R). | add(O,X,R). |
| |
| |
| |
| |
| |
</code> | </code> |
| |
{{:pl:miw:varda_pl2xml.pl|Translator}} {{:pl:miw:input.xml|Plik wejściowy}} {{:pl:miw:out.pl|Plik wyjściowy}} | {{:pl:miw:varda_pl2xml.pl|Translator}} {{:pl:miw:input.xml|Plik wejściowy}} {{:pl:miw:out.pl|Plik wyjściowy}} |
| |
| |
| Dla potrzeb wyświetlenia wyników plik wyjściowy został powyżej dołączony z rozszerzeniem pl. |
| |
| |
| |
| |
| |
| |
| |
| |
| ====Napotkane problemy==== |
| ===Usunięcie białych znaków=== |
| W pliku mamy znaczniki nowej lini np:('\n','\n ','\n ') bez ich usunięcia nie można dotrzeć do wnętrza pliku. |
| Korzystajac z "usun" |
| <code prolog> |
| usun(X,[X|Reszta],Reszta). |
| usun(X,[Y|Ogon],[Y|Reszta]) :- |
| usun(X,Ogon,Reszta). |
| </code> |
| nie daje oczekiwanych efektów. |
| element(hml,X,C),usun('\n',C,B),write(B). nie usuwa wszystkich znaków lecz tylko ostatni. |
| |
| http://www.swi-prolog.org/packages/sgml2pl.html#sec:sec-3.1 |
| http://www.swi-prolog.org/packages/sgml2pl.html#sec:space |
| |
| space(sgml) In SGML, newlines at the start and end of an element are removed.2 This is the default mode for the SGML dialect. |
| space(default) In addition to sgml space-mode, all consequtive white-space is reduced to a single space-character. This mode canonises all white space. |
| |
| |
| Za pomocą poniższego kodu ładuję plik usuwając spacje oraz znaczniki końca lini: |
| <code prolog> |
| load_structure('ard.xml',X,[space(remove),dialect(xml)]),Z=..X,assertz(Z). |
| </code> |
| |
| |
| ===Sprawdzanie długości stringów=== |
| W porównaniu z Varda w wersji 0, należało znależć i zastosować odpowiednie predykaty: string_lenht/2 oraz sub_string/4. |
| <code prolog> |
| string_length(Bb,L), |
| LEEE is L-4, |
| sub_string(Bb,4,LEEE,_,WYJ), |
| </code> |
| ===Rzutowanie z stringa na inta=== |
| <code prolog> |
| atom_number(Zr,Zrr) |
| </code> |
| ===Wyciąganie konkretnego elementu z listy=== |
| Wyciąganie odpowiednich elementów z listy wykonywałem poniższym predykatem. |
| <code prolog> |
| deleteOne(0,[F|_],F). |
| deleteOne(Poz,[_|Xx],W):- |
| Poz>0, |
| PozNew is Poz-1, |
| deleteOne(PozNew,Xx,W). |
| </code> |
| ===Przechowywanie listy w pamięci=== |
| W pamięciu przechowwane sa wszystkie atrybuty. |
| <code prolog> |
| writeProp(CA,C,[]),memoryList(Lista),assert(ard_property(Lista)), |
| view(ListaGlob),add(ListaGlob,Lista,ListaGlob2), |
| retractall(view(_)), |
| assert(view(ListaGlob2)), |
| retractall(memoryList(_)), |
| </code> |
| ===Podczas kompilacji=== |
| Podczas kompilacji otrzymuję komunikat, który jednak nie ma wpływu na otrzymany wynik translacji. |
| <code prolog> |
| ERROR: SGML2PL(xml): input.xml:2: file ".\hml.dtd" does not exist |
| </code> |
| Błąd ten dotyczy linijki, która pojawiła sie w wersji 1 Vardy. |
| <code prolog> |
| <!DOCTYPE hml SYSTEM "hml.dtd"> |
| |
| </code> |
| |
| ====Otrzymany wynik translacji==== |
| Otrzymujemy następujący efekt: |
| <code xml> |
| <?xml version="1.0" encoding="utf-8"?> |
| :- dynamic ard_att/1. |
| |
| ard_att('Thermostat'). |
| ard_att('Time'). |
| ard_att('Temperature'). |
| ard_att('Date'). |
| ard_att('Hour'). |
| ard_att(season). |
| ard_att(operation). |
| ard_att(thermostat_settings). |
| ard_att(day). |
| ard_att(month). |
| ard_att(today). |
| ard_att(hour). |
| |
| :- dynamic ard_property/1. |
| |
| ard_property(['Thermostat']). |
| ard_property(['Time', 'Temperature']). |
| ard_property(['Time']). |
| ard_property(['Temperature']). |
| ard_property(['Date', 'Hour', season, operation]). |
| ard_property(['Date', 'Hour']). |
| ard_property([season, operation]). |
| ard_property([thermostat_settings]). |
| ard_property([season]). |
| ard_property([operation]). |
| ard_property(['Date']). |
| ard_property(['Hour']). |
| ard_property([day, month, today]). |
| ard_property([month]). |
| ard_property([day, today]). |
| ard_property([day]). |
| ard_property([today]). |
| ard_property([hour]). |
| |
| :- dynamic ard_hist/2. |
| |
| ard_hist(['Thermostat'], ['Time', 'Temperature']). |
| ard_hist(['Time', 'Temperature'], ['Time']). |
| ard_hist(['Time', 'Temperature'], ['Temperature']). |
| ard_hist(['Time'], ['Date', 'Hour', season, operation]). |
| ard_hist(['Date', 'Hour', season, operation], ['Date', 'Hour']). |
| ard_hist(['Date', 'Hour', season, operation], [season, operation]). |
| ard_hist(['Temperature'], [thermostat_settings]). |
| ard_hist([season, operation], [season]). |
| ard_hist([season, operation], [operation]). |
| ard_hist(['Date', 'Hour'], ['Date']). |
| ard_hist(['Date', 'Hour'], ['Hour']). |
| ard_hist(['Date'], [day, month, today]). |
| ard_hist([day, month, today], [month]). |
| ard_hist([day, month, today], [day, today]). |
| ard_hist([day, today], [day]). |
| ard_hist([day, today], [today]). |
| ard_hist(['Hour'], [hour]). |
| |
| :- dynamic ard_depend/2. |
| |
| ard_depend([season], [thermostat_settings]). |
| ard_depend([operation], [thermostat_settings]). |
| ard_depend([month], [season]). |
| ard_depend([day], [today]). |
| ard_depend([today], [operation]). |
| ard_depend([hour], [operation]). |
| |
| |
| </code> |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
<code xml> | <code xml> |
[ | [, element(xtt_table, [id=xtt_1], [ |
, element(xtt_table, [id=xtt_1], [ | |
, element(schema, [], [ | , element(schema, [], [ |
, element(independent, [], [ | , element(independent, [], [ |
] | ] |
| |
<code> | </code> |
| |
====== Materiały ====== | ====== Materiały ====== |