Search in sources :

Example 41 with OntProperty

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

the class ObjectPropertyDaoJena method propertyFromOntProperty.

protected ObjectProperty propertyFromOntProperty(OntProperty op) {
    if (op == null) {
        return null;
    }
    if (op instanceof DatatypeProperty) {
        return null;
    }
    ObjectProperty p = new ObjectProperty();
    try {
        p.setURI(op.getURI());
        p.setNamespace(op.getNameSpace());
        p.setLocalName(op.getLocalName());
        p.setLocalNameWithPrefix(getWebappDaoFactory().makeLocalNameWithPrefix(p));
        if (op.getLabel(null) != null)
            p.setDomainPublic(getLabelOrId(op));
        else
            p.setDomainPublic(op.getLocalName());
        if (p.getDomainPublic() == null)
            p.setDomainPublic("[related to]");
        p.setPickListName(getWebappDaoFactory().makePickListName(p));
        if (op.getDomain() != null)
            p.setDomainVClassURI((op.getDomain().isAnon()) ? PSEUDO_BNODE_NS + op.getDomain().getId().toString() : op.getDomain().getURI());
        if (op.getRange() != null)
            p.setRangeVClassURI((op.getRange().isAnon()) ? PSEUDO_BNODE_NS + op.getRange().getId().toString() : op.getRange().getURI());
        OntProperty invOp = null;
        try {
            invOp = op.getInverse();
        } catch (ProfileException pe) {
        }
        try {
            if (op.getSuperProperty() != null)
                p.setParentURI(op.getSuperProperty().getURI());
        } catch (ConversionException ce) {
            StmtIterator parentStmtIt = op.listProperties(RDFS.subPropertyOf);
            if (parentStmtIt.hasNext()) {
                Statement parentStmt = parentStmtIt.nextStatement();
                if (parentStmt.getObject().isResource()) {
                    p.setParentURI(((Resource) parentStmt.getObject()).getURI());
                }
            }
            parentStmtIt.close();
        }
        if (invOp != null) {
            p.setURIInverse(invOp.getURI());
            p.setNamespaceInverse(invOp.getNameSpace());
            p.setLocalNameInverse(invOp.getLocalName());
            p.setRangePublic(getLabelOrId(invOp));
        }
        try {
            if (op.isTransitiveProperty() || (invOp != null && invOp.isTransitiveProperty())) {
                p.setTransitive(true);
            }
        } catch (ProfileException pe) {
        }
        try {
            if (op.isSymmetricProperty() || (invOp != null && invOp.isSymmetricProperty())) {
                p.setSymmetric(true);
            }
        } catch (ProfileException pe) {
        }
        try {
            if (op.isFunctionalProperty()) {
                p.setFunctional(true);
            }
        } catch (ProfileException pe) {
        }
        try {
            if (op.isInverseFunctionalProperty()) {
                p.setInverseFunctional(true);
            }
        } catch (ProfileException pe) {
        }
        p.setExample(getPropertyStringValue(op, EXAMPLE_ANNOT));
        p.setDescription(getPropertyStringValue(op, DESCRIPTION_ANNOT));
        p.setPublicDescription(getPropertyStringValue(op, PUBLIC_DESCRIPTION_ANNOT));
        p.setDomainDisplayTier(getPropertyNonNegativeIntegerValue(op, DISPLAY_RANK_ANNOT));
        p.setRangeDisplayTier(getPropertyNonNegativeIntegerValue(invOp, DISPLAY_RANK_ANNOT));
        p.setDomainDisplayLimit(getPropertyNonNegativeIntValue(op, DISPLAY_LIMIT));
        p.setRangeDisplayLimit(getPropertyNonNegativeIntValue(invOp, DISPLAY_LIMIT));
        RDFNode objectIndividualSortPropertyNode = op.getPropertyValue(PROPERTY_OBJECTINDIVIDUALSORTPROPERTY);
        if (objectIndividualSortPropertyNode instanceof Resource) {
            p.setObjectIndividualSortPropertyURI(((Resource) objectIndividualSortPropertyNode).getURI());
        }
        p.setDomainEntitySortDirection(getPropertyStringValue(op, PROPERTY_ENTITYSORTDIRECTION));
        p.setRangeEntitySortDirection(getPropertyStringValue(invOp, PROPERTY_ENTITYSORTDIRECTION));
        // There might be multiple HIDDEN_FROM_DISPLAY_BELOW_ROLE_LEVEL_ANNOT properties, only use the highest
        StmtIterator it = op.listProperties(HIDDEN_FROM_DISPLAY_BELOW_ROLE_LEVEL_ANNOT);
        BaseResourceBean.RoleLevel hiddenRoleLevel = null;
        while (it.hasNext()) {
            Statement stmt = it.nextStatement();
            RDFNode obj;
            if (stmt != null && (obj = stmt.getObject()) != null && obj.isURIResource()) {
                Resource res = obj.as(Resource.class);
                if (res != null && res.getURI() != null) {
                    BaseResourceBean.RoleLevel roleFromModel = BaseResourceBean.RoleLevel.getRoleByUri(res.getURI());
                    if (roleFromModel != null && (hiddenRoleLevel == null || roleFromModel.compareTo(hiddenRoleLevel) > 0)) {
                        hiddenRoleLevel = roleFromModel;
                    }
                }
            }
        }
        // this might get set to null
        p.setHiddenFromDisplayBelowRoleLevel(hiddenRoleLevel);
        // There might be multiple PROHIBITED_FROM_UPDATE_BELOW_ROLE_LEVEL_ANNOT properties, only use the highest
        it = op.listProperties(PROHIBITED_FROM_UPDATE_BELOW_ROLE_LEVEL_ANNOT);
        BaseResourceBean.RoleLevel prohibitedRoleLevel = null;
        while (it.hasNext()) {
            Statement stmt = it.nextStatement();
            RDFNode obj;
            if (stmt != null && (obj = stmt.getObject()) != null && obj.isURIResource()) {
                Resource res = obj.as(Resource.class);
                if (res != null && res.getURI() != null) {
                    BaseResourceBean.RoleLevel roleFromModel = BaseResourceBean.RoleLevel.getRoleByUri(res.getURI());
                    if (roleFromModel != null && (prohibitedRoleLevel == null || roleFromModel.compareTo(prohibitedRoleLevel) > 0)) {
                        prohibitedRoleLevel = roleFromModel;
                    }
                }
            }
        }
        // this might get set to null
        p.setProhibitedFromUpdateBelowRoleLevel(prohibitedRoleLevel);
        // There might be multiple HIDDEN_FROM_PUBLISH_BELOW_ROLE_LEVEL_ANNOT properties, only use the highest
        it = op.listProperties(HIDDEN_FROM_PUBLISH_BELOW_ROLE_LEVEL_ANNOT);
        BaseResourceBean.RoleLevel publishRoleLevel = null;
        while (it.hasNext()) {
            Statement stmt = it.nextStatement();
            RDFNode obj;
            if (stmt != null && (obj = stmt.getObject()) != null && obj.isURIResource()) {
                Resource res = obj.as(Resource.class);
                if (res != null && res.getURI() != null) {
                    BaseResourceBean.RoleLevel roleFromModel = BaseResourceBean.RoleLevel.getRoleByUri(res.getURI());
                    if (roleFromModel != null && (publishRoleLevel == null || roleFromModel.compareTo(publishRoleLevel) > 0)) {
                        publishRoleLevel = roleFromModel;
                    }
                }
            }
        }
        // this might get set to null
        p.setHiddenFromPublishBelowRoleLevel(publishRoleLevel);
        p.setCustomEntryForm(getPropertyStringValue(op, PROPERTY_CUSTOMENTRYFORMANNOT));
        Boolean selectFromObj = getPropertyBooleanValue(op, PROPERTY_SELECTFROMEXISTINGANNOT);
        p.setSelectFromExisting(selectFromObj == null ? true : selectFromObj);
        Boolean offerCreateObj = getPropertyBooleanValue(op, PROPERTY_OFFERCREATENEWOPTIONANNOT);
        p.setOfferCreateNewOption(offerCreateObj == null ? false : offerCreateObj);
        Boolean dependencyDeletionObj = getPropertyBooleanValue(op, PROPERTY_STUBOBJECTPROPERTYANNOT);
        p.setStubObjectRelation(dependencyDeletionObj == null ? false : dependencyDeletionObj);
        Boolean collateBySubclass = getPropertyBooleanValue(op, PROPERTY_COLLATEBYSUBCLASSANNOT);
        p.setCollateBySubclass(collateBySubclass == null ? false : collateBySubclass);
        Boolean editLinkSuppressed = getPropertyBooleanValue(op, PROPERTY_EDITLINKSUPPRESSED);
        p.setEditLinkSuppressed(editLinkSuppressed == null ? false : editLinkSuppressed);
        Boolean addLinkSuppressed = getPropertyBooleanValue(op, PROPERTY_ADDLINKSUPPRESSED);
        p.setAddLinkSuppressed(addLinkSuppressed == null ? false : addLinkSuppressed);
        Boolean deleteLinkSuppressed = getPropertyBooleanValue(op, PROPERTY_DELETELINKSUPPRESSED);
        p.setDeleteLinkSuppressed(deleteLinkSuppressed == null ? false : deleteLinkSuppressed);
        Resource groupRes = (Resource) op.getPropertyValue(PROPERTY_INPROPERTYGROUPANNOT);
        if (groupRes != null) {
            p.setGroupURI(groupRes.getURI());
        }
    } catch (Throwable t) {
        log.error(t, t);
    }
    return p;
}
Also used : ObjectProperty(edu.cornell.mannlib.vitro.webapp.beans.ObjectProperty) ConversionException(org.apache.jena.ontology.ConversionException) StmtIterator(org.apache.jena.rdf.model.StmtIterator) BaseResourceBean(edu.cornell.mannlib.vitro.webapp.beans.BaseResourceBean) OntProperty(org.apache.jena.ontology.OntProperty) Statement(org.apache.jena.rdf.model.Statement) ObjectPropertyStatement(edu.cornell.mannlib.vitro.webapp.beans.ObjectPropertyStatement) OntResource(org.apache.jena.ontology.OntResource) Resource(org.apache.jena.rdf.model.Resource) ProfileException(org.apache.jena.ontology.ProfileException) DatatypeProperty(org.apache.jena.ontology.DatatypeProperty) RDFNode(org.apache.jena.rdf.model.RDFNode)

