Search in sources :

Example 11 with OntProperty

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

the class SimpleReasonerInversePropertyTest method addABoxAssertion1.

/*
	* basic scenarios around adding abox data
	*
	* Create a Tbox with property P inverseOf property Q.
    * Add a statement a P b, and verify that b Q a is inferred.
    * Add a statement c Q d and verify that d Q c is inferred.
	*/
public void addABoxAssertion1(boolean sameAs) {
    // set up the tbox
    OntModel tBox = createTBoxModel();
    OntProperty P = createObjectProperty(tBox, "http://test.vivo/P", "property P");
    OntProperty Q = createObjectProperty(tBox, "http://test.vivo/Q", "property Q");
    setInverse(P, Q);
    // this is the model to receive abox inferences
    Model inf = ModelFactory.createDefaultModel();
    // create an abox and register the SimpleReasoner listener with it
    OntModel aBox = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
    SimpleReasoner sr = new SimpleReasoner(tBox, aBox, inf);
    sr.setSameAsEnabled(sameAs);
    aBox.register(sr);
    // add assertions to the abox 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");
    aBox.add(a, P, b);
    Assert.assertTrue(inf.contains(b, Q, a));
    aBox.add(c, Q, d);
    Assert.assertTrue(inf.contains(d, P, c));
    // delete assertions and verify that inferences go away
    aBox.remove(c, Q, d);
    Assert.assertFalse(inf.contains(d, P, c));
    aBox.remove(a, P, b);
    Assert.assertFalse(inf.contains(b, Q, a));
}
Also used : OntProperty(org.apache.jena.ontology.OntProperty) 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)

Example 12 with OntProperty

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

the class SimpleReasonerInversePropertyTest method addABoxAssertion2.

/*
	 * don't infer statements already in the abox
	 * (never infer because it's in the abox already)
	*/
public void addABoxAssertion2(boolean sameAs) {
    // set up the tbox
    OntModel tBox = createTBoxModel();
    OntProperty P = createObjectProperty(tBox, "http://test.vivo/P", "property P");
    OntProperty Q = createObjectProperty(tBox, "http://test.vivo/Q", "property Q");
    setInverse(P, Q);
    // this is the model to receive abox inferences
    Model inf = ModelFactory.createDefaultModel();
    // create an ABox and add data (no inferencing happening yet)
    OntModel aBox = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
    // Individuals a, b, c and d
    Resource a = aBox.createResource("http://test.vivo/a");
    Resource b = aBox.createResource("http://test.vivo/b");
    aBox.add(b, Q, a);
    // register SimpleReasoner
    aBox.register(new SimpleReasoner(tBox, aBox, inf));
    Assert.assertFalse(inf.contains(b, Q, a));
    // add data and verify inferences
    aBox.add(a, P, b);
    Assert.assertFalse(inf.contains(b, Q, a));
}
Also used : OntProperty(org.apache.jena.ontology.OntProperty) 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)

Example 13 with OntProperty

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

the class RestrictionOperationController method processCreate.

