Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
package cz.cvut.spipes.manager;

import cz.cvut.spipes.TestConstants;
import org.apache.jena.ontology.OntClass;
import org.apache.jena.ontology.OntDocumentManager;
import org.apache.jena.ontology.OntModel;
import org.apache.jena.ontology.OntModelSpec;
import org.apache.jena.util.LocationMapper;
import org.apache.jena.util.ResourceUtils;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertFalse;

public class OntoDocManagerTest {


static Path managerDirPath = TestConstants.TEST_RESOURCES_DIR_PATH.resolve("manager").toAbsolutePath();

@Test
Expand Down Expand Up @@ -101,6 +105,68 @@ public void registerDocumentsToLoadImportClosure2() {
assertEquals(3, loadedClassNames.size());
}

@Test //TODO move to a "jena experiment project"
public void jenaOntDocumentManagerLoadsDocumentChanges() throws IOException {

OntDocumentManager docManager = OntDocumentManager.getInstance();
String testOntologyPrefix = "http://onto.fel.cvut.cz/ontologies/test/";

Path sourceDirPath = managerDirPath.resolve("file-update");
List<String> ontologyNames = Arrays.asList(
"directly-imported",
"indirectly-imported",
"loading-test"
);

String loadingTestOntologyUri = testOntologyPrefix + "loading-test-ontology";
String indirectlyImportedOntologyUri = testOntologyPrefix + "indirectly-imported-ontology";

ontologyNames.forEach( on -> {
docManager.addAltEntry(
testOntologyPrefix + on + "-ontology",
String.valueOf(sourceDirPath.resolve(on + ".ttl").toUri().toURL())
);
});

OntModel model = docManager.getOntology(loadingTestOntologyUri, OntModelSpec.OWL_MEM);

assertTrue(containClassWithLocalName(model, "directly-imported-class"));
assertTrue(containClassWithLocalName(model,"indirectly-imported-class"));
assertTrue(containClassWithLocalName(model,"loading-test-class"));

assertEquals(3, getOntologyClassesLocalNames(model).size());

OntModel indirectlyImportedModel = docManager.getOntology(indirectlyImportedOntologyUri, OntModelSpec.OWL_MEM);

OntClass resource = indirectlyImportedModel
.listClasses()
.filterKeep(r -> r.isResource() && r.asResource().getLocalName().equals("indirectly-imported-class"))
.next();
ResourceUtils.renameResource(resource, resource.getNameSpace() + "new-indirectly-imported-class");

// check locally modified model
assertFalse(containClassWithLocalName(indirectlyImportedModel,"indirectly-imported-class"));
assertTrue(containClassWithLocalName(indirectlyImportedModel,"new-indirectly-imported-class"));
assertEquals(1, getOntologyClassesLocalNames(indirectlyImportedModel).size());

// update the model globally
docManager.getFileManager().addCacheModel(indirectlyImportedOntologyUri, indirectlyImportedModel);
Copy link
Contributor Author

@blcham blcham Jun 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chlupnoha this is method that you were looking for to replace cached model:
docManager.getFileManager().addCacheModel(....);

  • the issue is with concurrent access here

  • once, you find out that model has changed in FS, you can use it to update imported models

Copy link
Contributor

@chlupnoha chlupnoha Jun 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! I did not expect this kind of approach. However, I will stick to my solution, which is very costly but working as I want and do not require any other handling.

        ModelMakerImpl modelMaker = new ModelMakerImpl(new SimpleGraphMaker());
        OntModelSpec ontModelSpec = new OntModelSpec(modelMaker, null, null, ProfileRegistry.OWL_LANG);
        OntModel model = documentManager.getOntology(fileUri, ontModelSpec);

Your suggested still solution does not work correctly in the execution of multiple tests, but it may be related to the static context of the OntModelSpec or something else. Also, it will be tough to look for and manage more complex ontologies in the future.


// check new model is updated
assertTrue(containClassWithLocalName(model, "directly-imported-class"));
assertTrue(containClassWithLocalName(model,"new-indirectly-imported-class"));
assertTrue(containClassWithLocalName(model,"loading-test-class"));
assertEquals(3, getOntologyClassesLocalNames(model).size());
}

private List<String> getOntologyClassesLocalNames(OntModel model) {
return model.listClasses().mapWith(c -> c.asResource().getLocalName()).toList();
}

private boolean containClassWithLocalName(OntModel model, String localName) {
return getOntologyClassesLocalNames(model).contains(localName);
}



private int getLocationMapperEntriesCount(OntologyDocumentManager ontoDocManager) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
@prefix : <http://onto.fel.cvut.cz/ontologies/test/directly-imported-ontology/> .
@prefix test: <http://onto.fel.cvut.cz/ontologies/test/> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix xml: <http://www.w3.org/XML/1998/namespace> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@base <http://onto.fel.cvut.cz/ontologies/test/directly-imported-ontology> .

test:directly-imported-ontology rdf:type owl:Ontology ;
owl:imports test:indirectly-imported-ontology ;
.

:directly-imported-class rdf:type owl:Class .


### Generated by the OWL API (version 4.2.5.20160517-0735) https://github.com/owlcs/owlapi
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
@prefix : <http://onto.fel.cvut.cz/ontologies/test/indirectly-imported-ontology/> .
@prefix test: <http://onto.fel.cvut.cz/ontologies/test/> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix xml: <http://www.w3.org/XML/1998/namespace> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@base <http://onto.fel.cvut.cz/ontologies/test/indirectly-imported-ontology> .

test:indirectly-imported-ontology rdf:type owl:Ontology .

:indirectly-imported-class rdf:type owl:Class .
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
@prefix test: <http://onto.fel.cvut.cz/ontologies/test/> .
@prefix : <http://onto.fel.cvut.cz/ontologies/test/loading-test-ontology/> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix xml: <http://www.w3.org/XML/1998/namespace> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@base <http://onto.fel.cvut.cz/ontologies/test/loading-test-ontology> .

test:loading-test-ontology rdf:type owl:Ontology ;
owl:imports test:directly-imported-ontology ;
.

:loading-test-class rdf:type owl:Class .