Search in sources :

Example 16 with OntModel

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

the class VClassDaoJena method isSubClassOf.

public boolean isSubClassOf(String vclassURI1, String vclassURI2) {
    if (vclassURI1 == null || vclassURI2 == null) {
        return false;
    }
    OntModel ontModel = getOntModel();
    try {
        ontModel.enterCriticalSection(Lock.READ);
        Resource oc1 = ontModel.getResource(vclassURI1);
        Resource oc2 = ontModel.getResource(vclassURI2);
        return ontModel.contains(oc1, RDFS.subClassOf, oc2);
    } finally {
        ontModel.leaveCriticalSection();
    }
}
Also used : OntResource(org.apache.jena.ontology.OntResource) Resource(org.apache.jena.rdf.model.Resource) OntModel(org.apache.jena.ontology.OntModel)

Example 17 with OntModel

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

the class VClassDaoJena method getOntologyRootClasses.

public List<VClass> getOntologyRootClasses(String ontologyURI) {
    if (ontologyURI == null) {
        throw new RuntimeException("can't find root classes for null ontology URI");
    }
    // return getRootClasses(ontologyURI);
    List<VClass> ontologyRootClasses = new ArrayList<VClass>();
    OntModel ontModel = getOntModel();
    ontModel.enterCriticalSection(Lock.READ);
    try {
        Iterator<Resource> ontClassIt = ontModel.listResourcesWithProperty(RDF.type, OWL.Class);
        while (ontClassIt.hasNext()) {
            Resource ontClass = ontClassIt.next();
            if (ontologyURI.equals(ontClass.getNameSpace())) {
                boolean root = true;
                StmtIterator superStmtIt = ontModel.listStatements(ontClass, RDFS.subClassOf, (RDFNode) null);
                try {
                    while (superStmtIt.hasNext()) {
                        Statement superStmt = superStmtIt.nextStatement();
                        if (superStmt.getObject().isResource() && ontologyURI.equals(((Resource) superStmt.getObject()).getNameSpace())) {
                            root = false;
                            break;
                        }
                    }
                } finally {
                    superStmtIt.close();
                }
                if (root && ontClass.canAs(OntClass.class)) {
                    ontologyRootClasses.add(new VClassJena((OntClass) ontClass.as(OntClass.class), getWebappDaoFactory()));
                }
            }
        }
    } finally {
        ontModel.leaveCriticalSection();
    }
    return ontologyRootClasses;
}
Also used : VClass(edu.cornell.mannlib.vitro.webapp.beans.VClass) StmtIterator(org.apache.jena.rdf.model.StmtIterator) Statement(org.apache.jena.rdf.model.Statement) ArrayList(java.util.ArrayList) OntResource(org.apache.jena.ontology.OntResource) Resource(org.apache.jena.rdf.model.Resource) OntModel(org.apache.jena.ontology.OntModel) OntClass(org.apache.jena.ontology.OntClass)

Example 18 with OntModel

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

the class WebappDaoFactoryJena method getCommentsForResource.

@Override
public List<String> getCommentsForResource(String resourceURI) {
    List<String> commentList = new LinkedList<String>();
    OntModel ontModel = ontModelSelector.getFullModel();
    ontModel.enterCriticalSection(Lock.READ);
    try {
        OntResource res = ontModel.getOntResource(resourceURI);
        if (res != null) {
            ClosableIterator<RDFNode> closeIt = res.listComments(null);
            try {
                for (Iterator<RDFNode> commIt = closeIt; commIt.hasNext(); ) {
                    Literal lit = (Literal) commIt.next();
                    commentList.add(lit.getLexicalForm());
                }
            } finally {
                closeIt.close();
            }
        }
    } finally {
        ontModel.leaveCriticalSection();
    }
    return commentList;
}
Also used : Literal(org.apache.jena.rdf.model.Literal) OntModel(org.apache.jena.ontology.OntModel) OntResource(org.apache.jena.ontology.OntResource) LinkedList(java.util.LinkedList) RDFNode(org.apache.jena.rdf.model.RDFNode)

Example 19 with OntModel

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

the class WebappDaoFactoryJena method makeInMemoryDataset.

public static Dataset makeInMemoryDataset(Model assertions, Model inferences) {
    Dataset dataset = DatasetFactory.createMem();
    OntModel union = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
    if (assertions != null) {
        dataset.addNamedModel(ModelNames.ABOX_ASSERTIONS, assertions);
        union.addSubModel(assertions);
    }
    if (inferences != null) {
        dataset.addNamedModel(ModelNames.ABOX_INFERENCES, inferences);
        union.addSubModel(inferences);
    }
    dataset.setDefaultModel(union);
    return dataset;
}
Also used : Dataset(org.apache.jena.query.Dataset) OntModel(org.apache.jena.ontology.OntModel)

Example 20 with OntModel

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

the class WebappDaoFactoryJena method setSpecialDataModel.

/**
 * Method for using a special model, such as the display model, with the
 * WebappDaoFactory.  The goal here is to modify this WebappDaoFactory so
 * that it is using specialModel, specialTboxModel and specialDisplayModel
 * for individual editing.
 *
 * DAOs related to the application configuration and user accounts
 * should remain unchanged.
 */
public void setSpecialDataModel(OntModel specialModel, OntModel specialTboxModel, OntModel specialDisplayModel) {
    if (specialModel == null)
        throw new IllegalStateException("specialModel must not be null");
    // Can we get the "original" models here from somewhere?
    OntModelSelector originalSelector = this.getOntModelSelector();
    // Set up model selector for this special WDF
    // The selector is used by the object property DAO, therefore should be
    // set up even though we use the new webapp dao factory object to
    // generate portions to overwrite the regular webapp dao factory.
    // The WDF expects the full model in the OntModelSelect that has
    // both the ABox and TBox.  This is used to run SPARQL queries against.
    OntModel unionModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
    unionModel.addSubModel(specialModel);
    OntModelSelectorImpl specialSelector = new OntModelSelectorImpl();
    specialSelector.setFullModel(unionModel);
    // Keeping original application metadata model and adding special model.
    // Adding both  allows us to prevent errors in  ApplicationDao which may
    // depend on a specific individual from the regular application metadata
    // model to  pick the theme.   Adding the new  model would  take care of
    // special  situations  where  the switch  model  may  contain important
    // information.
    OntModel newApplicationModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
    newApplicationModel.add(specialModel);
    newApplicationModel.add(originalSelector.getApplicationMetadataModel());
    specialSelector.setApplicationMetadataModel(newApplicationModel);
    if (specialDisplayModel != null) {
        specialSelector.setDisplayModel(specialDisplayModel);
        unionModel.addSubModel(specialDisplayModel);
    } else {
        OntModel selectorDisplayModel = originalSelector.getDisplayModel();
        if (selectorDisplayModel != null) {
            specialSelector.setDisplayModel(originalSelector.getDisplayModel());
        }
    }
    if (specialTboxModel != null) {
        unionModel.addSubModel(specialTboxModel);
        specialSelector.setTBoxModel(specialTboxModel);
    } else {
        OntModel selectorTboxModel = originalSelector.getTBoxModel();
        if (selectorTboxModel != null) {
            specialSelector.setTBoxModel(originalSelector.getTBoxModel());
        }
    }
    specialSelector.setABoxModel(specialModel);
    specialSelector.setUserAccountsModel(specialModel);
    // Although we're only using part of the new wadf and copy over below,
    // the object property dao utilizes methods that will employ the display
    // model returned from the simple ontmodel selector, so if the object
    // property dao is to be copied over we need to ensure we have the
    // correct display model and tbox model.
    WebappDaoFactoryJena specialWadfj = new WebappDaoFactoryJena(specialSelector);
    entityWebappDao = specialWadfj.getIndividualDao();
    vClassGroupDao = specialWadfj.getVClassGroupDao();
    // To allow for testing, add a property group, this will allow
    // the unassigned group method section to be executed and main Image to
    // be assigned to that group.  Otherwise, the dummy group does not allow
    // for the unassigned group to be executed.
    propertyGroupDao = specialWadfj.getPropertyGroupDao();
    objectPropertyDao = specialWadfj.getObjectPropertyDao();
    objectPropertyStatementDao = specialWadfj.getObjectPropertyStatementDao();
    dataPropertyDao = specialWadfj.getDataPropertyDao();
    dataPropertyStatementDao = specialWadfj.getDataPropertyStatementDao();
    // Why can't we set the selector to be the same?
    ontModelSelector = specialSelector;
}
Also used : OntModel(org.apache.jena.ontology.OntModel)

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