Example 42 with OntProperty

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

the class ObjectPropertyDaoJena method getObjectPropertyByURI.

public ObjectProperty getObjectPropertyByURI(String propertyURI) {
    if (propertyURI == null)
        return null;
    OntModel ontModel = getOntModel();
    OntModel localModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
    ontModel.enterCriticalSection(Lock.READ);
    try {
        Resource prop = ontModel.getResource(propertyURI);
        localModel.add(ontModel.listStatements(prop, null, (RDFNode) null));
        StmtIterator invit = ontModel.listStatements(prop, OWL.inverseOf, (RDFNode) null);
        while (invit.hasNext()) {
            Statement invSit = invit.nextStatement();
            if (invSit.getObject().isURIResource()) {
                Resource invProp = (Resource) invSit.getObject();
                localModel.add(ontModel.listStatements(invProp, null, (RDFNode) null));
            }
        }
        OntProperty op = localModel.getObjectProperty(propertyURI);
        return propertyFromOntProperty(op);
    } finally {
        ontModel.leaveCriticalSection();
    }
}
Also used : StmtIterator(org.apache.jena.rdf.model.StmtIterator) Statement(org.apache.jena.rdf.model.Statement) ObjectPropertyStatement(edu.cornell.mannlib.vitro.webapp.beans.ObjectPropertyStatement) OntProperty(org.apache.jena.ontology.OntProperty) OntResource(org.apache.jena.ontology.OntResource) Resource(org.apache.jena.rdf.model.Resource) OntModel(org.apache.jena.ontology.OntModel) RDFNode(org.apache.jena.rdf.model.RDFNode)

