Пример #1
0
 public void visit(WhenClause whenClause) throws Exception {
   if (whenClause == null) return;
   if (whenClause.getWhenExpression() != null) whenClause.getWhenExpression().accept(this);
   if (whenClause.getThenExpression() != null) whenClause.getThenExpression().accept(this);
 }
    private void putArithmeticOnStack(
        TermMap left, TermMap right, Class<? extends BinaryExpression> arithmeticOp) {
      try {
        BinaryExpression arithmetical = arithmeticOp.newInstance();
        arithmetical.setLeftExpression(DataTypeHelper.uncast(right.getLiteralValNumeric()));

        arithmetical.setRightExpression(DataTypeHelper.uncast(left.getLiteralValNumeric()));

        // for division we always return decimal
        if (arithmeticOp.equals(Division.class)) {
          Expression datatype =
              dth.cast(
                  new StringValue("'" + XSDDatatype.XSDdecimal.getURI() + "'"),
                  dth.getStringCastType());
          tms.push(tmf.createNumericalTermMap(arithmetical, datatype));
        } else {

          // determine the datatype

          if (optConf.isShortcutFilters()) {
            // check if we can determine the datatype of both parameters
            Expression dtLeft = DataTypeHelper.uncast(left.getLiteralType());
            Expression dtRight = DataTypeHelper.uncast(right.getLiteralType());
            if (DataTypeHelper.constantValueExpressions.contains(dtLeft.getClass())
                && DataTypeHelper.constantValueExpressions.contains(dtRight.getClass())) {
              if (dtLeft.toString().equals(dtRight.toString())) {
                // the same, so we use
                tms.push(tmf.createNumericalTermMap(arithmetical, dtLeft));
              } else {
                // we just use decimal
                Expression datatype =
                    dth.cast(
                        new StringValue("'" + XSDDatatype.XSDdecimal.getURI() + "'"),
                        dth.getStringCastType());
                tms.push(tmf.createNumericalTermMap(arithmetical, datatype));
              }

              return;
            }
          }

          // it was not possible to short, so create the dynamic datatype expression
          CaseExpression datatypeCase = new CaseExpression();

          WhenClause datatypeEquals = new WhenClause();
          EqualsTo datatypesAreEqualwhen = new EqualsTo();
          datatypesAreEqualwhen.setLeftExpression(left.getLiteralType());
          Expression datatypesEqualThen = left.getLiteralType();

          datatypeEquals.setWhenExpression(datatypesAreEqualwhen);
          datatypeEquals.setThenExpression(datatypesEqualThen);

          Expression elseDataType = new StringValue("'" + XSDDatatype.XSDdecimal.getURI() + "'");

          datatypeCase.setWhenClauses(Arrays.asList((Expression) datatypeEquals));

          datatypeCase.setElseExpression(elseDataType);

          tms.push(tmf.createNumericalTermMap(arithmetical, datatypeCase));
        }

      } catch (InstantiationException | IllegalAccessException e) {
        log.error("Error creating arithmetic operator", e);
      }
    }