Search in sources :

Example 16 with OntProperty

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

the class PropertyDaoJena method getAllPropInstByVClasses.

public List<PropertyInstance> getAllPropInstByVClasses(List<VClass> vclasses) {
    List<PropertyInstance> propInsts = new ArrayList<PropertyInstance>();
    if (vclasses == null || vclasses.isEmpty()) {
        return propInsts;
    }
    // Removed: see VIVO-766.
    // Collections.sort(vclasses, new VClassHierarchyRanker(this.getWebappDaoFactory().getVClassDao()));
    OntModel ontModel = getOntModelSelector().getTBoxModel();
    try {
        ontModel.enterCriticalSection(Lock.READ);
        // map object property URI to an array of two resources:
        // the first is the "allValuesFrom" resource and the second is
        // "someValuesFrom"
        Map<String, Resource[]> applicableProperties = new HashMap<String, Resource[]>();
        try {
            for (VClass vclass : vclasses) {
                if (vclass.isAnonymous()) {
                    continue;
                }
                String VClassURI = vclass.getURI();
                OntClass ontClass = getOntClass(ontModel, VClassURI);
                if (ontClass == null) {
                    continue;
                }
                List<Restriction> relatedRestrictions = getRelatedRestrictions(ontClass);
                for (Restriction rest : relatedRestrictions) {
                    // find properties in restrictions
                    // TODO: check if restriction is something like
                    // maxCardinality 0 or allValuesFrom owl:Nothing,
                    // in which case the property is NOT applicable!
                    OntProperty onProperty = rest.getOnProperty();
                    if (onProperty != null) {
                        Resource[] ranges = new Resource[2];
                        if (rest.isAllValuesFromRestriction()) {
                            ranges[0] = (rest.asAllValuesFromRestriction()).getAllValuesFrom();
                            updatePropertyRangeMap(applicableProperties, onProperty.getURI(), ranges, true);
                        } else if (rest.isSomeValuesFromRestriction()) {
                            ranges[1] = (rest.asSomeValuesFromRestriction()).getSomeValuesFrom();
                            updatePropertyRangeMap(applicableProperties, onProperty.getURI(), ranges, false);
                        }
                    }
                }
                List<Resource> propertyList = getPropertiesWithAppropriateDomainFor(VClassURI);
                for (Resource prop : propertyList) {
                    if (prop.getNameSpace() != null && !NONUSER_NAMESPACES.contains(prop.getNameSpace())) {
                        StmtIterator rangeSit = prop.listProperties(RDFS.range);
                        Resource rangeRes = null;
                        while (rangeSit.hasNext()) {
                            Statement s = rangeSit.nextStatement();
                            if (s.getObject().isURIResource()) {
                                rangeRes = (Resource) s.getObject();
                            }
                        }
                        Resource[] ranges = new Resource[2];
                        ranges[0] = rangeRes;
                        updatePropertyRangeMap(applicableProperties, prop.getURI(), ranges, false);
                    }
                }
            }
        } catch (Exception e) {
            log.error("Unable to get applicable properties " + "by examining property restrictions and domains", e);
        }
        // make the PropertyInstance objects
        for (String propertyURI : applicableProperties.keySet()) {
            OntProperty op = ontModel.getOntProperty(propertyURI);
            if (op == null) {
                continue;
            }
            Resource[] foundRanges = applicableProperties.get(propertyURI);
            Resource rangeRes = (foundRanges[0] != null) ? foundRanges[0] : (op.getRange() == null && foundRanges[1] != null) ? foundRanges[1] : op.getRange();
            Resource domainRes = op.getDomain();
            propInsts.add(getPropInst(op, domainRes, rangeRes));
            List<FullPropertyKey> additionalFauxSubpropertyKeys = getAdditionalFauxSubpropertyKeysForPropertyURI(propertyURI);
            for (FullPropertyKey fauxSubpropertyKey : additionalFauxSubpropertyKeys) {
                boolean applicablePropInst = false;
                if (rangeRes == null || !getWebappDaoFactory().getVClassDao().isSubClassOf(rangeRes.getURI(), fauxSubpropertyKey.getRangeUri())) {
                    if (fauxSubpropertyKey.getDomainUri() == null) {
                        applicablePropInst = true;
                    } else {
                        for (VClass vclass : vclasses) {
                            if (vclass.getURI() != null && vclass.getURI().equals(fauxSubpropertyKey.getDomainUri())) {
                                applicablePropInst = true;
                                break;
                            }
                        }
                    }
                    if (applicablePropInst) {
                        propInsts.add(getPropInst(op, ResourceFactory.createResource(fauxSubpropertyKey.getDomainUri()), ResourceFactory.createResource(fauxSubpropertyKey.getRangeUri())));
                    }
                }
            }
        }
    } finally {
        ontModel.leaveCriticalSection();
    }
    // add any faux properties with applicable domain where the predicate URI
    // is not already on the list
    List<ObjectProperty> stragglers = getAdditionalFauxSubpropertiesForVClasses(vclasses, propInsts);
    for (ObjectProperty op : stragglers) {
        propInsts.add(makePropInst(op));
    }
    return propInsts;
}
Also used : ObjectProperty(edu.cornell.mannlib.vitro.webapp.beans.ObjectProperty) VClass(edu.cornell.mannlib.vitro.webapp.beans.VClass) StmtIterator(org.apache.jena.rdf.model.StmtIterator) HashMap(java.util.HashMap) OntProperty(org.apache.jena.ontology.OntProperty) Statement(org.apache.jena.rdf.model.Statement) ArrayList(java.util.ArrayList) Resource(org.apache.jena.rdf.model.Resource) PropertyInstance(edu.cornell.mannlib.vitro.webapp.beans.PropertyInstance) RDFServiceException(edu.cornell.mannlib.vitro.webapp.rdfservice.RDFServiceException) Restriction(org.apache.jena.ontology.Restriction) OntModel(org.apache.jena.ontology.OntModel) OntClass(org.apache.jena.ontology.OntClass)

Example 17 with OntProperty

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

the class PropertyDaoJena method getSubPropertyURIs.

@Override
public List<String> getSubPropertyURIs(String propertyURI) {
    List<String> subURIs = new LinkedList<String>();
    getOntModel().enterCriticalSection(Lock.READ);
    try {
        Iterator subIt = getOntModel().getOntProperty(propertyURI).listSubProperties(true);
        while (subIt.hasNext()) {
            try {
                OntProperty prop = (OntProperty) subIt.next();
                subURIs.add(prop.getURI());
            } catch (Exception cce) {
            }
        }
    } catch (Exception e) {
        log.error(e, e);
    } finally {
        getOntModel().leaveCriticalSection();
    }
    return subURIs;
}
Also used : OntProperty(org.apache.jena.ontology.OntProperty) StmtIterator(org.apache.jena.rdf.model.StmtIterator) Iterator(java.util.Iterator) LinkedList(java.util.LinkedList) RDFServiceException(edu.cornell.mannlib.vitro.webapp.rdfservice.RDFServiceException)

Example 18 with OntProperty

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

the class JenaBaseDaoTest method testPreventInvalidRestrictionsOnDeletion.

@Test
public /**
 * Test that removing classes or properties used in restrictions
 * does not leave behind broken, syntactically-invalid restrictions.
 * The restrictions should be deleted.
 */
void testPreventInvalidRestrictionsOnDeletion() {
    OntModel m = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
    WebappDaoFactoryJena wadf = new WebappDaoFactoryJena(m);
    String ns = "http://example.org/ontology/";
    String class1URI = ns + "Class1";
    String class2URI = ns + "Class2";
    String propURI = ns + "property";
    OntClass class1 = m.createClass(class1URI);
    OntClass class2 = m.createClass(class2URI);
    OntProperty prop = m.createObjectProperty(propURI);
    Restriction rest = m.createAllValuesFromRestriction(null, prop, class2);
    class1.addSuperClass(rest);
    ObjectProperty op = wadf.getObjectPropertyDao().getObjectPropertyByURI(propURI);
    wadf.getObjectPropertyDao().deleteObjectProperty(op);
    Assert.assertEquals(class1.listSuperClasses().toSet().size(), 0);
    // just rdf:type owl:Class for Class1 and Class2
    Assert.assertEquals(m.size(), 2);
    prop = m.createObjectProperty(propURI);
    rest = m.createAllValuesFromRestriction(null, prop, class2);
    class1.addSuperClass(rest);
    VClass vclass = wadf.getVClassDao().getVClassByURI(class2URI);
    wadf.getVClassDao().deleteVClass(vclass);
    Assert.assertEquals(class1.listSuperClasses().toSet().size(), 0);
    // just rdf:type for Class1 and Prop
    Assert.assertEquals(m.size(), 2);
}
Also used : ObjectProperty(edu.cornell.mannlib.vitro.webapp.beans.ObjectProperty) Restriction(org.apache.jena.ontology.Restriction) VClass(edu.cornell.mannlib.vitro.webapp.beans.VClass) OntProperty(org.apache.jena.ontology.OntProperty) OntModel(org.apache.jena.ontology.OntModel) OntClass(org.apache.jena.ontology.OntClass) Test(org.junit.Test)

Example 19 with OntProperty

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

the class SimpleReasoner method entailedStatement.

/**
 * Returns true if the triple is entailed by inverse property
 * reasoning or sameAs reasoning; otherwise returns false.
 */
