use of com.github.javaparser.ast.expr.LiteralExpr in project drools by kiegroup.
the class PatternExpressionBuilder method buildIndexedBy.
private Optional<MethodCallExpr> buildIndexedBy(SingleDrlxParseSuccess drlxParseResult) {
if (drlxParseResult.isUnification()) {
TypedExpression left = drlxParseResult.getLeft();
TypedExpression right = drlxParseResult.getRight();
LambdaExpr indexedByLeftOperandExtractor = createIndexedByLambda(drlxParseResult, left, right);
MethodCallExpr indexedByDSL = indexedByDSL(drlxParseResult, left);
indexedByDSL.addArgument(org.drools.model.Index.ConstraintType.class.getCanonicalName() + ".EQUAL");
indexedByDSL.addArgument("-1");
indexedByDSL.addArgument(indexedByLeftOperandExtractor);
indexedByDSL.addArgument("" + null);
return Optional.of(indexedByDSL);
}
if (!shouldCreateIndex(drlxParseResult)) {
return Optional.empty();
}
TypedExpression left = drlxParseResult.getLeft();
TypedExpression right = drlxParseResult.getRight();
Expression rightExpression = right.getExpression();
if (!drlxParseResult.isBetaConstraint() && !(rightExpression instanceof LiteralExpr || isStringToDateExpression(rightExpression) || isNumberToStringExpression(rightExpression))) {
return Optional.empty();
}
// not 100% accurate as the type in "nameExpr" is actually parsed if it was JavaParsers as a big chain of FieldAccessExpr
FieldAccessExpr indexedBy_constraintType = new FieldAccessExpr(new NameExpr(org.drools.model.Index.ConstraintType.class.getCanonicalName()), drlxParseResult.getDecodeConstraintType().toString());
LambdaExpr indexedByLeftOperandExtractor = createIndexedByLambda(drlxParseResult, left, right);
MethodCallExpr indexedByDSL = indexedByDSL(drlxParseResult, left);
indexedByDSL.addArgument(indexedBy_constraintType);
indexedByDSL.addArgument(getIndexIdArgument(drlxParseResult, left));
indexedByDSL.addArgument(indexedByLeftOperandExtractor);
Collection<String> usedDeclarations = drlxParseResult.getUsedDeclarations();
java.lang.reflect.Type leftType = left.getType();
if (drlxParseResult.isBetaConstraint()) {
addIndexedByDeclaration(left, right, left.containThis(), indexedByDSL, usedDeclarations);
} else {
indexedByDSL.addArgument(narrowExpressionToType(right, leftType));
}
return Optional.of(indexedByDSL);
}
use of com.github.javaparser.ast.expr.LiteralExpr in project drools by kiegroup.
the class ParserUtil method objectCreationExprToValue.
public static Object objectCreationExprToValue(ObjectCreationExpr objectCreationExpr, RuleContext context) {
// Only a few classes/constructors are handled. Otherwise, value becomes null so a link would be UNKNOWN. To be enhanced : DROOLS-6711
ClassOrInterfaceType type = objectCreationExpr.getType();
Class<?> clazz = getClassFromType(context.getTypeResolver(), type);
if (clazz.equals(BigDecimal.class)) {
NodeList<Expression> arguments = objectCreationExpr.getArguments();
Optional<Object> opt = arguments.stream().findFirst().filter(StringLiteralExpr.class::isInstance).map(literalExpr -> literalExpr.asStringLiteralExpr().asString()).map(BigDecimal::new);
return opt.orElse(null);
} else if (clazz.equals(BigInteger.class)) {
NodeList<Expression> arguments = objectCreationExpr.getArguments();
Optional<Object> opt = arguments.stream().findFirst().filter(StringLiteralExpr.class::isInstance).map(literalExpr -> literalExpr.asStringLiteralExpr().asString()).map(BigInteger::new);
return opt.orElse(null);
}
return null;
}
use of com.github.javaparser.ast.expr.LiteralExpr in project drools by kiegroup.
the class AccumulateVisitor method parseFirstParameter.
private Optional<NewBinding> parseFirstParameter(PatternDescr basePattern, BaseDescr input, AccumulateDescr.AccumulateFunctionCallDescr function, MethodCallExpr functionDSL, String bindingId) {
final String accumulateFunctionParameterStr = function.getParams()[0];
final Expression accumulateFunctionParameter = DrlxParseUtil.parseExpression(accumulateFunctionParameterStr).getExpr();
if (accumulateFunctionParameter instanceof BinaryExpr) {
return binaryExprParameter(basePattern, function, functionDSL, bindingId, accumulateFunctionParameterStr);
}
if (parameterNeedsConvertionToMethodCallExpr(accumulateFunctionParameter)) {
return methodCallExprParameter(basePattern, input, function, functionDSL, bindingId, accumulateFunctionParameter);
}
if (accumulateFunctionParameter instanceof DrlNameExpr) {
nameExprParameter(basePattern, function, functionDSL, bindingId, accumulateFunctionParameter);
} else if (accumulateFunctionParameter instanceof LiteralExpr) {
literalExprParameter(basePattern, function, functionDSL, bindingId, accumulateFunctionParameter);
} else {
context.addCompilationError(new InvalidExpressionErrorResult("Invalid expression " + accumulateFunctionParameterStr, Optional.of(context.getRuleDescr())));
throw new AccumulateParsingFailedException();
}
return Optional.empty();
}
Aggregations