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