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: object/2 Daniel@0: , resource/2 Daniel@0: , literal/2 Daniel@0: , max_reduce/2 Daniel@0: ]). Daniel@0: Daniel@0: :- use_module(library(semweb/rdf_db), [ rdf_current_ns/2 ]). Daniel@0: Daniel@0: :- multifile uripattern:macro/2. Daniel@0: Daniel@0: object(A,B) :- freeze(A, object1(A,B)). Daniel@0: literal(A,B) :- freeze(A, literal1(A,B)). Daniel@0: resource(A,B) :- freeze(A, resource1(A,B)). Daniel@0: plain(A,B) :- freeze(A, plain1(A,B)). Daniel@0: Daniel@0: object1(literal(A),literal(B)) :- !, literal(A,B). Daniel@0: object1(A,B) :- resource1(A,B). Daniel@0: Daniel@0: literal1(type(T,A),type(T1,A1)) :- !, resource(T,T1), plain(A,A1). Daniel@0: literal1(A,B) :- plain1(A,B). Daniel@0: Daniel@0: resource1(P:A,PB) :- !, rdf_current_ns(P,Q), atom_concat1(Q,B,PB), plain(A,B). Daniel@0: resource1(A,B) :- plain1(A,B). Daniel@0: Daniel@0: plain1(pat(L),B) :- freeze(L,conc(L,B)). Daniel@0: plain1(num(N),B) :- when(nonvar(N);nonvar(B),atom_number1(B,N)). Daniel@0: plain1(mac(M),B) :- when(nonvar(M);nonvar(B),macro(M,B)). Daniel@0: plain1(enc(A),B) :- plain(A,C), when(nonvar(C);nonvar(B),www_form_encode(C,B)). Daniel@0: plain1(atm(A),A). Daniel@0: plain1(A1/A2,B) :- freeze(B,sub_atom(B,_,_,_,'/')), plain1(A1+'/'+A2,B). Daniel@0: plain1(A1+A2,B) :- atom_concat1(B1,B2,B), plain(A1,B1), plain(A2,B2). Daniel@0: plain1(A,B) :- (atom(A);atom(B)), A=B. Daniel@0: Daniel@0: conc([],''). Daniel@0: conc([H|T],B) :- atom_concat1(H,TA,B), freeze(T,conc(T,TA)). Daniel@0: Daniel@0: atom_number1(A,N) :- Daniel@0: catch(atom_number(A,N), error(syntax_error(_),_), fail). Daniel@0: Daniel@0: % like atom_concat but with more modes, using frozen constraints. Daniel@0: atom_concat1(A,B,C) :- Daniel@0: freeze(A, (A=''->B=C;true)), Daniel@0: freeze(B, (B=''->A=C;true)), Daniel@0: when( ( nonvar(A),nonvar(B) Daniel@0: ; nonvar(B),nonvar(C) Daniel@0: ; nonvar(A),nonvar(C) Daniel@0: ), atom_concat(A,B,C)). Daniel@0: Daniel@0: Daniel@0: % non-deterministic maximal reduction Daniel@0: max_reduce(E1,E3) :- Daniel@0: reduce(E1,E2) *-> max_reduce(E2,E3); E1=E3. Daniel@0: Daniel@0: % simplify resource or literal expression if possible Daniel@0: reduce(A,A) :- var(A), !, fail. Daniel@0: reduce(A/B,A+'/'+B). Daniel@0: reduce(A/B,A1/B) :- reduce(A,A1). Daniel@0: reduce(A/B,A/B1) :- reduce(B,B1). Daniel@0: reduce(A/B/C,A/(B/C)). Daniel@0: reduce(A+B,A1+B) :- reduce(A,A1). Daniel@0: reduce(A+B,A+B1) :- reduce(B,B1). Daniel@0: reduce(A+B,AB) :- atom(A), atom(B), atom_concat(A,B,AB). Daniel@0: reduce(A+B,_) :- (var(A); var(B)), !, fail. Daniel@0: reduce(A+B/C,(A+B)/C). Daniel@0: reduce((A+B)+C,A+BC) :- atom(B), atom(C), atom_concat(B,C,BC). Daniel@0: reduce(A+(B+C),AB+C) :- atom(A), atom(B), atom_concat(A,B,AB). Daniel@0: reduce(P:A,Q+A) :- must_be(atom,P), Daniel@0: ( rdf_current_prefix(P,Q) -> true Daniel@0: ; throw(unknown_prefix(P)) Daniel@0: ). Daniel@0: Daniel@0: reduce(num(N),A) :- number(N), atom_number(A,N). Daniel@0: reduce(atm(A),A) :- atom(A). Daniel@0: reduce(mac(A),B) :- atom(A), macro(A,B). Daniel@0: reduce(enc(A),B) :- atom(A), www_form_encode(A,B). Daniel@0: reduce(enc(A),enc(B)):- reduce(A,B). Daniel@0: reduce(A+pat(P),pat([A|P])) :- atom(A), nonvar(P). Daniel@0: reduce(pat(P)+A,pat(P1)) :- atom(A), nonvar(P), concat(P,[A],P1). Daniel@0: reduce(pat([A1,A2|AX]),pat([A12|AX])) :- Daniel@0: atom(A1), atom(A2), atom_concat(A1,A2,A12). Daniel@0: Daniel@0: reduce(literal(E),literal(E1)) :- reduce(E,E1). Daniel@0: reduce(type(T,E),type(T1,E)) :- reduce(T,T1). Daniel@0: reduce(type(T,E),type(T,E1)) :- reduce(E,E1). Daniel@0: reduce(lang(V,L),lang(V1,L)) :- reduce(V,V1). Daniel@0: reduce(lang(V,L),lang(V,L1)) :- reduce(L,L1). Daniel@0: