forked from vangelisv/thea
-
Notifications
You must be signed in to change notification settings - Fork 3
/
owl2_from_rdf_utils.pl
271 lines (208 loc) · 7.3 KB
/
owl2_from_rdf_utils.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
% -----------------------------------------------------------------------
% UTILITY Predicates
% -----------------------------------------------------------------------
%% owl_clear_as
%
% Clears the prolog terms that store the Abstract Syntax
% implementation of the OWL ontology.
owl_clear_as :-
debug(owl_parser,'Clearing abstract syntax',[]),
forall((axiompred(PredSpec),predspec_head(PredSpec,Head)),
retractall(Head)).
predspec_head(Pred/A,Head) :- functor(Head,Pred,A).
u_assert(Term) :-
( call(Term)
-> true
; assert(Term)
).
convert(T,V,typed_value(T,V)).
%% rdf_2_owl(+Base, +Ont) is det
%
% Converts RDF triples to OWL/4 triples so that
% their use can tracked by the OWL parser.
rdf_2_owl(Base,Ont) :-
% debug(owl_parser, 'Removing existing owl triples',[]),
% retractall(owl(_,_,_,Ont)),
debug(owl_parser,'Copying RDF triples to OWL triples for Ontology ~w',[Ont]),
rdf(X,Y,Z,Base:_),
% owl_fix_no(X,X1), owl_fix_no(Y,Y1), owl_fix_no(Z,Z1),
assert(owl(X,Y,Z,Ont)), fail.
rdf_2_owl(_,Ont) :-
owl_count(Ont,Z),
debug(owl_parser,'Number of owl triples copied: ~w',[Z]).
%% owl_count(?U).
% Returns/Checks the number of unused OWL triples.
owl_count(O,U) :-
findall(1,owl(_,_,_,O),X), length(X,U).
%% expand_and_assert(S,P,O) is det
%
% adds a owl(S,P,O,not_used) after expanding namespaces.
% this is required for the triple replacement rules,
% which use shortened rdfs/owl namespaces.
% (or we could just use the expanded forms here which
% may be faster..)
expand_and_assert(X1,Y1,Z1) :-
expand_ns(X1,X),
expand_ns(Y1,Y),
expand_ns(Z1,Z),!,
retractall(owl(X,Y,Z, used1)),
assert(owl(X,Y,Z, not_used)).
%% test_use_owl(+Triples:list) is nondet
%
% As use_owl/1, but does not consume the triple. If owl(S,P,O)
% in Triples has a non-ground variable then this will succeed
% non-deterministically. If all variables are ground, then this
% will succeed semi-deterministically.
test_use_owl([]).
test_use_owl([owl(S,P,O)|Rest]) :-
test_use_owl(S,P,O),
test_use_owl(Rest).
%% test_use_owl(?S,?P,?O)
% As use_owl/3, but does not consume the triple. Expands the S,P,O.
%
% If any of S, P or O is non-ground then this will succeed
% non-deterministically. If all variables are ground, then this
% will succeed semi-deterministically.
test_use_owl(X1,Y1,Z1) :-
expand_ns(X1,X),
expand_ns(Y1,Y),
expand_ns(Z1,Z),!,
owl(X,Y,Z, not_used).
test_use_owl(X1,Y1,Z1,named) :-
expand_ns(X1,X),
expand_ns(Y1,Y),
expand_ns(Z1,Z),
owl(X,Y,Z, not_used),
\+ sub_string(X,0,2,_,'_:').
%% use_owl(+Triples:list)
% Marks a list of OWL triples as used, but only if all match. Expands the S,P,O.
use_owl(Triples) :-
test_use_owl(Triples),
use_owl_2(Triples).
% consume all triples; we have already tested the list and know that all match
use_owl_2([]).
use_owl_2([owl(S,P,O)|Triples]) :-
use_owl(S,P,O),
use_owl_2(Triples).
%% use_owl(?S,?P,?O)
% Marks an OWL triple as used. Expands the S,P,O.
use_owl(X1,Y1,Z1) :-
expand_ns(X1,X),
expand_ns(Y1,Y),
expand_ns(Z1,Z),
owl(X,Y,Z, not_used),
debug(owl_parser_detail,'using ~w ~w ~w',[X,Y,Z]),
retract(owl(X,Y,Z, not_used)),
assert(owl(X,Y,Z,used1)).
%% use_owl(?S,?P,?O,+Named)
%
% Named = named: Same as use_owl/3, but marks only if S is Named URI (i.e. non blank node).
use_owl(X1,Y1,Z1,named) :-
expand_ns(X1,X),
expand_ns(Y1,Y),
expand_ns(Z1,Z),
owl(X,Y,Z, not_used),
not(sub_string(X,0,2,_,'_:')),
retract(owl(X,Y,Z, not_used)),
assert(owl(X,Y,Z,used2)).
%% use_owl(?S,?P,?O,Term)
%
% Marks an OWL triple as used. Expands the S,P,O.
use_owl(X1,Y1,Z1,Term) :-
expand_ns(X1,X),
expand_ns(Y1,Y),
expand_ns(Z1,Z),
owl(X,Y,Z, not_used),
debug(owl_parser_detail,'using ~w ~w ~w',[X,Y,Z]),
retract(owl(X,Y,Z, not_used)),
assert(owl(X,Y,Z,used(Term))).
%% use_owl(?S,?P,?O,+Named,Term)
%
% Named = named: Same as use_owl/3, but marks only if S is Named URI (i.e. non blank node).
use_owl(X1,Y1,Z1,named,Term) :-
expand_ns(X1,X),
expand_ns(Y1,Y),
expand_ns(Z1,Z),
owl(X,Y,Z, not_used),
\+ sub_string(X,0,2,_,'_:'),
retract(owl(X,Y,Z, not_used)),
assert(owl(X,Y,Z,used(Term))).
%% expand_ns(+NS_URL, ?Full_URL)
%
% Expands a 'namespaced' URI of the form ns:fragment to a full URI
% substituting the full expansion for ns from the ns/2 facts
expand_ns(NS_URL, Full_URL) :-
nonvar(NS_URL),
NS_URL \= literal(_),
uri_split(NS_URL,Short_NS,Term, ':'),
rdf_db:ns(Short_NS,Long_NS),!,
atomic_list_concat([Long_NS,Term],Full_URL).
expand_ns(URL, URL).
%% collapse_ns(+FullURL, ?NSURL, +Char, +Options)
%
% Collapses a full URI of the form Path#fragment to a Namespaced
% URI NS:fragment substituting the full expansion for ns from
% the ns/2 facts
% Char is either ':' for normal ns notation or '_' for builing
% prolog terms.
% Options supported: no_base(ShortNs): Use only term!
collapse_ns(FullURL, NSURL,Char,Options) :-
nonvar(FullURL),
FullURL \= '$VAR'(_),
FullURL \= literal(_),
uri_split(FullURL,LongNS, Term, '#'),
concat(LongNS,'#',LongNS1),
rdf_db:ns(ShortNS,LongNS1),
( member(no_base(ShortNS),Options)
-> NSURL = Term
; atomic_list_concat([ShortNS,Char,Term],NSURL)
),!.
% CJM
collapse_ns(FullURL, NSURL,_Char,Options) :-
nonvar(FullURL),
FullURL \= '$VAR'(_),
FullURL \= literal(_),
uri_split(FullURL,LongNS, Term, '#'),
member(no_base(LongNS),Options),
!,
NSURL = Term.
collapse_ns(URL, URL,_,_).
%% uri_split(+URI,-Namespace,-Term,+Split_Char) is det
%
% Splits a URI into the Namespace and the Term parts
% separated by the Split_Char character.
% It supposes URI = concat(Namespace,Split_Char,Term)
uri_split(URI,Namespace,Term,Split_Char) :-
sub_atom(URI,Start,_,After,Split_Char),
sub_atom(URI,0,Start,_,Namespace),
Start1 is Start + 1,
sub_atom(URI,Start1,After,_,Term).
%% owl_collect_linked_nodes(+Node,+Predicate, +InList,-OutList)
% Appends Node to the InList, and recursively, all other
% Nodes that are linked with the Predicate to the Node. The
% result is returned to OutList.
owl_collect_linked_nodes(Node,Predicate,InList,OutList) :-
use_owl(Node,Predicate,A),!,
owl_collect_linked_nodes(Node,Predicate,InList,List1),
owl_collect_linked_nodes(A,Predicate,List1,OutList).
owl_collect_linked_nodes(Node,Predicate,InList,OutList) :-
use_owl(A,Predicate,Node),!,
owl_collect_linked_nodes(Node,Predicate,InList,List1),
owl_collect_linked_nodes(A,Predicate,List1,OutList).
owl_collect_linked_nodes(Node,_,List, [Node|List]) :-
not(memberchk(Node, List)),!.
owl_collect_linked_nodes(_,_,List, List) :- !.
% ----------------------------------------------------------------
% OWL Parser implementation predicates
% ----------------------------------------------------------------
%% owl_get_bnode(+Node,+Description)
%
% if Node is a blank (not named) node, then it is asserted in
% the database as a blanknode(Node,Description,used) term.
% The purpose is to record when a blank node has been used, so
% subsequent uses of it will result in structure sharing.
owl_get_bnode(Node,Description) :-
sub_string(Node,0,2,_,'_:'),!,
\+ blanknode(Node,_,_),
assert(blanknode(Node,Description, used)).
owl_get_bnode(_,_).