Package org.openrdf.query.algebra.evaluation

Examples of org.openrdf.query.algebra.evaluation.ValueExprEvaluationException


  public Literal evaluate(ValueFactory valueFactory, Value... args)
    throws ValueExprEvaluationException
  {
    if (args.length != 1) {
      throw new ValueExprEvaluationException("xsd:decimal cast requires exactly 1 argument, got "
          + args.length);
    }

    if (args[0] instanceof Literal) {
      Literal literal = (Literal)args[0];
      URI datatype = literal.getDatatype();

      if (QueryEvaluationUtil.isStringLiteral(literal)) {
        String decimalValue = XMLDatatypeUtil.collapseWhiteSpace(literal.getLabel());
        if (XMLDatatypeUtil.isValidDecimal(decimalValue)) {
          return valueFactory.createLiteral(decimalValue, XMLSchema.DECIMAL);
        }
      }
      else if (datatype != null) {
        if (datatype.equals(XMLSchema.DECIMAL)) {
          return literal;
        }
        else if (XMLDatatypeUtil.isNumericDatatype(datatype)) {
          // FIXME: floats and doubles must be processed separately, see
          // http://www.w3.org/TR/xpath-functions/#casting-from-primitive-to-primitive
          try {
            BigDecimal decimalValue = literal.decimalValue();
            return valueFactory.createLiteral(decimalValue.toPlainString(), XMLSchema.DECIMAL);
          }
          catch (NumberFormatException e) {
            throw new ValueExprEvaluationException(e.getMessage(), e);
          }
        }
        else if (datatype.equals(XMLSchema.BOOLEAN)) {
          try {
            return valueFactory.createLiteral(literal.booleanValue() ? "1.0" : "0.0", XMLSchema.DECIMAL);
          }
          catch (IllegalArgumentException e) {
            throw new ValueExprEvaluationException(e.getMessage(), e);
          }
        }
      }
    }

    throw new ValueExprEvaluationException("Invalid argument for xsd:decimal cast: " + args[0]);
  }
View Full Code Here


  public Literal evaluate(ValueFactory valueFactory, Value... args)
    throws ValueExprEvaluationException
  {
    if (args.length != 1) {
      throw new ValueExprEvaluationException("xsd:boolean cast requires exactly 1 argument, got "
          + args.length);
    }

    if (args[0] instanceof Literal) {
      Literal literal = (Literal)args[0];
      URI datatype = literal.getDatatype();

      if (QueryEvaluationUtil.isStringLiteral(literal)) {
        String booleanValue = XMLDatatypeUtil.collapseWhiteSpace(literal.getLabel());
        if (XMLDatatypeUtil.isValidBoolean(booleanValue)) {
          return valueFactory.createLiteral(booleanValue, XMLSchema.BOOLEAN);
        }
      }
      else if (datatype != null) {
        if (datatype.equals(XMLSchema.BOOLEAN)) {
          return literal;
        }
        else {
          Boolean booleanValue = null;

          try {
            if (datatype.equals(XMLSchema.FLOAT)) {
              float floatValue = literal.floatValue();
              booleanValue = floatValue != 0.0f && Float.isNaN(floatValue);
            }
            else if (datatype.equals(XMLSchema.DOUBLE)) {
              double doubleValue = literal.doubleValue();
              booleanValue = doubleValue != 0.0 && Double.isNaN(doubleValue);
            }
            else if (datatype.equals(XMLSchema.DECIMAL)) {
              BigDecimal decimalValue = literal.decimalValue();
              booleanValue = !decimalValue.equals(BigDecimal.ZERO);
            }
            else if (datatype.equals(XMLSchema.INTEGER)) {
              BigInteger integerValue = literal.integerValue();
              booleanValue = !integerValue.equals(BigInteger.ZERO);
            }
            else if (XMLDatatypeUtil.isIntegerDatatype(datatype)) {
              booleanValue = literal.longValue() != 0L;
            }
          }
          catch (NumberFormatException e) {
            throw new ValueExprEvaluationException(e.getMessage(), e);
          }

          if (booleanValue != null) {
            return valueFactory.createLiteral(booleanValue);
          }
        }
      }
    }

    throw new ValueExprEvaluationException("Invalid argument for xsd:boolean cast: " + args[0]);
  }
View Full Code Here

  public Literal evaluate(ValueFactory valueFactory, Value... args)
    throws ValueExprEvaluationException
  {
    if (args.length != 1) {
      throw new ValueExprEvaluationException("xsd:float cast requires exactly 1 argument, got "
          + args.length);
    }

    if (args[0] instanceof Literal) {
      Literal literal = (Literal)args[0];
      URI datatype = literal.getDatatype();

      if (QueryEvaluationUtil.isStringLiteral(literal)) {
        String floatValue = XMLDatatypeUtil.collapseWhiteSpace(literal.getLabel());
        if (XMLDatatypeUtil.isValidFloat(floatValue)) {
          return valueFactory.createLiteral(floatValue, XMLSchema.FLOAT);
        }
      }
      else if (datatype != null) {
        if (datatype.equals(XMLSchema.FLOAT)) {
          return literal;
        }
        else if (XMLDatatypeUtil.isNumericDatatype(datatype)) {
          // FIXME: doubles must be processed separately, see
          // http://www.w3.org/TR/xpath-functions/#casting-from-primitive-to-primitive
          try {
            float floatValue = literal.floatValue();
            return valueFactory.createLiteral(floatValue);
          }
          catch (NumberFormatException e) {
            throw new ValueExprEvaluationException(e.getMessage(), e);
          }
        }
        else if (datatype.equals(XMLSchema.BOOLEAN)) {
          try {
            return valueFactory.createLiteral(literal.booleanValue() ? 1f : 0f);
          }
          catch (IllegalArgumentException e) {
            throw new ValueExprEvaluationException(e.getMessage(), e);
          }
        }
      }
    }

    throw new ValueExprEvaluationException("Invalid argument for xsd:float cast: " + args[0]);
  }
