/
owl_writer.pl
95 lines (71 loc) · 2.29 KB
/
owl_writer.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
/* -*- Mode: Prolog -*- */
:- module(owl_writer,
[
export_owl/3
]).
:- use_module(cl_io).
:- use_module(cl_transform).
:- use_module(library('thea2/owl2_model')).
:- use_module(library('thea2/owl2_metamodel')).
:- use_module(library('thea2/owl2_io')).
:- use_module(library('thea2/owl2_util')).
:- use_module(library('thea2/owl2_from_rdf'),[expand_ns/2]).
:- multifile cl_io:serialize_cltext_hook/4.
cl_io:serialize_cltext_hook(File,owl,Text,Opts) :-
export_owl(File,Text,Opts).
export_owl(File,Text,_Opts) :-
Prefix='http://example.org#',
forall(text_sentence(Text,S),
s2owl(Prefix,S)),
expand_namespaces,
save_axioms(File,owl).
s2owl(Prefix,S) :-
( s2owl(Prefix,S,S2)
-> assert_axiom(S2)
; true).
s2owl(Prefix,S,S2) :-
S=..[P|Args],
cvt_pred(P,P2),
t2owl(Prefix,Args,Args2),
pred_args_term(P2,Args2,S2).
t2owl(_Prefix,[],[]) :- !.
t2owl(Prefix,[H|L],[H2|L2]) :-
!,
t2owl(Prefix,H,H2),
t2owl(Prefix,L,L2).
t2owl(_,literal(S),literal(S)) :- !.
t2owl(Prefix,S,S2) :-
atom(S),
!,
( expand_ns(S,S2), % e.g. rdfs:label
sub_atom(S2,_,_,_,':') % bit of a hack...
-> true
; atom_concat(Prefix,S,S2)).
t2owl(Prefix,S,S2) :-
S=..[P|Args],
cvt_pred(P,P2),
!,
t2owl(Prefix,Args,Args2),
pred_args_term(P2,Args2,S2).
t2owl(Prefix,S,S2) :-
S=..[P|Args],
!,
t2owl(Prefix,Args,Args2),
S2=..[P|Args2].
pred_args_term(P2,Args2,S2) :-
( pred_args(P2,[Type]),
format(user_error,'~w->~w~n',[P2,Type]),
( Type=list(_)
; Type=set(_))
-> S2=..[P2,Args2]
; S2=..[P2|Args2]).
pred_args(P,Args) :- owlpredicate_arguments(P,Args).
pred_args(P,Args) :- owlpredicate_typed(P,PT),owlpredicate_arguments(PT,Args).
% by convention, terms in the owl vocabulary are preceded by
% the prefix 'owl:' (rather than the full URI).
% the CL spec is a little unclear on 'network identifiers'.
% in future we can also translate the full URI.
cvt_pred(P,P2) :-
atom_chars(P,[o,w,l,':',C|Rest]),
downcase_atom(C,C2),
atom_chars(P2,[C2|Rest]).