From 9ed3cac11994b9d53cd1fd3a4a1fda3c75528b9b Mon Sep 17 00:00:00 2001 From: Jeen Broekstra Date: Sat, 17 Nov 2018 13:08:58 +1100 Subject: [PATCH] protegeproject/rdf-library#6 bumped owlapi to v5, got compilation errors fixed [WIP] Signed-off-by: Jeen Broekstra --- pom.xml | 7 +- .../java/org/protege/owl/rdf/Utilities.java | 10 +-- .../protege/owl/rdf/api/OwlTripleStore.java | 9 +- .../rdf/impl/AnonymousResourceHandler.java | 31 +++---- .../owl/rdf/impl/OwlTripleStoreImpl.java | 90 ++++++++++--------- .../protege/owl/rdf/impl/RDFTranslator.java | 70 ++++++++------- .../rdf/impl/RepositoryRuntimeException.java | 2 +- .../impl/SynchronizeTripleStoreListener.java | 2 +- .../org/protege/owl/rdf/AnonymityTests.java | 13 ++- .../java/org/protege/owl/rdf/ImportTests.java | 13 ++- .../java/org/protege/owl/rdf/PizzaTests.java | 5 +- .../protege/owl/rdf/SynchronizationTests.java | 2 +- 12 files changed, 131 insertions(+), 123 deletions(-) diff --git a/pom.xml b/pom.xml index 1cfea53..672ed69 100644 --- a/pom.xml +++ b/pom.xml @@ -44,9 +44,8 @@ - 1.1.3 - 1.7.12 - 4.2.8 + 5.1.7 + 2.4.1 @@ -54,7 +53,7 @@ org.eclipse.rdf4j rdf4j-bom - 2.4.1 + ${rdf4j.version} pom import diff --git a/src/main/java/org/protege/owl/rdf/Utilities.java b/src/main/java/org/protege/owl/rdf/Utilities.java index 96118c5..6f6402b 100644 --- a/src/main/java/org/protege/owl/rdf/Utilities.java +++ b/src/main/java/org/protege/owl/rdf/Utilities.java @@ -1,10 +1,10 @@ package org.protege.owl.rdf; -import org.openrdf.repository.Repository; -import org.openrdf.repository.RepositoryException; -import org.openrdf.repository.sail.SailRepository; -import org.openrdf.sail.Sail; -import org.openrdf.sail.memory.MemoryStore; +import org.eclipse.rdf4j.repository.Repository; +import org.eclipse.rdf4j.repository.RepositoryException; +import org.eclipse.rdf4j.repository.sail.SailRepository; +import org.eclipse.rdf4j.sail.Sail; +import org.eclipse.rdf4j.sail.memory.MemoryStore; import org.protege.owl.rdf.api.OwlTripleStore; import org.protege.owl.rdf.impl.OwlTripleStoreImpl; import org.protege.owl.rdf.impl.SynchronizeTripleStoreListener; diff --git a/src/main/java/org/protege/owl/rdf/api/OwlTripleStore.java b/src/main/java/org/protege/owl/rdf/api/OwlTripleStore.java index b47e1cf..19af64c 100644 --- a/src/main/java/org/protege/owl/rdf/api/OwlTripleStore.java +++ b/src/main/java/org/protege/owl/rdf/api/OwlTripleStore.java @@ -1,10 +1,9 @@ package org.protege.owl.rdf.api; -import info.aduna.iteration.CloseableIteration; - -import org.openrdf.model.BNode; -import org.openrdf.repository.Repository; -import org.openrdf.repository.RepositoryException; +import org.eclipse.rdf4j.common.iteration.CloseableIteration; +import org.eclipse.rdf4j.model.BNode; +import org.eclipse.rdf4j.repository.Repository; +import org.eclipse.rdf4j.repository.RepositoryException; import org.semanticweb.owlapi.model.OWLAxiom; import org.semanticweb.owlapi.model.OWLClassExpression; import org.semanticweb.owlapi.model.OWLOntologyID; diff --git a/src/main/java/org/protege/owl/rdf/impl/AnonymousResourceHandler.java b/src/main/java/org/protege/owl/rdf/impl/AnonymousResourceHandler.java index e09a882..5c971f9 100644 --- a/src/main/java/org/protege/owl/rdf/impl/AnonymousResourceHandler.java +++ b/src/main/java/org/protege/owl/rdf/impl/AnonymousResourceHandler.java @@ -17,6 +17,7 @@ import org.semanticweb.owlapi.model.OWLNamedIndividual; import org.semanticweb.owlapi.model.OWLObject; import org.semanticweb.owlapi.model.OWLOntologyID; +import org.semanticweb.owlapi.model.OWLOntologyManager; import org.semanticweb.owlapi.util.OWLObjectDuplicator; public class AnonymousResourceHandler { @@ -108,15 +109,15 @@ private Set duplicateAxiomAnnotations(OWLAxiom axiom, OWLObjectDu private class SurrogateInserter extends OWLObjectDuplicator { public SurrogateInserter(OWLDataFactory factory) { - super(factory); + super((OWLOntologyManager)factory); } - public void visit(OWLAnonymousIndividual i) { + public OWLAnonymousIndividual visit(OWLAnonymousIndividual i) { IRI iri = getSurrogateId(i); - setLastObject(factory.getOWLNamedIndividual(iri)); + return (OWLAnonymousIndividual)t(factory.getOWLNamedIndividual(iri)); } - public void visit(OWLAnnotationAssertionAxiom axiom) { + public OWLAnnotationAssertionAxiom visit(OWLAnnotationAssertionAxiom axiom) { OWLObject rawSubject = duplicateObject(axiom.getSubject()); OWLAnnotationSubject subject; if (rawSubject instanceof OWLNamedIndividual) { @@ -134,10 +135,10 @@ public void visit(OWLAnnotationAssertionAxiom axiom) { else { value = (OWLAnnotationValue) rawValue; } - setLastObject(factory.getOWLAnnotationAssertionAxiom(prop, subject, value, duplicateAxiomAnnotations(axiom, this))); + return t(factory.getOWLAnnotationAssertionAxiom(prop, subject, value, duplicateAxiomAnnotations(axiom, this))); } - public void visit(OWLAnnotation node) { + public OWLAnnotation visit(OWLAnnotation node) { OWLAnnotationProperty prop = duplicateObject(node.getProperty()); OWLObject rawValue = duplicateObject(node.getValue()); OWLAnnotationValue val; @@ -147,7 +148,7 @@ public void visit(OWLAnnotation node) { else { val = (OWLAnnotationValue) rawValue; } - setLastObject(factory.getOWLAnnotation(prop, val)); + return t(factory.getOWLAnnotation(prop, val)); } } @@ -155,19 +156,19 @@ public void visit(OWLAnnotation node) { private class SurrogateRemover extends OWLObjectDuplicator { public SurrogateRemover(OWLDataFactory factory) { - super(factory); + super((OWLOntologyManager)factory); } - public void visit(OWLNamedIndividual i) { + public OWLNamedIndividual visit(OWLNamedIndividual i) { if (isSurrogate(i.getIRI())) { - setLastObject(getAnonymousIndividual(i.getIRI())); + return (OWLNamedIndividual)t(getAnonymousIndividual(i.getIRI())); } else { - setLastObject(i); + return t(i); } } - public void visit(OWLAnnotationAssertionAxiom axiom) { + public OWLAnnotationAssertionAxiom visit(OWLAnnotationAssertionAxiom axiom) { OWLAnnotationSubject subject = duplicateObject(axiom.getSubject()); if (subject instanceof IRI && isSurrogate((IRI) subject)) { subject = getAnonymousIndividual((IRI) subject); @@ -177,17 +178,17 @@ public void visit(OWLAnnotationAssertionAxiom axiom) { if (value instanceof IRI && isSurrogate((IRI) value)) { value = getAnonymousIndividual((IRI) value); } - setLastObject(factory.getOWLAnnotationAssertionAxiom(prop, subject, value, duplicateAxiomAnnotations(axiom, this))); + return t(factory.getOWLAnnotationAssertionAxiom(prop, subject, value, duplicateAxiomAnnotations(axiom, this))); } - public void visit(OWLAnnotation node) { + public OWLAnnotation visit(OWLAnnotation node) { node.getProperty().accept(this); OWLAnnotationProperty prop = (OWLAnnotationProperty) duplicateObject(node.getProperty()); OWLAnnotationValue val = (OWLAnnotationValue) duplicateObject(node.getValue()); if (val instanceof IRI && isSurrogate((IRI) val)) { val = getAnonymousIndividual((IRI) val); } - setLastObject(factory.getOWLAnnotation(prop, val)); + return t(factory.getOWLAnnotation(prop, val)); } } } diff --git a/src/main/java/org/protege/owl/rdf/impl/OwlTripleStoreImpl.java b/src/main/java/org/protege/owl/rdf/impl/OwlTripleStoreImpl.java index 0410f57..6f9532c 100644 --- a/src/main/java/org/protege/owl/rdf/impl/OwlTripleStoreImpl.java +++ b/src/main/java/org/protege/owl/rdf/impl/OwlTripleStoreImpl.java @@ -1,22 +1,24 @@ package org.protege.owl.rdf.impl; -import info.aduna.iteration.CloseableIteration; - import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.util.UUID; -import org.openrdf.model.BNode; -import org.openrdf.model.Statement; -import org.openrdf.model.ValueFactory; -import org.openrdf.repository.Repository; -import org.openrdf.repository.RepositoryConnection; -import org.openrdf.repository.RepositoryException; -import org.openrdf.repository.RepositoryResult; -import org.openrdf.rio.RDFHandlerException; -import org.openrdf.rio.RDFWriter; -import org.openrdf.rio.rdfxml.RDFXMLWriter; +import org.eclipse.rdf4j.common.iteration.CloseableIteration; +import org.eclipse.rdf4j.model.BNode; +import org.eclipse.rdf4j.model.Literal; +import org.eclipse.rdf4j.model.Resource; +import org.eclipse.rdf4j.model.Statement; +import org.eclipse.rdf4j.model.URI; +import org.eclipse.rdf4j.model.ValueFactory; +import org.eclipse.rdf4j.repository.Repository; +import org.eclipse.rdf4j.repository.RepositoryConnection; +import org.eclipse.rdf4j.repository.RepositoryException; +import org.eclipse.rdf4j.repository.RepositoryResult; +import org.eclipse.rdf4j.rio.RDFHandlerException; +import org.eclipse.rdf4j.rio.RDFWriter; +import org.eclipse.rdf4j.rio.rdfxml.RDFXMLWriter; import org.protege.owl.rdf.api.OwlTripleStore; import org.semanticweb.owlapi.apibinding.OWLManager; import org.semanticweb.owlapi.model.IRI; @@ -48,10 +50,10 @@ public class OwlTripleStoreImpl implements OwlTripleStore { public static final String BNODE_PREFIX = "_:BNode"; - private org.openrdf.model.URI hashCodeProperty; - private org.openrdf.model.URI sourceOntologyProperty; - private org.openrdf.model.URI ontologyIdProperty; - private org.openrdf.model.URI ontologyVersionProperty; + private org.eclipse.rdf4j.model.URI hashCodeProperty; + private org.eclipse.rdf4j.model.URI sourceOntologyProperty; + private org.eclipse.rdf4j.model.URI ontologyIdProperty; + private org.eclipse.rdf4j.model.URI ontologyVersionProperty; private Repository repository; private AnonymousResourceHandler anonymousHandler; @@ -92,7 +94,7 @@ public Repository getRepository() { @Override public void addAxiom(OWLOntologyID ontologyId, OWLAxiom axiom) throws RepositoryException { axiom = anonymousHandler.insertSurrogates(axiom); - org.openrdf.model.URI ontologyRepresentative = getOntologyRepresentative(ontologyId); + URI ontologyRepresentative = getOntologyRepresentative(ontologyId); if (getAxiomId(ontologyId, axiom) != null) { return; } @@ -102,7 +104,7 @@ public void addAxiom(OWLOntologyID ontologyId, OWLAxiom axiom) throws Repository @Override public void removeAxiom(OWLOntologyID ontologyId, OWLAxiom axiom) throws RepositoryException { axiom = anonymousHandler.insertSurrogates(axiom); - org.openrdf.model.URI axiomResource = getAxiomId(ontologyId, axiom); + URI axiomResource = getAxiomId(ontologyId, axiom); if (axiomResource != null) { removeAxiom(axiomResource); } @@ -116,7 +118,7 @@ public boolean hasAxiom(OWLOntologyID ontologyId, OWLAxiom axiom) throws Reposit @Override public CloseableIteration listAxioms(OWLOntologyID ontologyId) throws RepositoryException { - org.openrdf.model.URI ontologyRepresentative = getOntologyRepresentative(ontologyId); + URI ontologyRepresentative = getOntologyRepresentative(ontologyId); final RepositoryConnection connection = repository.getConnection(); boolean success = false; try { @@ -131,7 +133,7 @@ public boolean hasNext() throws RepositoryException { @Override public OWLAxiom next() throws RepositoryException { Statement stmt = stmts.next(); - org.openrdf.model.URI axiomResource = (org.openrdf.model.URI) stmt.getSubject(); + URI axiomResource = (URI) stmt.getSubject(); RepositoryConnection connection = repository.getConnection(); try { return anonymousHandler.removeSurrogates(parseAxiom(connection, axiomResource)); @@ -190,18 +192,18 @@ public boolean incorporateExternalChanges() { * @return * @throws RepositoryException */ - private org.openrdf.model.URI getAxiomId(OWLOntologyID ontologyId, OWLAxiom axiom) throws RepositoryException { - org.openrdf.model.URI ontologyRepresentative = getOntologyRepresentative(ontologyId); + private URI getAxiomId(OWLOntologyID ontologyId, OWLAxiom axiom) throws RepositoryException { + URI ontologyRepresentative = getOntologyRepresentative(ontologyId); ValueFactory factory = repository.getValueFactory(); RepositoryConnection connection = repository.getConnection(); try { - org.openrdf.model.Literal hashCodeValue = factory.createLiteral(axiom.hashCode()); + Literal hashCodeValue = factory.createLiteral(axiom.hashCode()); RepositoryResult correctHashCodes = connection.getStatements(null, hashCodeProperty, hashCodeValue, false); try { while (correctHashCodes.hasNext()) { Statement stmt = correctHashCodes.next(); - if (stmt.getSubject() instanceof org.openrdf.model.URI) { - org.openrdf.model.URI axiomId = (org.openrdf.model.URI) stmt.getSubject(); + if (stmt.getSubject() instanceof URI) { + URI axiomId = (URI) stmt.getSubject(); if (connection.hasStatement(axiomId, sourceOntologyProperty, ontologyRepresentative, false) && axiom.equals(parseAxiom(connection, axiomId))) { return axiomId; @@ -239,7 +241,7 @@ private org.openrdf.model.URI getAxiomId(OWLOntologyID ontologyId, OWLAxiom axio * @throws IOException * @throws RDFHandlerException */ - private OWLAxiom parseAxiom(RepositoryConnection connection, org.openrdf.model.URI axiomId) throws OWLOntologyCreationException, RepositoryException, SAXException, IOException, RDFHandlerException { + private OWLAxiom parseAxiom(RepositoryConnection connection, URI axiomId) throws OWLOntologyCreationException, RepositoryException, SAXException, IOException, RDFHandlerException { if (LOGGER.isDebugEnabled()) { LOGGER.debug("Starting parse"); } @@ -268,7 +270,7 @@ public OWLClassExpression parseClassExpression(BNode classExpressionNode) throws try { RepositoryResult triples = connection.getStatements(classExpressionNode, null, null, false); Statement stmt = triples.next(); - org.openrdf.model.URI axiomId = (org.openrdf.model.URI) stmt.getContext(); + URI axiomId = (URI) stmt.getContext(); OWLRDFConsumer consumer = consumeTriples(connection, axiomId); String nodeName = generateName(classExpressionNode); OWLClassExpression ce = consumer.translateClassExpression(IRI.create(nodeName)); @@ -293,7 +295,7 @@ public OWLClassExpression parseClassExpression(BNode classExpressionNode) throws } } - private OWLRDFConsumer consumeTriples(RepositoryConnection connection, org.openrdf.model.URI axiomId) throws OWLOntologyCreationException, RepositoryException, IOException, RDFHandlerException, SAXException { + private OWLRDFConsumer consumeTriples(RepositoryConnection connection, URI axiomId) throws OWLOntologyCreationException, RepositoryException, IOException, RDFHandlerException, SAXException { OWLOntologyManager manager = OWLManager.createOWLOntologyManager(); OWLOntology ontology = manager.createOntology(); OWLRDFConsumer consumer = new OWLRDFConsumer(ontology, anonymousNodeChecker, new OWLOntologyLoaderConfiguration()); @@ -315,10 +317,10 @@ private OWLRDFConsumer consumeTriples(RepositoryConnection connection, org.openr } String subjectName = generateName(stmt.getSubject()); String predicateName = generateName(stmt.getPredicate()); - if (stmt.getObject() instanceof org.openrdf.model.Literal) { - addTriple(consumer, subjectName, predicateName, (org.openrdf.model.Literal) stmt.getObject()); + if (stmt.getObject() instanceof Literal) { + addTriple(consumer, subjectName, predicateName, (Literal) stmt.getObject()); } else { - addTriple(consumer, subjectName, predicateName, (org.openrdf.model.Resource) stmt.getObject()); + addTriple(consumer, subjectName, predicateName, (Resource) stmt.getObject()); } } if (LOGGER.isDebugEnabled()) { @@ -333,7 +335,7 @@ private OWLRDFConsumer consumeTriples(RepositoryConnection connection, org.openr } private void addTriple(RDFConsumer consumer, - String subjectName, String predicateName, org.openrdf.model.Literal literal) throws SAXException { + String subjectName, String predicateName, Literal literal) throws SAXException { String datatype; if (literal.getDatatype() == null) { datatype = null; // OWL2Datatype.RDF_PLAIN_LITERAL.getIRI().toString(); @@ -344,18 +346,18 @@ private void addTriple(RDFConsumer consumer, consumer.statementWithLiteralValue(subjectName, predicateName, literal.stringValue(), - literal.getLanguage(), + literal.getLanguage().orElse(null), datatype); } private void addTriple(RDFConsumer consumer, String subjectName, String predicateName, - org.openrdf.model.Resource value) throws SAXException { + Resource value) throws SAXException { consumer.statementWithResourceValue(subjectName, predicateName, generateName(value)); } - private void removeAxiom(org.openrdf.model.URI axiomResource) throws RepositoryException { + private void removeAxiom(URI axiomResource) throws RepositoryException { if (axiomResource == null) { return; } @@ -373,7 +375,7 @@ private void removeAxiom(org.openrdf.model.URI axiomResource) throws RepositoryE } } - private String generateName(org.openrdf.model.Resource resource) { + private String generateName(Resource resource) { String name; if (resource instanceof BNode) { name = BNODE_PREFIX + ((BNode) resource).getID(); @@ -385,7 +387,7 @@ private String generateName(org.openrdf.model.Resource resource) { } - private org.openrdf.model.URI getOntologyRepresentative(OWLOntologyID id) throws RepositoryException { + private URI getOntologyRepresentative(OWLOntologyID id) throws RepositoryException { if (id.isAnonymous()) { return repository.getValueFactory().createURI(anonymousHandler.getSurrogateId(id).toString()); } @@ -394,19 +396,19 @@ private org.openrdf.model.URI getOntologyRepresentative(OWLOntologyID id) throws } } - private org.openrdf.model.URI getNamedOntologyRepresentative(OWLOntologyID id) throws RepositoryException { - org.openrdf.model.URI result = null; + private URI getNamedOntologyRepresentative(OWLOntologyID id) throws RepositoryException { + URI result = null; RepositoryConnection connection = repository.getConnection(); try { - org.openrdf.model.URI rdfId = repository.getValueFactory().createURI(id.getOntologyIRI().toString()); - org.openrdf.model.URI rdfVersion = id.getVersionIRI().isPresent() ? repository + URI rdfId = repository.getValueFactory().createURI(id.getOntologyIRI().toString()); + URI rdfVersion = id.getVersionIRI().isPresent() ? repository .getValueFactory().createURI( id.getVersionIRI().get().toString()) : null; RepositoryResult idStatements = connection.getStatements(null, ontologyIdProperty, rdfId, false); try { while (idStatements.hasNext()) { Statement idStatement = idStatements.next(); - org.openrdf.model.URI possible = (org.openrdf.model.URI) idStatement.getSubject(); + URI possible = (URI) idStatement.getSubject(); RepositoryResult versionStatements = connection.getStatements(possible, ontologyVersionProperty, null, false); try { if (rdfVersion == null && !versionStatements.hasNext()) { @@ -444,9 +446,9 @@ private org.openrdf.model.URI getNamedOntologyRepresentative(OWLOntologyID id) t return result; } - private org.openrdf.model.URI createNamedOntologyRepresentative(org.openrdf.model.URI rdfId, org.openrdf.model.URI rdfVersion) throws RepositoryException { + private URI createNamedOntologyRepresentative(URI rdfId, URI rdfVersion) throws RepositoryException { String uriString = NS + "#" + UUID.randomUUID().toString().replaceAll("-", "_"); - org.openrdf.model.URI representative = repository.getValueFactory().createURI(uriString); + URI representative = repository.getValueFactory().createURI(uriString); RepositoryConnection connection = repository.getConnection(); try { connection.add(representative, ontologyIdProperty, rdfId); diff --git a/src/main/java/org/protege/owl/rdf/impl/RDFTranslator.java b/src/main/java/org/protege/owl/rdf/impl/RDFTranslator.java index e1dc0fa..b906895 100644 --- a/src/main/java/org/protege/owl/rdf/impl/RDFTranslator.java +++ b/src/main/java/org/protege/owl/rdf/impl/RDFTranslator.java @@ -1,28 +1,38 @@ package org.protege.owl.rdf.impl; -import org.openrdf.model.BNode; -import org.openrdf.model.Resource; -import org.openrdf.model.Value; -import org.openrdf.model.ValueFactory; -import org.openrdf.repository.Repository; -import org.openrdf.repository.RepositoryConnection; -import org.openrdf.repository.RepositoryException; +import java.util.HashSet; +import java.util.IdentityHashMap; +import java.util.Map; +import java.util.Set; +import java.util.UUID; + +import org.eclipse.rdf4j.model.BNode; +import org.eclipse.rdf4j.model.Literal; +import org.eclipse.rdf4j.model.Resource; +import org.eclipse.rdf4j.model.URI; +import org.eclipse.rdf4j.model.Value; +import org.eclipse.rdf4j.model.ValueFactory; +import org.eclipse.rdf4j.repository.Repository; +import org.eclipse.rdf4j.repository.RepositoryConnection; +import org.eclipse.rdf4j.repository.RepositoryException; import org.semanticweb.owlapi.apibinding.OWLManager; -import org.semanticweb.owlapi.model.*; +import org.semanticweb.owlapi.model.IRI; +import org.semanticweb.owlapi.model.OWLAxiom; +import org.semanticweb.owlapi.model.OWLEntity; +import org.semanticweb.owlapi.model.OWLLiteral; +import org.semanticweb.owlapi.model.OWLOntology; +import org.semanticweb.owlapi.model.OWLOntologyCreationException; +import org.semanticweb.owlapi.model.OWLOntologyManager; import org.semanticweb.owlapi.rdf.model.AbstractTranslator; -import org.semanticweb.owlapi.util.AlwaysOutputId; +import org.semanticweb.owlapi.util.OWLAnonymousIndividualsWithMultipleOccurrences; import org.semanticweb.owlapi.vocab.OWLRDFVocabulary; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import javax.annotation.Nonnull; -import java.util.*; -import java.util.concurrent.atomic.AtomicInteger; - -public class RDFTranslator extends AbstractTranslator { +public class RDFTranslator extends AbstractTranslator { public static final Logger LOGGER = LoggerFactory.getLogger(RDFTranslator.class); - private org.openrdf.model.URI axiomResource; + private URI axiomResource; /** * There is a dangerous bend coming up! If you don't use the identity hash map then this @@ -40,9 +50,9 @@ public class RDFTranslator extends AbstractTranslator