private void processCreate(VitroRequest request, EditProcessObject epo, OntModel origModel) {
    Model temp = ModelFactory.createDefaultModel();
    Model dynamicUnion = ModelFactory.createUnion(temp, origModel);
    OntModel ontModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM, dynamicUnion);
    OntProperty onProperty = ontModel.getOntProperty((String) request.getParameter("onProperty"));
    String conditionTypeStr = request.getParameter("conditionType");
    String restrictionTypeStr = (String) epo.getAttribute("restrictionType");
    Restriction rest = null;
    OntClass ontClass = ontModel.getOntClass((String) epo.getAttribute("VClassURI"));
    String roleFillerURIStr = request.getParameter("ValueClass");
    Resource roleFiller = null;
    if (roleFillerURIStr != null) {
        roleFiller = ontModel.getResource(roleFillerURIStr);
    }
    int cardinality = -1;
    String cardinalityStr = request.getParameter("cardinality");
    if (cardinalityStr != null) {
        cardinality = Integer.decode(cardinalityStr);
    }
    switch(restrictionTypeStr) {
        case "allValuesFrom":
            rest = ontModel.createAllValuesFromRestriction(null, onProperty, roleFiller);
            break;
        case "someValuesFrom":
            rest = ontModel.createSomeValuesFromRestriction(null, onProperty, roleFiller);
            break;
        case "hasValue":
            String valueURI = request.getParameter("ValueIndividual");
            if (valueURI != null) {
                Resource valueRes = ontModel.getResource(valueURI);
                if (valueRes != null) {
                    rest = ontModel.createHasValueRestriction(null, onProperty, valueRes);
                }
            } else {
                String valueLexicalForm = request.getParameter("ValueLexicalForm");
                if (valueLexicalForm != null) {
                    String valueDatatype = request.getParameter("ValueDatatype");
                    Literal value = null;
                    if (valueDatatype != null && valueDatatype.length() > 0) {
                        RDFDatatype dtype = null;
                        try {
                            dtype = TypeMapper.getInstance().getSafeTypeByName(valueDatatype);
                        } catch (Exception e) {
                            log.warn("Unable to get safe type " + valueDatatype + " using TypeMapper");
                        }
                        if (dtype != null) {
                            value = ontModel.createTypedLiteral(valueLexicalForm, dtype);
                        } else {
                            value = ontModel.createLiteral(valueLexicalForm);
                        }
                    } else {
                        value = ontModel.createLiteral(valueLexicalForm);
                    }
                    rest = ontModel.createHasValueRestriction(null, onProperty, value);
                }
            }
            break;
        case "minCardinality":
            rest = ontModel.createMinCardinalityRestriction(null, onProperty, cardinality);
            break;
        case "maxCardinality":
            rest = ontModel.createMaxCardinalityRestriction(null, onProperty, cardinality);
            break;
        case "cardinality":
            rest = ontModel.createCardinalityRestriction(null, onProperty, cardinality);
            break;
    }
    if (conditionTypeStr.equals("necessary")) {
        ontClass.addSuperClass(rest);
    } else if (conditionTypeStr.equals("necessaryAndSufficient")) {
        ontClass.addEquivalentClass(rest);
    }
    origModel.add(temp);
}
Also used : Restriction(org.apache.jena.ontology.Restriction) OntProperty(org.apache.jena.ontology.OntProperty) Literal(org.apache.jena.rdf.model.Literal) 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) RDFDatatype(org.apache.jena.datatypes.RDFDatatype)

Example 14 with OntProperty

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

the class ObjectPropertyDaoJena method getObjectPropertiesForObjectPropertyStatements.

public List<ObjectProperty> getObjectPropertiesForObjectPropertyStatements(List<ObjectPropertyStatement> objPropertyStmts) {
    if (objPropertyStmts == null || objPropertyStmts.size() < 1)
        return new ArrayList();
    HashMap<String, ObjectProperty> hash = new HashMap<String, ObjectProperty>();
    String uris = "";
    getOntModel().enterCriticalSection(Lock.READ);
    try {
        for (ObjectPropertyStatement objPropertyStmt : objPropertyStmts) {
            if (hash.containsKey(objPropertyStmt.getPropertyURI())) {
                ObjectProperty p = hash.get(objPropertyStmt.getPropertyURI());
                p.addObjectPropertyStatement(objPropertyStmt);
            } else {
                OntProperty op = getOntModel().getOntProperty(objPropertyStmt.getPropertyURI());
                if (op != null) {
                    ObjectProperty p = propertyFromOntProperty(op);
                    hash.put(p.getURI(), p);
                    p.addObjectPropertyStatement(objPropertyStmt);
                }
            }
        }
        List<ObjectProperty> props = new ArrayList<ObjectProperty>();
        for (String key : hash.keySet()) {
            props.add(hash.get(key));
        }
        return props;
    } finally {
        getOntModel().leaveCriticalSection();
    }
}
Also used : ObjectProperty(edu.cornell.mannlib.vitro.webapp.beans.ObjectProperty) HashMap(java.util.HashMap) OntProperty(org.apache.jena.ontology.OntProperty) ArrayList(java.util.ArrayList) ObjectPropertyStatement(edu.cornell.mannlib.vitro.webapp.beans.ObjectPropertyStatement)

Example 15 with OntProperty

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

the class PropertyDaoJena method getSuperPropertyURIs.

@Override
public List<String> getSuperPropertyURIs(String propertyURI, boolean direct) {
    List<String> supURIs = new LinkedList<String>();
    getOntModel().enterCriticalSection(Lock.READ);
    try {
        Iterator supIt = getOntModel().getOntProperty(propertyURI).listSuperProperties(direct);
        while (supIt.hasNext()) {
            try {
                OntProperty prop = (OntProperty) supIt.next();
                supURIs.add(prop.getURI());
            } catch (Exception cce) {
            }
        }
    } catch (Exception e) {
        log.error("Failed to get super-properties for " + propertyURI, e);
    } finally {
        getOntModel().leaveCriticalSection();
    }
    return supURIs;
}
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)

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