Search in sources :

Example 26 with OntModel

use of org.apache.jena.ontology.OntModel in project Vitro by vivo-project.

the class RDFFilesLoader method loadEveryTimeFiles.

/**
 * Load the "every time" files.
 *
 * The location is based on the home directory and the model path: "abox",
 * "display", etc.
 *
 * The files from the directory become a sub-model of the model.
 */
public static void loadEveryTimeFiles(String modelPath, OntModel model) {
    OntModel everytimeModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
    String home = locateHomeDirectory();
    Set<Path> paths = getPaths(home, RDF, modelPath, EVERY_TIME);
    for (Path p : paths) {
        log.info("Loading " + relativePath(p, home));
        readOntologyFileIntoModel(p, everytimeModel);
    }
    model.addSubModel(everytimeModel);
}
Also used : Path(java.nio.file.Path) OntModel(org.apache.jena.ontology.OntModel)

Example 27 with OntModel

use of org.apache.jena.ontology.OntModel in project Vitro by vivo-project.

the class UpdateKnowledgeBase method replaceTboxAndDisplayMetadata.

// replace
private void replaceTboxAndDisplayMetadata(OntModel displayModel, Model addStatements, Model removeStatements, UpdateSettings settings) {
    OntModel oldDisplayModelTboxModel = settings.getOldDisplayModelTboxModel();
    OntModel oldDisplayModelDisplayMetadataModel = settings.getOldDisplayModelDisplayMetadataModel();
    OntModel newDisplayModelTboxModel = settings.getNewDisplayModelTboxModel();
    OntModel newDisplayModelDisplayMetadataModel = settings.getNewDisplayModelDisplayMetadataModel();
    OntModel loadedAtStartup = settings.getLoadedAtStartupDisplayModel();
    OntModel oldVivoListView = settings.getVivoListViewConfigDisplayModel();
    // Remove old display model tbox and display metadata statements from display model
    removeStatements.add(oldDisplayModelTboxModel);
    removeStatements.add(oldDisplayModelDisplayMetadataModel);
    // the old startup folder only contained by oldVivoListView
    removeStatements.add(oldVivoListView);
    StringWriter sw = new StringWriter();
    try {
        log.debug("Adding old display tbox model, display metadata model, and oldVivoListView to remove statements.  Remove statements now include:");
        removeStatements.write(sw, "N3");
        log.debug(sw.toString());
        sw.close();
    } catch (Exception ex) {
        log.error("Exception occurred", ex);
    }
    // Add statements from new tbox and display metadata
    addStatements.add(newDisplayModelTboxModel);
    addStatements.add(newDisplayModelDisplayMetadataModel);
    // this should include the list view in addition to other files
    addStatements.add(loadedAtStartup);
    try {
        sw = new StringWriter();
        log.debug("Adding new display tbox model, display metadata model, and loaded at startup to add statements.  Add statements now include:");
        addStatements.write(sw, "N3");
        log.debug(sw.toString());
        sw.close();
    } catch (Exception ex) {
        log.error("Exception occurred in adding new display model tbox/metadata info to add statements ", ex);
    }
    log.debug("Adding new display tbox model, display metadata model, and all models loaded at startup");
}
Also used : StringWriter(java.io.StringWriter) OntModel(org.apache.jena.ontology.OntModel) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 28 with OntModel

use of org.apache.jena.ontology.OntModel in project Vitro by vivo-project.

the class UpdateKnowledgeBase method loadModelFromFile.

// load file from file path
private OntModel loadModelFromFile(String filePath) {
    log.debug("Load model from file " + filePath);
    OntModel om = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
    File file = new File(filePath);
    if (!file.isFile()) {
        throw new ModelFileNotFoundException(filePath + " must be a file " + "containing RDF files.");
    }
    readFile(file, om, filePath);
    return om;
}
Also used : OntModel(org.apache.jena.ontology.OntModel) File(java.io.File)

Example 29 with OntModel

use of org.apache.jena.ontology.OntModel in project Vitro by vivo-project.

the class UpdateKnowledgeBase method updateDataGetterLabels.

