Both sides previous revision
Poprzednia wersja
Nowa wersja
|
Poprzednia wersja
|
pl:miw:miw08_prolog_xml:prolog_api [2008/05/28 16:00] miw |
pl:miw:miw08_prolog_xml:prolog_api [2019/06/27 15:50] (aktualna) |
===== API ===== | ===== API ===== |
| |
| Plik API do pobrania tutaj: |
| {{:pl:miw:miw08_prolog_xml:xml_api.pl|plik xml_api.pl}} |
| |
===== Wersja 1 - płaska struktura ===== | |
<code prolog> | |
delete(A, [A|B], B). | |
delete(B, [A|C], [A|D]) :- | |
delete(B, C, D). | |
| |
add(A, B, [A|B]). | |
| |
| |
make_node(Name,List) :- | ===== Wersja główna ===== |
add(element(Name, [], []), [], List), | |
X=..List, | |
assert(X), | |
write(List). | |
| |
make_node(Name, Attr, Cont, List) :- | |
add(element(Name, Attr, Cont), [], List), | |
X=..List, | |
assert(X), | |
write(List). | |
| |
add_child(Parent,Child,Nlist):- | |
element(Parent,A,C), | |
retract(element(Parent,A,C)), | |
add(element(Child,[],[]),C,L), | |
make_node(Parent,A,L,Nlist). | |
| |
add_child(Parent,Child,Ch_att,Ch_cont,Nlist):- | |
element(Parent,A,C), | |
retract(element(Parent,A,C)), | |
add(element(Child,Ch_att,Ch_cont),C,L), | |
make_node(Parent,A,L,Nlist). | |
| |
make_list(List) :- | |
bagof(A,element(A,B,C),A), | |
L=..A, | |
add(element(L,B,C),[],List), | |
write(List). | |
| |
save :- | |
tell('wiedza.pl'), | |
listing(element), | |
told. | |
| |
prolog2xml(List,File):- | |
tell(File), | |
open(File,write,S), | |
xml_write(S,List,[]), | |
told, | |
close(S). | |
| |
---------------------------------- | |
| |
add_child(Parent,Child,Nlist):- | |
element(Parent,A,C), | |
element(Child,Chatt,Chcont), | |
retract(element(Parent,A,C)), | |
retract(element(Child,Chatt,Chcont)), | |
add(element(Child,Chatt,Chcont),C,L), | |
make_node(Parent,A,L,Nlist). | |
| |
add_child(Parent,A,Child,Chatt,Nlist):- | |
element(Parent,A,C), | |
element(Child,Chatt,Chcont), | |
retract(element(Parent,A,C)), | |
retract(element(Child,Chatt,Chcont)), | |
add(element(Child,Chatt,Chcont),C,L), | |
make_node(Parent,A,L,Nlist). | |
</code> | |
| |
| |
| |
| |
| |
| |
| |
===== Wersja 2 ===== | |
| |
| |
| |
</code> | </code> |
| |
| |
==== Predykaty związane z nadawaniem id ==== | ==== Predykaty związane z nadawaniem id ==== |
:- dynamic current_id/2. | :- dynamic current_id/2. |
| |
current_id(hml,0). | current_id(*,0). |
current_id(type_set,0). | |
current_id(property_set,0). | |
current_id(property,0). | |
current_id(attref,0). | |
current_id(attribute_set,0). | |
current_id(att,0). | |
current_id(tph,0). | |
current_id(trans,0). | |
current_id(ard,0). | |
current_id(dep,0). | |
| |
id(hml,X,Id):- | create_id(X,ID):- |
number_chars(X,N), | current_id(X,N), |
string_to_list(Ids,['h','m','l','_'|N]), | N1 is N + 1, |
string_to_atom(Ids,Id). | retractall(current_id(X,_)), |
| assert(current_id(X,N1)), |
| string_concat(X,'_',ID1), |
| string_concat(ID1,N1,ID2), |
| string_to_atom(ID2,ID). |
| |
| create_id(X,ID):- |
| assert(current_id(X,0)), |
| string_concat(X,'_',ID1), |
| string_concat(ID1,0,ID2), |
| string_to_atom(ID2,ID). |
| |
id(type_set,X,Id):- | </code> |
number_chars(X,N), | |
string_to_list(Ids,['t','y','p','_','s','_'|N]), | |
string_to_atom(Ids,Id). | |
| |
id(property_set,X,Id):- | |
number_chars(X,N), | |
string_to_list(Ids,['p','r','p','_','s','_'|N]), | |
string_to_atom(Ids,Id). | |
| |
id(property,X,Id):- | |
number_chars(X,N), | |
string_to_list(Ids,['p','r','p','_'|N]), | |
string_to_atom(Ids,Id). | |
| |
id(attref,X,Id):- | |
number_chars(X,N), | |
string_to_list(Ids,['a','t','t','_','r','_'|N]), | |
string_to_atom(Ids,Id). | |
| |
id(attribute_set,X,Id):- | |
number_chars(X,N), | |
string_to_list(Ids,['a','t','t','_','s','_'|N]), | |
string_to_atom(Ids,Id). | |
| |
id(att,X,Id):- | |
number_chars(X,N), | |
string_to_list(Ids,['a','t','t','_'|N]), | |
string_to_atom(Ids,Id). | |
| |
id(tph,X,Id):- | ==== Predykaty związane z przeszukiwaniem oraz budowaniem struktury prologowej xml ==== |
number_chars(X,N), | <code prolog> |
string_to_list(Ids,['t','p','h','_'|N]), | |
string_to_atom(Ids,Id). | |
| |
id(trans,X,Id):- | check_need_build(X,_,_,_,_,_):- |
number_chars(X,N), | X == 0,!. |
string_to_list(Ids,['t','r','n','_'|N]), | |
string_to_atom(Ids,Id). | |
| |
id(ard,X,Id):- | check_need_build(X,P,P1,P2,L,W):- |
number_chars(X,N), | X == 1, |
string_to_list(Ids,['a','r','d','_'|N]), | W == 1, |
string_to_atom(Ids,Id). | retract(pair(element(P,P1,P2),Z,Z1)), |
| assert(pair(element(P,P1,L),Z,Z1)), |
| retractall(element(_,_,_)), |
| maxo(I), |
| build1(I), |
| retractall(pair(_,_,_)), |
| retractall(need_build(_)). |
| |
id(dep,X,Id):- | check_need_build(X,P,P1,P2,L,W):- |
number_chars(X,N), | X == 1, |
string_to_list(Ids,['d','e','p','_'|N]), | W == 2, |
string_to_atom(Ids,Id). | retract(pair(element(P,P1,L),Z,Z1)), |
</code> | assert(pair(element(P,P2,L),Z,Z1)), |
| retractall(element(_,_,_)), |
| maxo(I), |
| build1(I), |
| retractall(pair(_,_,_)), |
| retractall(need_build(_)). |
==== Predykaty związane z przeszukiwaniem oraz budowaniem struktury prologowej xml ==== | |
<code prolog> | |
| |
search(N,LA,LN,_,P_id):- | search(N,LA,LN,_,P_id):- |
bagof(N,element(N,LA,LN),_), | element(N,LA,LN), |
| %bagof(N,element(N,LA,LN),_), |
belongs(P_id,LA). | belongs(P_id,LA). |
| |
element(X,X1,L), | element(X,X1,L), |
X\=N, | X\=N, |
member2(element(_,_,_),L,element(X,X1,[]),P1,P2),!, | member3(element(_,_,_),L,element(X,X1,[]),P1,P2),!, |
Pn is P2, | Pn is P2, |
retract(element(X,X1,L)), | retract(element(X,X1,L)), |
| |
maxo(X):- | maxo(X):- |
findall(X,wezel(_,_,X),L), | findall(X,pair(_,_,X),L), |
max(L,0,X). | max(L,0,X). |
| |
what(X,Y,Q):-wezel(_,element(X,Y,_),Q). | check_pair(X,Y,N,L):- |
| pair(_,element(X,Y,_),Q), |
| Q \= 1, |
| retract(pair(element(X,Y,_),element(R,R1,R2),R4)), |
| assert(pair(element(X,Y,N),element(R,R1,R2),R4)), |
| Ln is L-1, |
| build(Ln). |
| |
| check_pair(X,Y,_,L):- |
| pair(_,element(X,Y,_),Q), |
| Q == 1, |
| Ln is L-1, |
| build(Ln). |
| |
| check_id2(X,Y,A,B,C,_,_,_,W,W1,W2,L):- |
| X == Y, |
| add(element(A,B,C),W2,New), |
| assert(element(W,W1,New)), |
| retract(element(W,W1,W2)), |
| check_pair(W,W1,New,L). |
| |
| check_id2(X,Y,A,B,C,A1,B1,C1,W,W1,W2,L):- |
| X \= Y, |
| add(element(A,B,C),C1,New), |
| assert(element(A1,B1,New)), |
| retract(element(W,W1,W2)), |
| check_pair(A1,B1,New,L). |
| |
| check_id1(X,Y,_,_,_,_,A1,B1,C1,W,W1,W2,L):- |
| X == Y, |
| add(element(W,W1,W2),C1,NL), |
| assert(element(A1,B1,NL)), |
| retract(element(W,W1,W2)), |
| Ln is L-1, |
| build(Ln). |
| |
| check_id1(X,Y,Z,A,B,C,A1,B1,C1,W,W1,W2,L):- |
| X \= Y, |
| check_id2(X,Z,A,B,C,A1,B1,C1,W,W1,W2,L). |
| |
build(0):-!. | build(0):-!. |
build(L):- | build(L):- |
wezel(element(A,B,C),element(A1,B1,C1),L), | pair(element(A,B,C),element(A1,B1,C1),L), |
element(W,W1,W2), | element(W,W1,W2), |
first(ID1,W1), | first(ID1,W1), |
first(ID2,B1), | first(ID2,B1), |
( W == A | first(ID3,B), |
-> | check_id1(ID1,ID3,ID2,A,B,C,A1,B1,C1,W,W1,W2,L). |
add(element(W,W1,W2),C1,NL), | |
assert(element(A1,B1,NL)), | |
retract(element(W,W1,W2)), | |
Ln is L-1, | |
build(Ln) | |
; | |
%zgadza sie nazwa | |
( W == A1 | |
-> % zgadza sie id | |
( ID1 == ID2 | |
-> | |
add(element(A,B,C),W2,New), | |
assert(element(W,W1,New)), | |
retract(element(W,W1,W2)), | |
what(W,W1,Q), | |
%jesli nie nalezy do root | |
( Q \= 1 | |
-> retract(wezel(element(W,W1,_),element(R,R1,R2),R4)), | |
assert(wezel(element(W,W1,New),element(R,R1,R2),R4)), | |
Ln is L-1, | |
build(Ln) | |
; Ln is L-1, | |
build(Ln) | |
) | |
; | |
add(element(A,B,C),C1,New), | |
assert(element(A1,B1,New)), | |
retract(element(W,W1,W2)), | |
what(A1,B1,Q), | |
%jesli nie nalezy do root | |
( Q \= 1 | |
-> retract(wezel(element(A1,B1,_),element(R,R1,R2),R4)), | |
assert(wezel(element(A1,B1,New),element(R,R1,R2),R4)), | |
Ln is L-1, | |
build(Ln) | |
; Ln is L-1, | |
build(Ln) | |
) | |
) | |
; | |
add(element(A,B,C),C1,New), | |
assert(element(A1,B1,New)), | |
retract(element(W,W1,W2)), | |
what(A1,B1,Q), | |
%jesli nie nalezy do root | |
( Q \= 1 | |
-> retract(wezel(element(A1,B1,_),element(R,R1,R2),R4)), | |
assert(wezel(element(A1,B1,New),element(R,R1,R2),R4)), | |
Ln is L-1, | |
build(Ln) | |
; Ln is L-1, | |
build(Ln) | |
) | |
) | |
). | |
| |
build1(X):- | build1(X):- |
assert(element(*,[id=0],[])), | assert(element(*,[id=0],[])), |
build(X). | build(X). |
| |
| member3(_,L,_,P,P3):-L == [],P3 is P,!. |
member2(D,L,O,P,P3):- | member3(D,L,O,P,P3):- |
L\=[], | L\=[], |
P > 0, | |
belongs(D,L), | belongs(D,L), |
assert(D), | assert(D), |
assert(wezel(D,O,P)), | assert(pair(D,O,P)), |
P2 is P + 1, | P2 is P + 1, |
remove(D,L,Ln), | remove(D,L,Ln), |
retractall(bylem(_)), | retractall(need_build(_)), |
assert(bylem(1)), | assert(need_build(1)), |
( Ln ==[] | first(E,Ln), |
-> P3 is P2,true | member3(E,Ln,O,P2,P3). |
; first(E,Ln), | |
member2(E,Ln,O,P2,P3) | |
). | |
| |
</code> | </code> |
| |
</code> | </code> |
| |
| |
| |
| |
| |
| |
</code> | </code> |
| |
| ===== Wersja poprzednia - płaska struktura ===== |
| <code prolog> |
| delete(A, [A|B], B). |
| delete(B, [A|C], [A|D]) :- |
| delete(B, C, D). |
| |
| add(A, B, [A|B]). |
| |
| |
| make_node(Name,List) :- |
| add(element(Name, [], []), [], List), |
| X=..List, |
| assert(X), |
| write(List). |
| |
| make_node(Name, Attr, Cont, List) :- |
| add(element(Name, Attr, Cont), [], List), |
| X=..List, |
| assert(X), |
| write(List). |
| |
| add_child(Parent,Child,Nlist):- |
| element(Parent,A,C), |
| retract(element(Parent,A,C)), |
| add(element(Child,[],[]),C,L), |
| make_node(Parent,A,L,Nlist). |
| |
| add_child(Parent,Child,Ch_att,Ch_cont,Nlist):- |
| element(Parent,A,C), |
| retract(element(Parent,A,C)), |
| add(element(Child,Ch_att,Ch_cont),C,L), |
| make_node(Parent,A,L,Nlist). |
| |
| make_list(List) :- |
| bagof(A,element(A,B,C),A), |
| L=..A, |
| add(element(L,B,C),[],List), |
| write(List). |
| |
| save :- |
| tell('wiedza.pl'), |
| listing(element), |
| told. |
| |
| prolog2xml(List,File):- |
| tell(File), |
| open(File,write,S), |
| xml_write(S,List,[]), |
| told, |
| close(S). |
| |
| ---------------------------------- |
| |
| add_child(Parent,Child,Nlist):- |
| element(Parent,A,C), |
| element(Child,Chatt,Chcont), |
| retract(element(Parent,A,C)), |
| retract(element(Child,Chatt,Chcont)), |
| add(element(Child,Chatt,Chcont),C,L), |
| make_node(Parent,A,L,Nlist). |
| |
| add_child(Parent,A,Child,Chatt,Nlist):- |
| element(Parent,A,C), |
| element(Child,Chatt,Chcont), |
| retract(element(Parent,A,C)), |
| retract(element(Child,Chatt,Chcont)), |
| add(element(Child,Chatt,Chcont),C,L), |
| make_node(Parent,A,L,Nlist). |
| </code> |
| |
| |
| |
| |
| |