Both sides previous revision
Poprzednia wersja
Nowa wersja
|
Poprzednia wersja
|
pl:miw:miw08_prolog_xml:prolog_api [2008/05/26 00:08] 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 ===== | |
| |
| |
belongs(X,Yogon). | belongs(X,Yogon). |
| |
| first(_,L):- |
| L == [],!. |
first(E,L) :- | first(E,L) :- |
| L \= [], |
append([],[E|_],L). | append([],[E|_],L). |
| |
take1(1,[X|_],X). | |
take1(X,[_|T],Z):- | |
Y is X-1, | |
take1(Y,T,Z). | |
take(X,L):- | |
belongs(X,L), | |
assert(X), | |
write(X). | |
| |
max([], A, A). | max([], A, A). |
| |
</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), |
==== Predykaty związane z przeszukiwaniem oraz budowaniem struktury prologowej xml ==== | retractall(pair(_,_,_)), |
<code prolog> | retractall(need_build(_)). |
| |
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,Q):-wezel(_,element(X,_,_),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), |
( W == A | first(ID1,W1), |
-> | first(ID2,B1), |
add(element(W,W1,W2),C1,NL), | first(ID3,B), |
assert(element(A1,B1,NL)), | check_id1(ID1,ID3,ID2,A,B,C,A1,B1,C1,W,W1,W2,L). |
retract(element(W,W1,W2)), | |
Ln is L-1, | |
build(Ln) | |
; | |
( W == A1 | |
-> add(element(A,B,C),W2,New), | |
assert(element(W,W1,New)), | |
retract(element(W,W1,W2)), | |
what(W,Q), | |
%jesli nie nalezy do root | |
( Q \= 1 | |
-> retract(wezel(element(W,_,_),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,Q), | |
%jesli nie nalezy do root | |
( Q \= 1 | |
-> retract(wezel(element(A1,_,_),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(*,[],[])), | assert(element(*,[id=0],[])), |
build(X). | build(X). |
| |
member2(D,L,O,P,P3):- | member3(_,L,_,P,P3):-L == [],P3 is P,!. |
| 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> |
| |
| |
| |
| |
% tworzenie korzenia dokumentu | % tworzenie korzenia dokumentu |
create_root(Name):- | create_root(Name,ID_name):- |
current_id(Name,N), | create_id(ID_name,ID), |
id(Name,N,ID), | assert(element(Name,[id=ID],[])). |
Nn is N + 1, | |
assert(element(Name,[id=ID],[])), | |
assert(current_id(Name,Nn)), | |
retract(current_id(Name,N)). | |
| |
% dodawanie dzieci do wybranego rodzica o wybranym id | % dodawanie dzieci do wybranego rodzica o wybranym id |
app_child(Parent,P_id, Child):- | app_child(Parent,P_id,Child,ChildIdName):- |
Parent\=Child, | Parent\=Child, |
assert(bylem(0)), | assert(need_build(0)), |
search(Parent,A,L,1,P_id), | search(Parent,A,L,1,P_id), |
current_id(Child,CI), | create_id(ChildIdName,ID), |
id(Child,CI,ID), | |
CIn is CI + 1, | |
add2end(element(Child,[id=ID],[]),L,L1), | add2end(element(Child,[id=ID],[]),L,L1), |
retract(element(Parent,A,L)), | retract(element(Parent,A,L)), |
assert(element(Parent,A,L1)), | assert(element(Parent,A,L1)), |
bylem(Q), | need_build(Q), |
( Q==1 | check_need_build(Q,Parent,A,L,L1,1). |
-> retract(wezel(element(Parent,A,L),Z,P)), | |
assert(wezel(element(Parent,A,L1),Z,P)), | |
retractall(element(_,_,_)), | |
maxo(I), | |
build1(I), | |
retractall(wezel(_,_,_)), | |
retractall(bylem(_)) | |
; ! | |
), | |
retract(current_id(Child,CI)), | |
assert(current_id(Child,CIn)). | |
| |
% dodawanie dzieci do wybranego rodzica o wybranym id - wersja z mozliwym | % dodawanie dzieci do wybranego rodzica o wybranym id - wersja z mozliwym |
% okresleniem ilosci dodawanych dzieci | % okresleniem ilosci dodawanych dzieci |
app_child(Parent,P_id, Child,0):-true. | app_child(_,_,_,_,0). |
app_child(Parent,P_id, Child,How_much):- | app_child(Parent,P_id,Child,ChildIdName,How_much):- |
How_much > 0, | How_much > 0, |
app_child(Parent,P_id, Child), | app_child(Parent,P_id,Child,ChildIdName), |
New is How_much - 1, | New is How_much - 1, |
app_child(Parent,P_id, Child,New). | app_child(Parent,P_id,Child,ChildIdName,New). |
| |
% dodawanie atrybutu do elementu o zadanym id | % dodawanie atrybutu do elementu o zadanym id |
add_attribute(Parent,P_id,Attr):- | add_attribute(Parent,P_id,Attr):- |
assert(bylem(0)), | assert(need_build(0)), |
search(Parent,AttL,L,1,P_id), | search(Parent,AttL,L,1,P_id), |
add2end(Attr,AttL,A1), | add2end(Attr,AttL,A1), |
retract(element(Parent,AttL,L)), | retract(element(Parent,AttL,L)), |
assert(element(Parent,A1,L)), | assert(element(Parent,A1,L)), |
bylem(Q), | need_build(Q), |
( Q==1 | check_need_build(Q,Parent,AttL,A1,L,2). |
-> retract(wezel(element(Parent,AttL,L),Z,P)), | |
assert(wezel(element(Parent,A1,L),Z,P)), | |
retractall(element(_,_,_)), | |
maxo(I), | |
build1(I), | |
retractall(wezel(_,_,_)), | |
retractall(bylem(_)) | |
; | |
true | |
). | |
</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> |
| |
| |
| |
| |
| |