Daniel@0: /* Part of DML (Digital Music Laboratory) Daniel@0: Copyright 2014-2015 Samer Abdallah, University of London Daniel@0: Daniel@0: This program is free software; you can redistribute it and/or Daniel@0: modify it under the terms of the GNU General Public License Daniel@0: as published by the Free Software Foundation; either version 2 Daniel@0: of the License, or (at your option) any later version. Daniel@0: Daniel@0: This program is distributed in the hope that it will be useful, Daniel@0: but WITHOUT ANY WARRANTY; without even the implied warranty of Daniel@0: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Daniel@0: GNU General Public License for more details. Daniel@0: Daniel@0: You should have received a copy of the GNU General Public Daniel@0: License along with this library; if not, write to the Free Software Daniel@0: Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA Daniel@0: */ Daniel@0: Daniel@0: :- module(uripattern, [ Daniel@0: simplify/2 Daniel@0: , pattern_uri/2 Daniel@0: ]). Daniel@0: Daniel@0: :- use_module(library(semweb/rdf_db), [ rdf_global_id/2 ]). Daniel@0: :- use_module(library(uri), [ uri_encoded/3 ]). Daniel@0: :- use_module(library(dcg/basics)). Daniel@0: Daniel@0: :- multifile uripattern:def/2. Daniel@0: Daniel@0: %% pattern_uri(+P:uripattern,-X:uri) is det. Daniel@0: %% pattern_uri(-P:uripattern,+X:uri) is det. Daniel@0: pattern_uri(P,URI) :- Daniel@0: ( var(URI) -> uri(P,Codes,[]), atom_codes(URI,Codes) Daniel@0: ; atomic(URI) -> atom_codes(URI,Codes), uri(P,Codes,[]) Daniel@0: ). Daniel@0: Daniel@0: uri(P:A) --> !, {rdf_global_id(P:'',Q)}, atom(Q), uri(A). Daniel@0: uri(\(M)) --> !, {def(M,A)}, uri(A). Daniel@0: uri(num(N)) --> !, number(N). Daniel@0: uri(num(Len,N)) --> Daniel@0: { length(Codes,Len) }, Daniel@0: phrase(Codes), Daniel@0: { var(N) Daniel@0: -> number_codes(N,Codes) Daniel@0: ; format(codes(Codes),'~`0t~d~*+',[N,Len]) Daniel@0: }. Daniel@0: uri(tail(A)) --> !, Daniel@0: ( {nonvar(A)} -> atom(A), eos Daniel@0: ; string_without("",C), eos, {atom_codes(A,C)} Daniel@0: ). Daniel@0: uri(enc(A)) --> !, Daniel@0: ( {nonvar(A)} -> {uri_encoded(path,A,B)}, atom(B) Daniel@0: ; string_without("/",C), {atom_codes(B,C), uri_encoded(path,A,B)} Daniel@0: ). Daniel@0: uri(A1/A2) --> !, uri(A1), "/", uri(A2). Daniel@0: uri(A) --> {atomic(A)}, atom(A). Daniel@0: Daniel@0: %% simplify(+P1:uri_pattern, -P2:uri_pattern) is nondet. Daniel@0: % Non-deterministic maximal reduction of URI pattern. Daniel@0: simplify(E1,E3) :- Daniel@0: reduce(E1,E2) *-> simplify(E2,E3); E1=E3. Daniel@0: Daniel@0: % simplify resource if possible, fails otherwise Daniel@0: % The pattern must be nonvar. Daniel@0: reduce(A,B) :- must_be(nonvar,A), reduce1(A,B). Daniel@0: Daniel@0: reduce1(num(N),A) :- number(N), atom_number(A,N). Daniel@0: reduce1(num(L,N),A) :- number(N), format(atom(A),'~`0t~d~*+',[N,L]). % zero padded number Daniel@0: reduce1(\(A),B) :- must_be(nonvar,A), def(A,B). Daniel@0: reduce1(enc(A),B) :- atomic(A), uri_encoded(path,A,B). Daniel@0: reduce1(tail(A),A) :- atomic(A). Daniel@0: reduce1(P:A,P:B) :- reduce(A,B). Daniel@0: reduce1(P:A,PB) :- atomic(A), rdf_global_id(P:A,PB). Daniel@0: reduce1(P:(A/B),PA/B) :- reduce(P:A,PA). Daniel@0: reduce1(A/B,A1/B) :- reduce(A,A1). Daniel@0: reduce1(A/B,A/B1) :- reduce(B,B1). % nb to get past these, A and B must be nonvar Daniel@0: reduce1(A/B,AB) :- atom(A), atom(B), atomic_list_concat([A,B],'/',AB). Daniel@0: reduce1((A/B)/C,A/BC) :- reduce1(B/C,BC). Daniel@0: reduce1(A/(B/C),AB/C) :- reduce1(A/B,AB). Daniel@0: reduce1((A/B)/C,A/(B/C)). Daniel@0: