comparison cpack/p2r/lib/uripattern.old.pl @ 0:718306e29690 tip

commiting public release
author Daniel Wolff
date Tue, 09 Feb 2016 21:05:06 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:718306e29690
1 /* Part of DML (Digital Music Laboratory)
2 Copyright 2014-2015 Samer Abdallah, University of London
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 :- module(uripattern, [
20 object/2
21 , resource/2
22 , literal/2
23 , max_reduce/2
24 ]).
25
26 :- use_module(library(semweb/rdf_db), [ rdf_current_ns/2 ]).
27
28 :- multifile uripattern:macro/2.
29
30 object(A,B) :- freeze(A, object1(A,B)).
31 literal(A,B) :- freeze(A, literal1(A,B)).
32 resource(A,B) :- freeze(A, resource1(A,B)).
33 plain(A,B) :- freeze(A, plain1(A,B)).
34
35 object1(literal(A),literal(B)) :- !, literal(A,B).
36 object1(A,B) :- resource1(A,B).
37
38 literal1(type(T,A),type(T1,A1)) :- !, resource(T,T1), plain(A,A1).
39 literal1(A,B) :- plain1(A,B).
40
41 resource1(P:A,PB) :- !, rdf_current_ns(P,Q), atom_concat1(Q,B,PB), plain(A,B).
42 resource1(A,B) :- plain1(A,B).
43
44 plain1(pat(L),B) :- freeze(L,conc(L,B)).
45 plain1(num(N),B) :- when(nonvar(N);nonvar(B),atom_number1(B,N)).
46 plain1(mac(M),B) :- when(nonvar(M);nonvar(B),macro(M,B)).
47 plain1(enc(A),B) :- plain(A,C), when(nonvar(C);nonvar(B),www_form_encode(C,B)).
48 plain1(atm(A),A).
49 plain1(A1/A2,B) :- freeze(B,sub_atom(B,_,_,_,'/')), plain1(A1+'/'+A2,B).
50 plain1(A1+A2,B) :- atom_concat1(B1,B2,B), plain(A1,B1), plain(A2,B2).
51 plain1(A,B) :- (atom(A);atom(B)), A=B.
52
53 conc([],'').
54 conc([H|T],B) :- atom_concat1(H,TA,B), freeze(T,conc(T,TA)).
55
56 atom_number1(A,N) :-
57 catch(atom_number(A,N), error(syntax_error(_),_), fail).
58
59 % like atom_concat but with more modes, using frozen constraints.
60 atom_concat1(A,B,C) :-
61 freeze(A, (A=''->B=C;true)),
62 freeze(B, (B=''->A=C;true)),
63 when( ( nonvar(A),nonvar(B)
64 ; nonvar(B),nonvar(C)
65 ; nonvar(A),nonvar(C)
66 ), atom_concat(A,B,C)).
67
68
69 % non-deterministic maximal reduction
70 max_reduce(E1,E3) :-
71 reduce(E1,E2) *-> max_reduce(E2,E3); E1=E3.
72
73 % simplify resource or literal expression if possible
74 reduce(A,A) :- var(A), !, fail.
75 reduce(A/B,A+'/'+B).
76 reduce(A/B,A1/B) :- reduce(A,A1).
77 reduce(A/B,A/B1) :- reduce(B,B1).
78 reduce(A/B/C,A/(B/C)).
79 reduce(A+B,A1+B) :- reduce(A,A1).
80 reduce(A+B,A+B1) :- reduce(B,B1).
81 reduce(A+B,AB) :- atom(A), atom(B), atom_concat(A,B,AB).
82 reduce(A+B,_) :- (var(A); var(B)), !, fail.
83 reduce(A+B/C,(A+B)/C).
84 reduce((A+B)+C,A+BC) :- atom(B), atom(C), atom_concat(B,C,BC).
85 reduce(A+(B+C),AB+C) :- atom(A), atom(B), atom_concat(A,B,AB).
86 reduce(P:A,Q+A) :- must_be(atom,P),
87 ( rdf_current_prefix(P,Q) -> true
88 ; throw(unknown_prefix(P))
89 ).
90
91 reduce(num(N),A) :- number(N), atom_number(A,N).
92 reduce(atm(A),A) :- atom(A).
93 reduce(mac(A),B) :- atom(A), macro(A,B).
94 reduce(enc(A),B) :- atom(A), www_form_encode(A,B).
95 reduce(enc(A),enc(B)):- reduce(A,B).
96 reduce(A+pat(P),pat([A|P])) :- atom(A), nonvar(P).
97 reduce(pat(P)+A,pat(P1)) :- atom(A), nonvar(P), concat(P,[A],P1).
98 reduce(pat([A1,A2|AX]),pat([A12|AX])) :-
99 atom(A1), atom(A2), atom_concat(A1,A2,A12).
100
101 reduce(literal(E),literal(E1)) :- reduce(E,E1).
102 reduce(type(T,E),type(T1,E)) :- reduce(T,T1).
103 reduce(type(T,E),type(T,E1)) :- reduce(E,E1).
104 reduce(lang(V,L),lang(V1,L)) :- reduce(V,V1).
105 reduce(lang(V,L),lang(V,L1)) :- reduce(L,L1).
106