forked from conceptsinmotion/thea
-
Notifications
You must be signed in to change notification settings - Fork 0
/
owl2_plsyn.pl
449 lines (375 loc) · 11.9 KB
/
owl2_plsyn.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
/* -*- Mode: Prolog -*- */
:- module(owl2_plsyn,[
write_owl_as_plsyn/0,
write_owl_as_plsyn/1,
plsyn_owl/2,
plsyn_owl/3,
op(980,xfy,(--)),
op(950,xfy,\^), % disjoint classes
%op(950,fx,class),
op(950,fx,individual),
op(950,xfy,disjointUnion),
op(950,fx,class),
op(950,fx,functional),
op(950,fx,transitive),
op(950,fx,symmetric),
op(950,fx,asymmetric),
op(950,fx,reflexive),
op(950,fx,irreflexive),
% 700 <
% 700 =
op(700,xfy,inverseOf),
%op(700,xfy,(->)),
op(650,xfy,(::)),
op(600,fx,not),
op(500,xfy,or),
op(200,xfy,and),
op(200,xfy,that),
op(150,xfy,some),
op(150,xfy,only),
op(150,xfy,value),
op(150,xfy,min),
op(150,xfy,max),
op(150,xfy,exactly),
op(125,xfy,of) % required for QCRs
]).
:- use_module(owl2_model).
:- use_module(owl2_util).
:- use_module(swrl).
:- use_module(library(readutil)).
:- op(980,xfy,(--)).
:- op(950,fx,individual).
:- op(950,xfy,disjointUnion).
:- op(950,xfy,\^). % disjoint classes
:- op(950,fx,class).
:- op(950,fx,functional).
:- op(950,fx,transitive).
:- op(950,fx,symmetric).
:- op(950,fx,asymmetric).
:- op(950,fx,reflexive).
:- op(950,fx,irreflexive).
% 700 <
% 700 =
:- op(700,xfy,inverseOf).
%:- op(700,xfy,(->)).
:- op(650,xfy,(::)).
:- op(600,fx,not).
:- op(500,xfy,or).
:- op(200,xfy,and).
:- op(200,xfy,that).
:- op(150,xfy,some).
:- op(150,xfy,only).
:- op(150,xfy,value).
:- op(150,xfy,min).
:- op(150,xfy,max).
:- op(150,xfy,exactly).
:- op(125,xfy,of).
:- op(100,fx,(?)).
:- multifile owl2_io:load_axioms_hook/3.
owl2_io:load_axioms_hook(File,plsyn,Opts) :-
owl_parse_plsyn(File,Opts). % TODO
:- multifile owl2_io:save_axioms_hook/3.
owl2_io:save_axioms_hook(_File,plsyn,Opts) :-
write_owl_as_plsyn(Opts).
owl_parse_plsyn(File,_Opts) :-
open(File,read,IO,[]),
repeat,
( at_end_of_stream(IO)
-> true
; read_term(IO,PlTerm,[module(owl2_plsyn)]),
plsyn2owl(PlTerm,Axiom),
( nb_current(ontology,Ont)
-> assert_axiom(Axiom,Ont)
; assert_axiom(Axiom)),
( Axiom=ontology(OntNew)
-> nb_setval(ontology,OntNew)
; true),
fail),
close(IO).
write_owl_as_plsyn:-
write_owl_as_plsyn([]).
write_owl_as_plsyn(Opts):-
setof(Ont,member(ontology(Ont),Opts),Onts),
!,
% this clause optimized for ontology filtering
forall((member(Ont,Onts),
ontologyAxiom(Ont,A),
\+exclude_axiom(A,Opts)),
( plsyn_owl(Pl,A,Opts),
format('~q.~n',[Pl]))).
write_owl_as_plsyn(Opts):-
forall((axiom(A),\+exclude_axiom(A,Opts)),
( plsyn_owl(Pl,A,Opts),
format('~q.~n',[Pl]))).
% TODO: move somewhere generic
exclude_axiom(H,Opts) :-
setof(Ont,member(ontology(Ont),Opts),Onts),
\+ ((ontologyAxiom(Ont,H),
member(Ont,Onts))).
%% plsyn_owl(?Pl,?Owl)
% as plsyn_owl/3
plsyn_owl(Pl,Owl) :-
plsyn_owl(Pl,Owl,[]).
%% plsyn_owl(?Pl,?Owl,+Opts:list)
%
% convert between a plsyn prolog term and an owl2_model.pl prolog term.
% either one of Pl or Owl must be ground
plsyn_owl(Pl,Owl,Opts) :-
select(use_labels,Opts,Opts2),
!,
map_IRIs(owl2_util:use_label_as_IRI,[Owl],[Owl2]),
plsyn_owl(Pl,Owl2,Opts2).
plsyn_owl(Pl,Owl,_) :-
nonvar(Pl),
plsyn2owl(Pl,Owl),
!.
plsyn_owl(Pl,Owl,_) :-
nonvar(Owl),
owl2plsyn(Owl,Pl),
!.
plsyn_owl(Pl,Pl,_) :-
var(Pl).
% allow translation of vars, for example for queries or templates
plsyn2owl(V,V) :-
var(V),
!.
% e.g. r < r1 * r2 *r3 ...
plsyn2owl(R @< R1*R2,subPropertyOf(propertyChain(Chain),R)) :-
plsyn2owl_ec(R1*R2,(*),Chain),
!.
plsyn2owl(Pl,Owl) :-
Pl=..[PlPred,PlProp,of(Num,PlC)],
cardinality_pred(PlPred),
plpred2owlpred(PlPred,OwlPred),
!,
plsyn2owl(PlProp,Prop),
plsyn2owl(PlC,C),
Owl=..[OwlPred,Num,Prop,C].
plsyn2owl(Pl,Owl) :-
Pl=..[PlPred|Args],
plpred2owlpred(PlPred,OwlPred),
!,
maplist(plsyn2owl,Args,Args2),
Owl=..[OwlPred|Args2].
plsyn2owl(Pl,Owl) :-
Pl=..[PlPred|Args],
plpred2owlpred_list(PlPred,OwlPred), % TODO - reverse
!,
maplist(plsyn2owl,Args,Args2),
Owl=..[OwlPred,[Args2]].
% TODO: entity annotations
plsyn2owl(Ax--Comments,[PlAx,axiomAnnotation('rdfs:comment',literal(Comments))]) :-
!,
plsyn2owl(Ax,PlAx).
% we can chain over a=b=c=d as equivalent/sameAs is transitive
% (note we cannot do this for different/disjoint)
plsyn2owl(A=B,sameIndividual(ECs)) :-
!,
plsyn2owl_ec(A=B,(=),ECs).
plsyn2owl(A==B,equivalentClasses(ECs)) :-
!,
plsyn2owl_ec(A==B,(==),ECs).
plsyn2owl(A=@=B,equivalentProperties(ECs)) :-
!,
plsyn2owl_ec(A=@=B,(=@=),ECs).
plsyn2owl(A and B,intersectionOf(ECs)) :-
!,
plsyn2owl_ec(A and B,and,ECs).
plsyn2owl(A \^ B,disjointClasses(ECs)) :-
!,
plsyn2owl_ec(A \^ B,\^,ECs).
plsyn2owl(A or B,unionOf(ECs)) :-
!,
plsyn2owl_ec(A or B,or,ECs).
plsyn2owl(Pl,Owl) :-
% Assume OwlPred is valid, translate sub-args
Pl=..[OwlPred|Args],
Args\=[],
!,
maplist(plsyn2owl,Args,Args2),
Owl=..[OwlPred|Args2].
plsyn2owl(X,X) :- !.
%% plsyn2owl_ec(+Term,+Op,?Elts:list)
% e.g. a and b and c and V ==> [a,b,c,V]
plsyn2owl_ec(T,_,[T]) :-
var(T),
!.
plsyn2owl_ec(T,Op,L) :-
T=..[Op,A,B],
!,
plsyn2owl_ec(A,Op,LA),
plsyn2owl_ec(B,Op,LB),
append(LA,LB,L).
plsyn2owl_ec(A,_,[AX]) :-
plsyn2owl(A,AX).
owl2plsyn(Owl,Pl) :-
Owl=..[OwlPred|Args],
plpred2owlpred(PlPred,OwlPred),
!,
maplist(owl2plsyn,Args,Args2),
Pl=..[PlPred|Args2].
owl2plsyn(Owl,Pl) :-
Owl=..[OwlPred|Args],
plpred2owlpred_list(PlPred,OwlPred),
!,
maplist(owl2plsyn,Args,Args2),
Pl=..[PlPred,[Args2]].
owl2plsyn(propertyAssertion(P,S,O),Pl) :-
atom(P),
op(999,xfy,P),
owl2plsyn(S,SX),
owl2plsyn(O,OX),
Pl=..[P,SX,OX].
owl2plsyn(equivalentProperties(Args),Pl) :-
maplist(owl2plsyn,Args,Args2),
list_to_chain(Args2,(=@=),Pl).
owl2plsyn(equivalentClasses(Args),Pl) :-
maplist(owl2plsyn,Args,Args2),
list_to_chain(Args2,(==),Pl).
owl2plsyn(sameIndividuals(Args),Pl) :-
maplist(owl2plsyn,Args,Args2),
list_to_chain(Args2,(=),Pl).
owl2plsyn(intersectionOf(Args),Pl) :-
% sort atoms first for aesthetic reasons: prefer <a and (r some b)> over <(r some b) and a>
sort(Args,ArgsSorted),
maplist(owl2plsyn,ArgsSorted,Args2),
list_to_chain(Args2,and,Pl).
owl2plsyn(disjointClasses(Args),Pl) :-
maplist(owl2plsyn,Args,Args2),
list_to_chain(Args2,\^,Pl).
owl2plsyn(unionOf(Args),Pl) :-
maplist(owl2plsyn,Args,Args2),
list_to_chain(Args2,or,Pl).
owl2plsyn(implies(A,C),(A2->C2)) :-
swrlatoms2plsyn(A,A2),
swrlatoms2plsyn(C,C2).
owl2plsyn(literal(type(_,X)),X) :- !.
owl2plsyn(literal(X),X) :- atom(X),!.
owl2plsyn(Owl,Pl) :-
Owl=..[P|Args],
Args\=[],
!,
maplist(owl2plsyn,Args,Args2),
Pl=..[P|Args2].
owl2plsyn(X,X) :- !.
swrlatoms2plsyn(A,A2) :-
is_list(A),
!,
maplist(swrlatom2plsyn,A,AL),
list_to_chain(AL,(,),A2).
swrlatoms2plsyn(A,A2) :-
!,
swrlatom2plsyn(A,A2).
swrlatom2plsyn(description(CE,I),H) :-
!,
swrlatom2plsyn(I,I2),
H=..[CE,I2].
swrlatom2plsyn(differentFrom(X,Y),X2 \= Y2) :-
!,
swrlatom2plsyn(X,X2),
swrlatom2plsyn(Y,Y2).
swrlatom2plsyn(IPA,IPA2) :-
IPA=..[P,X,Y],
!,
swrlatom2plsyn(X,X2),
swrlatom2plsyn(Y,Y2),
IPA2=..[P,X2,Y2].
% TODO -- decide on correct functor for variables
swrlatom2plsyn(v(V),X) :- !, swrlatom2plsyn(i(V),X).
swrlatom2plsyn(i(V),X) :- number(V),!,VA is V+96,atom_codes(A,[VA]),atom_concat('?',A,X).
swrlatom2plsyn(i(V),X) :- atom_concat('?',V,X).
swrlatom2plsyn(X,X) :- !.
list_to_chain([X],_,Pl) :- !, owl2plsyn(X,Pl).
list_to_chain([X1|L],Op,Pl) :-
!,
list_to_chain(L,Op,X2),
owl2plsyn(X1,X1Pl),
Pl=..[Op,X1Pl,X2].
plpred2owlpred(transitive,transitiveProperty).
plpred2owlpred(functional,functionalProperty).
plpred2owlpred(symmetric,symmetricProperty).
plpred2owlpred(reflexive,reflexiveProperty).
%plpred2owlpred(inverseOf,inverseProperties).
plpred2owlpred(min,minCardinality).
plpred2owlpred(max,maxCardinality).
plpred2owlpred(exact,exactCardinality).
plpred2owlpred(some,someValuesFrom).
plpred2owlpred(only,allValuesFrom).
plpred2owlpred(value,hasValue).
plpred2owlpred(not,complementOf).
plpred2owlpred(inverseOf,inverseProperties).
plpred2owlpred(::,classAssertion).
plpred2owlpred(<,subClassOf).
plpred2owlpred(@<,subPropertyOf).
plpred2owlpred_list(\=,differentIndividuals).
%plpred2owlpred_list(\=,disjointClasses).
cardinality_pred(min).
cardinality_pred(max).
cardinality_pred(exact).
/** <module> prolog-style syntactic sugar for OWL
---+ Synopsis
Ontologies can be authored or written in prolog syntax, with
convenience predicates declared infix in order to resemble Manchester
Syntax.
The following is a valid plsyn file:
==
ontology(spicy).
spicy_tomato_pizza == pizza and hasPart some (topping and hasQuality some spicy) and hasPart some tomato.
pizza < hasPart some mozzarella.
pizza_with_4_cheeses == pizza and hasPart exactly 4 of cheese.
==
This file can be loaded via load_axioms/2 like this:
==
load_axioms('myfile.plsyn',plsyn).
==
---+ Details
The class expression syntax should resembly manchester syntax as far
as possible. Additional symbols such as <, =, ==, @< are also used for
axioms.
We are forced to introduce an extra keyword 'of' for cardinality
expressions to make this parseable by prolog.
TODO: show translation table
---++ Infix Predicate Symbols for Axioms
* < --- subClassOf/2
* @< --- subPropertyOf/2
* == --- equivalentClasses/1
* = --- sameIndividual/1
* \= --- differentIndividuals/1
* :: --- classAssertion/2
---++ Property Characteristic Prefix Predicates
the following are all declared prefix
* transitive --- transitiveProperty/1
* symmetric --- symmetricProperty/1
* reflexive --- reflexiveProperty/1
* functional --- functionalProperty/1
---++ Other Infix Predicates
* inverseOf --- inverseProperties/2
---++ Class Expressions
* and --- intersectionOf
* or --- unionOf
* some --- someValuesFrom
* only --- allValuesFrom
* value --- hasValue
* not --- complementOf (prefix)
* exactly --- exactCardinality
* min --- minCardinality
* max --- maxCardinality
* of --- required when making QCRs. E.g. exactly 5 of finger
---++ Property Expressions
Use the '*' symbol. For example
==
uncleOf @< fatherOf * brotherOf.
==
---++ Uses
plsyn makes it easier to mix OWL ontologies and ontology templates into prolog programs.
For example, consider the following failure-driven loop to add an OWL
axiom with a complex nested expression for all facts of a 4-argument predicate:
==
generate_ownership_assertions :-
owns(Person,Number,Thing,Loc),
plsyn_owl( owns exactly Number of (Thing and located_in value Loc) :: Person, OwlAxiom),
assert_axiom(OwlAxiom),
fail.
==
The owl2_popl.pl module facilitates this kind of translation
*/