Example 43 with OntProperty

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

the class PropertyInstanceDaoJena method insertProp.

public int insertProp(PropertyInstanceIface prop, OntModelSelector oms) {
    OntModel ontModel = oms.getABoxModel();
    OntModel tboxModel = oms.getTBoxModel();
    ontModel.enterCriticalSection(Lock.WRITE);
    try {
        tboxModel.enterCriticalSection(Lock.READ);
        try {
            Resource subjRes = ontModel.getResource(prop.getSubjectEntURI());
            OntProperty pred = tboxModel.getOntProperty(prop.getPropertyURI());
            Resource objRes = ontModel.getResource(prop.getObjectEntURI());
            if ((subjRes != null) && (pred != null) && (objRes != null)) {
                getOntModel().getBaseModel().notifyEvent(new IndividualUpdateEvent(getWebappDaoFactory().getUserURI(), true, prop.getSubjectEntURI()));
                try {
                    ontModel.add(subjRes, pred, objRes);
                } finally {
                    getOntModel().getBaseModel().notifyEvent(new IndividualUpdateEvent(getWebappDaoFactory().getUserURI(), false, prop.getSubjectEntURI()));
                }
                OntProperty invPred = pred.getInverse();
                if (invPred != null) {
                    getOntModel().getBaseModel().notifyEvent(new IndividualUpdateEvent(getWebappDaoFactory().getUserURI(), true, prop.getObjectEntURI()));
                    try {
                        ontModel.add(objRes, invPred, subjRes);
                    } finally {
                        getOntModel().getBaseModel().notifyEvent(new IndividualUpdateEvent(getWebappDaoFactory().getUserURI(), false, prop.getSubjectEntURI()));
                    }
                }
            }
            return 0;
        } finally {
            tboxModel.leaveCriticalSection();
        }
    } finally {
        ontModel.leaveCriticalSection();
    }
}
Also used : OntProperty(org.apache.jena.ontology.OntProperty) Resource(org.apache.jena.rdf.model.Resource) OntModel(org.apache.jena.ontology.OntModel) IndividualUpdateEvent(edu.cornell.mannlib.vitro.webapp.dao.jena.event.IndividualUpdateEvent)

