use of org.drools.javaparser.ast.expr.StringLiteralExpr in project drools by kiegroup.
the class ModelGenerator method addVariable.
private static void addVariable(KnowledgeBuilderImpl kbuilder, BlockStmt ruleBlock, DeclarationSpec decl) {
if (decl.getDeclarationClass() == null) {
kbuilder.addBuilderResult(new UnknownDeclarationError(decl.getBindingId()));
return;
}
Type declType = classToReferenceType(decl.getDeclarationClass());
ClassOrInterfaceType varType = JavaParser.parseClassOrInterfaceType(Variable.class.getCanonicalName());
varType.setTypeArguments(declType);
VariableDeclarationExpr var_ = new VariableDeclarationExpr(varType, toVar(decl.getBindingId()), Modifier.FINAL);
MethodCallExpr declarationOfCall = new MethodCallExpr(null, DECLARATION_OF_CALL);
declarationOfCall.addArgument(new ClassExpr(decl.getType()));
declarationOfCall.addArgument(new StringLiteralExpr(decl.getVariableName().orElse(decl.getBindingId())));
decl.getDeclarationSource().ifPresent(declarationOfCall::addArgument);
decl.getEntryPoint().ifPresent(ep -> {
MethodCallExpr entryPointCall = new MethodCallExpr(null, "entryPoint");
entryPointCall.addArgument(new StringLiteralExpr(ep));
declarationOfCall.addArgument(entryPointCall);
});
for (BehaviorDescr behaviorDescr : decl.getBehaviors()) {
MethodCallExpr windowCall = new MethodCallExpr(null, "window");
if (Behavior.BehaviorType.TIME_WINDOW.matches(behaviorDescr.getSubType())) {
windowCall.addArgument("Window.Type.TIME");
windowCall.addArgument("" + TimeUtils.parseTimeString(behaviorDescr.getParameters().get(0)));
}
if (Behavior.BehaviorType.LENGTH_WINDOW.matches(behaviorDescr.getSubType())) {
windowCall.addArgument("Window.Type.LENGTH");
windowCall.addArgument("" + Integer.valueOf(behaviorDescr.getParameters().get(0)));
}
declarationOfCall.addArgument(windowCall);
}
AssignExpr var_assign = new AssignExpr(var_, declarationOfCall, AssignExpr.Operator.ASSIGN);
ruleBlock.addStatement(var_assign);
}
use of org.drools.javaparser.ast.expr.StringLiteralExpr in project drools by kiegroup.
the class ConstraintParser method handleSpecialComparisonCases.
private static Expression handleSpecialComparisonCases(BinaryExpr.Operator operator, TypedExpression left, TypedExpression right) {
if (isComparisonOperator(operator)) {
MethodCallExpr compareMethod = null;
if (left.getType() == String.class && right.getType() == String.class) {
compareMethod = new MethodCallExpr(null, "org.drools.modelcompiler.util.EvaluationUtil.compareStringsAsNumbers");
} else if (Comparable.class.isAssignableFrom(left.getType()) && Comparable.class.isAssignableFrom(right.getType())) {
compareMethod = new MethodCallExpr(null, "org.drools.modelcompiler.util.EvaluationUtil.compare");
}
if (compareMethod != null) {
compareMethod.addArgument(left.getExpression());
compareMethod.addArgument(right.getExpression());
compareMethod.addArgument(new StringLiteralExpr(operator.asString()));
return compareMethod;
}
}
if (isAnyOperandBigDecimal(left, right) && (isComparisonOperator(operator))) {
return compareBigDecimal(operator, left, right);
}
return new BinaryExpr(left.getExpression(), right.getExpression(), operator);
}
use of org.drools.javaparser.ast.expr.StringLiteralExpr in project drools by kiegroup.
the class QueryGenerator method processQueryDef.
public static void processQueryDef(KnowledgeBuilderImpl kbuilder, TypeResolver typeResolver, PackageModel packageModel, QueryDescr queryDescr, boolean isPattern) {
RuleContext context = new RuleContext(kbuilder, packageModel, queryDescr, typeResolver, isPattern);
String queryName = queryDescr.getName();
final String queryDefVariableName = toQueryDef(queryName);
context.setQueryName(Optional.of(queryDefVariableName));
parseQueryParameters(context, packageModel, queryDescr);
ClassOrInterfaceType queryDefType = getQueryType(context.getQueryParameters());
MethodCallExpr queryCall = new MethodCallExpr(null, QUERY_CALL);
if (!queryDescr.getNamespace().isEmpty()) {
queryCall.addArgument(new StringLiteralExpr(queryDescr.getNamespace()));
}
queryCall.addArgument(new StringLiteralExpr(queryName));
for (QueryParameter qp : context.getQueryParameters()) {
queryCall.addArgument(new ClassExpr(JavaParser.parseType(qp.type.getCanonicalName())));
queryCall.addArgument(new StringLiteralExpr(qp.name));
}
packageModel.getQueryDefWithType().put(queryDefVariableName, new QueryDefWithType(queryDefType, queryCall, context));
}
use of org.drools.javaparser.ast.expr.StringLiteralExpr in project drools by kiegroup.
the class ExpressionTyper method processFirstNode.
private Optional<TypedExpressionCursor> processFirstNode(Expression drlxExpr, List<Node> childNodes, Node firstNode, boolean isInLineCast, Class<?> originalTypeCursor) {
final Optional<TypedExpressionCursor> result;
if (firstNode instanceof ThisExpr || (firstNode instanceof NameExpr && firstNode.toString().equals(bindingId))) {
result = of(thisExpr(drlxExpr, childNodes, isInLineCast, originalTypeCursor));
} else if (firstNode instanceof NameExpr) {
result = nameExpr(drlxExpr, (NameExpr) firstNode, isInLineCast, originalTypeCursor);
} else if (firstNode instanceof FieldAccessExpr && ((FieldAccessExpr) firstNode).getScope() instanceof ThisExpr) {
result = of(fieldAccessExpr((FieldAccessExpr) firstNode, originalTypeCursor));
} else if (firstNode instanceof MethodCallExpr) {
result = of(methodCallExpr((MethodCallExpr) firstNode, originalTypeCursor, new NameExpr("_this")));
} else if (firstNode instanceof StringLiteralExpr) {
result = of(stringLiteralExpr((StringLiteralExpr) firstNode));
} else if (firstNode instanceof EnclosedExpr) {
result = processFirstNode(drlxExpr, childNodes, ((EnclosedExpr) firstNode).getInner(), isInLineCast, originalTypeCursor);
} else if (firstNode instanceof CastExpr) {
result = castExpr((CastExpr) firstNode, drlxExpr, childNodes, isInLineCast, originalTypeCursor);
} else if (firstNode instanceof ArrayCreationExpr) {
result = of(arrayCreationExpr(((ArrayCreationExpr) firstNode)));
} else if (firstNode instanceof BinaryExpr) {
result = of(binaryExpr((BinaryExpr) firstNode));
} else {
result = of(new TypedExpressionCursor((Expression) firstNode, getExpressionType(ruleContext, ruleContext.getTypeResolver(), (Expression) firstNode, context.getUsedDeclarations())));
}
result.ifPresent(te -> {
if (drlxExpr instanceof NullSafeFieldAccessExpr) {
extractPrefixExpressions((NullSafeFieldAccessExpr) drlxExpr, te.expressionCursor);
}
});
return result.map(te -> {
if (isInLineCast) {
Expression exprWithInlineCast = addCastToExpression(te.typeCursor, te.expressionCursor, isInLineCast);
return new TypedExpressionCursor(exprWithInlineCast, te.typeCursor);
} else {
return te;
}
});
}
use of org.drools.javaparser.ast.expr.StringLiteralExpr in project drools by kiegroup.
the class CustomOperatorSpec method getExpression.
public Expression getExpression(RuleContext context, PointFreeExpr pointFreeExpr, TypedExpression left) {
MethodCallExpr methodCallExpr = new MethodCallExpr(null, "eval");
String opName = pointFreeExpr.getOperator().asString();
Operator operator = Operator.Register.getOperator(opName);
try {
// if the operator has an INSTANCE field avoid the operator lookup at runtime
operator.getClass().getField("INSTANCE");
methodCallExpr.addArgument(operator.getClass().getCanonicalName() + ".INSTANCE");
} catch (NoSuchFieldException e) {
methodCallExpr.addArgument(new StringLiteralExpr(opName));
}
methodCallExpr.addArgument(left.getExpression());
for (Expression rightExpr : pointFreeExpr.getRight()) {
if (rightExpr instanceof LiteralExpr) {
methodCallExpr.addArgument(rightExpr);
} else {
TypedExpression typedExpression = DrlxParseUtil.toMethodCallWithClassCheck(context, rightExpr, null, null, context.getTypeResolver());
methodCallExpr.addArgument(typedExpression.getExpression());
}
}
return pointFreeExpr.isNegated() ? new UnaryExpr(methodCallExpr, UnaryExpr.Operator.LOGICAL_COMPLEMENT) : methodCallExpr;
}
Aggregations