use of com.github.javaparser.ast.expr.MethodCallExpr in project drools by kiegroup.
the class WindowReferenceGenerator method addField.
private void addField(KnowledgeBuilderImpl kbuilder, PackageModel packageModel, WindowDeclarationDescr descr) {
final String windowName = toVar(descr.getName());
final MethodCallExpr initializer = createDslTopLevelMethod(WINDOW_CALL);
final PatternDescr pattern = descr.getPattern();
ParsedBehavior behavior = pattern.getBehaviors().stream().map(this::parseTypeFromBehavior).findFirst().orElseThrow(RuntimeException::new);
final WindowDefinition.Type windowType = behavior.windowType;
initializer.addArgument(new NameExpr(windowType.getDeclaringClass().getCanonicalName() + "." + windowType.toString()));
initializer.addArgument(new IntegerLiteralExpr(behavior.duration.getValue()));
final TimeUnit timeUnit = behavior.duration.getTimeUnit();
initializer.addArgument(new NameExpr(timeUnit.getDeclaringClass().getCanonicalName() + "." + timeUnit.name()));
final Class<?> initClass = DrlxParseUtil.getClassFromContext(typeResolver, pattern.getObjectType());
initializer.addArgument(new ClassExpr(toJavaParserType(initClass)));
if (pattern.getSource() != null) {
String epName = ((EntryPointDescr) pattern.getSource()).getEntryId();
MethodCallExpr entryPointCall = createDslTopLevelMethod(ENTRY_POINT_CALL);
entryPointCall.addArgument(toStringLiteral(epName));
initializer.addArgument(entryPointCall);
}
parseConditions(kbuilder, packageModel, pattern, initClass).forEach(initializer::addArgument);
packageModel.addAllWindowReferences(windowName, initializer);
}
use of com.github.javaparser.ast.expr.MethodCallExpr in project drools by kiegroup.
the class ConstraintUtil method isProperty.
private static boolean isProperty(Expression expr) {
if (expr instanceof CastExpr) {
expr = ((CastExpr) expr).getExpression();
}
if (expr instanceof MethodCallExpr) {
MethodCallExpr mcExpr = (MethodCallExpr) expr;
if (mcExpr.getName().asString().equals(TO_BIG_DECIMAL) && mcExpr.getArgument(0) instanceof MethodCallExpr) {
mcExpr = (MethodCallExpr) mcExpr.getArgument(0);
}
Optional<Expression> thisScope = getRootScope(mcExpr).filter(scope -> scope.equals(new NameExpr(THIS_PLACEHOLDER)));
if (thisScope.isPresent()) {
return true;
}
}
return false;
}
use of com.github.javaparser.ast.expr.MethodCallExpr in project drools by kiegroup.
the class ConstraintUtil method processExpression.
private static void processExpression(Expression expr) {
if (expr instanceof MethodCallExpr) {
MethodCallExpr mcExpr = (MethodCallExpr) expr;
if (canInverse(mcExpr)) {
inverseMethodCallExpr(mcExpr);
}
} else if (expr instanceof BinaryExpr) {
BinaryExpr bExpr = (BinaryExpr) expr;
if (bExpr.getOperator() == BinaryExpr.Operator.AND || bExpr.getOperator() == BinaryExpr.Operator.OR) {
Expression left = bExpr.getLeft();
processExpression(left);
Expression right = bExpr.getRight();
processExpression(right);
}
} else if (expr instanceof UnaryExpr) {
Expression expression = ((UnaryExpr) expr).getExpression();
processExpression(expression);
} else if (expr instanceof EnclosedExpr) {
Expression inner = ((EnclosedExpr) expr).getInner();
processExpression(inner);
}
}
use of com.github.javaparser.ast.expr.MethodCallExpr in project drools by kiegroup.
the class ModelGenerator method processRule.
private static void processRule(PackageDescr packageDescr, RuleContext context) {
PackageModel packageModel = context.getPackageModel();
RuleDescr ruleDescr = context.getRuleDescr();
context.addGlobalDeclarations();
context.setDialectFromAttributes(ruleDescr.getAttributes().values());
for (Entry<String, Object> kv : ruleDescr.getNamedConsequences().entrySet()) {
context.addNamedConsequence(kv.getKey(), kv.getValue().toString());
}
RuleUnitDescription ruleUnitDescr = context.getRuleUnitDescr();
BlockStmt ruleVariablesBlock = context.getRuleVariablesBlock();
new ModelGeneratorVisitor(context, packageModel).visit(getExtendedLhs(packageDescr, ruleDescr));
if (context.hasCompilationError()) {
return;
}
final String ruleMethodName = "rule_" + toId(ruleDescr.getName());
MethodDeclaration ruleMethod = new MethodDeclaration(NodeList.nodeList(Modifier.publicModifier(), Modifier.staticModifier()), toClassOrInterfaceType(Rule.class), ruleMethodName);
ruleMethod.setJavadocComment(" Rule name: " + ruleDescr.getName() + " ");
VariableDeclarationExpr ruleVar = new VariableDeclarationExpr(toClassOrInterfaceType(Rule.class), "rule");
MethodCallExpr ruleCall = createDslTopLevelMethod(RULE_CALL);
if (!ruleDescr.getNamespace().isEmpty()) {
ruleCall.addArgument(toStringLiteral(ruleDescr.getNamespace()));
}
ruleCall.addArgument(toStringLiteral(ruleDescr.getName()));
MethodCallExpr buildCallScope = ruleUnitDescr != null ? new MethodCallExpr(ruleCall, UNIT_CALL).addArgument(new ClassExpr(toClassOrInterfaceType(ruleUnitDescr.getCanonicalName()))) : 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()));
createVariables(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"));
packageModel.putRuleMethod(ruleUnitDescr != null ? ruleUnitDescr.getSimpleName() : DEFAULT_RULE_UNIT, ruleMethod, context.getRuleIndex());
}
use of com.github.javaparser.ast.expr.MethodCallExpr 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}.<br/>
* Based on {@link org.drools.modelcompiler.KiePackagesBuilder#setRuleMetaAttributes(Rule, RuleImpl)} the reserved annotation keywords are:
* Propagation, All, Direct.
*/
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(toStringLiteral(metaAttr));
AnnotationDescr ad = ruleDescr.getAnnotation(metaAttr);
String adFqn = ad.getFullyQualifiedName();
if ("Propagation".equals(metaAttr)) {
// legacy case, as explained in the javadoc annotation above, ref. DROOLS-5685
metaAttributeCall.addArgument(parseExpression(org.kie.api.definition.rule.Propagation.Type.class.getCanonicalName() + "." + ad.getSingleValueAsString()));
} else 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(toStringLiteral(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.getValueMap().size() == 1) {
metaAttributeCall.addArgument(annotationSingleValueExpression(ad));
} else {
metaAttributeCall.addArgument(objectAsJPExpression(ad.getValueMap()));
}
} else {
metaAttributeCall.addArgument(new NullLiteralExpr());
}
}
ruleMetaAttributes.add(metaAttributeCall);
}
return ruleMetaAttributes;
}
Aggregations