Search in sources :

Example 1 with ExprEvalTypeException

use of org.apache.jena.sparql.expr.ExprEvalTypeException in project jena by apache.

the class NodeValueOps method divisionNV.

public static NodeValue divisionNV(NodeValue nv1, NodeValue nv2) {
    ValueSpaceClassification vs1 = nv1.getValueSpace();
    ValueSpaceClassification vs2 = nv2.getValueSpace();
    if (vs1.equals(VSPACE_NUM) && vs2.equals(VSPACE_NUM))
        return XSDFuncOp.numDivide(nv1, nv2);
    throw new ExprEvalTypeException("Operator '/' : Undefined division: " + nv1 + " and " + nv2);
}
Also used : ExprEvalTypeException(org.apache.jena.sparql.expr.ExprEvalTypeException) ValueSpaceClassification(org.apache.jena.sparql.expr.ValueSpaceClassification)

Example 2 with ExprEvalTypeException

use of org.apache.jena.sparql.expr.ExprEvalTypeException in project jena by apache.

the class NodeValueOps method additionNV.

public static NodeValue additionNV(NodeValue nv1, NodeValue nv2) {
    ValueSpaceClassification vs1 = nv1.getValueSpace();
    ValueSpaceClassification vs2 = nv2.getValueSpace();
    if (vs1.equals(VSPACE_NUM) && vs2.equals(VSPACE_NUM))
        return XSDFuncOp.numAdd(nv1, nv2);
    if (vs1.equals(VSPACE_STRING) && vs2.equals(VSPACE_STRING))
        return NodeValue.makeString(nv1.asString() + nv2.asString());
    if (vs1.equals(VSPACE_DURATION) && vs2.equals(VSPACE_DURATION)) {
        // A lot of testing to keep it as derived types.
        boolean isDTDur = dtXSDdayTimeDuration.equals(nv1.getDatatypeURI()) && dtXSDdayTimeDuration.equals(nv2.getDatatypeURI());
        boolean isYMDur = dtXSDyearMonthDuration.equals(nv1.getDatatypeURI()) && dtXSDyearMonthDuration.equals(nv2.getDatatypeURI());
        Duration d3 = nv1.getDuration().add(nv2.getDuration());
        String lex = d3.toString();
        Node n;
        if (isDTDur)
            n = NodeFactoryExtra.createLiteralNode(lex, null, dtXSDdayTimeDuration);
        else if (isYMDur)
            n = NodeFactoryExtra.createLiteralNode(lex, null, dtXSDyearMonthDuration);
        else
            n = org.apache.jena.graph.NodeFactory.createLiteral(lex, XSDDatatype.XSDduration);
        return NodeValue.makeNodeDuration(d3, n);
    }
    // Loose style. Add any duration to any date or time value.
    if (vs1.equals(VSPACE_DATETIME) && vs2.equals(VSPACE_DURATION)) {
        XMLGregorianCalendar cal = nv1.getDateTime();
        XMLGregorianCalendar result = xsd_add(cal, nv2.getDuration());
        NodeValue r = NodeValue.makeDateTime(result);
        return r;
    }
    // Loose style. Add any duration to any date or time value.
    if (vs1.equals(VSPACE_DATE) && vs2.equals(VSPACE_DURATION)) {
        XMLGregorianCalendar cal = nv1.getDateTime();
        XMLGregorianCalendar result = xsd_add(cal, nv2.getDuration());
        NodeValue r = NodeValue.makeDate(result);
        return r;
    }
    // Loose style. Add any duration to any date or time value.
    if (vs1.equals(VSPACE_TIME) && vs2.equals(VSPACE_DURATION)) {
        // ONLY dayTime.
        XMLGregorianCalendar cal = nv1.getDateTime();
        XMLGregorianCalendar result = xsd_add(cal, nv2.getDuration());
        NodeValue r = NodeValue.makeNode(result.toXMLFormat(), XSDDatatype.XSDtime);
        return r;
    }
    if (isDT(vs2) && vs1.equals(VSPACE_DURATION))
        // Carefully ...
        return additionNV(nv2, nv1);
    throw new ExprEvalTypeException("Operator '+' : Undefined addition: " + nv1 + " and " + nv2);
}
Also used : XMLGregorianCalendar(javax.xml.datatype.XMLGregorianCalendar) NodeValue(org.apache.jena.sparql.expr.NodeValue) ExprEvalTypeException(org.apache.jena.sparql.expr.ExprEvalTypeException) Node(org.apache.jena.graph.Node) Duration(javax.xml.datatype.Duration) ValueSpaceClassification(org.apache.jena.sparql.expr.ValueSpaceClassification)

Example 3 with ExprEvalTypeException

use of org.apache.jena.sparql.expr.ExprEvalTypeException in project jena by apache.

the class NodeValueOps method subtractionNV.

