/
Cookbook.txt
175 lines (122 loc) · 5 KB
/
Cookbook.txt
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
---+ Thea2 Cookbook
Here are some recipes for doing common tasks in Thea2
---++ Manipulating Ontologies
In most of these examples, there are 3 steps:
* load the ontology using load_axioms/2
* manipulate the ontology in a forall goal or a failure-driven-loop, called assert_axiom/1
* save the ontology using save_axioms/2
Another way to manipulate ontologies is via the OPPL language. See:
* See http://www.cs.man.ac.uk/~iannonel/oppl/documentation.html
One advantage of prolog over OPPL is its expressivity
---+++ Asserting all subclasses are mutually disjoint
==
subClassOf(X,gender),
subClassOf(Y,gender),
X\=Y,
assert_axiom(disjointProperties[X,Y]),
fail.
==
---+++ Replacing simple assertions with complex descriptions
See http://www.cs.man.ac.uk/~iannonel/oppl/documentation.html
==
% setup boilerplate
[owl2_io],
[owl2_java_owlapi],
[library('semweb/rdf_db'),
rdf_register_ns(c,'http://www.co-ode.org/roberts/country.owl#'),
load_axioms('testfiles/country.owl'),
% owlapi boilerplate
create_reasoner(Man,pellet,Reasoner),
create_factory(Man,Fac),
build_ontology(Man,Fac,Ont),
reasoner_classify(Reasoner,Man,Ont),
reasoner_objectPropertyAssertion(Reasoner,Fac,C1,'c:adjacentTo',C2),
retract_axiom(propertyAssertion(C1,'c:adjacentTo',C2),
Desc = someValuesFrom('c:hasLandBoundry',intersectionOf('c:LandBoundryFragment',
hasValue('c:boundaryOf',C2))),
assert_axiom(classAssertion(Desc,C2)),
fail.
% then save:
save_axioms('countries2.owl',owl).
==
---++ Working with other LP systems
---+++ Converting prolog to OWL
A certain subset of pure prolog programs can be converted to OWL and
reasoner over using OWL reasoners. This is currently a two-step
process.
* prolog to SWRL
* SWRL to OWL
For the first step, only binary and unary predicates are
converted. For the second step, certain rule patterns can be
translated to OWL axioms.
==
:- use_module(library('thea2/owl2_io')).
:- use_module(library('thea2/swrl')).
demo :-
load_axioms('testfiles/dlptest.pro',pl_swrl_owl,[]),
save_axioms(_,owlpl). % TODO - change to owl once we have rdf writing
==
For example, the following piece of prolog:
==
r(X,Y):-
s(X,Z),t(Y,Z).
==
is first translated to a SWRL rule and then to an OWL subPropertyOf/2 axiom:
==
subPropertyOf(r, propertyChain([s, inverseOf(t)]))
==
Currently this process is incomplete, more patterns can potentially be
matched. In the future there will also be the option of defining hooks
to deal with n-ary predicates.
For more, see
* http://blipkit.wordpress.com/2009/06/19/translating-between-logic-programs-and-owlswrl/
---+++ Converting OWL to Logic Programs
The DLP subset of OWL2-DL can be translated to logic programs using a
transormation defined by Grosof. See owl2_to_prolog_dlp.pl
The resulting programs can be used with Prologs such as Yap and
XSB. There are also hooks for answer set programming and disjunctive
datalog systems such as DLV. The same programs can also be used in
inductive logic programming systems such as ProGol. Possibly also
probabilistic systems such as PRISM, but this has yet to be tested.
The following program will convert an OWL ontology suitable for use in
Yap or XSB:
==
:- use_module(library('thea2/owl2_io')).
:- use_module(library('thea2/owl2_to_prolog_dlp)).
demo :-
load_axioms('testfiles/wine.owl'),
save_axioms('wine.pl',dlp,[no_base(_),write_directives(table)]).
==
(You can also use bin/thea-owl-to-dlp-yap)
For Yap you need explicit tabling directives (for XSB can you
table_all), which is specified in the options list for save_axioms/3
Once you have generated the above DLP you can query the ABox using Yap:
==
yap -g wine
?- 'DessertWine'(Wine),locatedIn(Wine,'USRegion'),hasBody(Wine,'Light').
Wine = 'WhitehallLanePrimavera' ? ;
no
==
Prolog DLPs can not have disjunctions in the head of
rules. Disjunctive datalog systems such as DLV can.
The follow will translate an OWL ontology into DLV syntax:
==
:- use_module(library('thea2/owl2_io')).
:- use_module(library('thea2/owl2_to_prolog_dlp)).
demo :-
load_axioms('testfiles/cell.owlpl'),
save_axioms('cell.dlv',dlp,[disjunctive_datalog(true),head_disjunction_symbol(v),no_base(_),suppress_literals(true)]).
==
(You can also use bin/thea-owl-to-dlv)
DLV uses 'v' as a disjunction symbol rather than ';'
DLV tells us that the above 'cell' ontology has two stable models:
==
dlv cell.dlv
{nuc(n1), chromosome(chr1), cell(c1), cell(c2), n_cell(c1), has_part(c1,chr1), has_part(c1,n1), has_part(n1,chr1), part_of(chr1,c1), part_of(chr1,n1), part_of(n1,c1), n_cell(c2)}
{nuc(n1), chromosome(chr1), cell(c1), cell(c2), n_cell(c1), has_part(c1,chr1), has_part(c1,n1), has_part(n1,chr1), part_of(chr1,c1), part_of(chr1,n1), part_of(n1,c1), e_cell(c2)}
==
This is because of the OWL following axiom:
==
subClassOf(cell,unionOf([e_cell,n_cell])).
==
In the abox we state =|cell(c2)|= but not the specific subtype.