private void putLogicalOnStack(
        TermMap left, TermMap right, Class<? extends BinaryExpression> logicalClass) {
      try {
        BinaryExpression logical = logicalClass.newInstance();
        logical.setLeftExpression(DataTypeHelper.uncast(left.getLiteralValBool()));
        logical.setRightExpression(DataTypeHelper.uncast(right.getLiteralValBool()));

        tms.push(tmf.createBoolTermMap(logical));
      } catch (InstantiationException | IllegalAccessException e) {
        log.error("Error creating logical operator", e);
      }
    }
示例#2
0
 public OutputStream getOutputStream() {
   OutputStream outputStream = null;
   try {
     outputStream = DataTypeHelper.writeString(content); // 将String转换成ByteArrayOutputStream
   } catch (Exception ex) {
   }
   return outputStream;
 }
 private Expression isExpressionBound(Expression expr) {
   expr = DataTypeHelper.uncast(expr);
   // date col check
   if (optConf.shortcutFilters && expr instanceof NullValue) {
     // do nothing
     return new StringExpression("false");
   }
   if (optConf.shortcutFilters
       && (DataTypeHelper.constantValueExpressions.contains(expr.getClass()))) {
     // constant value detected is therefore bound
     return new StringExpression("true");
   } else {
     IsNullExpression isNullExpr = new IsNullExpression();
     isNullExpr.setLeftExpression(expr);
     isNullExpr.setNot(true);
     return isNullExpr;
   }
 }
    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);
      }
    }
    @Override
    public void visit(ExprFunction1 func) {
      if (func instanceof E_Bound) {
        TermMap boundCheck = tms.pop();
        tms.push(translateIsBound(boundCheck));

      } else if (func instanceof E_LogicalNot) {
        TermMap notCheck = tms.pop();
        Expression bool = DataTypeHelper.uncast(notCheck.getLiteralValBool());
        if (bool instanceof IsNullExpression) {
          ((IsNullExpression) bool).setNot(!((IsNullExpression) bool).isNot());

        } else {
          Parenthesis parenthesis = new Parenthesis(bool);
          parenthesis.setNot();

          notCheck = tmf.createBoolTermMap(parenthesis);
        }

        tms.push(notCheck);

      } else if (func instanceof E_Lang) {
        TermMap langFunc = tms.pop();
        Expression lang = DataTypeHelper.uncast(langFunc.getLiteralLang());
        TermMap langTermMap = tmf.createStringTermMap(lang);
        tms.push(langTermMap);

      } else if (func instanceof E_Str) {
        TermMap strParam = tms.pop();

        // create the coalesce function here

        List<Expression> strExpressions = new ArrayList<Expression>();

        strExpressions.add(
            dth.cast(
                DataTypeHelper.uncast(strParam.getLiteralValBinary()), dth.getStringCastType()));
        strExpressions.add(
            dth.cast(DataTypeHelper.uncast(strParam.getLiteralValBool()), dth.getStringCastType()));
        strExpressions.add(
            dth.cast(DataTypeHelper.uncast(strParam.getLiteralValDate()), dth.getStringCastType()));
        strExpressions.add(
            dth.cast(
                DataTypeHelper.uncast(strParam.getLiteralValNumeric()), dth.getStringCastType()));
        strExpressions.add(
            dth.cast(
                DataTypeHelper.uncast(strParam.getLiteralValString()), dth.getStringCastType()));

        strExpressions.add(FilterUtil.concat(strParam.getExpressions().toArray(new Expression[0])));

        Expression toString = FilterUtil.coalesce(strExpressions.toArray(new Expression[0]));

        tms.push(tmf.createStringTermMap(toString));
      } else if (func instanceof E_IsBlank) {
        TermMap isBlank = tms.pop();
        EqualsTo eq = new EqualsTo();
        eq.setLeftExpression(isBlank.getTermType());
        eq.setRightExpression(new StringValue("'" + ColumnHelper.COL_VAL_TYPE_BLANK + "'"));
        tms.push(tmf.createBoolTermMap(eq));
      } else if (func instanceof E_IsIRI) {
        TermMap isIri = tms.pop();
        EqualsTo eq = new EqualsTo();
        eq.setLeftExpression(isIri.getTermType());
        eq.setRightExpression(new StringValue("'" + ColumnHelper.COL_VAL_TYPE_RESOURCE + "'"));
        tms.push(tmf.createBoolTermMap(eq));
      } else if (func instanceof E_IsLiteral) {
        TermMap isLiteral = tms.pop();
        EqualsTo eq = new EqualsTo();
        eq.setLeftExpression(isLiteral.getTermType());
        eq.setRightExpression(new StringValue("'" + ColumnHelper.COL_VAL_TYPE_LITERAL + "'"));
        tms.push(tmf.createBoolTermMap(eq));
      } else {
        throw new ImplementationException("Implement Conversion for " + func.toString());
      }
    }
  public Expression asFilter(Expr expr, Map<String, TermMap> var2termMap) {

    TermMap tm = asTermMap(expr, var2termMap);
    return DataTypeHelper.uncast(tm.literalValBool);
  }
示例#7
0
 public void setInputStream(InputStream in) {
   try {
     this.content = DataTypeHelper.getString(in); // 将ByteArrayInputStream转换成String
   } catch (Exception ex) {
   }
 }