// update any new labels
private void updateDataGetterLabels(OntModel displayModel, Model addStatements, Model removeStatements, UpdateSettings settings) {
    log.debug("Checking: BEFORE adding any statements, what do we have for pageList page");
    Resource testResource = ResourceFactory.createResource(DisplayVocabulary.DISPLAY_NS + "pageListPage");
    StmtIterator testIt = addStatements.listStatements(testResource, null, (RDFNode) null);
    if (!testIt.hasNext()) {
        log.debug("Add statements does not have the page list page resource " + testResource.getURI());
    }
    while (testIt.hasNext()) {
        log.debug("Statement for page list resource: " + testIt.nextStatement().toString());
    }
    log.debug("Triple checking -- before this method, the add statements model contains");
    StringWriter sw = new StringWriter();
    try {
        addStatements.write(sw, "N3");
        log.debug(sw.toString());
        sw.close();
    } catch (Exception ex) {
        log.error("Error occurred in adding resource labels ", ex);
    }
    OntModel newDisplayModel = settings.getNewDisplayModelFromFile();
    List<Resource> resourcesForLabels = new ArrayList<Resource>();
    resourcesForLabels.add(ResourceFactory.createResource("java:edu.cornell.mannlib.vitro.webapp.utils.dataGetter.ClassGroupPageData"));
    resourcesForLabels.add(ResourceFactory.createResource("java:edu.cornell.mannlib.vitro.webapp.utils.dataGetter.BrowseDataGetter"));
    resourcesForLabels.add(ResourceFactory.createResource("java:edu.cornell.mannlib.vitro.webapp.utils.dataGetter.IndividualsForClassesDataGetter"));
    resourcesForLabels.add(ResourceFactory.createResource("java:edu.cornell.mannlib.vitro.webapp.utils.dataGetter.InternalClassesDataGetter"));
    resourcesForLabels.add(ResourceFactory.createResource("java:edu.cornell.mannlib.vitro.webapp.utils.dataGetter.SparqlQueryDataGetter"));
    for (Resource r : resourcesForLabels) {
        log.debug("Adding the following for " + r.getURI());
        log.debug(newDisplayModel.listStatements(r, RDFS.label, (RDFNode) null).toList().toString());
        addStatements.add(newDisplayModel.listStatements(r, RDFS.label, (RDFNode) null));
        log.debug("After adding statements, we now have the following in addStatements:::");
        sw = new StringWriter();
        try {
            addStatements.write(sw, "N3");
            log.debug(sw.toString());
            sw.close();
        } catch (Exception ex) {
            log.error("Error occurred in adding resource labels ", ex);
        }
    }
    // Add statements now includes
    log.debug("AFTER all resources added, Add statements now includes ");
    sw = new StringWriter();
    try {
        addStatements.write(sw, "N3");
        log.debug(sw.toString());
        sw.close();
    } catch (Exception ex) {
        log.error("Error occurred in adding resource labels ", ex);
    }
}
Also used : StmtIterator(org.apache.jena.rdf.model.StmtIterator) StringWriter(java.io.StringWriter) Resource(org.apache.jena.rdf.model.Resource) ArrayList(java.util.ArrayList) OntModel(org.apache.jena.ontology.OntModel) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) RDFNode(org.apache.jena.rdf.model.RDFNode)

Example 30 with OntModel

use of org.apache.jena.ontology.OntModel in project Vitro by vivo-project.

the class SimpleReasonerSameAsTest method mostSpecificTypeTest1.

/*
	 * test that mostSpecificType inferences propagate to sameAs
	 * individuals
	 */
