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