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 simplify/2
|
Daniel@0
|
21 , pattern_uri/2
|
Daniel@0
|
22 ]).
|
Daniel@0
|
23
|
Daniel@0
|
24 :- use_module(library(semweb/rdf_db), [ rdf_global_id/2 ]).
|
Daniel@0
|
25
|
Daniel@0
|
26 :- multifile uripattern:def/2.
|
Daniel@0
|
27
|
Daniel@0
|
28 %% uri_constraint(+P:uripattern,?X:uri) is nondet.
|
Daniel@0
|
29 % Logical predicate implemented using delayed goals.
|
Daniel@0
|
30 pattern_uri(A,B) :- must_be(nonvar,A), resource(A,B).
|
Daniel@0
|
31
|
Daniel@0
|
32 resource(P:A,PB) :- !, rdf_global_id(P:'',Q), plain(Q+A,PB).
|
Daniel@0
|
33 resource(A,B) :- plain(A,B).
|
Daniel@0
|
34
|
Daniel@0
|
35 plain(\(M),B) :- def(M,A), (var(A) -> true; plain(A,B)).
|
Daniel@0
|
36 plain(A1/A2,B) :- plain(A1+'/'+A2,B).
|
Daniel@0
|
37 plain(num(N),B) :- when((nonvar(N);nonvar(B)), atom_number1(B,N)).
|
Daniel@0
|
38 plain(enc(A),B) :- when((nonvar(A);nonvar(B)), www_form_encode1(A,B)).
|
Daniel@0
|
39 plain(atm(A),A).
|
Daniel@0
|
40 plain(A1+A2,B) :-
|
Daniel@0
|
41 freeze(A1,plain(A1,B1)),
|
Daniel@0
|
42 freeze(A2,plain(A2,B2)),
|
Daniel@0
|
43 when((ground(B1),ground(B2);ground(B)), atom_concat1(B1,B2,B)).
|
Daniel@0
|
44 plain(A,A) :- atom(A).
|
Daniel@0
|
45
|
Daniel@0
|
46 %% simplify(+P1:uri_pattern, -P2:uri_pattern) is nondet.
|
Daniel@0
|
47 % Non-deterministic maximal reduction of URI pattern.
|
Daniel@0
|
48 simplify(E1,E3) :-
|
Daniel@0
|
49 reduce(E1,E2) *-> simplify(E2,E3); E1=E3.
|
Daniel@0
|
50
|
Daniel@0
|
51 % simplify resource if possible, fails otherwise
|
Daniel@0
|
52 % The pattern must be nonvar.
|
Daniel@0
|
53 reduce(A,B) :- must_be(nonvar,A), reduce1(A,B).
|
Daniel@0
|
54
|
Daniel@0
|
55 reduce1(num(N),A) :- number(N), atom_number(A,N).
|
Daniel@0
|
56 reduce1(atm(A),A) :- atomic(A).
|
Daniel@0
|
57 reduce1(\(A),B) :- nonvar(A), def(A,B).
|
Daniel@0
|
58 reduce1(enc(A),B) :- atomic(A), www_form_encode(A,B).
|
Daniel@0
|
59 reduce1(P:A,Q+A) :- rdf_global_id(P:'',Q).
|
Daniel@0
|
60 reduce1(A/B,A+'/'+B).
|
Daniel@0
|
61 reduce1(A+B,A1+B) :- reduce(A,A1).
|
Daniel@0
|
62 reduce1(A+B,A+B1) :- reduce(B,B1). % nb to get past these, A and B must be nonvar
|
Daniel@0
|
63 reduce1(A+B,AB) :- atom(A), atom(B), atom_concat(A,B,AB).
|
Daniel@0
|
64 reduce1((A+B)+C,A+BC) :- reduce1(B+C,BC).
|
Daniel@0
|
65 reduce1(A+(B+C),AB+C) :- reduce1(A+B,AB).
|
Daniel@0
|
66
|
Daniel@0
|
67 % ---------- support predicates ---------
|
Daniel@0
|
68
|
Daniel@0
|
69 atom_number1(Atom,Number) :-
|
Daniel@0
|
70 (nonvar(Number) -> number(Number); true),
|
Daniel@0
|
71 atom_number(Atom,Number).
|
Daniel@0
|
72
|
Daniel@0
|
73 www_form_encode1(Atom,Encoded) :-
|
Daniel@0
|
74 (nonvar(Encoded) -> atomic(Encoded); true),
|
Daniel@0
|
75 www_form_encode(Atom,Encoded).
|
Daniel@0
|
76
|
Daniel@0
|
77 atom_concat1(A,B,AB) :-
|
Daniel@0
|
78 (nonvar(AB) -> atomic(AB); true),
|
Daniel@0
|
79 atom_concat(A,B,AB).
|
Daniel@0
|
80
|