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;
}
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;
}
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);
}
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;
}
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;
}
Aggregations