comparison cpack/p2r/lib/uripattern_detdcg.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 simplify/2
21 , pattern_uri/2
22 ]).
23
24 :- use_module(library(semweb/rdf_db), [ rdf_global_id/2 ]).
25 :- use_module(library(uri), [ uri_encoded/3 ]).
26 :- use_module(library(dcg/basics)).
27
28 :- multifile uripattern:def/2.
29
30 %% pattern_uri(+P:uripattern,-X:uri) is det.
31 %% pattern_uri(-P:uripattern,+X:uri) is det.
32 pattern_uri(P,URI) :-
33 ( var(URI) -> uri(P,Codes,[]), atom_codes(URI,Codes)
34 ; atomic(URI) -> atom_codes(URI,Codes), uri(P,Codes,[])
35 ).
36
37 uri(P:A) --> !, {rdf_global_id(P:'',Q)}, atom(Q), uri(A).
38 uri(\(M)) --> !, {def(M,A)}, uri(A).
39 uri(num(N)) --> !, number(N).
40 uri(num(Len,N)) -->
41 { length(Codes,Len) },
42 phrase(Codes),
43 { var(N)
44 -> number_codes(N,Codes)
45 ; format(codes(Codes),'~`0t~d~*+',[N,Len])
46 }.
47 uri(tail(A)) --> !,
48 ( {nonvar(A)} -> atom(A), eos
49 ; string_without("",C), eos, {atom_codes(A,C)}
50 ).
51 uri(enc(A)) --> !,
52 ( {nonvar(A)} -> {uri_encoded(path,A,B)}, atom(B)
53 ; string_without("/",C), {atom_codes(B,C), uri_encoded(path,A,B)}
54 ).
55 uri(A1/A2) --> !, uri(A1), "/", uri(A2).
56 uri(A) --> {atomic(A)}, atom(A).
57
58 %% simplify(+P1:uri_pattern, -P2:uri_pattern) is nondet.
59 % Non-deterministic maximal reduction of URI pattern.
60 simplify(E1,E3) :-
61 reduce(E1,E2) *-> simplify(E2,E3); E1=E3.
62
63 % simplify resource if possible, fails otherwise
64 % The pattern must be nonvar.
65 reduce(A,B) :- must_be(nonvar,A), reduce1(A,B).
66
67 reduce1(num(N),A) :- number(N), atom_number(A,N).
68 reduce1(num(L,N),A) :- number(N), format(atom(A),'~`0t~d~*+',[N,L]). % zero padded number
69 reduce1(\(A),B) :- must_be(nonvar,A), def(A,B).
70 reduce1(enc(A),B) :- atomic(A), uri_encoded(path,A,B).
71 reduce1(tail(A),A) :- atomic(A).
72 reduce1(P:A,P:B) :- reduce(A,B).
73 reduce1(P:A,PB) :- atomic(A), rdf_global_id(P:A,PB).
74 reduce1(P:(A/B),PA/B) :- reduce(P:A,PA).
75 reduce1(A/B,A1/B) :- reduce(A,A1).
76 reduce1(A/B,A/B1) :- reduce(B,B1). % nb to get past these, A and B must be nonvar
77 reduce1(A/B,AB) :- atom(A), atom(B), atomic_list_concat([A,B],'/',AB).
78 reduce1((A/B)/C,A/BC) :- reduce1(B/C,BC).
79 reduce1(A/(B/C),AB/C) :- reduce1(A/B,AB).
80 reduce1((A/B)/C,A/(B/C)).
81