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