-
Notifications
You must be signed in to change notification settings - Fork 14
/
OWLAPIIdentityReasoner.java
703 lines (554 loc) · 22.7 KB
/
OWLAPIIdentityReasoner.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
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
/**
* Copyright (C) 2016 Czech Technical University in Prague
* <p>
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any
* later version.
* <p>
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details. You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cz.cvut.kbss.jopa.owlapi.identityreasoner;
import org.semanticweb.owlapi.model.*;
import org.semanticweb.owlapi.reasoner.*;
import org.semanticweb.owlapi.reasoner.impl.*;
import org.semanticweb.owlapi.search.EntitySearcher;
import org.semanticweb.owlapi.util.Version;
import javax.annotation.Nonnull;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@Deprecated
class OWLAPIIdentityReasoner implements OWLReasoner {
private final OWLOntology o;
public OWLAPIIdentityReasoner(final OWLOntology o) {
this.o = o;
}
public void dispose() {
// do nothing
}
public void flush() {
// do nothing
}
@Nonnull
public Node<OWLClass> getBottomClassNode() {
return new OWLClassNode(o.getOWLOntologyManager().getOWLDataFactory()
.getOWLNothing());
}
@Nonnull
public Node<OWLDataProperty> getBottomDataPropertyNode() {
return new OWLDataPropertyNode(o.getOWLOntologyManager()
.getOWLDataFactory().getOWLBottomDataProperty());
}
@Nonnull
public Node<OWLObjectPropertyExpression> getBottomObjectPropertyNode() {
return new OWLObjectPropertyNode(o.getOWLOntologyManager()
.getOWLDataFactory().getOWLBottomObjectProperty());
}
@Nonnull
public BufferingMode getBufferingMode() {
return BufferingMode.NON_BUFFERING;
}
@Nonnull
public NodeSet<OWLClass> getDataPropertyDomains(@Nonnull OWLDataProperty pe,
boolean direct) throws InconsistentOntologyException,
ReasonerInterruptedException,
TimeOutException {
final OWLClassNodeSet s = new OWLClassNodeSet();
for (final OWLDataPropertyDomainAxiom a : o
.getAxioms(AxiomType.DATA_PROPERTY_DOMAIN)) {
if (a.getProperty().equals(pe) && !a.getDomain().isAnonymous()) {
s.addEntity(a.getDomain().asOWLClass());
}
}
return s;
}
@Nonnull
public Set<OWLLiteral> getDataPropertyValues(@Nonnull OWLNamedIndividual ind,
@Nonnull OWLDataProperty pe) throws InconsistentOntologyException,
ReasonerInterruptedException,
TimeOutException {
final Set<OWLLiteral> literals = new HashSet<>();
for (final OWLDataPropertyAssertionAxiom a : o
.getAxioms(AxiomType.DATA_PROPERTY_ASSERTION)) {
if (a.getProperty().equals(pe) && a.getSubject().equals(ind)) {
// if (a.getObject().isNamed()) {
// pn.addEntity(a.getObject().asOWLNamedIndividual());
// }
literals.add(a.getObject());
}
}
return literals;
}
@Nonnull
public NodeSet<OWLNamedIndividual> getDifferentIndividuals(
@Nonnull OWLNamedIndividual ind) throws InconsistentOntologyException,
ReasonerInterruptedException, TimeOutException {
final OWLNamedIndividualNodeSet pn = new OWLNamedIndividualNodeSet();
for (final OWLDifferentIndividualsAxiom a : o
.getAxioms(AxiomType.DIFFERENT_INDIVIDUALS)) {
if (a.getIndividuals().contains(ind)) {
for (OWLIndividual e : a.getIndividuals()) {
if (e.isNamed()) {
pn.addEntity(e.asOWLNamedIndividual());
}
}
}
}
return pn;
}
//
// public NodeSet<OWLClass> getDisjointClasses(OWLClassExpression ce,
// boolean direct) {
// final OWLClassNodeSet pn = new OWLClassNodeSet();
//
// for (final OWLDisjointClassesAxiom a : o
// .getAxioms(AxiomType.DISJOINT_CLASSES)) {
// if (a.getClassExpressions().contains(ce)) {
// for (OWLClassExpression e : a.getClassExpressions()) {
// if (!e.isAnonymous()) {
// pn.addEntity(e.asOWLClass());
// }
// }
// }
// }
//
// return pn;
// }
//
//
// public NodeSet<OWLDataProperty> getDisjointDataProperties(
// OWLDataPropertyExpression pe, boolean direct)
// throws InconsistentOntologyException, ReasonerInterruptedException,
// TimeOutException {
// final OWLDataPropertyNodeSet pn = new OWLDataPropertyNodeSet();
//
// for (final OWLDisjointDataPropertiesAxiom a : o
// .getAxioms(AxiomType.DISJOINT_DATA_PROPERTIES)) {
// if (a.getProperties().contains(pe)) {
// for (OWLDataPropertyExpression e : a.getProperties()) {
// if (!e.isAnonymous()) {
// pn.addEntity(e.asOWLDataProperty());
// }
// }
// }
// }
//
// return pn;
// }
//
//
// public NodeSet<OWLObjectProperty> getDisjointObjectProperties(
// OWLObjectPropertyExpression pe, boolean direct)
// throws InconsistentOntologyException, ReasonerInterruptedException,
// TimeOutException {
// final OWLObjectPropertyNodeSet pn = new OWLObjectPropertyNodeSet();
//
// for (final OWLDisjointObjectPropertiesAxiom a : o
// .getAxioms(AxiomType.DISJOINT_OBJECT_PROPERTIES)) {
// if (a.getProperties().contains(pe)) {
// for (OWLObjectPropertyExpression e : a.getProperties()) {
// if (!e.isAnonymous()) {
// pn.addEntity(e.asOWLObjectProperty());
// }
// }
// }
// }
//
// return pn;
// }
@Nonnull
public Node<OWLClass> getEquivalentClasses(@Nonnull OWLClassExpression ce)
throws InconsistentOntologyException,
ClassExpressionNotInProfileException, ReasonerInterruptedException,
TimeOutException {
final OWLClassNode pn = new OWLClassNode();
for (final OWLEquivalentClassesAxiom a : o
.getAxioms(AxiomType.EQUIVALENT_CLASSES)) {
if (a.getClassExpressions().contains(ce)) {
for (OWLClassExpression e : a.getClassExpressions()) {
if (!e.isAnonymous()) {
pn.add(e.asOWLClass());
}
}
}
}
return pn;
}
@Nonnull
public Node<OWLDataProperty> getEquivalentDataProperties(@Nonnull OWLDataProperty pe)
throws InconsistentOntologyException, ReasonerInterruptedException,
TimeOutException {
final OWLDataPropertyNode pn = new OWLDataPropertyNode(pe);
for (final OWLDataPropertyExpression p : EntitySearcher.getEquivalentProperties(pe, o)) {
if (!p.isAnonymous()) {
pn.add(p.asOWLDataProperty());
}
}
pn.add(pe);
return pn;
}
@Nonnull
public Node<OWLObjectPropertyExpression> getEquivalentObjectProperties(
@Nonnull OWLObjectPropertyExpression pe)
throws InconsistentOntologyException, ReasonerInterruptedException,
TimeOutException {
final OWLObjectPropertyNode pn = new OWLObjectPropertyNode();
for (final OWLEquivalentObjectPropertiesAxiom a : o
.getAxioms(AxiomType.EQUIVALENT_OBJECT_PROPERTIES)) {
if (a.getProperties().contains(pe)) {
for (OWLObjectPropertyExpression e : a.getProperties()) {
if (!e.isAnonymous()) {
pn.add(e.asOWLObjectProperty());
}
}
}
}
return pn;
}
@Nonnull
public IndividualNodeSetPolicy getIndividualNodeSetPolicy() {
return IndividualNodeSetPolicy.BY_NAME;
}
@Nonnull
public NodeSet<OWLNamedIndividual> getInstances(@Nonnull OWLClassExpression ce,
boolean direct) throws InconsistentOntologyException,
ClassExpressionNotInProfileException,
ReasonerInterruptedException,
TimeOutException {
final OWLNamedIndividualNodeSet s = new OWLNamedIndividualNodeSet();
for (final OWLClassAssertionAxiom a : o
.getAxioms(AxiomType.CLASS_ASSERTION)) {
if (a.getClassExpression().equals(ce)
&& a.getIndividual().isNamed()) {
s.addEntity(a.getIndividual().asOWLNamedIndividual());
}
}
return s;
}
@Nonnull
public Node<OWLObjectPropertyExpression> getInverseObjectProperties(
@Nonnull OWLObjectPropertyExpression pe)
throws InconsistentOntologyException, ReasonerInterruptedException,
TimeOutException {
final OWLObjectPropertyNode s = new OWLObjectPropertyNode();
for (final OWLInverseObjectPropertiesAxiom a : o
.getAxioms(AxiomType.INVERSE_OBJECT_PROPERTIES)) {
if (a.getFirstProperty().equals(pe)) {
if (!a.getSecondProperty().isAnonymous()) {
s.add(a.getSecondProperty().asOWLObjectProperty());
}
} else if (a.getSecondProperty().equals(pe)) {
if (!a.getFirstProperty().isAnonymous()) {
s.add(a.getFirstProperty().asOWLObjectProperty());
}
}
}
return s;
}
@Nonnull
public NodeSet<OWLClass> getObjectPropertyDomains(
@Nonnull OWLObjectPropertyExpression pe, boolean direct)
throws InconsistentOntologyException, ReasonerInterruptedException,
TimeOutException {
final OWLClassNodeSet s = new OWLClassNodeSet();
for (final OWLObjectPropertyDomainAxiom a : o
.getAxioms(AxiomType.OBJECT_PROPERTY_DOMAIN)) {
if (a.getProperty().equals(pe) && !a.getDomain().isAnonymous()) {
s.addEntity(a.getDomain().asOWLClass());
}
}
return s;
}
@Nonnull
public NodeSet<OWLClass> getObjectPropertyRanges(
@Nonnull OWLObjectPropertyExpression pe, boolean direct)
throws InconsistentOntologyException, ReasonerInterruptedException,
TimeOutException {
final OWLClassNodeSet s = new OWLClassNodeSet();
for (final OWLObjectPropertyRangeAxiom a : o
.getAxioms(AxiomType.OBJECT_PROPERTY_RANGE)) {
if (a.getProperty().equals(pe) && !a.getRange().isAnonymous()) {
s.addEntity(a.getRange().asOWLClass());
}
}
return s;
}
@Nonnull
public NodeSet<OWLNamedIndividual> getObjectPropertyValues(@Nonnull OWLNamedIndividual ind, @Nonnull
OWLObjectPropertyExpression pe)
throws InconsistentOntologyException, ReasonerInterruptedException,
TimeOutException {
final OWLNamedIndividualNodeSet pn = new OWLNamedIndividualNodeSet();
for (final OWLIndividual i : EntitySearcher.getObjectPropertyValues(ind, pe, o)) {
if (i.isNamed()) {
pn.addEntity(i.asOWLNamedIndividual());
}
}
//
// o.getOWLOntologyManager().getOWLDataFactory().getOWLNamedIndividual(iri)
//
// for (final OWLObjectPropertyAssertionAxiom a : o
// .getAxioms(AxiomType.OBJECT_PROPERTY_ASSERTION)) {
// if (a.getProperty().equals(pe) && a.getSubject().equals(ind)) {
// if (a.getObject().isNamed()) {
// pn.addEntity(a.getObject().asOWLNamedIndividual());
// }
// }
// }
return pn;
}
@Nonnull
public Set<OWLAxiom> getPendingAxiomAdditions() {
return Collections.emptySet();
}
@Nonnull
public Set<OWLAxiom> getPendingAxiomRemovals() {
return Collections.emptySet();
}
@Nonnull
public List<OWLOntologyChange> getPendingChanges() {
return Collections.emptyList();
}
@Nonnull
public String getReasonerName() {
return "identity reasoner";
}
@Nonnull
public Version getReasonerVersion() {
return new Version(0, 1, 0, 1);
}
@Nonnull
public OWLOntology getRootOntology() {
return o;
}
@Nonnull
public Node<OWLNamedIndividual> getSameIndividuals(@Nonnull OWLNamedIndividual ind)
throws InconsistentOntologyException, ReasonerInterruptedException,
TimeOutException {
final OWLNamedIndividualNode pn = new OWLNamedIndividualNode();
for (final OWLSameIndividualAxiom a : o
.getAxioms(AxiomType.SAME_INDIVIDUAL)) {
if (a.getIndividuals().contains(ind)) {
for (OWLIndividual e : a.getIndividuals()) {
if (e.isNamed()) {
pn.add(e.asOWLNamedIndividual());
}
}
}
}
return pn;
}
@Nonnull
public NodeSet<OWLClass> getSubClasses(@Nonnull OWLClassExpression ce, boolean direct) {
final OWLClassNodeSet pn = new OWLClassNodeSet();
for (final OWLSubClassOfAxiom a : o.getAxioms(AxiomType.SUBCLASS_OF)) {
if (a.getSuperClass().equals(ce)) {
if (!a.getSubClass().isAnonymous()) {
pn.addEntity(a.getSubClass().asOWLClass());
}
}
}
return pn;
}
@Nonnull
public NodeSet<OWLDataProperty> getSubDataProperties(@Nonnull OWLDataProperty pe,
boolean direct) throws InconsistentOntologyException,
ReasonerInterruptedException,
TimeOutException {
final OWLDataPropertyNodeSet pn = new OWLDataPropertyNodeSet();
for (final OWLSubDataPropertyOfAxiom a : o
.getAxioms(AxiomType.SUB_DATA_PROPERTY)) {
if (a.getSuperProperty().equals(pe)) {
if (!a.getSubProperty().isAnonymous()) {
pn.addEntity(a.getSubProperty().asOWLDataProperty());
}
}
}
return pn;
}
@Nonnull
public NodeSet<OWLObjectPropertyExpression> getSubObjectProperties(
@Nonnull OWLObjectPropertyExpression pe, boolean direct)
throws InconsistentOntologyException, ReasonerInterruptedException,
TimeOutException {
final OWLObjectPropertyNodeSet pn = new OWLObjectPropertyNodeSet();
for (final OWLSubObjectPropertyOfAxiom a : o
.getAxioms(AxiomType.SUB_OBJECT_PROPERTY)) {
if (a.getSuperProperty().equals(pe)) {
if (!a.getSubProperty().isAnonymous()) {
pn.addEntity(a.getSubProperty().asOWLObjectProperty());
}
}
}
return pn;
}
@Nonnull
public NodeSet<OWLClass> getSuperClasses(@Nonnull OWLClassExpression ce,
boolean direct) throws InconsistentOntologyException,
ClassExpressionNotInProfileException,
ReasonerInterruptedException,
TimeOutException {
final OWLClassNodeSet pn = new OWLClassNodeSet();
for (final OWLSubClassOfAxiom a : o.getAxioms(AxiomType.SUBCLASS_OF)) {
if (a.getSubClass().equals(ce)) {
if (!a.getSuperClass().isAnonymous()) {
pn.addEntity(a.getSuperClass().asOWLClass());
}
}
}
return pn;
}
@Nonnull
public NodeSet<OWLDataProperty> getSuperDataProperties(@Nonnull OWLDataProperty pe,
boolean direct) throws InconsistentOntologyException,
ReasonerInterruptedException,
TimeOutException {
final OWLDataPropertyNodeSet pn = new OWLDataPropertyNodeSet();
for (final OWLSubDataPropertyOfAxiom a : o
.getAxioms(AxiomType.SUB_DATA_PROPERTY)) {
if (a.getSubProperty().equals(pe)) {
if (!a.getSuperProperty().isAnonymous()) {
pn.addEntity(a.getSuperProperty().asOWLDataProperty());
}
}
}
return pn;
}
@Nonnull
public NodeSet<OWLObjectPropertyExpression> getSuperObjectProperties(
@Nonnull OWLObjectPropertyExpression pe, boolean direct)
throws InconsistentOntologyException, ReasonerInterruptedException,
TimeOutException {
final OWLObjectPropertyNodeSet pn = new OWLObjectPropertyNodeSet();
for (final OWLSubObjectPropertyOfAxiom a : o
.getAxioms(AxiomType.SUB_OBJECT_PROPERTY)) {
if (a.getSubProperty().equals(pe)) {
if (!a.getSuperProperty().isAnonymous()) {
pn.addEntity(a.getSuperProperty().asOWLObjectProperty());
}
}
}
return pn;
}
public long getTimeOut() {
return Long.MAX_VALUE;
}
@Nonnull
public Node<OWLClass> getTopClassNode() {
return new OWLClassNode(o.getOWLOntologyManager().getOWLDataFactory()
.getOWLThing());
}
@Nonnull
public Node<OWLDataProperty> getTopDataPropertyNode() {
return new OWLDataPropertyNode(o.getOWLOntologyManager()
.getOWLDataFactory().getOWLTopDataProperty());
}
@Nonnull
public Node<OWLObjectPropertyExpression> getTopObjectPropertyNode() {
return new OWLObjectPropertyNode(o.getOWLOntologyManager()
.getOWLDataFactory().getOWLTopObjectProperty());
}
@Nonnull
public NodeSet<OWLClass> getTypes(@Nonnull OWLNamedIndividual ind, boolean direct)
throws InconsistentOntologyException, ReasonerInterruptedException,
TimeOutException {
final OWLClassNodeSet s = new OWLClassNodeSet();
// for (final OWLClassAssertionAxiom a : o
// .getAxioms(AxiomType.CLASS_ASSERTION)) {
// if (a.getIndividual().equals(ce)
// && a.getIndividual().isNamed()) {
// s.addEntity(a.getIndividual().asOWLNamedIndividual());
// }
// }
for (final OWLClassExpression ce : EntitySearcher.getTypes(ind, o)) {
if (!ce.isAnonymous()) {
s.addEntity(ce.asOWLClass());
}
}
return s;
}
@Nonnull
public Node<OWLClass> getUnsatisfiableClasses()
throws ReasonerInterruptedException, TimeOutException {
return new OWLClassNode();
}
public void interrupt() {
// do nothing
}
public boolean isConsistent() throws ReasonerInterruptedException,
TimeOutException {
return true;
}
public boolean isEntailed(@Nonnull OWLAxiom axiom)
throws ReasonerInterruptedException,
UnsupportedEntailmentTypeException, TimeOutException,
AxiomNotInProfileException {
return o.containsAxiom(axiom);
}
public boolean isEntailed(@Nonnull Set<? extends OWLAxiom> axioms)
throws ReasonerInterruptedException,
UnsupportedEntailmentTypeException, TimeOutException,
AxiomNotInProfileException {
for (final OWLAxiom a : axioms) {
if (!o.containsAxiom(a)) {
return false;
}
}
return true;
}
public boolean isEntailmentCheckingSupported(@Nonnull AxiomType<?> axiomType) {
return true;
}
public boolean isSatisfiable(@Nonnull OWLClassExpression classExpression)
throws ReasonerInterruptedException, TimeOutException,
ClassExpressionNotInProfileException, InconsistentOntologyException {
return true;
}
@Nonnull
public FreshEntityPolicy getFreshEntityPolicy() {
return FreshEntityPolicy.DISALLOW;
}
@Nonnull
public NodeSet<OWLClass> getDisjointClasses(@Nonnull OWLClassExpression arg0)
throws ReasonerInterruptedException, TimeOutException,
FreshEntitiesException, InconsistentOntologyException {
// TODO Auto-generated method stub
return null;
}
@Nonnull
public NodeSet<OWLDataProperty> getDisjointDataProperties(
@Nonnull OWLDataPropertyExpression arg0)
throws InconsistentOntologyException, FreshEntitiesException,
ReasonerInterruptedException, TimeOutException {
// TODO Auto-generated method stub
return null;
}
@Nonnull
public NodeSet<OWLObjectPropertyExpression> getDisjointObjectProperties(
@Nonnull OWLObjectPropertyExpression arg0)
throws InconsistentOntologyException, FreshEntitiesException,
ReasonerInterruptedException, TimeOutException {
// TODO Auto-generated method stub
return null;
}
@Nonnull
public Set<InferenceType> getPrecomputableInferenceTypes() {
// TODO Auto-generated method stub
return null;
}
public boolean isPrecomputed(@Nonnull InferenceType arg0) {
// TODO Auto-generated method stub
return false;
}
public void precomputeInferences(@Nonnull InferenceType... arg0)
throws ReasonerInterruptedException, TimeOutException,
InconsistentOntologyException {
// TODO Auto-generated method stub
}
}