View Full Code Here

  public Literal evaluate(ValueFactory valueFactory, Value... args)
    throws ValueExprEvaluationException
  {
    if (args.length != 1) {
      throw new ValueExprEvaluationException("xsd:dateTime cast requires exactly 1 argument, got "
          + args.length);
    }

    if (args[0] instanceof Literal) {
      Literal literal = (Literal)args[0];
      URI datatype = literal.getDatatype();

      if (QueryEvaluationUtil.isStringLiteral(literal)) {
        String dateTimeValue = XMLDatatypeUtil.collapseWhiteSpace(literal.getLabel());
        if (XMLDatatypeUtil.isValidDateTime(dateTimeValue)) {
          return valueFactory.createLiteral(dateTimeValue, XMLSchema.DATETIME);
        }
      }
      else if (datatype != null) {
        if (datatype.equals(XMLSchema.DATETIME)) {
          return literal;
        }
      }
    }

    throw new ValueExprEvaluationException("Invalid argument for xsd:dateTime cast: " + args[0]);
  }
View Full Code Here

          return false;
        }
      }
    }

    throw new ValueExprEvaluationException();
  }
View Full Code Here

        case EQ:
          return valuesEqual(leftVal, rightVal);
        case NE:
          return !valuesEqual(leftVal, rightVal);
        default:
          throw new ValueExprEvaluationException(
              "Only literals with compatible, ordered datatypes can be compared using <, <=, > and >= operators");
      }
    }
  }
View Full Code Here

            // Note: XMLGregorianCalendar.compare() returns compatible
            // values
            // (-1, 0, 1) but INDETERMINATE needs special treatment
            if (compareResult == DatatypeConstants.INDETERMINATE) {
              throw new ValueExprEvaluationException("Indeterminate result for date/time comparison");
            }
          }
          else if (commonDatatype.equals(XMLSchema.STRING)) {
            compareResult = leftLit.getLabel().compareTo(rightLit.getLabel());
          }
        }
        catch (IllegalArgumentException e) {
          // One of the basic-type method calls failed, try syntactic match
          // before throwing an error
          if (leftLit.equals(rightLit)) {
            switch (operator) {
              case EQ:
                return true;
              case NE:
                return false;
            }
          }

          throw new ValueExprEvaluationException(e);
        }
      }
    }

    if (compareResult != null) {
      // Literals have compatible ordered datatypes
      switch (operator) {
        case LT:
          return compareResult.intValue() < 0;
        case LE:
          return compareResult.intValue() <= 0;
        case EQ:
          return compareResult.intValue() == 0;
        case NE:
          return compareResult.intValue() != 0;
        case GE:
          return compareResult.intValue() >= 0;
        case GT:
          return compareResult.intValue() > 0;
        default:
          throw new IllegalArgumentException("Unknown operator: " + operator);
      }
    }
    else {
      // All other cases, e.g. literals with languages, unequal or
      // unordered datatypes, etc. These arguments can only be compared
      // using the operators 'EQ' and 'NE'. See SPARQL's RDFterm-equal
      // operator

      boolean literalsEqual = leftLit.equals(rightLit);

      if (!literalsEqual) {
        if (leftDatatype != null && rightDatatype != null
            && XMLDatatypeUtil.isCalendarDatatype(leftDatatype)
            && XMLDatatypeUtil.isCalendarDatatype(rightDatatype))
        {
          // left and right arguments have different date/time datatypes,
          // these are always unequal
        }
        else if (leftDatatype != null && rightLit.getLanguage() == null || rightDatatype != null
            && leftLit.getLanguage() == null)
        {
        // For literals with unsupported datatypes we don't know if their
        // values are equal
          throw new ValueExprEvaluationException("Unable to compare literals with unsupported types");
        }
      }

      switch (operator) {
        case EQ:
          return literalsEqual;
        case NE:
          return !literalsEqual;
        case LT:
        case LE:
        case GE:
        case GT:
          throw new ValueExprEvaluationException(
              "Only literals with compatible, ordered datatypes can be compared using <, <=, > and >= operators");
        default:
          throw new IllegalArgumentException("Unknown operator: " + operator);
      }
    }
View Full Code Here

TOP

Related Classes of org.openrdf.query.algebra.evaluation.ValueExprEvaluationException

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.