forked from vangelisv/thea
-
Notifications
You must be signed in to change notification settings - Fork 3
/
owl2_export_rdf.pl
721 lines (590 loc) · 29.1 KB
/
owl2_export_rdf.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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
/* -*- Mode: Prolog -*- */
% **********************************************************************
% OWL to RDF Generator
% Author: Vangelis Vassiliadis
% Change Log:
% Feb 09: Ported to Thea2 [CJM]
% Mar 07: Version 0.5.5: Changes to the use_module and
% definitions for Thea 0.5.5 release.
% Sep 05: Initial release 0.4 (as part of Thea OWL
% Prolog library)
%
% **********************************************************************
:- module(owl2_export_rdf,
[owl_generate_rdf/2, % FileName, RDF_Load_Mode (complete/not)
owl_generate_rdf/3, % Ontology,FileName, RDF_Load_Mode (complete/not)
owl_generate_rdf/4, % Ontology,FileName, RDF_Load_Mode (complete/not), Opts
owl_synchronize_to_rdf/0,
owl_synchronize_to_rdf/1,
owl_rdf2n3/0,
% JW: needed in owl2_util.pl
owl2_export_list/2, % +List, -Node
owl_rdf_assert/3, % +S, +P, +O
as2rdf_bnode/2 % +X, -Node
]).
:- use_module(owl2_model).
:- use_module(owl2_from_rdf).
:- use_module(swrl_rdf_hooks).
:- use_module(library(semweb/rdf_db)).
:- use_module(library(semweb/turtle)).
:- multifile owl2_io:save_axioms_hook/3.
:- dynamic
blanknode_gen/2.
owl2_io:save_axioms_hook(File,ttl,Opts) :-
ensure_loaded(library('semweb/rdf_turtle_write')),
owl2_io:save_axioms_hook(File,owl,[rdf_syntax(ttl)|Opts]).
owl2_io:save_axioms_hook(File,owl,Opts) :-
( member(rdf_load_mode(RDF_Load_Mode),Opts)
-> true
; RDF_Load_Mode=complete),
( var(File)
-> tmp_file(owl,File),
IsTemp=true
; IsTemp=false),
% ontology merging:
% user can select this to indicate that
% all axioms should be treated as belonging to the
% same ontology.
%
% bind Merge only if user selects this option
( member(merge(Merge),Opts)
-> true
; true),
% select which ontology is to be saved. If none is selected,
% choose this automatically, but only if there is only one
% ontology in memory
( member(ontology(O),Opts)
-> true
; setof(O,ontology(O),[O])
-> debug(owl_generate_rdf,'auto-selecting ontology: ~w',[O])
; true),
% if the user did not select an ontology, and there is not
% exactly 1 ontology in memory, then they are implicitly choosing
% to ignore ontologyAxiom/2 and save all axioms in one file.
( ( var(O)
; \+ ontologyAxiom(_,_))
-> ( nonvar(Merge),Merge=false
-> throw(error('cannot override merge(true) unless there is a single ontology'))
; Merge=true,
debug(owl_generate_rdf,'ignoring source of axiom, bundling all together',[]))
; true),
% add Merge to Opts
( var(Merge)
-> Opts2=Opts
; Opts2=[merge(Merge)|Opts]),
owl_generate_rdf(O,File,RDF_Load_Mode,Opts2),
% hack to allow 'saving' to standard output
( IsTemp
-> sformat(Cmd,'cat ~w',[File]),
shell(Cmd)
; true).
%owl2_from_rdf:owl_repository('http://www.w3.org/TR/2003/PR-owl-guide-20031209/food','testfiles/food.owl').
%% owl_generate_rdf(+FileName,+RDF_Load_Mode) is det
% see owl_generate_rdf/1 - derives ontology using ontology/1
owl_generate_rdf(FileName,RDF_Load_Mode) :-
setof(Ontology,ontology(Ontology),[Ontology]),
!,
owl_generate_rdf(Ontology,FileName,RDF_Load_Mode).
owl_generate_rdf(FileName,RDF_Load_Mode) :-
% leave Ontology variable unbound - this will save all ontologies together
owl_generate_rdf(_,FileName,RDF_Load_Mode).
%% owl_generate_rdf(+Ontology,+FileName,+RDF_Load_Mode) is det
%
% as owl_generate_rdf/4, default Opts
owl_generate_rdf(Ontology,FileName,RDF_Load_Mode) :-
owl_generate_rdf(Ontology,FileName,RDF_Load_Mode,[]).
%% owl_generate_rdf(+Ontology,+FileName,+RDF_Load_Mode,+Opts:list) is det
%
% writes in-memory ontology from owl2_model to an RDF-OWL file.
% You can also use save_axioms/2 with Format='owl'.
% With save_axioms/3 use rdf_load_mode(RDF_Load_Mode) as
% an option.
%
% @param Ontology - IRI
% @param FileName - path to save
% @param RDF_Load_Mode (complete/not)
owl_generate_rdf(Ontology,FileName,RDF_Load_Mode,Opts) :-
owl_generate_rdf_in_memory(Ontology,RDF_Load_Mode,Opts),
( member(rdf_syntax(ttl),Opts) % TODO - use rdf_db hooks
-> rdf_save_turtle(FileName,Opts)
; rdf_db:rdf_save(FileName)).
owl_generate_rdf_in_memory(Ontology,RDF_Load_Mode,Opts) :-
( RDF_Load_Mode=complete -> rdf_retractall(_,_,_); true),
retractall(blanknode_gen(_,_)),retractall(blanknode(_,_,_)),
% export a fake ontology directive if none specified
( var(Ontology)
-> owl2_export_axiom(ontology('http://example.org'),_)
; owl2_export_axiom(ontology(Ontology),_)),
( member(merge(true),Opts)
-> true
; SrcOntology=Ontology),
debug(owl_generate_rdf,'exporting Ont:~w',[Ontology]),
debug(owl_generate_rdf,' gen: ontologyAxiom/2',[]),
forall(ontologyAxiom(SrcOntology,Axiom),
(owl2_export_axiom(Axiom,main_triple(S,P,O)),
owl2_export_annotation(Axiom,'owl:Axiom',S,P,O))),
debug(owl_generate_rdf,' stray axioms',[]),
% TODO - better way of doing this - stray axioms
( var(Ontology)
-> forall((axiom(Axiom),\+ontologyAxiom(_,Axiom)),
( owl2_export_axiom(Axiom,main_triple(S,P,O)),
owl2_export_annotation(Axiom,'owl:Axiom',S,P,O)))
; true),
% TODO - make SWRL export a hook?
forall(axiom(implies(A,C)),
owl2_export_axiom(implies(A,C),_)).
%% owl_synchronize_to_rdf is det
%% owl_synchronize_to_rdf(Ont) is det
%
% translates existing owl2_model.pl database into current rdf_db
owl_synchronize_to_rdf :-
setof(Ont,ontology(Ont),Onts),
!,
maplist(owl_synchronize_to_rdf,Onts).
owl_synchronize_to_rdf :-
owl_generate_rdf_in_memory(_,false,[]).
owl_synchronize_to_rdf(Ont) :-
!,
debug(owl_sync,'Synchronizing: ~w',[Ont]),
% this is not very efficient!
% see email http://groups.google.com/group/thea-owl-lib/browse_thread/thread/d9fcc8cc28a2d347
% TODO: refactor code such that rdf can be asserted in other graphs during export
owl_generate_rdf_in_memory(Ont,false,[]),
debug(owl_sync,'Copying from user to ~w',[Ont]),
rdf_transaction(forall(rdf(S,P,O,user),
rdf_assert(S,P,O,Ont))),
debug(owl_sync,' Clearing user',[]),
rdf_retractall(_,_,_,user).
/*
owl_rdf2n3
Prints out the RDF triples in N3 notation.
*/
owl_rdf2n3 :-
rdf_db:rdf(S,P,O),
collapse_ns(S,S1,':',[]),collapse_ns(P,P1,':',[]),collapse_ns(O,O1,':',[]),
write(S1), write(' '), write(P1), write(' '), write(O1), write(' .'),nl,
fail.
%% owl2_export_axiom(+Axiom, ?Triple) is semidet
/*
owl2_export_axiom(X,main_triple(Node,_,_)) :-
blanknode_gen(Node,X),!.
*/
owl2_export_axiom(ontology(O),main_triple(O,'rdf:type','owl:Ontology')) :-
owl_rdf_assert(O,'rdf:type','owl:Ontology'),
forall(ontologyImport(O,Import),owl_rdf_assert(O,'owl:imports',Import)),!.
owl2_export_axiom(class(C1),main_triple(C,'rdf:type','owl:Class')) :-
translate_iri(C1,C),
owl_rdf_assert(C,'rdf:type','owl:Class'),!.
owl2_export_axiom(objectProperty(C1),main_triple(C,'rdf:type','owl:ObjectProperty')) :-
translate_iri(C1,C),
owl_rdf_assert(C,'rdf:type','owl:ObjectProperty'),!.
owl2_export_axiom(dataProperty(C1),main_triple(C,'rdf:type','owl:DatatypeProperty')) :-
translate_iri(C1,C),
owl_rdf_assert(C,'rdf:type','owl:DatatypeProperty'),!.
owl2_export_axiom(annotationProperty(C1),main_triple(C,'rdf:type','owl:AnnotationProperty')) :-
translate_iri(C1,C),
owl_rdf_assert(C,'rdf:type','owl:AnnotationProperty'),!.
owl2_export_axiom(namedIndividual(C1),main_triple(C,'rdf:type','owl:NamedIndividual')) :-
translate_iri(C1,C),
owl_rdf_assert(C,'rdf:type','owl:NamedIndividual'),!.
owl2_export_axiom(subClassOf(C1,C2),main_triple(TC1,'rdfs:subClassOf',TC2)) :-
owl2_export_axiom(C1,main_triple(TC1,_,_)),
owl2_export_axiom(C2,main_triple(TC2,_,_)),
owl_rdf_assert(TC1,'rdfs:subClassOf',TC2),!.
owl2_export_axiom(equivalentClasses([X,Y]),main_triple(Tx,'owl:equivalentClass',Ty)) :-
owl2_export_axiom(X,main_triple(Tx,_,_)),
owl2_export_axiom(Y,main_triple(Ty,_,_)),!,
owl_rdf_assert(Tx,'owl:equivalentClass',Ty).
owl2_export_axiom(disjointClasses([X,Y]),main_triple(Tx,'owl:disjointWith',Ty)) :-
owl2_export_axiom(X,main_triple(Tx,_,_)),
owl2_export_axiom(Y,main_triple(Ty,_,_)),!,
owl_rdf_assert(Tx,'owl:disjointWith',Ty).
owl2_export_axiom(disjointClasses(List),main_triple(BNode,'rdf:type','owl:AlldisjointClasses')) :-
as2rdf_bnode(disjointClasses(List),BNode),
owl2_export_list(List,LNode),
owl_rdf_assert(BNode,'rdf:type','owl:AlldisjointClasses'),
owl_rdf_assert(BNode,'owl:members',LNode),!.
owl2_export_axiom(disjointUnion(C,Rest),main_triple(Tc,'owl:disjointUnionOf',LNode)) :-
owl2_export_axiom(C,main_triple(Tc,_,_)),
owl2_export_list(Rest,LNode),
owl_rdf_assert(Tc,'owl:disjointUnionOf',LNode),!.
owl2_export_axiom(subPropertyOf(propertyChain(PL),P2),main_triple(Tp2,'owl:propertyChainAxiom',LNode)) :-
owl2_export_axiom(P2,main_triple(Tp2,_,_)),
owl2_export_list(PL,LNode),
owl_rdf_assert(Tp2,'owl:propertyChainAxiom',LNode),!.
owl2_export_axiom(subPropertyOf(P1,P2),main_triple(Tp1,'rdfs:subPropertyOf',Tp2)) :-
owl2_export_axiom(P1,main_triple(Tp1,_,_)),
owl2_export_axiom(P2,main_triple(Tp2,_,_)),
owl_rdf_assert(Tp1,'rdfs:subPropertyOf',Tp2),!.
owl2_export_axiom(equivalentProperties([X,Y]),main_triple(Tx,'owl:equivalentProperty',Ty)) :-
owl2_export_axiom(X,main_triple(Tx,_,_)),
owl2_export_axiom(Y,main_triple(Ty,_,_)),
owl_rdf_assert(Tx,'owl:equivalentProperty',Ty),!.
% TODO: >2 equivalent Properties
owl2_export_axiom(disjointProperties([X,Y]),main_triple(Tx,'owl:propertyDisjointWith',Ty)) :-
owl2_export_axiom(X,main_triple(Tx,_,_)),
owl2_export_axiom(Y,main_triple(Ty,_,_)),
owl_rdf_assert(Tx,'owl:propertyDisjointWith',Ty),!.
owl2_export_axiom(disjointProperties(List),main_triple(BNode,'rdf:type','owl:AlldisjointProperties')) :-
as2rdf_bnode(disjointClasses(List),BNode),
owl2_export_list(List,LNode),
owl_rdf_assert(BNode,'rdf:type','owl:AlldisjointProperties'),
owl_rdf_assert(BNode,'owl:members',LNode),!.
owl2_export_axiom(propertyDomain(PE,CE),main_triple(Tpe,'rdfs:domain',Tce)) :-
owl2_export_axiom(PE,main_triple(Tpe,_,_)),
owl2_export_axiom(CE,main_triple(Tce,_,_)),
owl_rdf_assert(Tpe,'rdfs:domain',Tce),!.
owl2_export_axiom(propertyRange(PE,CE),main_triple(Tpe,'rdfs:range',Tce)) :-
owl2_export_axiom(PE,main_triple(Tpe,_,_)),
owl2_export_axiom(CE,main_triple(Tce,_,_)),
owl_rdf_assert(Tpe,'rdfs:range',Tce),!.
owl2_export_axiom(inverseProperties(P1,P2),main_triple(Tp1,'owl:inverseOf',Tp2)) :-
owl2_export_axiom(P1,main_triple(Tp1,_,_)),
owl2_export_axiom(P2,main_triple(Tp2,_,_)),
owl_rdf_assert(Tp1,'owl:inverseOf',Tp2),!.
owl2_export_axiom(functionalProperty(P),main_triple(Tp,'rdf:type','owl:FunctionalProperty')) :-
owl2_export_axiom(P,main_triple(Tp,_,_)),owl_rdf_assert(Tp,'rdf:type','owl:FunctionalProperty'),!.
owl2_export_axiom(inverseFunctionalProperty(P),main_triple(Tp,'rdf:type','owl:InverseFunctionalProperty')) :-
owl2_export_axiom(P,main_triple(Tp,_,_)),owl_rdf_assert(Tp,'rdf:type','owl:InverseFunctionalProperty'),!.
owl2_export_axiom(reflexiveProperty(P),main_triple(Tp,'rdf:type','owl:ReflexiveProperty')) :-
owl2_export_axiom(P,main_triple(Tp,_,_)),owl_rdf_assert(Tp,'rdf:type','owl:ReflexiveProperty'),!.
owl2_export_axiom(irreflexiveProperty(P),main_triple(Tp,'rdf:type','owl:IrreflexiveProperty')) :-
owl2_export_axiom(P,main_triple(Tp,_,_)),owl_rdf_assert(Tp,'rdf:type','owl:IrreflexiveProperty'),!.
owl2_export_axiom(symmetricProperty(P),main_triple(Tp,'rdf:type','owl:SymmetricProperty')) :-
owl2_export_axiom(P,main_triple(Tp,_,_)),owl_rdf_assert(Tp,'rdf:type','owl:SymmetricProperty'),!.
owl2_export_axiom(asymmetricProperty(P),main_triple(Tp,'rdf:type','owl:AsymmetricProperty')) :-
owl2_export_axiom(P,main_triple(Tp,_,_)),owl_rdf_assert(Tp,'rdf:type','owl:AsymmetricProperty'),!.
owl2_export_axiom(transitiveProperty(P),main_triple(Tp,'rdf:type','owl:TransitiveProperty')) :-
owl2_export_axiom(P,main_triple(Tp,_,_)),owl_rdf_assert(Tp,'rdf:type','owl:TransitiveProperty'),!.
owl2_export_axiom(hasKey([C|Rest]),main_triple(Tc,'owl:hasKey',LNode)) :-
owl2_export_axiom(C,main_triple(Tc,_,_)),
owl2_export_list(Rest,LNode),
owl_rdf_assert(Tc,'owl:hasKey',LNode),!.
%
% Individuals
%
owl2_export_axiom(sameIndividual([X,Y]),main_triple(Tx,'owl:sameAs',Ty)) :-
owl2_export_axiom(X,main_triple(Tx,_,_)),
owl2_export_axiom(Y,main_triple(Ty,_,_)),
owl_rdf_assert(Tx,'owl:sameAs',Ty),!.
owl2_export_axiom(differentIndividuals([X,Y]),main_triple(Tx,'owl:differentFrom',Ty)) :-
owl2_export_axiom(X,main_triple(Tx,_,_)),
owl2_export_axiom(Y,main_triple(Ty,_,_)),
owl_rdf_assert(Tx,'owl:differentFrom',Ty),!.
owl2_export_axiom(differentIndividuals(List),main_triple(BNode,'rdf:type','owl:AllDifferent')) :-
as2rdf_bnode(differentIndividuals(List),BNode),
owl2_export_list(List,LNode),
owl_rdf_assert(BNode,'rdf:type','owl:AllDifferent'),
owl_rdf_assert(BNode,'owl:members',LNode),!.
owl2_export_axiom(classAssertion(CE,A),main_triple(Ta,'rdf:type',Tce)) :-
owl2_export_axiom(A,main_triple(Ta,_,_)),
owl2_export_axiom(CE,main_triple(Tce,_,_)),
owl_rdf_assert(Ta,'rdf:type',Tce),
!.
owl2_export_axiom(propertyAssertion(P,A1,A2),main_triple(Ta1,Tp,Ta2)) :-
owl2_export_axiom(P,main_triple(Tp,_,_)),
owl2_export_axiom(A1,main_triple(Ta1,_,_)),
owl2_export_axiom(A2,main_triple(Ta2,_,_)),
owl_rdf_assert(Ta1,Tp,Ta2),!.
owl2_export_axiom(propertyAssertion(inverseOf(P),A1,A2),main_triple(Ta2,Tp,Ta1)) :-
owl2_export_axiom(P,main_triple(Tp,_,_)),
owl2_export_axiom(A1,main_triple(Ta1,_,_)),
owl2_export_axiom(A2,main_triple(Ta2,_,_)),
owl_rdf_assert(Ta2,Tp,Ta1),!.
owl2_export_axiom(negativePropertyAssertion(P,A1,A2),main_triple(BNode,'rdf:type','owl:NegativePropertyAssertion')) :-
as2rdf_bnode(negativePropertyAssertion(P,A1,A2),BNode),
owl2_export_axiom(P,main_triple(Tp,_,_)),
owl2_export_axiom(A1,main_triple(Ta1,_,_)),
owl2_export_axiom(A2,main_triple(Ta2,_,_)),
owl_rdf_assert(BNode,'rdf:type','owl:NegativePropertyAssertion'),
owl_rdf_assert(BNode,'owl:sourceIndividual',Ta1),
owl_rdf_assert(BNode,'owl:assertionProperty',Tp),
owl_rdf_assert(BNode,'owl:targetIndividual',Ta2),!.
%
% AnnotationAssestions
%
owl2_export_axiom(annotationAssertion(AP,As,Av),main_triple(TAs,AP,TAv)) :-
atom(As), % TODO: see issue#8. axiom annotations result is As=annotation(_,_,_)
!,
owl2_export_axiom(As,main_triple(TAs,_,_)),
owl2_export_axiom(Av,main_triple(TAv,_,_)),
owl_rdf_assert(TAs,AP,TAv),!.
owl2_export_axiom(annotationAssertion(Ap,As,Av),main_triple(TAs,TAp,TAv)) :-
as2rdf_bnode(As,BNode), % As is complex term - see above
owl2_export_axiom(Ap,main_triple(TAp,_,_)),
owl2_export_axiom(Av,main_triple(TAv,_,_)),
owl2_export_axiom(BNode,main_triple(TAs,_,_)).
%reify(TAs,TAp,TAv,_,_,_).
%
% Property Expressions (Descriptions)
%
owl2_export_axiom(inverseOf(P),main_triple(BNode,'owl:inverseOf',P)) :-
as2rdf_bnode(inverseOf(P),BNode),
owl_rdf_assert(BNode,'owl:inverseOf', P),!.
%
% Class Expressions (Descriptions)
%
owl2_export_axiom(intersectionOf([E|Rest]),main_triple(BNode,'rdf:type',Type)) :-
as2rdf_bnode(intersectionOf([E|Rest]),BNode),
owl2_export_list([E|Rest],LNode),
( classExpression(E) -> Type = 'owl:Class'; Type = 'owl:Datatype'),
owl_rdf_assert(BNode,'rdf:type',Type),
owl_rdf_assert(BNode,'owl:intersectionOf', LNode),
!.
owl2_export_axiom(unionOf([E|Rest]),main_triple(BNode,'rdf:type',Type)) :-
as2rdf_bnode(unionOf([E|Rest]),BNode),
owl2_export_list([E|Rest],LNode),
( classExpression(E) -> Type = 'owl:Class'; Type = 'owl:Datatype'),
owl_rdf_assert(BNode,'rdf:type',Type),
owl_rdf_assert(BNode,'owl:unionOf', LNode),!.
owl2_export_axiom(oneOf([E|Rest]),main_triple(BNode,'rdf:type',Type)) :-
as2rdf_bnode(oneOf([E|Rest]),BNode),
owl2_export_list([E|Rest],LNode),
( classExpression(E)
-> Type = 'owl:Class'
; Type = 'owl:Datatype'),
owl_rdf_assert(BNode,'rdf:type',Type),
owl_rdf_assert(BNode,'owl:oneOf', LNode),!.
owl2_export_axiom(datatypeRestriction(DT,FVs),main_triple(BNode,'rdf:type','rdfs:Datatype')) :-
as2rdf_bnode(datatypeRestriction(DT,FVs),BNode),
owl_rdf_assert(BNode,'rdf:type','rdfs:Datatype'),
owl2_export_axiom(DT,main_triple(Tpe,_,_)),owl_rdf_assert(BNode,'owl:onDatatype',Tpe),
owl2_export_list(FVs,LNode),
owl_rdf_assert(BNode,'owl:withRestrictions',LNode).
owl2_export_axiom(facetRestriction(F,V),main_triple(BNode,F2,V2)) :-
( sub_atom(F,_,_,_,'#')
-> F2=F2
; atom_concat('xsd:',F,F2)),
as2rdf_bnode(facetRestriction(F2,V2),BNode),
owl_rdf_assert(BNode,F,V). % TODO -- check
owl2_export_axiom(complementOf(E),main_triple(BNode,'rdf:type',Type)) :-
as2rdf_bnode(complementOf(E),BNode),
owl2_export_axiom(E,main_triple(Te,_,_)),
( classExpression(E) -> Type = 'owl:complementOf'; Type = 'owl:datatypeComplementOf'),
owl_rdf_assert(BNode,'owl:complementOf', Te),!.
owl2_export_axiom(someValuesFrom(PE,CEorDR),main_triple(BNode,'rdf:type','owl:Restriction')) :-
as2rdf_bnode(someValuesFrom(PE,CEorDR),BNode),
owl_rdf_assert(BNode,'rdf:type','owl:Restriction'),
owl2_export_axiom(PE,main_triple(Tpe,_,_)),owl_rdf_assert(BNode,'owl:onProperty',Tpe),
owl2_export_axiom(CEorDR,main_triple(Tce,_,_)),owl_rdf_assert(BNode,'owl:someValuesFrom',Tce),!.
owl2_export_axiom(allValuesFrom(PE,CEorDR),main_triple(BNode,'rdf:type','owl:Restriction')) :-
as2rdf_bnode(allValuesFrom(PE,CEorDR),BNode),
owl_rdf_assert(BNode,'rdf:type','owl:Restriction'),
owl2_export_axiom(PE,main_triple(Tpe,_,_)),owl_rdf_assert(BNode,'owl:onProperty',Tpe),
owl2_export_axiom(CEorDR,main_triple(Tce,_,_)),owl_rdf_assert(BNode,'owl:allValuesFrom',Tce),!.
owl2_export_axiom(hasValue(PE,Value),main_triple(BNode,'rdf:type','owl:Restriction')) :-
as2rdf_bnode(hasValue(PE,Value),BNode),
owl_rdf_assert(BNode,'rdf:type','owl:Restriction'),
owl2_export_axiom(PE,main_triple(Tpe,_,_)),owl_rdf_assert(BNode,'owl:onProperty',Tpe),
owl2_export_axiom(Value,main_triple(TValue,_,_)),owl_rdf_assert(BNode,'owl:hasValue',TValue),!.
owl2_export_axiom(hasSelf(OPE),main_triple(BNode,'rdf:type','owl:Restriction')) :-
as2rdf_bnode(hasValue(OPE),BNode),
owl_rdf_assert(BNode,'rdf:type','owl:Restriction'),
owl2_export_axiom(OPE,main_triple(Tope,_,_)),owl_rdf_assert(BNode,'owl:onProperty',Tope),
owl_rdf_assert(BNode,'owl:hasSelf', literal(type('http://www.w3.org/2001/XMLSchema#boolean','true'))),!.
owl2_export_axiom(minCardinality(N,OPE),main_triple(BNode,'rdf:type','owl:Restriction')) :-
as2rdf_bnode(minCardinality(N,OPE),BNode),
owl_rdf_assert(BNode,'rdf:type','owl:Restriction'),
owl_rdf_assert(BNode,'owl:minCardinality',literal(type('http://www.w3.org/2001/XMLSchema#nonNegativeInteger',N))),
owl2_export_axiom(OPE,main_triple(Tope,_,_)),owl_rdf_assert(BNode,'owl:onProperty',Tope),!.
owl2_export_axiom(minCardinality(N,OPE,CEorDR),main_triple(BNode,'rdf:type','owl:Restriction')) :-
as2rdf_bnode(minCardinality(N,OPE,CEorDR),BNode),
owl_rdf_assert(BNode,'rdf:type','owl:Restriction'),
owl_rdf_assert(BNode,'owl:minQualifiedCardinality',literal(type('http://www.w3.org/2001/XMLSchema#nonNegativeInteger',N))),
owl2_export_axiom(OPE,main_triple(Tope,_,_)),owl_rdf_assert(BNode,'owl:onProperty',Tope),
owl2_export_axiom(CEorDR,main_triple(Tce,_,_)),
( classExpression(CEorDR) -> owl_rdf_assert(BNode,'owl:onClass',Tce); owl_rdf_assert(BNode,'owl:onDataRange',Tce)),!.
owl2_export_axiom(maxCardinality(N,OPE),main_triple(BNode,'rdf:type','owl:Restriction')) :-
as2rdf_bnode(maxCardinality(N,OPE),BNode),
owl_rdf_assert(BNode,'rdf:type','owl:Restriction'),
owl_rdf_assert(BNode,'owl:maxCardinality',literal(type('http://www.w3.org/2001/XMLSchema#nonNegativeInteger',N))),
owl2_export_axiom(OPE,main_triple(Tope,_,_)),owl_rdf_assert(BNode,'owl:onProperty',Tope),!.
owl2_export_axiom(maxCardinality(N,OPE,CEorDR),main_triple(BNode,'rdf:type','owl:Restriction')) :-
as2rdf_bnode(maxCardinality(N,OPE,CEorDR),BNode),
owl_rdf_assert(BNode,'rdf:type','owl:Restriction'),
owl_rdf_assert(BNode,'owl:maxQualifiedCardinality',literal(type('http://www.w3.org/2001/XMLSchema#nonNegativeInteger',N))),
owl2_export_axiom(OPE,main_triple(Tope,_,_)),owl_rdf_assert(BNode,'owl:onProperty',Tope),
owl2_export_axiom(CEorDR,main_triple(Tce,_,_)),
( classExpression(CEorDR) -> owl_rdf_assert(BNode,'owl:onClass',Tce); owl_rdf_assert(BNode,'owl:onDataRange',Tce)),!.
owl2_export_axiom(exactCardinality(N,OPE),main_triple(BNode,'rdf:type','owl:Restriction')) :-
as2rdf_bnode(exactCardinality(N,OPE),BNode),
owl_rdf_assert(BNode,'rdf:type','owl:Restriction'),
owl_rdf_assert(BNode,'owl:cardinality',literal(type('http://www.w3.org/2001/XMLSchema#nonNegativeInteger',N))),
owl2_export_axiom(OPE,main_triple(Tope,_,_)),owl_rdf_assert(BNode,'owl:onProperty',Tope),!.
owl2_export_axiom(exactCardinality(N,OPE,CEorDR),main_triple(BNode,'rdf:type','owl:Restriction')) :-
as2rdf_bnode(exactCardinality(N,OPE,CEorDR),BNode),
owl_rdf_assert(BNode,'rdf:type','owl:Restriction'),
owl_rdf_assert(BNode,'owl:qualifiedCardinality',literal(type('http://www.w3.org/2001/XMLSchema#nonNegativeInteger',N))),
owl2_export_axiom(OPE,main_triple(Tope,_,_)),owl_rdf_assert(BNode,'owl:onProperty',Tope),
owl2_export_axiom(CEorDR,main_triple(Tce,_,_)),
( classExpression(CEorDR) -> owl_rdf_assert(BNode,'owl:onClass',Tce); owl_rdf_assert(BNode,'owl:onDataRange',Tce)),!.
%
% SWRL
% (this may eventually end up in a separate hooks file?)
owl2_export_axiom(implies(AL,CL),main_triple(RuleNode,'rdf:type','swrl:Imp')) :-
% TODO: allow rules to have IDs in swrl.pl
% for now we force it to be a bnode
!,
%rdf_bnode(RuleNode),
as2rdf_bnode(implies(AL,CL),RuleNode),
owl_rdf_assert(RuleNode,'rdf:type','swrl:Imp'),
swrl_export_atom_list(AL,ALNode),
swrl_export_atom_list(CL,CLNode),
owl_rdf_assert(RuleNode,'swrl:body', ALNode),
owl_rdf_assert(RuleNode,'swrl:head', CLNode).
:- multifile translate_iri_hook/2.
owl2_export_axiom(IRI,main_triple(T,_,_)) :- translate_iri_hook(IRI,T), !.
owl2_export_axiom(IRI,main_triple(IRI,_,_)) :- atom(IRI),!. % better iri(IRI).
owl2_export_axiom(X,main_triple(X,_,_)) :- X=literal(_),!. % better iri(IRI).
owl2_export_axiom(Any,main_triple(Any,_,_)) :- format(user_error,'unresolved: ~w~n',[Any]).
translate_iri(X,Y) :- translate_iri_hook(X,Y),!.
translate_iri(X,X).
/*
owl2_export_annotation(Parent)
Parent can be an axiom or an annotation itself (nested annotations)
*/
owl2_export_annotation(annotation(_,_,_),_,S,_,_) :-
\+ atom(S), % do nothing with this for now - see issue #8
!.
owl2_export_annotation(Parent,ParentType,S,ParentP,ParentO) :-
( Parent = annotation(_,ParentAP,ParentAV)
-> P = ParentAP,
O = ParentAV,
owl_rdf_assert(S,P,O)
; P = ParentP, O = ParentO),
findall(annotation(Parent,AP,AV),annotation(Parent,AP,AV),ANNs),
( ANNs = [_|_]
-> as2rdf_bnode(Parent,BNode),
reify(BNode,'rdf:type',ParentType,S,P,O),
forall(member(ANN,ANNs),owl2_export_annotation(ANN,'owl:Annotation',BNode,_,_))
; true).
reify(SNode,PTerm,OTerm,S,P,O) :-
owl_rdf_assert(SNode,PTerm,OTerm),
owl_rdf_assert(SNode,'owl:annotatedSource',S),
owl_rdf_assert(SNode,'owl:annotatedProperty',P),
owl_rdf_assert(SNode,'owl:annotatedTarget',O).
/*
owl2_export_list(+List, -Node).
Generates RDF triples for the List of construct based on
Abstract Syntax list transformation rules. Node represents the
List in the resulting RDF graph
*/
owl2_export_list([],'rdf:nil').
owl2_export_list([S|Rest],Node) :-
as2rdf_bnode([S|Rest],Node),
owl2_export_axiom(S,main_triple(Ts,_,_)),
% this is to circumvent a bug in list writing:
( S=literal(_)
-> true
; owl_rdf_assert(Node,'rdf:type', 'rdf:List')),
owl_rdf_assert(Node,'rdf:first', Ts),
owl2_export_list(Rest,Node2),
owl_rdf_assert(Node,'rdf:rest',Node2).
swrl_export_atom_list([],'rdf:nil').
swrl_export_atom_list([S|Rest],Node) :-
as2rdf_bnode([S|Rest],Node),
swrl_export_atom(S,main_triple(Ts,_,_)),
%owl_rdf_assert(Node,'rdf:type', 'swrl:AtomList'),
owl_rdf_assert(Node,'rdf:type', 'rdf:List'),
owl_rdf_assert(Node,'rdf:first', Ts),
swrl_export_atom_list(Rest,Node2),
owl_rdf_assert(Node,'rdf:rest',Node2).
% in swrl.pl atom lists are weakly typed - we allow a swrl atom to
% stand in for an atom list for convenience
swrl_export_atom_list(X,Node) :-
\+ atom(X),
swrl_export_atom_list([X],Node).
swrl_export_argument_list([],'rdf:nil').
swrl_export_argument_list([S|Rest],Node) :-
as2rdf_bnode([S|Rest],Node),
swrl_export_argument(S,main_triple(Ts,_,_)),
owl_rdf_assert(Node,'rdf:type', 'swrl:ArgumentList'),
owl_rdf_assert(Node,'rdf:first', Ts),
swrl_export_argument_list(Rest,Node2),
owl_rdf_assert(Node,'rdf:rest',Node2).
swrl_export_atom(propertyAssertion(OPE,A1,A2),main_triple(BNode,'rdf:type','swrl:DatavaluedPropertyAtom')) :-
dataProperty(OPE),
!,
rdf_bnode(BNode),
owl_rdf_assert(BNode,'rdf:type','swrl:DatavaluedPropertyAtom'),
owl2_export_axiom(OPE,main_triple(Tope,_,_)),owl_rdf_assert(BNode,'swrl:propertyPredicate',Tope),
swrl_export_argument(A1,main_triple(TA1,_,_)),owl_rdf_assert(BNode,'swrl:argument1',TA1),
swrl_export_argument(A2,main_triple(TA2,_,_)),owl_rdf_assert(BNode,'swrl:argument2',TA2).
swrl_export_atom(propertyAssertion(OPE,A1,A2),main_triple(BNode,'rdf:type','swrl:IndividualPropertyAtom')) :-
!,
rdf_bnode(BNode),
%as2rdf_bnode(propertyAssertion(OPE,A1,A2),BNode),
owl_rdf_assert(BNode,'rdf:type','swrl:IndividualPropertyAtom'),
owl2_export_axiom(OPE,main_triple(Tope,_,_)),owl_rdf_assert(BNode,'swrl:propertyPredicate',Tope),
swrl_export_argument(A1,main_triple(TA1,_,_)),owl_rdf_assert(BNode,'swrl:argument1',TA1),
swrl_export_argument(A2,main_triple(TA2,_,_)),owl_rdf_assert(BNode,'swrl:argument2',TA2).
swrl_export_atom(builtin(OPE,Args),main_triple(BNode,'rdf:type','swrl:BuiltinAtom')) :-
!,
rdf_bnode(BNode),
owl_rdf_assert(BNode,'rdf:type','swrl:BuiltinAtom'),
%owl2_export_axiom(OPE,main_triple(Tope,_,_)),owl_rdf_assert(BNode,'swrl:builtin',Tope),
atom_concat('swrlb:',OPE,Builtin),owl_rdf_assert(BNode,'swrl:builtin',Builtin),
swrl_export_argument_list(Args,ListNode),
owl_rdf_assert(BNode,'swrl:arguments',ListNode).
swrl_export_atom(description(C,A1),main_triple(BNode,'rdf:type','swrl:ClassAtom')) :-
!,
rdf_bnode(BNode),
%as2rdf_bnode(description(C,A1),BNode),
owl_rdf_assert(BNode,'rdf:type','swrl:ClassAtom'),
owl2_export_axiom(C,main_triple(TC,_,_)),owl_rdf_assert(BNode,'swrl:classPredicate',TC),
swrl_export_argument(A1,main_triple(TA1,_,_)),owl_rdf_assert(BNode,'swrl:argument1',TA1).
swrl_export_atom(differentFrom(A1,A2),main_triple(BNode,'rdf:type','swrl:DifferentIndividualsAtom')) :-
!,
rdf_bnode(BNode),
%as2rdf_bnode(differentFrom(A1,A2),BNode),
owl_rdf_assert(BNode,'rdf:type','swrl:DifferentIndividualsAtom'),
swrl_export_argument(A1,main_triple(TA1,_,_)),owl_rdf_assert(BNode,'swrl:argument1',TA1),
swrl_export_argument(A2,main_triple(TA2,_,_)),owl_rdf_assert(BNode,'swrl:argument2',TA2).
swrl_export_atom(sameAs(A1,A2),main_triple(BNode,'rdf:type','swrl:SameIndividualAtom')) :-
!,
rdf_bnode(BNode),
%as2rdf_bnode(sameAs(A1,A2),BNode),
owl_rdf_assert(BNode,'rdf:type','swrl:SameIndividualAtom'),
swrl_export_argument(A1,main_triple(TA1,_,_)),owl_rdf_assert(BNode,'swrl:argument1',TA1),
swrl_export_argument(A2,main_triple(TA2,_,_)),owl_rdf_assert(BNode,'swrl:argument2',TA2).
% allow convenient non-canonical form
swrl_export_atom(CA,T) :-
CA=..[C,A],
!,
swrl_export_atom(class(C,A),T).
swrl_export_atom(PA,T) :-
PA=..[P,A1,A2],
!,
swrl_export_atom(propertyAssertion(P,A1,A2),T).
swrl_export_argument(v(V),main_triple(V1,'rdf:type','swrl:Variable')) :-
!,
( number(V)
-> atom_concat('#v',V,V1)
; V1=V),
owl_rdf_assert(V1,'rdf:type','swrl:Variable'),!.
swrl_export_argument(d(V),T) :- swrl_export_argument(v(V),T).
swrl_export_argument(i(V),T) :- swrl_export_argument(v(V),T).
swrl_export_argument(V,T) :- owl2_export_axiom(V,T).
/*
owl_rdf_assert(S,P,O).
Expands the NS the S, P, O terms and asserts into the RDF
database
*/
owl_rdf_assert(S,P,O) :-
expand_ns(S,S1),expand_ns(P,P1),expand_ns(O,O1),
debug(rdf_assert,'assert: ~w ~w ~w.',[S,P,O]),
rdf_db:rdf_assert(S1,P1,O1), !.
/*
as2rdf_bnode(+X,-Node).
It generates a bnode Node for construct X in case it does not
exist already as a blanknode/2 clause.
*/
as2rdf_bnode(X,Node) :-
blanknode_gen(Node,X),
debug(bnode,'bnode REUSE ~w ==> ~w',[X,Node]),
!.
as2rdf_bnode(X,Node) :-
rdf_db:rdf_bnode(Node),
assert(blanknode_gen(Node,X)),
debug(bnode,'bnode NEW ~w ==> ~w',[X,Node]),
!.