@Test
public void mostSpecificTypeTest1() {
    // Create a Tbox with a simple class hierarchy. B is a subclass of A.
    OntModel tBox = createTBoxModel();
    OntClass classA = createClass(tBox, "http://test.vivo/A", "class A");
    OntClass classC = createClass(tBox, "http://test.vivo/C", "class C");
    OntClass classD = createClass(tBox, "http://test.vivo/D", "class D");
    OntClass classE = createClass(tBox, "http://test.vivo/E", "class E");
    // A is superclass of C, C is superclass of D and E
    addSubclass(classA, classC);
    addSubclass(classC, classD);
    addSubclass(classC, classE);
    // SimpleReasonerTBoxListener is not being used.
    AnnotationProperty mostSpecificType = tBox.createAnnotationProperty(mostSpecificTypePropertyURI);
    // this will receive the abox inferences
    Model inf = ModelFactory.createDefaultModel();
    // abox
    OntModel aBox = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
    // set up SimpleReasoner and register it with abox
    SimpleReasoner simpleReasoner = new SimpleReasoner(tBox, aBox, inf);
    aBox.register(simpleReasoner);
    // add & remove ABox type statements and verify inferences
    Resource a = aBox.createResource("http://test.vivo/a");
    Resource b = aBox.createResource("http://test.vivo/b");
    Resource c = aBox.createResource("http://test.vivo/c");
    Resource d = aBox.createResource("http://test.vivo/d");
    // Assert a, b, c and d are the same
    aBox.add(a, OWL.sameAs, b);
    aBox.add(c, OWL.sameAs, b);
    aBox.add(d, OWL.sameAs, a);
    // Assert a is D, and d ic C
    // All resources will therefore be C and D
    aBox.add(a, RDF.type, classD);
    aBox.add(d, RDF.type, classC);
    // All resources should be inferred as C and D, except a and d which have direct assertions
    // D is asserted for a
    Assert.assertFalse(inf.contains(a, RDF.type, classD));
    Assert.assertTrue(inf.contains(a, RDF.type, classC));
    Assert.assertTrue(inf.contains(b, RDF.type, classD));
    Assert.assertTrue(inf.contains(b, RDF.type, classC));
    Assert.assertTrue(inf.contains(c, RDF.type, classD));
    Assert.assertTrue(inf.contains(c, RDF.type, classC));
    Assert.assertTrue(inf.contains(d, RDF.type, classD));
    // C is asserted for d
    Assert.assertFalse(inf.contains(d, RDF.type, classC));
    // In all cases, mostSpecificType should be D, not C
    Assert.assertTrue(inf.contains(a, mostSpecificType, ResourceFactory.createResource(classD.getURI())));
    Assert.assertTrue(inf.contains(b, mostSpecificType, ResourceFactory.createResource(classD.getURI())));
    Assert.assertTrue(inf.contains(c, mostSpecificType, ResourceFactory.createResource(classD.getURI())));
    Assert.assertTrue(inf.contains(d, mostSpecificType, ResourceFactory.createResource(classD.getURI())));
    Assert.assertFalse(inf.contains(a, mostSpecificType, ResourceFactory.createResource(classC.getURI())));
    Assert.assertFalse(inf.contains(b, mostSpecificType, ResourceFactory.createResource(classC.getURI())));
    Assert.assertFalse(inf.contains(c, mostSpecificType, ResourceFactory.createResource(classC.getURI())));
    Assert.assertFalse(inf.contains(d, mostSpecificType, ResourceFactory.createResource(classC.getURI())));
    aBox.remove(a, RDF.type, classD);
    Assert.assertFalse(inf.contains(a, RDF.type, classD));
    Assert.assertTrue(inf.contains(a, RDF.type, classC));
    Assert.assertFalse(inf.contains(b, RDF.type, classD));
    Assert.assertTrue(inf.contains(b, RDF.type, classC));
    Assert.assertFalse(inf.contains(c, RDF.type, classD));
    Assert.assertTrue(inf.contains(c, RDF.type, classC));
    Assert.assertFalse(inf.contains(d, RDF.type, classD));
    Assert.assertFalse(inf.contains(d, RDF.type, classC));
    Assert.assertTrue(inf.contains(a, mostSpecificType, ResourceFactory.createResource(classC.getURI())));
    Assert.assertTrue(inf.contains(b, mostSpecificType, ResourceFactory.createResource(classC.getURI())));
    Assert.assertTrue(inf.contains(c, mostSpecificType, ResourceFactory.createResource(classC.getURI())));
    Assert.assertTrue(inf.contains(d, mostSpecificType, ResourceFactory.createResource(classC.getURI())));
    Assert.assertFalse(inf.contains(a, mostSpecificType, ResourceFactory.createResource(classD.getURI())));
    Assert.assertFalse(inf.contains(b, mostSpecificType, ResourceFactory.createResource(classD.getURI())));
    Assert.assertFalse(inf.contains(c, mostSpecificType, ResourceFactory.createResource(classD.getURI())));
    Assert.assertFalse(inf.contains(d, mostSpecificType, ResourceFactory.createResource(classD.getURI())));
}
Also used : AnnotationProperty(org.apache.jena.ontology.AnnotationProperty) OntModel(org.apache.jena.ontology.OntModel) Model(org.apache.jena.rdf.model.Model) Resource(org.apache.jena.rdf.model.Resource) OntModel(org.apache.jena.ontology.OntModel) OntClass(org.apache.jena.ontology.OntClass) Test(org.junit.Test)

Aggregations

OntModel (org.apache.jena.ontology.OntModel)272 Model (org.apache.jena.rdf.model.Model)112 Resource (org.apache.jena.rdf.model.Resource)92 Test (org.junit.Test)51 Statement (org.apache.jena.rdf.model.Statement)41 OntClass (org.apache.jena.ontology.OntClass)33 StmtIterator (org.apache.jena.rdf.model.StmtIterator)31 ArrayList (java.util.ArrayList)29 OntProperty (org.apache.jena.ontology.OntProperty)29 OntResource (org.apache.jena.ontology.OntResource)26 Literal (org.apache.jena.rdf.model.Literal)26 RDFNode (org.apache.jena.rdf.model.RDFNode)26 IOException (java.io.IOException)24 Individual (org.apache.jena.ontology.Individual)22 Property (org.apache.jena.rdf.model.Property)21 QueryExecution (org.apache.jena.query.QueryExecution)16 StringReader (java.io.StringReader)15 DatatypeProperty (org.apache.jena.ontology.DatatypeProperty)14 InputStream (java.io.InputStream)12 DataProperty (edu.cornell.mannlib.vitro.webapp.beans.DataProperty)11