-
Notifications
You must be signed in to change notification settings - Fork 2
/
ESOntologyHandler.java
252 lines (205 loc) · 10.8 KB
/
ESOntologyHandler.java
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
package javareasoner.ontology;
import java.io.FileNotFoundException;
import java.util.HashSet;
import org.semanticweb.owlapi.model.OWLClass;
import org.semanticweb.owlapi.model.OWLDataProperty;
import org.semanticweb.owlapi.model.OWLDataPropertyDomainAxiom;
import org.semanticweb.owlapi.model.OWLDataPropertyRangeAxiom;
import org.semanticweb.owlapi.model.OWLDatatype;
import org.semanticweb.owlapi.model.OWLDeclarationAxiom;
import org.semanticweb.owlapi.model.OWLDisjointClassesAxiom;
import org.semanticweb.owlapi.model.OWLDisjointUnionAxiom;
import org.semanticweb.owlapi.model.OWLEquivalentClassesAxiom;
import org.semanticweb.owlapi.model.OWLFunctionalDataPropertyAxiom;
import org.semanticweb.owlapi.model.OWLNamedIndividual;
import org.semanticweb.owlapi.model.OWLObjectProperty;
import org.semanticweb.owlapi.model.OWLObjectPropertyDomainAxiom;
import org.semanticweb.owlapi.model.OWLObjectPropertyRangeAxiom;
import org.semanticweb.owlapi.model.OWLOntologyCreationException;
import org.semanticweb.owlapi.model.OWLOntologyStorageException;
import org.semanticweb.owlapi.model.OWLSubClassOfAxiom;
import org.semanticweb.owlapi.model.OWLSubObjectPropertyOfAxiom;
import org.semanticweb.owlapi.reasoner.NodeSet;
import javareasoner.server.ReasoningServer;
public class ESOntologyHandler extends OntologyHandler implements ReasoningServer {
public ESOntologyHandler() {
super();
}
/**
* Override the method specifying the EShop ontology's axioms through
* the OWLAPI.
*/
@Override
public void initOntology() throws OWLOntologyStorageException, FileNotFoundException, OWLOntologyCreationException {
appOntology = manager.createOntology(IOR);
df = appOntology.getOWLOntologyManager().getOWLDataFactory();
//CLASSES
//Note: Declare Axiom can be omitted because triggered by subclass axioms
OWLClass user = declareClass("User");
OWLClass guest = declareClass("Guest");
OWLClass customer = declareClass("Customer");
OWLClass simpleCustomer = declareClass("SimpleCustomer");
OWLClass vipCustomer = declareClass("VIPCustomer");
OWLClass product = declareClass("Product");
OWLClass productA = declareClass("ProductA");
OWLClass productB = declareClass("ProductB");
OWLClass productC = declareClass("ProductC");
OWLSubClassOfAxiom g_sub_u = df.getOWLSubClassOfAxiom(guest, user);
appOntology.add(g_sub_u);
OWLSubClassOfAxiom c_sub_u = df.getOWLSubClassOfAxiom(customer, user);
appOntology.add(c_sub_u);
OWLSubClassOfAxiom sc_sub_c = df.getOWLSubClassOfAxiom(simpleCustomer, customer);
appOntology.add(sc_sub_c);
OWLSubClassOfAxiom vc_sub_c = df.getOWLSubClassOfAxiom(vipCustomer, customer);
appOntology.add(vc_sub_c);
OWLSubClassOfAxiom pA_sub_P = df.getOWLSubClassOfAxiom(productA, product);
appOntology.add(pA_sub_P);
OWLSubClassOfAxiom pB_sub_P = df.getOWLSubClassOfAxiom(productB, product);
appOntology.add(pB_sub_P);
OWLSubClassOfAxiom pC_sub_P = df.getOWLSubClassOfAxiom(productC, product);
appOntology.add(pC_sub_P);
//OBJECT PROPERTIES
OWLObjectProperty interestedIn = df.getOWLObjectProperty(IOR + "#interestedIn");
OWLObjectPropertyDomainAxiom i_domain = df.getOWLObjectPropertyDomainAxiom(interestedIn, user);
appOntology.add(i_domain);
OWLObjectPropertyRangeAxiom i_range = df.getOWLObjectPropertyRangeAxiom(interestedIn, product);
appOntology.add(i_range);
OWLObjectProperty productOnOffer = df.getOWLObjectProperty(IOR + "#productOnOffer");
OWLObjectPropertyDomainAxiom p_domain = df.getOWLObjectPropertyDomainAxiom(productOnOffer, customer);
appOntology.add(p_domain);
OWLObjectPropertyRangeAxiom p_range = df.getOWLObjectPropertyRangeAxiom(productOnOffer, product);
appOntology.add(p_range);
OWLObjectProperty perc10Offer = df.getOWLObjectProperty(IOR + "#perc10Offer");
OWLObjectProperty perc20Offer = df.getOWLObjectProperty(IOR + "#perc20Offer");
OWLObjectPropertyDomainAxiom p20_domain = df.getOWLObjectPropertyDomainAxiom(perc20Offer, vipCustomer);
appOntology.add(p20_domain);
OWLSubObjectPropertyOfAxiom p10_sub_p = df.getOWLSubObjectPropertyOfAxiom(perc10Offer, productOnOffer);
appOntology.add(p10_sub_p);
OWLSubObjectPropertyOfAxiom p20_sub_p = df.getOWLSubObjectPropertyOfAxiom(perc20Offer, productOnOffer);
appOntology.add(p20_sub_p);
//DATA PROPERTIES
OWLDatatype stringDatatype = df.getStringOWLDatatype();
OWLDataProperty username = df.getOWLDataProperty(IOR + "#username");
OWLDataPropertyDomainAxiom username_domain = df.getOWLDataPropertyDomainAxiom(username, user);
appOntology.add(username_domain);
OWLDataPropertyRangeAxiom username_range = df.getOWLDataPropertyRangeAxiom(username, stringDatatype);
appOntology.add(username_range);
OWLFunctionalDataPropertyAxiom fun_username = df.getOWLFunctionalDataPropertyAxiom(username);
appOntology.add(fun_username);
OWLDataProperty id = df.getOWLDataProperty(IOR + "#id");
OWLDataPropertyDomainAxiom id_domain = df.getOWLDataPropertyDomainAxiom(id, product);
appOntology.add(id_domain);
OWLDataPropertyRangeAxiom id_range = df.getOWLDataPropertyRangeAxiom(id, stringDatatype);
appOntology.add(id_range);
OWLFunctionalDataPropertyAxiom fun_id = df.getOWLFunctionalDataPropertyAxiom(id);
appOntology.add(fun_id);
OWLDatatype floatDatatype = df.getFloatOWLDatatype();
OWLDataProperty price = df.getOWLDataProperty(IOR + "#price");
OWLDataPropertyDomainAxiom price_domain = df.getOWLDataPropertyDomainAxiom(price, product);
appOntology.add(price_domain);
OWLDataPropertyRangeAxiom price_range = df.getOWLDataPropertyRangeAxiom(price, floatDatatype);
appOntology.add(price_range);
OWLFunctionalDataPropertyAxiom price_id = df.getOWLFunctionalDataPropertyAxiom(price);
appOntology.add(price_id);
HashSet<OWLClass> disclasses = new HashSet<>();
disclasses.add(user);
disclasses.add(product);
OWLDisjointClassesAxiom discla = df.getOWLDisjointClassesAxiom(disclasses);
appOntology.add(discla);
HashSet<OWLClass> disclassesProducts = new HashSet<>();
disclassesProducts.add(productA);
disclassesProducts.add(productB);
disclassesProducts.add(productC);
OWLDisjointClassesAxiom disclaProducts = df.getOWLDisjointClassesAxiom(disclassesProducts);
appOntology.add(disclaProducts);
HashSet<OWLClass> disuni = new HashSet<>();
disuni.add(guest);
disuni.add(customer);
OWLDisjointUnionAxiom du_u_gc = df.getOWLDisjointUnionAxiom(user, disuni);
appOntology.add(du_u_gc);
HashSet<OWLClass> disclassesCustomers = new HashSet<>();
disclassesCustomers.add(simpleCustomer);
disclassesCustomers.add(vipCustomer);
OWLDisjointClassesAxiom disclaCustomers = df.getOWLDisjointClassesAxiom(disclassesCustomers);
appOntology.add(disclaCustomers);
OWLClass popularProduct = declareClass("PopularProduct");
OWLSubClassOfAxiom pp_sub_p = df.getOWLSubClassOfAxiom(popularProduct, product);
appOntology.add(pp_sub_p);
OWLClass popularProductSC = declareClass("PopularProductSC");
OWLSubClassOfAxiom ppsc_sub_p = df.getOWLSubClassOfAxiom(popularProductSC, popularProduct);
appOntology.add(ppsc_sub_p);
OWLClass popularProductVC = declareClass("PopularProductVC");
OWLSubClassOfAxiom ppvc_sub_p = df.getOWLSubClassOfAxiom(popularProductVC, popularProduct);
appOntology.add(ppvc_sub_p);
OWLEquivalentClassesAxiom e_pp = df.getOWLEquivalentClassesAxiom(popularProduct, df.getOWLObjectMinCardinality(2,
df.getOWLObjectInverseOf(df.getOWLObjectProperty(IOR + "#interestedIn")),
df.getOWLClass(IOR + "#User")));
appOntology.add(e_pp);
OWLEquivalentClassesAxiom e_ppSC = df.getOWLEquivalentClassesAxiom(popularProductSC, df.getOWLObjectMinCardinality(2,
df.getOWLObjectInverseOf(df.getOWLObjectProperty(IOR + "#interestedIn")),
df.getOWLClass(IOR + "#SimpleCustomer")));
appOntology.add(e_ppSC);
OWLEquivalentClassesAxiom e_ppVC = df.getOWLEquivalentClassesAxiom(popularProductVC, df.getOWLObjectMinCardinality(2,
df.getOWLObjectInverseOf(df.getOWLObjectProperty(IOR + "#interestedIn")),
df.getOWLClass(IOR + "#VIPCustomer")));
appOntology.add(e_ppVC);
saveOntology();
getReasoner();
}
/**
* Utility method to declare a class with the IRI fragment given as string.
* @param id String representing IRI fragment
* @return The OWLClass declared
*/
private OWLClass declareClass(String id) {
OWLClass owlClass = df.getOWLClass(IOR + "#" + id);
OWLDeclarationAxiom da = df.getOWLDeclarationAxiom(owlClass);
appOntology.add(da);
return owlClass;
}
/**
* Return all individuals targeted by more than {@code card} relations of type {@code objectPropertyId}.
* Reasoning enabled.<br>
* Manchester syntax DL query: inverse {@code objectPropertyId} min {@code <card>} {@code classId}
* @param objectPropertyId IRI fragment of the property
* @param int min cardinality value
* @param classId If null OWLThing is considered
* @return individuals of the class defined by the query
*/
public NodeSet<OWLNamedIndividual> getInstancesGreaterCardInvObjectProperty(String objectPropertyId, int card, String classId){
r.flush();
if (classId != null) {
return r.getInstances(df.getOWLObjectMinCardinality(card,
df.getOWLObjectInverseOf(df.getOWLObjectProperty(IOR + "#" + objectPropertyId)),
df.getOWLClass(IOR + "#" + classId)));
} else {
return r.getInstances(df.getOWLObjectMinCardinality(card,
df.getOWLObjectInverseOf(df.getOWLObjectProperty(IOR + "#" + objectPropertyId))));
}
}
@Override
public void reasoningRoutine() {
boolean consistent = isConsistent();
System.out.println("\nIs ontology still consistent? " + consistent);
if (consistent) {
System.out.println("\nReasoning Routine");
System.out.println("\nGuest(*)");
getInstances("Guest").forEach(System.out::println);
System.out.println("\nCustomer(*)");
getInstances("Customer").forEach(System.out::println);
System.out.println("\nproductOnOffer(*)");
getInstances("productOnOffer", null).forEach(System.out::println);
System.out.println("\nproductOnOffer(*) some ProductB");
getInstances("productOnOffer", "ProductB").forEach(System.out::println);
System.out.println("\nPopularProduct (equivalent to: inverse interestedIn min 2)");
getInstances("PopularProduct").forEach(System.out::println);
System.out.println("\nPopularProductSC (equivalent to: inverse interestedIn min 2 SimpleCustomer)");
getInstances("PopularProductSC").forEach(System.out::println);
System.out.println("\nPopularProductVC (equivalent to: inverse interestedIn min 2 VIPCustomer)");
getInstances("PopularProductVC").forEach(System.out::println);
System.out.println("\ninverse interestedIn some Guest");
getInstancesGreaterCardInvObjectProperty("interestedIn", 1, "Guest").forEach(System.out::println);
}
System.out.println("*******************************************************");
}
}