Skip to content

Commit 79db9d1

Browse files
committed
ont-api: [#55] support listing raw (inf) data (Ontology#axioms(Imports.INCLUDED)); some renaming; update README.md
1 parent f0939dc commit 79db9d1

12 files changed

+144
-70
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,14 @@ import org.apache.jena.query.ResultSet;
4141
import org.apache.jena.vocabulary.OWL;
4242
import org.semanticweb.owlapi.model.IRI;
4343
import org.semanticweb.owlapi.model.OWLDataFactory;
44+
import org.semanticweb.owlapi.model.parameters.Imports;
4445

4546
public class Examples {
4647
public static void main(String... args) {
4748
String iri = "https://github.com/owlcs/ont-api";
4849

4950
OWLDataFactory df = OntManagers.getDataFactory();
50-
OntologyManager om = OntManagers.createManager();
51+
OntologyManager om = OntManagers.createDirectManager();
5152

5253
// set ontology specification, see jena-owl2 for more details
5354
om.getOntologyConfigurator().setSpecification(OntSpecification.OWL2_EL_MEM_RULES_INF);
@@ -62,8 +63,8 @@ public class Examples {
6263
// add OWL class declaration
6364
jena.createResource(iri + "#Class2", OWL.Class);
6465

65-
// lists axioms (finds axioms from base graph, inferred by Jena Reasoner are not included)
66-
owlapi.axioms().forEach(System.out::println);
66+
// lists all axioms inferred by Jena Reasoner
67+
owlapi.axioms(Imports.INCLUDED).forEach(System.out::println);
6768

6869
// and print to stdout in turtle format
6970
jena.write(System.out, "ttl");

src/main/java/com/github/owlcs/ontapi/OntBaseModel.java renamed to src/main/java/com/github/owlcs/ontapi/BaseOntologyModel.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,23 +38,23 @@
3838
* <p>
3939
* Created by @ssz on 07.04.2017.
4040
*/
41-
public interface OntBaseModel {
41+
public interface BaseOntologyModel {
4242

4343
/**
4444
* Returns an encapsulated {@link InternalGraphModel} instance -
4545
* the facility to work both with Jena and OWL-API objects simultaneously.
4646
*
4747
* @return {@link InternalGraphModel}
4848
*/
49-
InternalGraphModel getGraphModel();
49+
InternalGraphModel getBaseGraphModel();
5050

5151
/**
5252
* Sets new internals.
5353
* Not for public use: only Java Serialization mechanisms can explicitly call this method.
5454
*
5555
* @param m {@link InternalGraphModel}, not {@code null}
5656
*/
57-
void setGraphModel(InternalGraphModel m);
57+
void setBaseGraphModel(InternalGraphModel m);
5858

5959
/**
6060
* Returns a model config instance, that is a collection of settings and, also,

src/main/java/com/github/owlcs/ontapi/OntBaseModelImpl.java renamed to src/main/java/com/github/owlcs/ontapi/BaseOntologyModelImpl.java

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import javax.annotation.ParametersAreNonnullByDefault;
2121
import org.apache.jena.graph.Graph;
2222
import org.apache.jena.graph.GraphMemFactory;
23+
import org.apache.jena.ontapi.impl.UnionGraphImpl;
2324
import org.apache.jena.ontapi.model.OntModel;
2425
import org.apache.jena.ontapi.utils.Graphs;
2526
import org.apache.jena.riot.RDFDataMgr;
@@ -120,7 +121,7 @@
120121
*/
121122
@SuppressWarnings("WeakerAccess")
122123
@ParametersAreNonnullByDefault
123-
public abstract class OntBaseModelImpl implements OWLOntology, OntBaseModel {
124+
public abstract class BaseOntologyModelImpl implements OWLOntology, BaseOntologyModel {
124125
// binary format to provide serialization:
125126
public static final OntFormat DEFAULT_SERIALIZATION_FORMAT = OntFormat.RDF_THRIFT;
126127
@Serial
@@ -131,19 +132,31 @@ public abstract class OntBaseModelImpl implements OWLOntology, OntBaseModel {
131132

132133
protected int hashCode;
133134

134-
protected OntBaseModelImpl(Graph graph, ModelConfig conf) {
135+
protected BaseOntologyModelImpl(Graph graph, ModelConfig conf) {
135136
this.config = Objects.requireNonNull(conf);
136-
this.base = OntBaseModel.createInternalGraphModel(graph, conf.getSpecification(), conf,
137+
this.base = BaseOntologyModel.createInternalGraphModel(graph, conf.getSpecification(), conf,
137138
conf.getManager().getOWLDataFactory(), conf.getManagerCaches());
138139
}
139140

140141
@Override
141-
public InternalGraphModel getGraphModel() {
142+
public InternalGraphModel getBaseGraphModel() {
142143
return base;
143144
}
144145

146+
/**
147+
* A helper which is used in methods with {@link Imports#INCLUDED} and if model's cache is disabled.
148+
* It wraps {@link OntModel#getGraph()}, which can be {@link org.apache.jena.reasoner.InfGraph}.
149+
*
150+
* @return {@link InternalGraphModel}
151+
*/
152+
public InternalGraphModel getFullGraphModel() {
153+
var graph = new UnionGraphImpl(base.getGraph(), false);
154+
return BaseOntologyModel.createInternalGraphModel(graph, config.getSpecification(), config,
155+
config.getManager().getOWLDataFactory(), config.getManagerCaches());
156+
}
157+
145158
@Override
146-
public void setGraphModel(InternalGraphModel m) {
159+
public void setBaseGraphModel(InternalGraphModel m) {
147160
this.base = Objects.requireNonNull(m);
148161
}
149162

@@ -1172,6 +1185,14 @@ public Stream<OWLDifferentIndividualsAxiom> differentIndividualAxioms(OWLIndivid
11721185
return base.listOWLDifferentIndividualsAxioms(individual);
11731186
}
11741187

1188+
@Override
1189+
public Stream<OWLAxiom> axioms(Imports imports) {
1190+
if (imports == Imports.INCLUDED && !config.useContentCache()) {
1191+
return getFullGraphModel().listOWLAxioms();
1192+
}
1193+
return imports.stream(this).flatMap(OWLOntology::axioms);
1194+
}
1195+
11751196
/**
11761197
* Reads the object while serialization.
11771198
* Note: only the base graph is serialized.
@@ -1188,7 +1209,7 @@ private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundE
11881209
Graph base = GraphMemFactory.createDefaultGraph();
11891210
RDFDataMgr.read(base, in, DEFAULT_SERIALIZATION_FORMAT.getLang());
11901211
// set temporary model with default personality, it will be reset inside manager while its #readObject
1191-
setGraphModel(OntBaseModel.createInternalGraphModel(base));
1212+
setBaseGraphModel(BaseOntologyModel.createInternalGraphModel(base));
11921213
}
11931214

11941215
/**
@@ -1243,7 +1264,7 @@ public boolean equals(@Nullable Object obj) {
12431264
return false;
12441265
}
12451266
OntModel right = ((Ontology) obj).asGraphModel();
1246-
OntModel left = getGraphModel();
1267+
OntModel left = getBaseGraphModel();
12471268
return left.id().filter(id -> right.id().filter(id::sameAs).isPresent()).isPresent();
12481269
}
12491270
}

src/main/java/com/github/owlcs/ontapi/OWLAdapter.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,10 +201,10 @@ public OntologyFactory asONT(OWLOntologyFactory factory) {
201201
* Performs mapping {@code OntologyModel -> InternalModelHolder}.
202202
*
203203
* @param ont {@link Ontology}, not {@code null}
204-
* @return {@link OntBaseModel}
204+
* @return {@link BaseOntologyModel}
205205
*/
206-
public OntBaseModel asBaseModel(Ontology ont) {
207-
return (OntBaseModel) ont;
206+
public BaseOntologyModel asBaseModel(Ontology ont) {
207+
return (BaseOntologyModel) ont;
208208
}
209209

210210
/**

src/main/java/com/github/owlcs/ontapi/OWLFactoryWrapper.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -199,18 +199,18 @@ protected OWLOntologyChangeVisitorEx<ChangeApplied> createChangeProcessor() {
199199
@Override
200200
public ChangeApplied visit(SetOntologyID change) {
201201
ChangeApplied res = super.visit(change);
202-
getGraphModel().forceLoad();
202+
getBaseGraphModel().forceLoad();
203203
return res;
204204
}
205205

206206
@Override
207207
public ChangeApplied visit(AddAxiom change) {
208-
return of(getGraphModel().add(change.getAxiom()));
208+
return of(getBaseGraphModel().add(change.getAxiom()));
209209
}
210210

211211
@Override
212212
public ChangeApplied visit(AddOntologyAnnotation change) {
213-
return of(getGraphModel().add(change.getAnnotation()));
213+
return of(getBaseGraphModel().add(change.getAnnotation()));
214214
}
215215

216216
@Override
@@ -219,13 +219,13 @@ public ChangeApplied visit(RemoveAxiom change) {
219219
// I observe this situation only when there are grammatical mistakes in the document,
220220
// so it cannot be loaded by Jena.
221221
LOGGER.warn("Suspicious: {}", change);
222-
return of(getGraphModel().remove(change.getAxiom()));
222+
return of(getBaseGraphModel().remove(change.getAxiom()));
223223
}
224224

225225
@Override
226226
public ChangeApplied visit(RemoveOntologyAnnotation change) {
227227
LOGGER.warn("Suspicious: {}", change);
228-
return of(getGraphModel().remove(change.getAnnotation()));
228+
return of(getBaseGraphModel().remove(change.getAnnotation()));
229229
}
230230

231231
private ChangeApplied of(boolean res) {

src/main/java/com/github/owlcs/ontapi/OntologyManagerImpl.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1800,7 +1800,7 @@ public void write(OWLOntology ontology,
18001800
protected void writeUsingOWLStore(Ontology ont,
18011801
OWLDocumentFormat doc,
18021802
OWLOntologyDocumentTarget target) throws OWLOntologyStorageException {
1803-
getAdapter().asBaseModel(ont).getGraphModel().clearCacheIfNeeded();
1803+
getAdapter().asBaseModel(ont).getBaseGraphModel().clearCacheIfNeeded();
18041804
try {
18051805
for (OWLStorerFactory storer : getOntologyStorers()) {
18061806
OWLStorer writer = storer.createStorer();
@@ -1840,7 +1840,7 @@ public PrefixMapping getPrefixMapping() {
18401840
* @param in {@link ObjectInputStream}
18411841
* @throws IOException exception
18421842
* @throws ClassNotFoundException exception
1843-
* @see OntBaseModelImpl#readObject(ObjectInputStream)
1843+
* @see BaseOntologyModelImpl#readObject(ObjectInputStream)
18441844
*/
18451845
@Serial
18461846
@SuppressWarnings("JavadocReference")
@@ -1849,14 +1849,14 @@ private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundE
18491849
this.iris = createIRICache();
18501850
this.content.values().forEach(info -> {
18511851
ModelConfig conf = info.getModelConfig();
1852-
OntBaseModel bm = getAdapter().asBaseModel(info.get());
1852+
BaseOntologyModel bm = getAdapter().asBaseModel(info.get());
18531853
bm.setConfig(conf);
1854-
UnionGraph union = bm.getGraphModel().getUnionGraph();
1854+
UnionGraph union = bm.getBaseGraphModel().getUnionGraph();
18551855
Stream<UnionGraph> imports = Graphs.getImports(union).stream()
18561856
.map(s -> this.content.values()
18571857
.map(OntInfo::get)
1858-
.map(OntBaseModel.class::cast)
1859-
.map(OntBaseModel::getGraphModel)
1858+
.map(BaseOntologyModel.class::cast)
1859+
.map(BaseOntologyModel::getBaseGraphModel)
18601860
.map(InternalGraphModel::getUnionGraph)
18611861
.filter(g -> Graphs.ontologyNode(g.getBaseGraph())
18621862
.filter(Node::isURI)
@@ -1870,9 +1870,9 @@ private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundE
18701870
.filter(Objects::nonNull);
18711871
imports.forEach(union::addSubGraph);
18721872

1873-
InternalGraphModel internalModel = OntBaseModel.createInternalGraphModel(union, conf.getSpecification(), conf,
1873+
InternalGraphModel internalModel = BaseOntologyModel.createInternalGraphModel(union, conf.getSpecification(), conf,
18741874
getOWLDataFactory(), conf.getManagerCaches());
1875-
bm.setGraphModel(internalModel);
1875+
bm.setBaseGraphModel(internalModel);
18761876
});
18771877
}
18781878

src/main/java/com/github/owlcs/ontapi/OntologyModelImpl.java

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@
5454
* Created by @ssz on 27.09.2016.
5555
*
5656
* @see <a href='https://github.com/owlcs/owlapi/blob/version5/impl/src/main/java/uk/ac/manchester/cs/owl/owlapi/OWLOntologyImpl.java'>uk.ac.manchester.cs.owl.owlapi.OWLOntologyImpl</a>
57-
* @see OntBaseModelImpl
57+
* @see BaseOntologyModelImpl
5858
* @see Ontology
5959
*/
6060
@SuppressWarnings("WeakerAccess")
61-
public class OntologyModelImpl extends OntBaseModelImpl implements Ontology, OWLMutableOntology {
61+
public class OntologyModelImpl extends BaseOntologyModelImpl implements Ontology, OWLMutableOntology {
6262

6363
@Serial
6464
private static final long serialVersionUID = -2882895355499914294L;
@@ -91,7 +91,7 @@ protected OWLOntologyChangeVisitorEx<ChangeApplied> createChangeProcessor() {
9191

9292
@Override
9393
public void clearCache() {
94-
getGraphModel().clearCache();
94+
getBaseGraphModel().clearCache();
9595
}
9696

9797
/**
@@ -101,7 +101,7 @@ public void clearCache() {
101101
*/
102102
@Override
103103
public OntModel asGraphModel() {
104-
return getGraphModel();
104+
return getBaseGraphModel();
105105
}
106106

107107
@Override
@@ -124,7 +124,7 @@ public ChangeApplied visit(@Nonnull AddAxiom change) {
124124
if (containsAxiom(axiom)) {
125125
return ChangeApplied.NO_OPERATION;
126126
}
127-
getGraphModel().add(axiom);
127+
getBaseGraphModel().add(axiom);
128128
return ChangeApplied.SUCCESSFULLY;
129129
}
130130

@@ -133,7 +133,7 @@ public ChangeApplied visit(@Nonnull RemoveAxiom change) {
133133
beforeChange();
134134
OWLAxiom axiom = change.getAxiom();
135135
if (containsAxiom(axiom)) {
136-
getGraphModel().remove(axiom);
136+
getBaseGraphModel().remove(axiom);
137137
return ChangeApplied.SUCCESSFULLY;
138138
}
139139
return ChangeApplied.NO_OPERATION;
@@ -163,10 +163,10 @@ public ChangeApplied visit(@Nonnull RemoveImport change) {
163163
public ChangeApplied visit(@Nonnull AddOntologyAnnotation change) {
164164
beforeChange();
165165
OWLAnnotation annotation = change.getAnnotation();
166-
if (getGraphModel().contains(annotation)) {
166+
if (getBaseGraphModel().contains(annotation)) {
167167
return ChangeApplied.NO_OPERATION;
168168
}
169-
getGraphModel().add(annotation);
169+
getBaseGraphModel().add(annotation);
170170
return ChangeApplied.SUCCESSFULLY;
171171
}
172172

@@ -175,7 +175,7 @@ public ChangeApplied visit(@Nonnull RemoveOntologyAnnotation change) {
175175
beforeChange();
176176
OWLAnnotation annotation = change.getAnnotation();
177177
if (annotations().anyMatch(annotation::equals)) {
178-
getGraphModel().remove(annotation);
178+
getBaseGraphModel().remove(annotation);
179179
return ChangeApplied.SUCCESSFULLY;
180180
}
181181
return ChangeApplied.NO_OPERATION;
@@ -211,7 +211,7 @@ protected void beforeChange() {
211211
if (!getConfig().useContentCache()) {
212212
throw new OntApiException.ModificationDenied("Direct mutations through OWL-API interface are not allowed");
213213
}
214-
getGraphModel().forceLoad();
214+
getBaseGraphModel().forceLoad();
215215
}
216216

217217
/**
@@ -226,10 +226,10 @@ protected void addImport(OWLImportsDeclaration declaration) {
226226
// either ontology IRI or specified declaration IRI.
227227
Ontology ont = getOWLOntologyManager().getImportedOntology(declaration);
228228
if (ont == null) {
229-
getGraphModel().getID().addImport(declaration.getIRI().getIRIString());
229+
getBaseGraphModel().getID().addImport(declaration.getIRI().getIRIString());
230230
return;
231231
}
232-
getGraphModel().addImport(getAdapter().asBaseModel(ont).getGraphModel());
232+
getBaseGraphModel().addImport(getAdapter().asBaseModel(ont).getBaseGraphModel());
233233
}
234234

235235
/**
@@ -244,9 +244,9 @@ protected void removeImport(OWLImportsDeclaration declaration) {
244244
// (could be different in case of renaming)
245245
Ontology ont = getOWLOntologyManager().getImportedOntology(declaration);
246246
if (ont != null) {
247-
getGraphModel().removeImport(getAdapter().asBaseModel(ont).getGraphModel());
247+
getBaseGraphModel().removeImport(getAdapter().asBaseModel(ont).getBaseGraphModel());
248248
}
249-
getGraphModel().getID().removeImport(declaration.getIRI().getIRIString());
249+
getBaseGraphModel().getID().removeImport(declaration.getIRI().getIRIString());
250250
}
251251

252252
@Override
@@ -261,7 +261,7 @@ public OWLAdapter getAdapter() {
261261
* Created @ssz on 22.12.2016.
262262
*/
263263
@SuppressWarnings("WeakerAccess")
264-
public static class Concurrent extends OWLOntologyWrapper implements Ontology, OntBaseModel {
264+
public static class Concurrent extends OWLOntologyWrapper implements Ontology, BaseOntologyModel {
265265

266266
@Serial
267267
private static final long serialVersionUID = 5823394836022970162L;
@@ -297,7 +297,7 @@ protected <X> Stream<X> withReadLockToStream(@Nonnull Supplier<Stream<X>> op) {
297297

298298
/**
299299
* Creates a concurrent version of Ontology Graph Model with R/W Lock inside, backed by the given model.
300-
* The internal Jena model, which is provided by the method {@link #getGraphModel()}, does not contain any lock.
300+
* The internal Jena model, which is provided by the method {@link #getBaseGraphModel()}, does not contain any lock.
301301
* This is due to the danger of deadlock or livelock,
302302
* which are possible when working with a (caffeine) cache and a locked graph simultaneously.
303303
*
@@ -307,7 +307,7 @@ protected <X> Stream<X> withReadLockToStream(@Nonnull Supplier<Stream<X>> op) {
307307
public OntModel asGraphModel() {
308308
lock.readLock().lock();
309309
try {
310-
InternalGraphModel base = getGraphModel();
310+
InternalGraphModel base = getBaseGraphModel();
311311
return asConcurrent(base.getUnionGraph(), base.getOntPersonality(), lock);
312312
} finally {
313313
lock.readLock().unlock();
@@ -334,13 +334,13 @@ public OntologyManager getOWLOntologyManager() {
334334
}
335335

336336
@Override
337-
public InternalGraphModel getGraphModel() {
338-
return delegate().getGraphModel();
337+
public InternalGraphModel getBaseGraphModel() {
338+
return delegate().getBaseGraphModel();
339339
}
340340

341341
@Override
342-
public void setGraphModel(InternalGraphModel m) {
343-
delegate().setGraphModel(m);
342+
public void setBaseGraphModel(InternalGraphModel m) {
343+
delegate().setBaseGraphModel(m);
344344
}
345345

346346
@Override

0 commit comments

Comments
 (0)