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(p2r_entailment,[ op(1200,xfx,<==), op(1200,xfx,==>), assert_all/1 ]).
|
Daniel@0
|
20
|
Daniel@0
|
21 /** <module> Prolog-to-RDF mapping entailment module
|
Daniel@0
|
22
|
Daniel@0
|
23 This module provides a mechanism for exposing information in the Prolog
|
Daniel@0
|
24 database as RDF triples. The basis of this is a syntax for multiheaded
|
Daniel@0
|
25 Horn clauses with RDF triples in the head and Prolog goals in the body.
|
Daniel@0
|
26 The rules can be written in either direction: as =|Heads <== Body|=
|
Daniel@0
|
27 or =|Body ==> Heads|=, whichever is most convenient.
|
Daniel@0
|
28
|
Daniel@0
|
29 There is also a grammar for writing URI patterns. These are used to
|
Daniel@0
|
30 to build URIs from Prolog values and to parse URIs to extract Prolog
|
Daniel@0
|
31 values. The cost of this URI processing can be completely avoided by
|
Daniel@0
|
32 writing only variables in the head rdf/3 terms.
|
Daniel@0
|
33 */
|
Daniel@0
|
34
|
Daniel@0
|
35 :- use_module(library(uripattern_detdcg)).
|
Daniel@0
|
36 :- use_module(library(semweb/rdf_db),[]).
|
Daniel@0
|
37
|
Daniel@0
|
38 :- rdf_meta rdf(r,r,o).
|
Daniel@0
|
39 :- rdf_meta rdf(r,r,o,-).
|
Daniel@0
|
40
|
Daniel@0
|
41 :- multifile cliopatria:entailment/2.
|
Daniel@0
|
42 :- multifile rdf/4.
|
Daniel@0
|
43 :- op(1200,xfx,<==).
|
Daniel@0
|
44 :- op(1200,xfx,==>).
|
Daniel@0
|
45
|
Daniel@0
|
46
|
Daniel@0
|
47 cliopatria:entailment(p2r, p2r_entailment).
|
Daniel@0
|
48
|
Daniel@0
|
49 rdf(S,P,O) :- rdf(S,P,O,_).
|
Daniel@0
|
50
|
Daniel@0
|
51 user:term_expansion( (Body ==> Heads), Clauses) :-
|
Daniel@0
|
52 user:term_expansion( (Heads <== Body), Clauses).
|
Daniel@0
|
53 user:term_expansion( (Heads <== Body), Clauses) :-
|
Daniel@0
|
54 debug(p2r,'Expanding: ~w',[Heads<==Body]),
|
Daniel@0
|
55 prolog_load_context(module,Mod),
|
Daniel@0
|
56 expand_clauses(Heads,Mod:Body,Clauses,[]).
|
Daniel@0
|
57
|
Daniel@0
|
58
|
Daniel@0
|
59 expand_clauses((H1,H2),Body) --> !,
|
Daniel@0
|
60 expand_clauses(H1,Body),
|
Daniel@0
|
61 expand_clauses(H2,Body).
|
Daniel@0
|
62
|
Daniel@0
|
63 expand_clauses(rdf(SPat,PPat,OPat),Mod:Body) -->
|
Daniel@0
|
64 { call_dcg( ( opt_match(resource,SPat,S),
|
Daniel@0
|
65 opt_match(resource,PPat,P),
|
Daniel@0
|
66 opt_match(object,OPat,O) ), Mod:Body, Body1),
|
Daniel@0
|
67 Clause = (p2r_entailment:rdf(S,P,O,Mod) :- Body1),
|
Daniel@0
|
68 debug(p2r,'Asserting clause: ~w',[Clause])
|
Daniel@0
|
69 },
|
Daniel@0
|
70 [Clause].
|
Daniel@0
|
71
|
Daniel@0
|
72 % builds an optimised goal to do pattern matching
|
Daniel@0
|
73 opt_match(_,X,X) --> {var(X)}, !.
|
Daniel@0
|
74 opt_match(object,X,Y) --> {X=literal(_)}, !, {rdf_global_object(X,Y)}.
|
Daniel@0
|
75 opt_match(_,P1,URI) -->
|
Daniel@0
|
76 {debug(p2r,'Simplifying URI pattern: ~w',[P1])},
|
Daniel@0
|
77 {simplify(P1,P2)}, opt_match(P2,URI).
|
Daniel@0
|
78 opt_match(X,X) --> {atomic(X)}, !.
|
Daniel@0
|
79 opt_match(P,U) --> by_inst(U,uripattern:pattern_uri(P,U)).
|
Daniel@0
|
80
|
Daniel@0
|
81 % this generates code to call G and G1 in an order determined
|
Daniel@0
|
82 % by the instantiation state of X at run time.
|
Daniel@0
|
83 by_inst(X,G1,G, (var(X) -> G, G1; G1, G)).
|
Daniel@0
|
84
|
Daniel@0
|
85
|
Daniel@0
|
86 %% assert_all(+Module) is det.
|
Daniel@0
|
87 % This asserts all the RDF triples entailed by the p2r module Module.
|
Daniel@0
|
88 % Informational messages are printed, including the time taken.
|
Daniel@0
|
89 assert_all(G) :-
|
Daniel@0
|
90 forall(p2r_entailment:rdf(S,P,O,G), rdf_assert(S,P,O,G)).
|
Daniel@0
|
91
|