protected boolean entailedStatement(Statement stmt) {
    // TODO think about checking class subsumption here (for convenience)
    // Inverse properties
    List<OntProperty> inverses = getInverseProperties(stmt);
    Iterator<OntProperty> iIter = inverses.iterator();
    if (iIter.hasNext()) {
        aboxModel.enterCriticalSection(Lock.READ);
        try {
            while (iIter.hasNext()) {
                Property invProp = iIter.next();
                Statement invStmt = ResourceFactory.createStatement(stmt.getObject().asResource(), invProp, stmt.getSubject());
                if (aboxModel.contains(invStmt)) {
                    return true;
                }
            }
        } finally {
            aboxModel.leaveCriticalSection();
        }
    }
    // individuals sameAs each other
    if (doSameAs) {
        List<Resource> sameIndividuals = getSameIndividuals(stmt.getSubject().asResource(), inferenceModel);
        Iterator<Resource> rIter = sameIndividuals.iterator();
        if (rIter.hasNext()) {
            aboxModel.enterCriticalSection(Lock.READ);
            try {
                while (rIter.hasNext()) {
                    Resource subject = rIter.next();
                    if (aboxModel.contains(subject, stmt.getPredicate(), stmt.getObject())) {
                        return true;
                    }
                }
            } finally {
                aboxModel.leaveCriticalSection();
            }
        }
    }
    return false;
}
Also used : OntProperty(org.apache.jena.ontology.OntProperty) Statement(org.apache.jena.rdf.model.Statement) Resource(org.apache.jena.rdf.model.Resource) AnnotationProperty(org.apache.jena.ontology.AnnotationProperty) OntProperty(org.apache.jena.ontology.OntProperty) Property(org.apache.jena.rdf.model.Property)

Example 20 with OntProperty

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

the class SimpleReasoner method getInverseProperties.

/**
 * Returns a list of properties that are inverses of the property
 * in the given statement.
 */
protected List<OntProperty> getInverseProperties(Statement stmt) {
    List<OntProperty> inverses = new ArrayList<OntProperty>();
    if (stmt.getSubject().isAnon() || stmt.getObject().isAnon()) {
        return inverses;
    }
    tboxModel.enterCriticalSection(Lock.READ);
    try {
        OntProperty prop = tboxModel.getOntProperty(stmt.getPredicate().getURI());
        if (prop != null) {
            if (!prop.isObjectProperty()) {
                return inverses;
            }
            if (!stmt.getObject().isResource()) {
                log.debug("The predicate of this statement is an object property, " + "but the object is not a resource.");
                return inverses;
            }
            // not reasoning on properties in the OWL, RDF or RDFS namespace
            if ((prop.getNameSpace()).equals(OWL.NS) || (prop.getNameSpace()).equals(RDFS.getURI()) || (prop.getNameSpace()).equals(RDF.getURI())) {
                return inverses;
            }
            ExtendedIterator<? extends OntProperty> iter = prop.listInverse();
            while (iter.hasNext()) {
                OntProperty invProp = iter.next();
                if ((invProp.getNameSpace()).equals(OWL.NS) || (invProp.getNameSpace()).equals(RDFS.getURI()) || (invProp.getNameSpace()).equals(RDF.getURI())) {
                    continue;
                }
                inverses.add(invProp);
            }
        }
    } finally {
        tboxModel.leaveCriticalSection();
    }
    return inverses;
}
Also used : OntProperty(org.apache.jena.ontology.OntProperty) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Aggregations

OntProperty (org.apache.jena.ontology.OntProperty)44 Resource (org.apache.jena.rdf.model.Resource)34 OntModel (org.apache.jena.ontology.OntModel)31 Model (org.apache.jena.rdf.model.Model)25 StmtIterator (org.apache.jena.rdf.model.StmtIterator)13 Literal (org.apache.jena.rdf.model.Literal)12 Test (org.junit.Test)10 Statement (org.apache.jena.rdf.model.Statement)9 RDFNode (org.apache.jena.rdf.model.RDFNode)8 ArrayList (java.util.ArrayList)7 OntClass (org.apache.jena.ontology.OntClass)7 OntResource (org.apache.jena.ontology.OntResource)7 Iterator (java.util.Iterator)6 ObjectPropertyStatement (edu.cornell.mannlib.vitro.webapp.beans.ObjectPropertyStatement)5 LinkedList (java.util.LinkedList)5 ProfileException (org.apache.jena.ontology.ProfileException)5 Restriction (org.apache.jena.ontology.Restriction)5 ObjectProperty (edu.cornell.mannlib.vitro.webapp.beans.ObjectProperty)4 InsertException (edu.cornell.mannlib.vitro.webapp.dao.InsertException)4 RDFServiceException (edu.cornell.mannlib.vitro.webapp.rdfservice.RDFServiceException)4