use of org.drools.javaparser.ast.expr.StringLiteralExpr in project drools by kiegroup.
the class ModelGenerator method ruleMetaAttributes.
/**
* Build a list of method calls, representing each needed {@link org.drools.model.impl.RuleBuilder#metadata(String, Object)}
* starting from a drools-compiler {@link RuleDescr}.
*/
private static List<MethodCallExpr> ruleMetaAttributes(RuleContext context, RuleDescr ruleDescr) {
List<MethodCallExpr> ruleMetaAttributes = new ArrayList<>();
for (String metaAttr : ruleDescr.getAnnotationNames()) {
MethodCallExpr metaAttributeCall = new MethodCallExpr(METADATA_CALL);
metaAttributeCall.addArgument(new StringLiteralExpr(metaAttr));
AnnotationDescr ad = ruleDescr.getAnnotation(metaAttr);
String adFqn = ad.getFullyQualifiedName();
if (adFqn != null) {
AnnotationDefinition annotationDefinition;
try {
annotationDefinition = AnnotationDefinition.build(context.getTypeResolver().resolveType(adFqn), ad.getValueMap(), context.getTypeResolver());
} catch (NoSuchMethodException | ClassNotFoundException e) {
throw new RuntimeException(e);
}
if (annotationDefinition.getValues().size() == 1 && annotationDefinition.getValues().containsKey(AnnotationDescr.VALUE)) {
Object annValue = annotationDefinition.getPropertyValue(AnnotationDescr.VALUE);
metaAttributeCall.addArgument(new StringLiteralExpr(annValue.toString()));
} else {
Map<String, Object> map = new HashMap<>(annotationDefinition.getValues().size());
for (String key : annotationDefinition.getValues().keySet()) {
map.put(key, annotationDefinition.getPropertyValue(key));
}
metaAttributeCall.addArgument(objectAsJPExpression(map));
}
} else {
if (ad.hasValue()) {
if (ad.getValues().size() == 1) {
metaAttributeCall.addArgument(objectAsJPExpression(resolveValue(ad.getSingleValueAsString())));
} else {
metaAttributeCall.addArgument(objectAsJPExpression(ad.getValueMap()));
}
} else {
metaAttributeCall.addArgument(new NullLiteralExpr());
}
}
ruleMetaAttributes.add(metaAttributeCall);
}
return ruleMetaAttributes;
}
use of org.drools.javaparser.ast.expr.StringLiteralExpr in project drools by kiegroup.
the class ModelGenerator method processRule.
private static void processRule(KnowledgeBuilderImpl kbuilder, TypeResolver typeResolver, PackageModel packageModel, PackageDescr packageDescr, RuleDescr ruleDescr, boolean isPattern) {
RuleContext context = new RuleContext(kbuilder, packageModel, ruleDescr, typeResolver, isPattern);
for (Entry<String, Object> kv : ruleDescr.getNamedConsequences().entrySet()) {
context.addNamedConsequence(kv.getKey(), kv.getValue().toString());
}
new ModelGeneratorVisitor(context, packageModel).visit(getExtendedLhs(packageDescr, ruleDescr));
final String ruleMethodName = "rule_" + toId(ruleDescr.getName());
MethodDeclaration ruleMethod = new MethodDeclaration(EnumSet.of(Modifier.PUBLIC, Modifier.STATIC), RULE_TYPE, ruleMethodName);
ruleMethod.setJavadocComment(" Rule name: " + ruleDescr.getName() + " ");
VariableDeclarationExpr ruleVar = new VariableDeclarationExpr(RULE_TYPE, RULE_CALL);
MethodCallExpr ruleCall = new MethodCallExpr(null, RULE_CALL);
if (!ruleDescr.getNamespace().isEmpty()) {
ruleCall.addArgument(new StringLiteralExpr(ruleDescr.getNamespace()));
}
ruleCall.addArgument(new StringLiteralExpr(ruleDescr.getName()));
RuleUnitDescr ruleUnitDescr = context.getRuleUnitDescr();
MethodCallExpr buildCallScope = ruleUnitDescr != null ? new MethodCallExpr(ruleCall, UNIT_CALL).addArgument(new ClassExpr(classToReferenceType(ruleUnitDescr.getRuleUnitClass()))) : ruleCall;
for (MethodCallExpr attributeExpr : ruleAttributes(context, ruleDescr)) {
attributeExpr.setScope(buildCallScope);
buildCallScope = attributeExpr;
}
for (MethodCallExpr metaAttributeExpr : ruleMetaAttributes(context, ruleDescr)) {
metaAttributeExpr.setScope(buildCallScope);
buildCallScope = metaAttributeExpr;
}
MethodCallExpr buildCall = new MethodCallExpr(buildCallScope, BUILD_CALL, NodeList.nodeList(context.getExpressions()));
BlockStmt ruleVariablesBlock = new BlockStmt();
createUnitData(ruleUnitDescr, ruleVariablesBlock);
createVariables(kbuilder, ruleVariablesBlock, packageModel, context);
ruleMethod.setBody(ruleVariablesBlock);
MethodCallExpr executeCall = new Consequence(context).createCall(ruleDescr, ruleDescr.getConsequence().toString(), ruleVariablesBlock, false);
buildCall.addArgument(executeCall);
ruleVariablesBlock.addStatement(new AssignExpr(ruleVar, buildCall, AssignExpr.Operator.ASSIGN));
ruleVariablesBlock.addStatement(new ReturnStmt(RULE_CALL));
packageModel.putRuleMethod(ruleMethodName, ruleMethod);
}
use of org.drools.javaparser.ast.expr.StringLiteralExpr in project drools by kiegroup.
the class POJOGenerator method registerTypeMetaData.
private static MethodCallExpr registerTypeMetaData(String pkg, String name) {
MethodCallExpr typeMetaDataCall = new MethodCallExpr(null, TYPE_META_DATA_CALL);
typeMetaDataCall.addArgument(new StringLiteralExpr(pkg));
typeMetaDataCall.addArgument(new StringLiteralExpr(name));
return typeMetaDataCall;
}
use of org.drools.javaparser.ast.expr.StringLiteralExpr in project drools by kiegroup.
the class POJOGenerator method processType.
private static void processType(PackageModel packageModel, TypeDeclarationDescr typeDescr, Class<?> type) {
MethodCallExpr typeMetaDataCall = registerTypeMetaData(type.getPackage().getName(), type.getSimpleName());
for (AnnotationDescr ann : typeDescr.getAnnotations()) {
typeMetaDataCall = new MethodCallExpr(typeMetaDataCall, "addAnnotation");
typeMetaDataCall.addArgument(new StringLiteralExpr(ann.getName()));
for (Map.Entry<String, Object> entry : ann.getValueMap().entrySet()) {
MethodCallExpr annotationValueCall = new MethodCallExpr(null, "annotationValue");
annotationValueCall.addArgument(new StringLiteralExpr(entry.getKey()));
annotationValueCall.addArgument(new StringLiteralExpr(entry.getValue().toString()));
typeMetaDataCall.addArgument(annotationValueCall);
}
}
packageModel.addTypeMetaDataExpressions(typeMetaDataCall);
}
use of org.drools.javaparser.ast.expr.StringLiteralExpr in project drools by kiegroup.
the class ConstraintParser method getDrlxParseResult.
private DrlxParseResult getDrlxParseResult(Class<?> patternType, String bindingId, String expression, DrlxExpression drlx, boolean isPositional) {
Expression drlxExpr = drlx.getExpr();
boolean isEnclosed = false;
while (drlxExpr instanceof EnclosedExpr) {
drlxExpr = ((EnclosedExpr) drlxExpr).getInner();
isEnclosed = true;
}
if (drlxExpr instanceof MethodCallExpr && !((MethodCallExpr) drlxExpr).getScope().isPresent() && ((MethodCallExpr) drlxExpr).getNameAsString().equals("eval")) {
drlxExpr = ((MethodCallExpr) drlxExpr).getArgument(0);
}
String exprId;
if (GENERATE_EXPR_ID) {
exprId = context.getExprId(patternType, expression);
}
if (drlxExpr instanceof BinaryExpr) {
BinaryExpr binaryExpr = (BinaryExpr) drlxExpr;
BinaryExpr.Operator operator = binaryExpr.getOperator();
IndexUtil.ConstraintType decodeConstraintType = DrlxParseUtil.toConstraintType(operator);
final ExpressionTyperContext expressionTyperContext = new ExpressionTyperContext();
final ExpressionTyper expressionTyper = new ExpressionTyper(context, patternType, bindingId, isPositional, expressionTyperContext);
TypedExpressionResult leftTypedExpressionResult = expressionTyper.toTypedExpression(binaryExpr.getLeft());
Optional<TypedExpression> optLeft = leftTypedExpressionResult.getTypedExpression();
if (!optLeft.isPresent()) {
return new DrlxParseFail();
}
List<String> usedDeclarationsOnLeft = drlx.getBind() == null ? null : new ArrayList<>(expressionTyperContext.getUsedDeclarations());
TypedExpressionResult rightExpressionResult = expressionTyper.toTypedExpression(binaryExpr.getRight());
Optional<TypedExpression> optRight = rightExpressionResult.getTypedExpression();
if (!optRight.isPresent()) {
context.addCompilationError(new ParseExpressionErrorResult(drlxExpr));
return new DrlxParseFail();
}
TypedExpression left = optLeft.get();
TypedExpression right = optRight.get();
Expression combo;
if (left.isPrimitive()) {
if (!right.getType().isPrimitive() && !Number.class.isAssignableFrom(right.getType()) && !Boolean.class.isAssignableFrom(right.getType()) && !String.class.isAssignableFrom(right.getType())) {
context.addCompilationError(new InvalidExpressionErrorResult("Comparison operation requires compatible types. Found " + left.getType() + " and " + right.getType()));
return new DrlxParseFail();
}
if (right.getExpression() instanceof StringLiteralExpr) {
right.setExpression(new IntegerLiteralExpr(((StringLiteralExpr) right.getExpression()).asString()));
} else if (right.getExpression() instanceof LiteralStringValueExpr) {
right.setExpression(coerceLiteralExprToType((LiteralStringValueExpr) right.getExpression(), left.getType()));
}
combo = new BinaryExpr(left.getExpression(), right.getExpression(), operator);
} else {
coerceRightExpression(left, right);
switch(operator) {
case EQUALS:
case NOT_EQUALS:
combo = getEqualityExpression(left, right, operator);
break;
default:
if (left.getExpression() == null || right.getExpression() == null) {
context.addCompilationError(new ParseExpressionErrorResult(drlxExpr));
return new DrlxParseFail();
}
combo = handleSpecialComparisonCases(operator, left, right);
}
}
for (Expression e : leftTypedExpressionResult.getPrefixExpressions()) {
combo = new BinaryExpr(e, combo, BinaryExpr.Operator.AND);
}
boolean isBetaNode = false;
if (right.getExpression() instanceof BinaryExpr) {
if (((BinaryExpr) right.getExpression()).getRight() instanceof MethodCallExpr) {
isBetaNode = true;
}
} else if (right.getExpression() instanceof NameExpr) {
isBetaNode = true;
}
if (isEnclosed) {
combo = new EnclosedExpr(combo);
}
return new DrlxParseSuccess(patternType, exprId, bindingId, combo, left.getType()).setDecodeConstraintType(decodeConstraintType).setUsedDeclarations(expressionTyperContext.getUsedDeclarations()).setUsedDeclarationsOnLeft(usedDeclarationsOnLeft).setReactOnProperties(expressionTyperContext.getReactOnProperties()).setLeft(left).setRight(right).setBetaNode(isBetaNode);
}
if (drlxExpr instanceof UnaryExpr) {
UnaryExpr unaryExpr = (UnaryExpr) drlxExpr;
TypedExpressionResult typedExpressionResult = new ExpressionTyper(context, patternType, bindingId, isPositional).toTypedExpression(unaryExpr);
return typedExpressionResult.getTypedExpression().<DrlxParseResult>map(left -> {
return new DrlxParseSuccess(patternType, exprId, bindingId, left.getExpression(), left.getType()).setUsedDeclarations(typedExpressionResult.getUsedDeclarations()).setReactOnProperties(typedExpressionResult.getReactOnProperties()).setLeft(left);
}).orElse(new DrlxParseFail());
}
if (drlxExpr instanceof PointFreeExpr) {
PointFreeExpr pointFreeExpr = (PointFreeExpr) drlxExpr;
TypedExpressionResult typedExpressionResult = new ExpressionTyper(context, patternType, bindingId, isPositional).toTypedExpression(pointFreeExpr);
final Optional<TypedExpression> optTypedExpression = typedExpressionResult.getTypedExpression();
return optTypedExpression.<DrlxParseResult>map(typedExpression -> {
final Expression returnExpression = typedExpression.getExpression();
final Class<?> returnType = typedExpression.getType();
return new DrlxParseSuccess(patternType, exprId, bindingId, returnExpression, returnType).setUsedDeclarations(typedExpressionResult.getUsedDeclarations()).setReactOnProperties(typedExpressionResult.getReactOnProperties()).setLeft(typedExpression.getLeft()).setStatic(typedExpression.isStatic()).setValidExpression(true);
}).orElse(new DrlxParseFail());
}
if (drlxExpr instanceof MethodCallExpr) {
MethodCallExpr methodCallExpr = (MethodCallExpr) drlxExpr;
// when the methodCallExpr will be placed in the model/DSL, any parameter being a "this" need to be implemented as _this by convention.
List<ThisExpr> rewriteThisExprs = recurseCollectArguments(methodCallExpr).stream().filter(ThisExpr.class::isInstance).map(ThisExpr.class::cast).collect(Collectors.toList());
for (ThisExpr t : rewriteThisExprs) {
methodCallExpr.replace(t, new NameExpr("_this"));
}
Optional<MethodDeclaration> functionCall = packageModel.getFunctions().stream().filter(m -> m.getName().equals(methodCallExpr.getName())).findFirst();
if (functionCall.isPresent()) {
Class<?> returnType = DrlxParseUtil.getClassFromContext(context.getTypeResolver(), functionCall.get().getType().asString());
NodeList<Expression> arguments = methodCallExpr.getArguments();
List<String> usedDeclarations = new ArrayList<>();
for (Expression arg : arguments) {
if (arg instanceof NameExpr && !arg.toString().equals("_this")) {
usedDeclarations.add(arg.toString());
} else if (arg instanceof MethodCallExpr) {
TypedExpressionResult typedExpressionResult = new ExpressionTyper(context, null, bindingId, isPositional).toTypedExpression(arg);
usedDeclarations.addAll(typedExpressionResult.getUsedDeclarations());
}
}
return new DrlxParseSuccess(patternType, exprId, bindingId, methodCallExpr, returnType).setUsedDeclarations(usedDeclarations);
} else if (methodCallExpr.getScope().isPresent() && methodCallExpr.getScope().get() instanceof StringLiteralExpr) {
TypedExpressionResult typedExpressionResult = new ExpressionTyper(context, String.class, bindingId, isPositional).toTypedExpression(methodCallExpr);
Optional<TypedExpression> optConverted = typedExpressionResult.getTypedExpression();
return optConverted.<DrlxParseResult>map(converted -> {
return new DrlxParseSuccess(String.class, exprId, bindingId, converted.getExpression(), converted.getType()).setLeft(converted).setUsedDeclarations(typedExpressionResult.getUsedDeclarations());
}).orElse(new DrlxParseFail());
} else if (patternType != null) {
NameExpr _this = new NameExpr("_this");
TypedExpression converted = DrlxParseUtil.toMethodCallWithClassCheck(context, methodCallExpr, bindingId, patternType, context.getTypeResolver());
Expression withThis = DrlxParseUtil.prepend(_this, converted.getExpression());
return new DrlxParseSuccess(patternType, exprId, bindingId, withThis, converted.getType()).setLeft(converted);
} else {
return new DrlxParseSuccess(patternType, exprId, bindingId, methodCallExpr, null);
}
}
if (drlxExpr instanceof FieldAccessExpr) {
FieldAccessExpr fieldCallExpr = (FieldAccessExpr) drlxExpr;
NameExpr _this = new NameExpr("_this");
TypedExpression converted = DrlxParseUtil.toMethodCallWithClassCheck(context, fieldCallExpr, bindingId, patternType, context.getTypeResolver());
Expression withThis = DrlxParseUtil.prepend(_this, converted.getExpression());
return new DrlxParseSuccess(patternType, exprId, bindingId, withThis, converted.getType()).setLeft(converted);
}
if (drlxExpr instanceof NameExpr) {
NameExpr nameExpr = (NameExpr) drlxExpr;
NameExpr _this = new NameExpr("_this");
TypedExpression converted = DrlxParseUtil.toMethodCallWithClassCheck(context, nameExpr, bindingId, patternType, context.getTypeResolver());
Expression withThis = DrlxParseUtil.prepend(_this, converted.getExpression());
if (drlx.getBind() != null) {
return new DrlxParseSuccess(patternType, exprId, bindingId, null, converted.getType()).setLeft(new TypedExpression(withThis, converted.getType())).addReactOnProperty(lcFirst(nameExpr.getNameAsString()));
} else {
return new DrlxParseSuccess(patternType, exprId, bindingId, withThis, converted.getType()).addReactOnProperty(nameExpr.getNameAsString());
}
}
if (drlxExpr instanceof OOPathExpr) {
return new DrlxParseSuccess(patternType, exprId, bindingId, drlxExpr, null);
}
if (drlxExpr instanceof LiteralExpr) {
return new DrlxParseSuccess(patternType, exprId, bindingId, drlxExpr, getLiteralExpressionType(((LiteralExpr) drlxExpr)));
}
// TODO
throw new UnsupportedOperationException("Unknown expression: " + toDrlx(drlxExpr));
}
Aggregations