Example 44 with OntProperty

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

the class VClassDaoJena method getLabelForClass.

/* ************************************************** */
public String getLabelForClass(OntClass cls, boolean withPrefix, boolean forPickList) {
    cls.getModel().enterCriticalSection(Lock.READ);
    try {
        if (cls.isAnon()) {
            if (cls.isRestriction()) {
                Restriction rest = cls.asRestriction();
                OntProperty onProperty = rest.getOnProperty();
                StringBuilder labelStr = new StringBuilder();
                labelStr.append("restriction on ").append(getLabelOrId(onProperty)).append(": ");
                if (rest.isAllValuesFromRestriction() || rest.isSomeValuesFromRestriction()) {
                    Resource fillerRes = null;
                    if (rest.isAllValuesFromRestriction()) {
                        AllValuesFromRestriction avfRest = rest.asAllValuesFromRestriction();
                        fillerRes = avfRest.getAllValuesFrom();
                        labelStr.append("all values from ");
                    } else {
                        SomeValuesFromRestriction svfRest = rest.asSomeValuesFromRestriction();
                        fillerRes = svfRest.getSomeValuesFrom();
                        labelStr.append("some values from ");
                    }
                    if (fillerRes.canAs(OntClass.class)) {
                        OntClass avf = fillerRes.as(OntClass.class);
                        labelStr.append(getLabelForClass(avf, withPrefix, forPickList));
                    } else {
                        try {
                            labelStr.append(getLabelOrId(fillerRes.as(OntResource.class)));
                        } catch (Exception e) {
                            labelStr.append("???");
                        }
                    }
                } else if (rest.isHasValueRestriction()) {
                    HasValueRestriction hvRest = rest.asHasValueRestriction();
                    labelStr.append("has value ");
                    RDFNode fillerNode = hvRest.getHasValue();
                    try {
                        if (fillerNode.isResource()) {
                            labelStr.append(getLabelOrId(fillerNode.as(OntResource.class)));
                        } else {
                            labelStr.append(fillerNode.as(Literal.class).getLexicalForm());
                        }
                    } catch (Exception e) {
                        labelStr.append("???");
                    }
                } else if (rest.isMinCardinalityRestriction()) {
                    MinCardinalityRestriction mcRest = rest.asMinCardinalityRestriction();
                    labelStr.append("minimum cardinality ");
                    labelStr.append(mcRest.getMinCardinality());
                } else if (rest.isMaxCardinalityRestriction()) {
                    MaxCardinalityRestriction mcRest = rest.asMaxCardinalityRestriction();
                    labelStr.append("maximum cardinality ");
                    labelStr.append(mcRest.getMaxCardinality());
                } else if (rest.isCardinalityRestriction()) {
                    CardinalityRestriction cRest = rest.asCardinalityRestriction();
                    labelStr.append("cardinality ");
                    labelStr.append(cRest.getCardinality());
                }
                return labelStr.toString();
            } else if (isBooleanClassExpression(cls)) {
                StringBuilder labelStr = new StringBuilder("(");
                if (cls.isComplementClass()) {
                    labelStr.append("not ");
                    ComplementClass ccls = cls.as(ComplementClass.class);
                    labelStr.append(getLabelForClass(ccls.getOperand(), withPrefix, forPickList));
                } else if (cls.isIntersectionClass()) {
                    IntersectionClass icls = cls.as(IntersectionClass.class);
                    for (Iterator<? extends OntClass> operandIt = icls.listOperands(); operandIt.hasNext(); ) {
                        OntClass operand = operandIt.next();
                        labelStr.append(getLabelForClass(operand, withPrefix, forPickList));
                        if (operandIt.hasNext()) {
                            labelStr.append(" and ");
                        }
                    }
                } else if (cls.isUnionClass()) {
                    UnionClass icls = cls.as(UnionClass.class);
                    for (Iterator<? extends OntClass> operandIt = icls.listOperands(); operandIt.hasNext(); ) {
                        OntClass operand = operandIt.next();
                        labelStr.append(getLabelForClass(operand, withPrefix, forPickList));
                        if (operandIt.hasNext()) {
                            labelStr.append(" or ");
                        }
                    }
                }
                return labelStr.append(")").toString();
            } else {
                // but obviously we need to factor this out into the display layer.
                return "<a href=\"vclassEdit?uri=" + URLEncoder.encode(getClassURIStr(cls)) + "\">[anonymous class]</a>";
            }
        } else {
            if (withPrefix || forPickList) {
                OntologyDao oDao = getWebappDaoFactory().getOntologyDao();
                Ontology o = oDao.getOntologyByURI(cls.getNameSpace());
                if (o != null) {
                    if (withPrefix) {
                        return (o.getPrefix() == null ? (o.getName() == null ? "unspec:" + getLabelOrId(cls) : o.getName() + ":" + getLabelOrId(cls)) : o.getPrefix() + ":" + getLabelOrId(cls));
                    } else {
                        return (getLabelOrId(cls) + (o.getPrefix() == null ? (o.getName() == null ? " (unspec)" : " (" + o.getName() + ")") : " (" + o.getPrefix() + ")"));
                    }
                } else {
                    return getLabelOrId(cls);
                }
            }
            return getLabelOrId(cls);
        }
    } catch (Exception e) {
        log.error(e, e);
        return "???";
    } finally {
        cls.getModel().leaveCriticalSection();
    }
}
Also used : AllValuesFromRestriction(org.apache.jena.ontology.AllValuesFromRestriction) Ontology(edu.cornell.mannlib.vitro.webapp.beans.Ontology) MaxCardinalityRestriction(org.apache.jena.ontology.MaxCardinalityRestriction) OntProperty(org.apache.jena.ontology.OntProperty) OntResource(org.apache.jena.ontology.OntResource) Resource(org.apache.jena.rdf.model.Resource) OntResource(org.apache.jena.ontology.OntResource) ProfileException(org.apache.jena.ontology.ProfileException) InsertException(edu.cornell.mannlib.vitro.webapp.dao.InsertException) AllValuesFromRestriction(org.apache.jena.ontology.AllValuesFromRestriction) MinCardinalityRestriction(org.apache.jena.ontology.MinCardinalityRestriction) CardinalityRestriction(org.apache.jena.ontology.CardinalityRestriction) SomeValuesFromRestriction(org.apache.jena.ontology.SomeValuesFromRestriction) MaxCardinalityRestriction(org.apache.jena.ontology.MaxCardinalityRestriction) Restriction(org.apache.jena.ontology.Restriction) HasValueRestriction(org.apache.jena.ontology.HasValueRestriction) MinCardinalityRestriction(org.apache.jena.ontology.MinCardinalityRestriction) SomeValuesFromRestriction(org.apache.jena.ontology.SomeValuesFromRestriction) IntersectionClass(org.apache.jena.ontology.IntersectionClass) Literal(org.apache.jena.rdf.model.Literal) MinCardinalityRestriction(org.apache.jena.ontology.MinCardinalityRestriction) CardinalityRestriction(org.apache.jena.ontology.CardinalityRestriction) MaxCardinalityRestriction(org.apache.jena.ontology.MaxCardinalityRestriction) OntologyDao(edu.cornell.mannlib.vitro.webapp.dao.OntologyDao) ComplementClass(org.apache.jena.ontology.ComplementClass) UnionClass(org.apache.jena.ontology.UnionClass) ClosableIterator(org.apache.jena.util.iterator.ClosableIterator) StmtIterator(org.apache.jena.rdf.model.StmtIterator) ResIterator(org.apache.jena.rdf.model.ResIterator) Iterator(java.util.Iterator) OntClass(org.apache.jena.ontology.OntClass) HasValueRestriction(org.apache.jena.ontology.HasValueRestriction) RDFNode(org.apache.jena.rdf.model.RDFNode)

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