forked from vangelisv/thea
-
Notifications
You must be signed in to change notification settings - Fork 3
/
owl2_from_rdf.pl
1407 lines (1109 loc) · 51.3 KB
/
owl2_from_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
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
% * -*- Mode: Prolog -*- */
% **********************************************************************
% OWL PARSER
% Author: Vangelis Vassiliadis
% Change Log:
% May 04: Initial release 0.1
% Jan 05: Version 0.2 - Some code optimisation.
% Feb 05: DisjointClass, EquivalentClass
% Mar 05: DifferentFrom
% Feb 09: OWL2 (CJM)
%
% Version 0.3 First release, March 22, 2005
%
% Version 0.4 Changes (use of SWI's rdf parser 5.5.12)
% * rdf_load/2 options to noshare of blank nodes and use a
% convert function to hold datatyped values.
% * removed fix_owl. Apparently is not needed with SWI 5.5.x
% * Implemented equivalentProperties by re-using the logic
% of equivalentClass and sameAs.
% * Implementation of owl:imports (handled at RDF/triple
% level) by importing all RDF triples prior to building
% the the OWL abstract syntax terms. Flag to select if
% imports will be handled.
% * OWL parser can parse now either local files or URLs (in-
% line with Semweb's package RDF parser). Use of SWI's
% http package.
% * owl_pack_ontology, owl_report
% Version 0.5.5: March 07: Changes to the use_module and definitions for Thea 0.5.5 release.
% To do Check and report for purely Internal (DL errors)
% Inferences
% Changes for GIT
% **********************************************************************
:- module(owl2_from_rdf,
[
owl_parse_rdf/1,
owl_parse_rdf/2,
translate_rdf_db/1,
owl_parse/4,
rdf_db_to_owl/0,
convert/3,
expand_ns/2, % ?NS_URL, ?Full_URL
collapse_ns/4,
valid_axiom_annotation_mode/5,
uri_split/4,
owl_description/2,
blanknode/3,
use_owl/4,
test_use_owl/3 % expose them to allow external handling of triples, e.g. for rdfs support
% owl_parser_log/2 -- deprecated, use debug instead.
]).
/** <module> Translates an RDF database to OWL2 axioms
---+ Synopsis 2
==
:- use_module(bio(owl2_from_rdf)).
%
==
---+ Details
---++ Hooks
* owl_parse_axiom_hook/3
---+ See Also
The file owl2_from_rdf.plt has some examples
*/
:- use_module(owl2_model).
:- use_module(library(debug)).
:- use_module(library('semweb/rdf_db')).
:- use_module(library('semweb/rdf_edit')).
:- use_module(library('semweb/rdfs')).
:- use_module(library('url')).
:- use_module(library('http/http_open')).
:- dynamic(owl/4).
%% blanknode(Node,Description,Used)
% see owl_get_bnode/2
% Node - bNodeId
% Description - prolog term corresponding to owl Description
% Used - used | shared
:- dynamic(blanknode/3).
:- dynamic(outstream/1).
:- dynamic(aNN/3). % implements the ANN(X) function.
:- dynamic(annotation_r_node/4). % annotation_r_node(S,P,O,Node)
:- dynamic(axiom_r_node/4). % axiom_r_node(S,P,O,Node)
:- dynamic(owl_repository/2). % implements a simple OWL repository: if URL not found, Ontology is read from a repository (local) RURL
:- multifile(owl_repository/2).
% we make this discontiguous so that the code can follow the structure of the document as much as possible
:- discontiguous owl_parse_axiom/3.
:- discontiguous dothislater/1.
% hookable
:- multifile owl_parse_axiom_hook/3.
:- include('owl2_from_rdf_utils.pl').
% -----------------------------------------------------------------------
% Top Level Predicates
% -----------------------------------------------------------------------
:- multifile owl2_io:load_axioms_hook/3.
owl2_io:load_axioms_hook(File,owl,Opts) :-
owl_parse_rdf(File,Opts).
owl2_io:load_axioms_hook(File,ttl,Opts) :-
ensure_loaded(library('semweb/rdf_turtle')),
owl_parse_rdf(File,Opts).
%% owl_parse_rdf(+File)
% as owl_parse_rdf/1 with empty Opts
owl_parse_rdf(F):-
owl_parse_rdf(F,[]).
%% owl_parse_rdf(+File,+Opts:list)
% @param Opts
% * imports(ImportFlag:Boolean) if true, follow imports
% * clear(Clear) if Clear=complete, clears all axioms in owl2_model
owl_parse_rdf(F,Opts):-
( member(imports(Imports),Opts)
-> true
; Imports=false),
( member(clear(Clear),Opts)
-> true
; Clear=false),
owl_parse(F,Clear,Clear,Imports),
debug(owl_parser,'parsed ~w',[F]).
%% owl_parse(+URL, +RDF_Load_Mode, +OWL_Parse_Mode, +ImportFlag:boolean)
%
% Top level: parse a set of RDF triples and produce an
% AS representation of an OWL ontology.
%
% Calls the rdf_load_stream predicate to parse RDF stream in URL.
% If RDF_Load_Mode = complete it first retacts all rdf triples.
% If ImportFlag = true it handles owl:import clause at RDF level.
%
% This implements the mapping defined here:
% http://www.w3.org/TR/2008/WD-owl2-mapping-to-rdf-20081202/
owl_parse(URL, RDF_Load_Mode, OWL_Parse_Mode,ImportFlag) :-
( RDF_Load_Mode=complete
-> rdf_retractall(_,_,_), retractall(rdf_db:rdf_source(_,_,_,_))
; true),
( OWL_Parse_Mode=complete
-> owl_clear_as,retractall(blanknode(_,_,_)), retractall(owl(_,_,_,_))
; true),
!,
debug(owl_parser,'Loading stream ~w',[URL]),
owl_canonical_parse_2([URL],URL,ImportFlag,[],ProcessedIRIs),
debug(owl_parser,'rdf_db populated, the following IRIs were processed: ~w',[ProcessedIRIs]),
owl2_model_init,
owl_canonical_parse_3(ProcessedIRIs).
%% owl_canonical_parse_2(+IRIs:list,+ParentIRI,+ImportFlag:boolean,+ProcessedURIsIn:list,?ProcessedURIsOut:list) is det
% recursively parses all ontologies in IRIs into rdf_db, ensuring none are processed twice.
owl_canonical_parse_2([],_,_,Processed,Processed) :- !.
owl_canonical_parse_2([IRI|ToProcessRest],Parent,ImportFlag,ProcessedIn,ProcessedOut) :-
member(IRI,ProcessedIn),
!,
owl_canonical_parse_2(ToProcessRest,Parent,ImportFlag,ProcessedIn,ProcessedOut).
owl_canonical_parse_2([IRI|ToProcessRest],Parent,ImportFlag,ProcessedIn,ProcessedOut) :-
% Get rdf triples, *Ontology* and Imports
rdf_load_stream(IRI,O,BaseURI,Imports),
( nonvar(O)
-> Ont = O
; Ont = Parent), % in the include case we may need to remove the import...
debug(owl_parser,'Commencing rdf_2_owl. Generating owl/4',[]),
rdf_2_owl(BaseURI,Ont), % move the RDF triples into the owl-Ont/4 facts
( ImportFlag = true
-> owl_canonical_parse_2(Imports,Ont,ImportFlag,[Ont|ProcessedIn],ProcessedIn1)
; ProcessedIn1=[Ont|ProcessedIn]),
owl_canonical_parse_2(ToProcessRest,Parent,ImportFlag,ProcessedIn1,ProcessedOut).
%% owl_canonical_parse_3(+IRIs:list) is det
% translate the current rdf_db into owl2_model axioms.
% First owl/4 facts are populated, and then these are translated
% according to:
% http://www.w3.org/TR/2008/WD-owl2-mapping-to-rdf-20081202/
% (table references refer to this document).
% we use an intermediate owl/4 database because the mapping
% is non-monotonic, and triples are 'consumed'
owl_canonical_parse_3([]).
owl_canonical_parse_3([IRI|Rest]) :-
% Remove any existing not used owl fact
retractall(owl(_,_,_,not_used)),
% Copy the owl facts of the IRI document to the 'not_used'
forall(owl(S,P,O,IRI),assert(owl(S,P,O,not_used))),
debug(owl_parser,'Anon individuals in reification [see table 8]',[]),
collect_r_nodes,
% First parse the Ontology axiom
owl_parse_annotated_axioms(ontology/1),
debug(owl_parser,'Replacing patterns [see table 5]',[]),
% remove triples based on pattern match (Table 5)
( forall((triple_remove(Pattern,Remove), test_use_owl(Pattern)),
forall(member(owl(S,P,O),Remove),use_owl(S,P,O,removed))) -> true ; true),
% temporary fix to make up for bug in rdf parsing
% see email to JanW July-1-2009
forall((test_use_owl(S,P,BNode),
is_bnode(BNode),
test_use_owl(BNode,'http://www.w3.org/1999/02/22-rdf-syntax-ns#datatype',literal(_))),
( use_owl(S,P,BNode,datatype_fix),
use_owl(BNode,'http://www.w3.org/1999/02/22-rdf-syntax-ns#datatype',literal(_)),
expand_and_assert(S,P,literal('')))),
% replace matched patterns (Table 6)
debug(owl_parser,'Replacing patterns [see table 6]',[]),
( setof(ReplaceWith,
Pattern^( triple_replace(Pattern,ReplaceWith), % +Triples:list, ?Triples:list
use_owl(Pattern),
debug(owl_parser,'Replacing ~w ==> ~w [see table 6]',[Pattern,ReplaceWith])),
ReplacementSetList)
-> forall((member(ReplacementSet,ReplacementSetList),member(owl(S,P,O),ReplacementSet)),
expand_and_assert(S,P,O))
; debug(owl_parser,'No replacements required',[])),
/*
forall(triple_replace(Pattern,ReplaceWith),
forall(use_owl(Pattern),
forall(member(owl(S,P,O),ReplaceWith),
( expand_and_assert(S,P,O),
debug(owl_parser,'Replacing ~w ==> ~w [see table 6]',[Pattern,owl(S,P,O)]))))),
*/
% continue with parsing using the rules...
% Table 8, get the set of RIND - anonymous individuals in reification
findall(X, (member(Y,['owl:Axiom','owl:Annotation',
'owl:AllDisjointClasses','owl:AllDisljointProperties',
'owl:AllDifferent','owl:NegativePropertyAssertion']),
test_use_owl(X,'rdf:type',Y)
),
RIND),
nb_setval(rind,RIND),
% Table 9, row 5
% VV 10/3/2010 get the annotation properties before collecting the annotations.
debug(owl_parser,'asserting annotationProperty/1 for all APs',[]),
forall( test_use_owl(D,'rdf:type','owl:AnnotationProperty'),
assert_axiom(annotationProperty(D))),
% TODO - make this faster
debug(owl_parser,'Implements function ANN(x) 3.2.2 Table 10.',[]),
findall(_,ann(_,_),_), % find all annotations, assert annotation(X,AP,AV) axioms.
debug(owl_parser,'Commencing parse of annotated axioms',[]),
forall((axiompred(PredSpec),\+dothislater(PredSpec),\+omitthis(PredSpec)),
owl_parse_annotated_axioms(PredSpec)),
forall((axiompred(PredSpec),dothislater(PredSpec),\+omitthis(PredSpec)),
owl_parse_annotated_axioms(PredSpec)),
debug(owl_parser_detail,'Commencing parse of unannotated axioms',[]),
forall((axiompred(PredSpec),\+dothislater(PredSpec),\+omitthis(PredSpec)),
owl_parse_nonannotated_axioms(PredSpec)),
forall((axiompred(PredSpec),dothislater(PredSpec),\+omitthis(PredSpec)),
owl_parse_nonannotated_axioms(PredSpec)),!,
% annotation Assertion
parse_annotation_assertions,
forall(owl_parse_compatibility_DL(Axiom),assert_axiom(Axiom)),
owl_canonical_parse_3(Rest).
rdf_db_to_owl :-
owl2_model_init,
findall(BaseURI,
( rdf(Ont,'http://www.w3.org/1999/02/22-rdf-syntax-ns#type','http://www.w3.org/2002/07/owl#Ontology',BaseURI:_),
rdf_2_owl(BaseURI,Ont),
owl_canonical_parse_3(IRIs)),
IRIs).
%% translate_rdf_db(+IRI)
% translates a graph in current rdf_db instance into an owl2_model.pl set of facts.
% assumes that IRI has already been loaded using the semweb package
translate_rdf_db(BaseURI) :-
rdf(Ont,'http://www.w3.org/1999/02/22-rdf-syntax-ns#type','http://www.w3.org/2002/07/owl#Ontology',BaseURI:_),
!,
rdf_2_owl(BaseURI,Ont),
owl2_model_init,
owl_canonical_parse_3(BaseURI).
omitthis(ontology/1).
owl_parse_annotated_axioms(Pred/Arity) :-
debug(owl_parser_detail,'[ann] Parsing all of type: ~w',[Pred]),
functor(Head,Pred,Arity),
% forall(owl_parse_axiom(Mod:Head),
% ( debug(owl_parser_detail,' parsed: [~w] ~w',[Mod,Head]),
% assert(Mod:Head))).
forall(owl_parse_axiom(Head,true,Annotations),
( assert_axiom(Head),
debug(owl_parser_detail_anns,' parsed: ~w : anns: ~w',[Head,Annotations]),
forall(member(X,Annotations),
forall(aNN(X,AP,AV),
assert_axiom(annotation(Head,AP,AV)))
)
)
),
debug(owl_parser_detail,'[ann] Done parsing all of type: ~w',[Pred]).
owl_parse_nonannotated_axioms(Pred/Arity) :-
debug(owl_parser_detail,'[unann] Parsing all of type: ~w',[Pred]),
functor(Head,Pred,Arity),
forall(owl_parse_axiom(Head,false,_),
assert_axiom(Head)
).
%% rdf_load_stream(+URL, -Ontology, -BaseURI, -Imports:list) is det
%
% This predicate calls the rdf parser to parse the RDF/XML URL
% into RDF triples. URL can be a local file or a URL.
% The predicate returns all Imports based on the owl:imports predicate.
% Also the Ontology of the URL if an owl:Ontology exists, var
% otherise.
%
% If owl_repository/2 is defined, then this is used to map URLs
% prior to loading.
rdf_load_stream(URL,Ontology,BaseURI,Imports) :-
owl_repository(URL,RURL),
!,
% note: users responsibility to avoid infinite loops by avoid cycles in repository mappings!
rdf_load_stream(RURL,Ontology,BaseURI,Imports).
rdf_load_stream(URL,Ontology,BaseURI,Imports) :-
BaseURI = URL,
( sub_atom(URL,0,4,_,'http')
-> catch((http_open(URL,RDF_Stream,[]),
rdf_load(RDF_Stream,[if(true),base_uri(BaseURI),blank_nodes(noshare),
result(Action, Triples, MD5),register_namespaces(true)]),
debug(owl_parser,' Loaded ~w stream: ~w Action: ~w Triples:~w MD5: ~w',[URL,RDF_Stream,Action,Triples,MD5]),
close(RDF_Stream)),
Message,
throw(io_error(URL,'rdf_load/2 failed',Message))) % re-throw with more information
; RDF_Stream = URL, rdf_load(RDF_Stream,[blank_nodes(noshare),if(true),base_uri(BaseURI),register_namespaces(true)])
),
% collect all imports directives
( rdf(Ontology,'http://www.w3.org/1999/02/22-rdf-syntax-ns#type','http://www.w3.org/2002/07/owl#Ontology',BaseURI:_)
-> findall(I,rdf(Ontology,'http://www.w3.org/2002/07/owl#imports',I,BaseURI:_),Imports)
; Imports = []
).
% ----------------------------------------------------------------
% 3 Mapping from RDF Graphs to the Structural Specification
% ----------------------------------------------------------------
/*
This section specifies the results of steps CP-2.2 and CP-3.3 of the
canonical parsing process from Section 3.6 of the OWL 2
Specification [OWL 2 Specification] on an ontology document D that
can be parsed into an RDF graph G. ...
*/
% owl_description_list(+Node, -List)
%
% If +Node is defined as rdf:type rdf:List, then List returns
% a prolog list of descriptions for this Node.
owl_description_list('http://www.w3.org/1999/02/22-rdf-syntax-ns#nil',[]) :- !.
owl_description_list(X,[F|R]) :-
% use_owl(X,'rdf:type','rdf:List',list), % this is now removed from graph
use_owl(X,'rdf:first',Element,first),
owl_description(Element,F),
use_owl(X,'rdf:rest',Y,rest),
!,owl_description_list(Y,R).
% owl_individual_list(+Node, -List)
%
% If +Node is defined as rdf:type rdf:List, then List returns
% a prolog list of individuals for this Node.
owl_individual_list('http://www.w3.org/1999/02/22-rdf-syntax-ns#nil',[]) :- !.
owl_individual_list(X,[F|R]) :-
% use_owl(X,'rdf:type','rdf:List',list), % this is now removed from graph
use_owl(X,'rdf:first',F,first),
use_owl(X,'rdf:rest',Y,rest),
!,owl_individual_list(Y,R).
% owl_property_list(+Node, -List)
%
% If +Node is defined as rdf:type rdf:List, then List returns
% a prolog list of properties for this Node.
owl_property_list('http://www.w3.org/1999/02/22-rdf-syntax-ns#nil',[]) :- !.
owl_property_list(X,[F|R]) :-
% use_owl(X,'rdf:type','rdf:List',list), % this is now removed from graph
use_owl(X,'rdf:first',Element,first),
owl_property_expression(Element,F),
use_owl(X,'rdf:rest',Y,rest),
!,owl_property_list(Y,R).
% owl_datarange_list(+Node, -List)
%
% If +Node is defined as rdf:type rdf:List, then List returns
% a prolog list of dataranges for this Node.
owl_datarange_list('http://www.w3.org/1999/02/22-rdf-syntax-ns#nil',[]) :- !.
owl_datarange_list(X,[F|R]) :-
% use_owl(X,'rdf:type','rdf:List',list), % this is now removed from graph
use_owl(X,'rdf:first',Element,first),
owl_datarange(Element,F),
use_owl(X,'rdf:rest',Y,rest),
!,owl_datarange_list(Y,R).
% owl_datatype_restriction_list(+Node, -List)
%
% If +Node is defined as rdf:type rdf:List, then List returns
% a prolog list of datatype restrictions for this Node.
owl_datatype_restriction_list('http://www.w3.org/1999/02/22-rdf-syntax-ns#nil',[]) :- !.
owl_datatype_restriction_list(X,[facetRestriction(W2,L)|R]) :-
% use_owl(X,'rdf:type','rdf:List'), % this is now removed from graph
use_owl(X,'rdf:first',Element,first_datatype_restr),
use_owl(Element,W,L,datatype_restr),
( concat_atom([_,W2],'#',W)
-> true
; W2=W),
use_owl(X,'rdf:rest',Y,rest_datatype_restr),
!,owl_datatype_restriction_list(Y,R).
% 3.1 Extracting Declarations and the IRIs of the Directly Imported Ontology Documents
% This section specifies the result of step CP-2.2 of the canonical parsing process on an RDF graph G
% 3.1.2 Parsing of the Ontology Header and Declarations
% Table 4.
owl_parse_axiom(ontology(O),AnnMode,List) :-
test_use_owl(O,'rdf:type','owl:Ontology'),
\+ test_use_owl([owl(U,_W,O),owl(U,'rdf:type','owl:Ontology')]),
valid_axiom_annotation_mode(AnnMode,O,'rdf:type','owl:Ontology',List),
use_owl(O,'rdf:type','owl:Ontology',ontology),
nb_setval(current_ontology,O),
forall(use_owl(O,'owl:imports',IRI,ontology_import), assert_axiom(ontologyImport(O,IRI))),
forall(use_owl(O,'owl:versionInfo',IRI2,ontology_version_info), assert_axiom(ontologyVersionInfo(O,IRI2))),!. % Do Once
% See table 5.
% triple_remove(Pattern:list,Remove:list)
% if Pattern is present, remove triples in Remove
triple_remove([owl(X,'rdf:type','owl:Ontology')],[owl(X,'rdf:type','owl:Ontology')]).
triple_remove([owl(X,'rdf:type','owl:Class'),owl(X,'rdf:type','rdfs:Class')],[owl(X,'rdf:type','rdfs:Class')]).
triple_remove([owl(X,'rdf:type','rdfs:Datatype'),owl(X,'rdf:type','rdfs:Class')],[owl(X,'rdf:type','rdfs:Class')]).
triple_remove([owl(X,'rdf:type','owl:DataRange'),owl(X,'rdf:type','rdfs:Class')],[owl(X,'rdf:type','rdfs:Class')]).
triple_remove([owl(X,'rdf:type','owl:Restriction'),owl(X,'rdf:type','rdfs:Class')],[owl(X,'rdf:type','rdfs:Class')]).
triple_remove([owl(X,'rdf:type','owl:Restriction'),owl(X,'rdf:type','owl:Class')],[owl(X,'rdf:type','owl:Class')]).
triple_remove([owl(X,'rdf:type','owl:ObjectProperty'),owl(X,'rdf:type','rdf:Property')],[owl(X,'rdf:type','rdf:Property')]).
triple_remove([owl(X,'rdf:type','owl:FunctionalProperty'),owl(X,'rdf:type','rdf:Property')],[owl(X,'rdf:type','rdf:Property')]).
triple_remove([owl(X,'rdf:type','owl:InverseFunctionalProperty'),owl(X,'rdf:type','rdf:Property')],[owl(X,'rdf:type','rdf:Property')]).
triple_remove([owl(X,'rdf:type','owl:TransitiveProperty'),owl(X,'rdf:type','rdf:Property')],[owl(X,'rdf:type','rdf:Property')]).
triple_remove([owl(X,'rdf:type','owl:DatatypeProperty'),owl(X,'rdf:type','rdf:Property')],[owl(X,'rdf:type','rdf:Property')]).
triple_remove([owl(X,'rdf:type','owl:AnnotationProperty'),owl(X,'rdf:type','rdf:Property')],[owl(X,'rdf:type','rdf:Property')]).
triple_remove([owl(X,'rdf:type','owl:OntologyProperty'),owl(X,'rdf:type','rdf:Property')],[owl(X,'rdf:type','rdf:Property')]).
triple_remove([owl(X,'rdf:type','rdf:List'),owl(X,'rdf:first',_Y),owl(X,'rdf:rest',_Z)],[owl(X,'rdf:type','rdf:List')]).
/*
triple_remove([owl(X,'rdf:type','owl:Thing')],[owl(X,'rdf:type','owl:Thing')]).
*/
% See table 6.
% http://www.w3.org/TR/2008/WD-owl2-mapping-to-rdf-20081202/
triple_replace([owl(X,'rdf:type','owl:OntologyProperty')],[owl(X,'rdf:type','owl:AnnotationProperty')]).
triple_replace([owl(X,'rdf:type','owl:InverseFunctionalProperty')],[owl(X,'rdf:type','owl:ObjectProperty'),owl(X,'rdf:type','owl:InverseFunctionalProperty')]).
triple_replace([owl(X,'rdf:type','owl:TransitiveProperty')],[owl(X,'rdf:type','owl:ObjectProperty'),owl(X,'rdf:type','owl:TransitiveProperty')]).
triple_replace([owl(X,'rdf:type','owl:SymmetricProperty')],[owl(X,'rdf:type','owl:ObjectProperty'),owl(X,'rdf:type','owl:SymmetricProperty')]).
% NOTE: this is not specified in table 6. However, we treat rdfs:Classes as equivalent to owl:Classes
triple_replace([owl(X,'rdf:type','rdfs:Class')],[owl(X,'rdf:type','owl:Class')]).
% DECLARATIONS
%
% See table 7.
% http://www.w3.org/TR/2008/WD-owl2-mapping-to-rdf-20081202/
%% owl_parse_axiom(+AxiomSpec,+AnnMode:boolean,?AnnList:list) is det
%
% None
%
owl_parse_axiom(class(C),AnnMode,List) :-
test_use_owl(C,'rdf:type','owl:Class'),
valid_axiom_annotation_mode(AnnMode,C,'rdf:type','owl:Class',List),
( use_owl(C,'rdf:type','owl:Class',named,class(C)) -> true ; use_owl(C,'rdf:type','rdfs:Class',named,class(C))),
not(class(C)).
owl_parse_axiom(datatype(D), AnnMode, List) :-
test_use_owl(D,'rdf:type','rdf:Datatype'),
valid_axiom_annotation_mode(AnnMode,D,'rdf:type','rdf:Datatype',List),
use_owl(D,'rdf:type','rdf:Datatype',datatype(D)).
owl_parse_axiom(objectProperty(D), AnnMode, List) :-
test_use_owl(D,'rdf:type','owl:ObjectProperty'),
valid_axiom_annotation_mode(AnnMode,D,'rdf:type','owl:ObjectProperty',List),
use_owl(D,'rdf:type','owl:ObjectProperty',objectProperty(D)),
not(objectProperty(D)).
% note the difference in names between syntax and rdf
owl_parse_axiom(dataProperty(D), AnnMode, List) :-
test_use_owl(D,'rdf:type','owl:DatatypeProperty'),
valid_axiom_annotation_mode(AnnMode,D,'rdf:type','rdf:DatatypeProperty',List),
use_owl(D,'rdf:type','owl:DatatypeProperty',dataProperty(D)),
not(dataProperty(D)).
owl_parse_axiom(annotationProperty(D), AnnMode, List) :-
test_use_owl(D,'rdf:type','owl:AnnotationProperty'),
valid_axiom_annotation_mode(AnnMode,D,'rdf:type','rdf:AnnotationProperty',List),
use_owl(D,'rdf:type','owl:AnnotationProperty',annotationProperty(D)),
not(annotationProperty(D)).
% TODO: check this. do we need to assert individual axioms if all we have is an rdf:type?
owl_parse_axiom(namedIndividual(D), AnnMode, List) :-
test_use_owl(D,'rdf:type','owl:NamedIndividual'),
valid_axiom_annotation_mode(AnnMode,D,'rdf:type','rdf:NamedIndividual',List),
use_owl(D,'rdf:type','owl:NamedIndividual',namedIndividual(D)).
% Table 8. Identifying Anonymous Individuals in Reification
% TODO
% 3.2 Populating an Ontology
% 3.2.1 Analyzing Declarations
% 3.2.2 Parsing of Annotations
%
% ann(?X, -Extension List)
%
% Implements function ANN(x) 3.2.2 Table 10
%
% The annotations in G are parsed next. The function ANN assigns a
% set of annotations ANN(x) to each IRI or blank node x. This
% function is initialized by setting ANN(x) = ∅ for each each IRI
% or blank node x. Next, the triple patterns from Table 10 are
% matched in G and, for each matched pattern, ANN(x) is extended
% with an annotation from the right column. Each time one of these
% triple patterns is matched, the matched triples are removed from
% G. This process is repeated until no further matches are
% possible
ann(X,Y) :-
ann(X,X,Y).
ann(X,X1, annotation(X1,Y,Z)) :-
annotationProperty(Y),
debug(owl_parser_detail,'annotation property: ~w',[Y]),
owl(X,Y,Z,not_used),
use_owl(X,Y,Z,annotationProperty(Y)),
u_assert(aNN(X1,Y,Z)),
ann2(X,Y,Z,X1).
ann2(X,Y,Z,X1) :-
annotation_r_node(X,Y,Z,W),
ann(W,annotation(X1,Y,Z),Term),
u_assert(Term).
ann2(X,Y,Z,X1) :-
axiom_r_node(X,Y,Z,W),
ann(W,annotation(X1,Y,Z),Term),
u_assert(Term).
ann2(_,_,_,_).
% 3.2.4 Parsing of Expressions
is_bnode(C) :-
atom(C),
sub_atom(C,0,_,_,'_:').
% Table 11. Parsing Object Property Expressions
owl_property_expression(C,C) :-
not(is_bnode(C)), % better: IRI(C).
% VV added 10/3/2011
not(C='http://www.w3.org/1999/02/22-rdf-syntax-ns#first'),
not(C='http://www.w3.org/1999/02/22-rdf-syntax-ns#rest'),
!.
owl_property_expression(C,D) :-
blanknode(C,D,Use),
( Use = used,
retractall(blanknode(C,D,used)),
assert(blanknode(C,D,shared))
;
true).
owl_property_expression(P,inverseOf(Q)) :-
use_owl(P,'owl:inverseOf',Q,inverseof(P,Q)),
owl_get_bnode(P,inverseOf(Q)).
% Table 12. Parsing of Data Ranges
owl_datarange(D,D) :-
not(is_bnode(D)),!. % better: IRI(C).
owl_datarange(C,D) :-
blanknode(C,D,Use),
( Use = used,
retractall(blanknode(C,D,used)),
assert(blanknode(C,D,shared))
;
true).
owl_datarange(D,intersectionOf(L)) :-
use_owl(D,'rdf:type','rdfs:Datatype',datarange(D)),
use_owl(D,'owl:intersectionOf',Y,datarange(D)),
%print(D-inter-Y),nl,
owl_datarange_list(Y,L),
owl_get_bnode(D,intersectionOf(L)).
owl_datarange(D,unionOf(L)) :-
use_owl(D,'rdf:type','rdfs:Datatype',datarange(D)),
use_owl(D,'owl:unionOf',Y,datarange(D)),
owl_datarange_list(Y,L),
owl_get_bnode(D,unionOf(L)).
owl_datarange(D,complementOf(DY)) :-
use_owl(D,'rdf:type','rdfs:Datatype',dataRange(D)),
use_owl(D,'owl:datatypeComplementOf',Y,datacomplement(D)),
owl_datarange(Y,DY),
owl_get_bnode(D,complementOf(DY)).
% Table 14, case 2
owl_datarange(D,complementOf('rdfs:Literal')) :-
use_owl(D,'rdf:type','rdfs:DataRange',dataRange(D)),
use_owl(D,'owl:oneOf',[],oneOf(D)),
owl_get_bnode(D,complementOf('rdfs:Literal')).
owl_datarange(D,oneOf(L)) :-
use_owl(D,'rdf:type','rdfs:Datatype',dataType(D)),
use_owl(D,'owl:oneOf',L1,oneOf(D)),
owl_individual_list(L1,L),
owl_get_bnode(D,oneOf(L)).
% Table 14, case 1
owl_datarange(D,oneOf(L)) :-
use_owl(D,'rdf:type','rdfs:DataRange',datarange(D)),
use_owl(D,'owl:oneOf',L1,datarange(D)),
owl_individual_list(L1,L),
owl_get_bnode(D,oneOf(L)).
owl_datarange(D,datatypeRestriction(DY,L)) :-
use_owl(D,'rdf:type','rdfs:Datatype',datarange(D)),
use_owl(D,'owl:onDatatype',Y,datarange(D)),
owl_datarange(Y,DY),
use_owl(D,'owl:withRestrictions',L1,datarange(D)),
owl_datatype_restriction_list(L1,L),
owl_get_bnode(D,datatypeRestriction(DY,L)).
% Table 13. Parsing of Class Expressions
% ----------------------------------------------------------------------
% owl_description(+Node,-Description).
%
% It implements OWL AS production rules for Descriptions.
% During the construction of the Description any blank node
% is recorded for later structure sharing checks.
owl_description(C,C) :-
not(is_bnode(C)),!. % better: IRI(C).
owl_description(C,D) :-
blanknode(C,D,Use),
( Use = used,
retractall(blanknode(C,D,used)),
assert(blanknode(C,D,shared))
;
true),!.
% TODO: this leaves behind classAssertions of type owlClass for the bnodes
owl_description(D,intersectionOf(L)) :-
use_owl(D,'owl:intersectionOf',L1,intersectionOf(D)),
owl_description_list(L1,L),
\+L = [],
owl_get_bnode(D,intersectionOf(L)),!.
owl_description(D,unionOf(L)) :-
use_owl(D,'owl:unionOf',L1,union(D)),
owl_description_list(L1,L),
owl_get_bnode(D,unionOf(L)),!.
owl_description(D,complementOf(Descr)) :-
use_owl(D,'owl:complementOf',D1,complementOf(D)),
owl_description(D1,Descr),
owl_get_bnode(D,complementOf(Descr)),!.
owl_description(D,oneOf(L)) :-
use_owl(D,'owl:oneOf',L1,oneOf(D)),
( use_owl(D,'rdf:type','owl:Class',oneOf(D,L)) ; true),
owl_individual_list(L1,L),
owl_get_bnode(D,oneOf(L)),!.
owl_description(D,datatypeRestriction(DY,L)) :-
use_owl(D,'rdf:type','rdfs:Datatype',datatypeRestr(D)),
use_owl(D,'owl:onDatatype',Y,dataType(D)),
owl_datarange(Y,DY),
use_owl(D,'owl:withRestrictions',L1,withRestrictions(D)),
owl_datatype_restriction_list(L1,L),
owl_get_bnode(D,datatypeRestriction(DY,L)).
owl_description(D,Restriction) :-
owl_restriction(D, Restriction),
owl_get_bnode(D,Restriction),!.
% Table 15 - OWL DL compatibility class expressions
%
owl_description(D,Result) :-
not(is_bnode(D)), % better: IRI(C).
use_owl(D,'rdf:type','owl:Class',description(D)),
use_owl(D,'owl:unionOf',L,unionOf(L)),
owl_description_list(L,DL),
( DL = [], Result = 'owl:Nothing' ;
DL = [D1], Result = D1),
owl_get_bnode(D,Result),!.
owl_description(D,Result) :-
not(is_bnode(D)), % better: IRI(C).
use_owl(D,'rdf:type','owl:Class',dl_compatibility_descr(D)),
use_owl(D,'owl:intersectionOf',L,intersectionOf(D)),
owl_description_list(L,DL),
( DL = [], Result = 'owl:Thing' ;
DL = [D1], Result = D1),
owl_get_bnode(D,Result),!.
owl_description(D,Result) :-
not(is_bnode(D)),!, % better: IRI(C).
use_owl(D,'rdf:type','owl:Class',dl_compatibility_descr(D)),
use_owl(D,'owl:oneOf',[],oneOf(D)),
Result = 'owl:Nothing',
owl_get_bnode(D,Result).
% support older deprecated versions of OWL2 spec. See for example hydrology.owl
onClass(E,D) :- use_owl(E,'http://www.w3.org/2006/12/owl2#onClass',D,onClass(E)).
onClass(E,D) :- use_owl(E,'owl:onClass',D,onClass(E)).
onDataRange(E,D) :- use_owl(E, 'owl:onDataRange',D,onDatarange(E)).
% owl_restriction(+Element,-Restriction).
%
% If Element is defined as a owl:Restriction on property P then
% Restriction binds to a restriction(Property,Type) term,
% according to OWL Abstract syntax specification.
owl_restriction(Element,Restriction) :-
use_owl(Element,'rdf:type','owl:Restriction',restriction(Element)),
( use_owl(Element, 'owl:onProperty',PropertyID,onProperty(Element,PropertyID)) ;
use_owl(Element, 'owl:onProperties',PropertyID,onProperties(Element,PropertyID))
),
owl_restriction_type(Element,PropertyID, Restriction),
debug(owl_parser_detail,'Restriction: ~w',[Restriction]).
owl_restriction_type(E, P, someValuesFrom(PX, DX)) :-
use_owl(E, 'owl:someValuesFrom',D,someValuesFrom(E,P)),
( owl_description(D, DX) ; owl_datarange(D,DX)),
( P = [_|_], owl_property_list(P,PX) ; owl_property_expression(P, PX)).
owl_restriction_type(E, P, allValuesFrom(PX,DX)) :-
use_owl(E, 'owl:allValuesFrom',D,allValuesFrom(E,P)),
( owl_description(D, DX) ; owl_datarange(D,DX)),
( P = [_|_], owl_property_list(P,PX) ; owl_property_expression(P, PX)).
% changed from thea value-->hasValue
owl_restriction_type(E, P, hasValue(PX,Value)) :-
use_owl(E, 'owl:hasValue',Value,hasValue(E)),
owl_property_expression(P, PX).
% VV:check if RDF parser returns a triple with O=true for
% "true"^^xsd:boolean
owl_restriction_type(E, P, hasSelf(PX)) :-
use_owl(E, 'owl:hasSelf', true,hasSelf(E)),
owl_property_expression(P, PX).
% Support of deprecated translations:
% in the OWL2 RDF mapping, unqualified CRs use owl:{min,max}Cardinality
% and QCQs use owl:{min,ax}QualifiedCardinality
%
% however, there appear to be some ontologies; e.g. Hydrology.owl.
% that use an older mapping, where the same properties are used
% for QCR and unqCR
%
% it is relatively easy to support this legacy ontologies; however
% we must process these BEFORE unqualified cardinality restrictions.
owl_restriction_type(E, P, exactCardinality(N,PX,DX)) :-
test_use_owl(E, 'owl:cardinality',Lit),
onClass(E,D),
owl_description(D, DX),!,
use_owl(E, 'owl:cardinality',Lit,cardinality(E)),
literal_integer(Lit,N),
owl_property_expression(P, PX).
owl_restriction_type(E, P, minCardinality(N,PX,DX)) :-
test_use_owl(E, 'owl:minCardinality',Lit),
( onClass(E,D),owl_description(D, DX)
; onDataRange(E,D), owl_datarange(D,DX)),
!,
% we are sure this is an old-style unqualified CR - now consume triples
use_owl(E, 'owl:minCardinality',Lit,minCardinality(E)),
literal_integer(Lit,N),
owl_property_expression(P, PX).
owl_restriction_type(E, P, maxCardinality(N,PX,DX)) :-
test_use_owl(E, 'owl:maxCardinality',Lit),
( onClass(E,D),owl_description(D, DX)
; onDataRange(E,D), owl_datarange(D,DX)),
!,
% we are sure this is an old-style unqualified CR - now consume triples
use_owl(E, 'owl:maxCardinality',Lit,maxCard(E)),
literal_integer(Lit,N),
owl_property_expression(P, PX).
% END OF Support of deprecated translations:
% the following are all in the spec:
% changed from Thea1->2: cardinality->exactCardinality
owl_restriction_type(E, P,exactCardinality(N,PX)) :-
use_owl(E, 'owl:cardinality',Lit,cardinality(E)),
literal_integer(Lit,N),
owl_property_expression(P, PX).
owl_restriction_type(E, P,exactCardinality(N,PX,DX)) :-
use_owl(E, 'owl:qualifiedCardinality',Lit),literal_integer(Lit,N),
( onClass(E,D),owl_description(D, DX) ;
onDataRange(E,D), owl_datarange(D,DX)
),
owl_property_expression(P, PX).
owl_restriction_type(E, P, minCardinality(N,PX)) :-
use_owl(E, 'owl:minCardinality',Lit,cardinality(E)),literal_integer(Lit,N),
owl_property_expression(P, PX).
owl_restriction_type(E, P, minCardinality(N,PX,DX)) :-
use_owl(E, 'owl:minQualifiedCardinality',Lit,cardinality(E)),literal_integer(Lit,N),
( onClass(E,D),owl_description(D, DX);
onDataRange(E,D), owl_datarange(D,DX)
),
owl_property_expression(P, PX).
owl_restriction_type(E, P, maxCardinality(N,PX)) :-
use_owl(E, 'owl:maxCardinality',Lit,maxCardinality(E)),literal_integer(Lit,N),
owl_property_expression(P, PX).
owl_restriction_type(E, P, maxCardinality(N,PX,DX)) :-
use_owl(E, 'owl:maxQualifiedCardinality',Lit,cardinality(E,Lit)),
literal_integer(Lit,N),
( onClass(E,D),owl_description(D, DX);
onDataRange(E,D), owl_datarange(D,DX)),
owl_property_expression(P, PX).
% Table 14. Parsing of Data Ranges for Compatibility with OWL DL
% Included into owl_datarange clauses above
% Table 15. Parsing of Class Expressions for Compatibility with OWL DL
% Included into owl_dexcription clauses above
% Table 16. Parsing of Axioms without Annotations
% Declarations handled previously
% CLASS AXIOMS
% valid_axiom_annotation_mode: add clauses for the disjoint etc ....
collect_r_nodes :-
retractall(axiom_r_node(_,_,_,_)),
forall(( test_use_owl(Node,'rdf:type','owl:Axiom'),
test_use_owl(Node,'owl:annotatedSource',S),
test_use_owl(Node,'owl:annotatedProperty',P),
test_use_owl(Node,'owl:annotatedTarget',O)),
(assert(axiom_r_node(S,P,O,Node)),
debug(owl_parser_detail,'~w',[axiom_r_node(S,P,O,Node)]),
use_owl([owl(Node,'rdf:type','owl:Axiom'),
owl(Node,'owl:annotatedSource',S),
owl(Node,'owl:annotatedProperty',P),
owl(Node,'owl:annotatedTarget',O)]))),
retractall(annotation_r_node(_,_,_,_)),
forall(( test_use_owl(W,'rdf:type','owl:Annotation'),
test_use_owl(W,'owl:annotatedSource',S),
test_use_owl(W,'owl:annotatedProperty',P),
test_use_owl(W,'owl:annotatedTarget',O)),
(assert(annotation_r_node(S,P,O,Node)),
debug(owl_parser_detail,'~w',[annotation_r_node(S,P,O,Node)]),
use_owl([owl(W,'rdf:type','owl:Annotation'),
owl(W,'owl:annotatedSource',S),
owl(W,'owl:annotatedProperty',P),
owl(W,'owl:annotatedTarget',O)]))).
%% valid_axiom_annotation_mode(+AnnMode,+S,+P,+O,?AnnotationNodes:list) is det
% if AnnMode is true and annotation triples can be found then
% unify AnnotationNodes with the Nodes that annotate the triple,
% otherwise []
valid_axiom_annotation_mode(_Mode,S,P,O,List) :-
findall(Node,axiom_r_node(S,P,O,Node),List).
owl_parse_axiom(subClassOf(DX,DY),AnnMode,List) :-
test_use_owl(X,'rdfs:subClassOf',Y),
valid_axiom_annotation_mode(AnnMode,X,'rdfs:subClassOf',Y,List),
use_owl(X,'rdfs:subClassOf',Y,subclassOf(X,Y)),
owl_description(X,DX),
owl_description(Y,DY).
% Process each equivalentClass pair separately in order to capture
% annotations. Block the maximally connected subgraph.
% TODO. Process the equivalent(L) axioms to generate maximally connected
% equivalentClasses(L) axioms. (but without annotations?)
owl_parse_axiom(equivalentClasses(DL),AnnMode,List) :-
test_use_owl(X,'owl:equivalentClass',Y),
valid_axiom_annotation_mode(AnnMode,X,'owl:equivalentClass',Y,List),
use_owl(X,'owl:equivalentClass',Y,equivalentClass(X,Y)),
% maximally_connected_subgraph_over('owl:equivalentClass',L),
maplist(owl_description,[X,Y],DL),
debug(owl_parser_detail,'equivalentClasses Descs: ~w',[DL]).
% SUPPORT FOR IMPLICT equivalentClass axioms:
% Some OWL RDF encodings look like this:
% the preferred form is to use an equivalentClass axiom, but
% we should support this style too
% <owl:Class rdf:ID="WhiteWine">
% <owl:intersectionOf rdf:parseType="Collection">
% <owl:Class rdf:about="#Wine" />% Table 17. Parsing of Annotated Axioms
% <owl:Restriction>
% <owl:onProperty rdf:resource="#hasColor" />
% <owl:hasValue rdf:resource="#White" />
% </owl:Restriction>
% </owl:intersectionOf>
% </owl:Class>
owl_parse_axiom(equivalentClasses([C,intersectionOf(D)]),AnnMode,List) :-
class(C),
test_use_owl(C,'owl:intersectionOf',D1),
debug(owl_parser,'equivalent collection; intersection for ~w',[C]),
valid_axiom_annotation_mode(AnnMode,C,'owl:intersectionOf',D1,List),
owl_description(C,intersectionOf(D)).
owl_parse_axiom(equivalentClasses([C,unionOf(D)]),AnnMode,List) :-
class(C),
test_use_owl(C,'owl:unionOf',D1),
debug(owl_parser,'equivalent collection; union for ~w',[C]),
valid_axiom_annotation_mode(AnnMode,C,'owl:unionOf',D1,List),
owl_description(C,unionOf(D)).
owl_parse_axiom(equivalentClasses([C,oneOf(D)]),AnnMode,List) :-