// NodeFunctions
public static NodeValue subtractionNV(NodeValue nv1, NodeValue nv2) {
    ValueSpaceClassification vs1 = nv1.getValueSpace();
    ValueSpaceClassification vs2 = nv2.getValueSpace();
    if (vs1.equals(VSPACE_NUM) && vs2.equals(VSPACE_NUM))
        return XSDFuncOp.numSubtract(nv1, nv2);
    if (vs1.equals(VSPACE_DURATION) && vs2.equals(VSPACE_DURATION)) {
        // A lot of testing to keep it as derived types.
        boolean isDTDur = dtXSDdayTimeDuration.equals(nv1.getDatatypeURI()) && dtXSDdayTimeDuration.equals(nv2.getDatatypeURI());
        boolean isYMDur = dtXSDyearMonthDuration.equals(nv1.getDatatypeURI()) && dtXSDyearMonthDuration.equals(nv2.getDatatypeURI());
        Duration d3 = nv1.getDuration().subtract(nv2.getDuration());
        String lex = d3.toString();
        Node n;
        if (isDTDur)
            n = NodeFactoryExtra.createLiteralNode(lex, null, dtXSDdayTimeDuration);
        else if (isYMDur)
            n = NodeFactoryExtra.createLiteralNode(lex, null, dtXSDyearMonthDuration);
        else
            n = org.apache.jena.graph.NodeFactory.createLiteral(lex, XSDDatatype.XSDduration);
        return NodeValue.makeNodeDuration(d3, n);
    }
    if (isDT(vs1) && isDT(vs2)) {
        XMLGregorianCalendar cal1 = nv1.getDateTime();
        XMLGregorianCalendar cal2 = nv2.getDateTime();
        boolean isDef1 = (cal1.getTimezone() == FIELD_UNDEFINED);
        boolean isDef2 = (cal2.getTimezone() == FIELD_UNDEFINED);
        if ((isDef1 && !isDef2) || (!isDef1 && isDef2))
            throw new ExprEvalTypeException("Operator '-': can't substract timezone/non-timezone values");
        // Inspect duration and force to better type? xsd:dayTimeDuration
        return NodeValue.makeDuration(xsd_substract(cal1, cal2));
    }
    // Loose style. Subtract any duration to any date or time value.
    if (vs1.equals(VSPACE_DATETIME) && vs2.equals(VSPACE_DURATION)) {
        XMLGregorianCalendar cal = nv1.getDateTime();
        // add-negation
        XMLGregorianCalendar result = xsd_subtract(cal, nv2.getDuration());
        NodeValue r = NodeValue.makeDateTime(result);
        return r;
    }
    if (vs1.equals(VSPACE_DATE) && vs2.equals(VSPACE_DURATION)) {
        XMLGregorianCalendar cal = nv1.getDateTime();
        // add-negation
        XMLGregorianCalendar result = xsd_subtract(cal, nv2.getDuration());
        NodeValue r = NodeValue.makeDate(result);
        return r;
    }
    if (vs1.equals(VSPACE_TIME) && vs2.equals(VSPACE_DURATION)) {
        XMLGregorianCalendar cal = nv1.getDateTime();
        // add-negation
        XMLGregorianCalendar result = xsd_subtract(cal, nv2.getDuration());
        NodeValue r = NodeValue.makeNode(result.toXMLFormat(), XSDDatatype.XSDtime);
        return r;
    }
    throw new ExprEvalTypeException("Operator '-' : Undefined subtraction: " + nv1 + " and " + nv2);
}
Also used : XMLGregorianCalendar(javax.xml.datatype.XMLGregorianCalendar) NodeValue(org.apache.jena.sparql.expr.NodeValue) ExprEvalTypeException(org.apache.jena.sparql.expr.ExprEvalTypeException) Node(org.apache.jena.graph.Node) Duration(javax.xml.datatype.Duration) ValueSpaceClassification(org.apache.jena.sparql.expr.ValueSpaceClassification)

Example 4 with ExprEvalTypeException

use of org.apache.jena.sparql.expr.ExprEvalTypeException in project jena by apache.

the class NodeValueOps method multiplicationNV.

public static NodeValue multiplicationNV(NodeValue nv1, NodeValue nv2) {
    ValueSpaceClassification vs1 = nv1.getValueSpace();
    ValueSpaceClassification vs2 = nv2.getValueSpace();
    if (vs1.equals(VSPACE_NUM) && vs2.equals(VSPACE_NUM))
        return XSDFuncOp.numMultiply(nv1, nv2);
    if (vs1.equals(VSPACE_DURATION) && vs2.equals(VSPACE_NUM)) {
        // ONLY defined for dayTime.
        Duration dur = nv1.getDuration();
        boolean valid = XSDFuncOp.isDayTime(dur);
        if (!valid)
            throw new ExprEvalTypeException("Operator '*': only dayTime duration.  Got: " + nv1);
        BigDecimal dec = nv2.getDecimal();
        Duration r = dur.multiply(dec);
        Node n = NodeFactoryExtra.createLiteralNode(r.toString(), null, dtXSDdayTimeDuration);
        return NodeValue.makeNodeDuration(r, n);
    }
    throw new ExprEvalTypeException("Operator '*' : Undefined multiply: " + nv1 + " and " + nv2);
}
Also used : ExprEvalTypeException(org.apache.jena.sparql.expr.ExprEvalTypeException) Node(org.apache.jena.graph.Node) Duration(javax.xml.datatype.Duration) ValueSpaceClassification(org.apache.jena.sparql.expr.ValueSpaceClassification) BigDecimal(java.math.BigDecimal)

Aggregations

ExprEvalTypeException (org.apache.jena.sparql.expr.ExprEvalTypeException)4 ValueSpaceClassification (org.apache.jena.sparql.expr.ValueSpaceClassification)4 Duration (javax.xml.datatype.Duration)3 Node (org.apache.jena.graph.Node)3 XMLGregorianCalendar (javax.xml.datatype.XMLGregorianCalendar)2 NodeValue (org.apache.jena.sparql.expr.NodeValue)2 BigDecimal (java.math.BigDecimal)1