use of com.github.javaparser.ast.expr.VariableDeclarationExpr in project drools by kiegroup.
the class ModelGenerator method addVariable.
private static void addVariable(BlockStmt ruleBlock, DeclarationSpec declaration, RuleContext context, boolean domainClass) {
if (declaration.getDeclarationClass() == null) {
context.addCompilationError(new UnknownDeclarationError(declaration.getBindingId()));
return;
}
Type declType = classToReferenceType(declaration);
ClassOrInterfaceType varType = toClassOrInterfaceType(Variable.class);
varType.setTypeArguments(declType);
VariableDeclarationExpr var_ = new VariableDeclarationExpr(varType, context.getVar(declaration.getBindingId()), Modifier.finalModifier());
MethodCallExpr declarationOfCall = createDslTopLevelMethod(DECLARATION_OF_CALL);
declarationOfCall.addArgument(new ClassExpr(declaration.getBoxedType()));
if (domainClass) {
String domainClassSourceName = asJavaSourceName(declaration.getDeclarationClass());
declarationOfCall.addArgument(DOMAIN_CLASSESS_METADATA_FILE_NAME + context.getPackageModel().getPackageUUID() + "." + domainClassSourceName + DOMAIN_CLASS_METADATA_INSTANCE);
}
declarationOfCall.addArgument(toStringLiteral(declaration.getVariableName().orElse(declaration.getBindingId())));
declaration.getDeclarationSource().ifPresent(declarationOfCall::addArgument);
declaration.getEntryPoint().ifPresent(ep -> {
MethodCallExpr entryPointCall = createDslTopLevelMethod(ENTRY_POINT_CALL);
entryPointCall.addArgument(toStringLiteral(ep));
declarationOfCall.addArgument(entryPointCall);
});
for (BehaviorDescr behaviorDescr : declaration.getBehaviors()) {
MethodCallExpr windowCall = createDslTopLevelMethod(WINDOW_CALL);
if (Behavior.BehaviorType.TIME_WINDOW.matches(behaviorDescr.getSubType())) {
windowCall.addArgument("org.drools.model.Window.Type.TIME");
windowCall.addArgument("" + TimeUtils.parseTimeString(behaviorDescr.getParameters().get(0)));
}
if (Behavior.BehaviorType.LENGTH_WINDOW.matches(behaviorDescr.getSubType())) {
windowCall.addArgument("org.drools.model.Window.Type.LENGTH");
windowCall.addArgument("" + Integer.valueOf(behaviorDescr.getParameters().get(0)));
}
declarationOfCall.addArgument(windowCall);
}
AssignExpr var_assign = new AssignExpr(var_, declarationOfCall, AssignExpr.Operator.ASSIGN);
if (!DrlxParseUtil.hasDuplicateExpr(ruleBlock, var_assign)) {
ruleBlock.addStatement(var_assign);
}
}
use of com.github.javaparser.ast.expr.VariableDeclarationExpr in project drools by kiegroup.
the class QueryGenerator method processQuery.
public static void processQuery(PackageModel packageModel, QueryDescr queryDescr) {
String queryDefVariableName = toQueryDef(queryDescr.getName());
RuleContext context = packageModel.getQueryDefWithType().get(queryDefVariableName).getContext();
context.addGlobalDeclarations();
context.setDialectFromAttributes(queryDescr.getAttributes().values());
new ModelGeneratorVisitor(context, packageModel).visit(queryDescr.getLhs());
if (context.getRuleUnitDescr() != null) {
Map<String, Class<?>> queryBindings = new HashMap<>();
for (DeclarationSpec declr : context.getAllDeclarations()) {
if (!declr.isGlobal() && !declr.getBindingId().startsWith(GENERATED_VARIABLE_PREFIX)) {
queryBindings.put(declr.getBindingId(), declr.getDeclarationClass());
}
}
QueryModel queryModel = new QueryModel(queryDescr.getName(), queryDescr.getNamespace(), queryDescr.getParameters(), queryBindings);
packageModel.addQueryInRuleUnit(context.getRuleUnitDescr(), queryModel);
}
final Type queryType = toClassOrInterfaceType(Query.class);
MethodDeclaration queryMethod = new MethodDeclaration(NodeList.nodeList(Modifier.privateModifier()), queryType, QUERY_METHOD_PREFIX + toId(queryDescr.getName()));
BlockStmt queryBody = new BlockStmt();
ModelGenerator.createVariables(queryBody, packageModel, context);
queryMethod.setBody(queryBody);
String queryBuildVarName = toId(queryDescr.getName()) + "_build";
VariableDeclarationExpr queryBuildVar = new VariableDeclarationExpr(queryType, queryBuildVarName);
MethodCallExpr buildCall = new MethodCallExpr(new NameExpr(queryDefVariableName), BUILD_CALL);
context.getExpressions().forEach(buildCall::addArgument);
AssignExpr queryBuildAssign = new AssignExpr(queryBuildVar, buildCall, AssignExpr.Operator.ASSIGN);
queryBody.addStatement(queryBuildAssign);
queryBody.addStatement(new ReturnStmt(queryBuildVarName));
packageModel.putQueryMethod(queryMethod);
RuleUnitDescription ruleUnitDescr = context.getRuleUnitDescr();
if (ruleUnitDescr != null) {
packageModel.putRuleUnit(ruleUnitDescr.getSimpleName());
}
}
use of com.github.javaparser.ast.expr.VariableDeclarationExpr in project drools by kiegroup.
the class BoxedParameters method getBoxedParametersWithUnboxedAssignment.
// Types in the executable model are promoted to boxed to type check the Java DSL.
// We add such promoted types as _<PARAMETER_NAME> (with the underscore prefix)
// and then we downcast to the original unboxed type in the body of the function (methodBody)
public NodeList<Parameter> getBoxedParametersWithUnboxedAssignment(Collection<String> declarationUsedInRHS, BlockStmt methodBody) {
NodeList<Parameter> parameters = NodeList.nodeList();
for (String parameterName : declarationUsedInRHS) {
DeclarationSpec declaration = context.getDeclarationByIdWithException(parameterName);
Parameter boxedParameter;
Type boxedType = declaration.getBoxedType();
if (declaration.isBoxed()) {
String boxedParameterName = "_" + parameterName;
boxedParameter = new Parameter(boxedType, boxedParameterName);
Expression unboxedTypeDowncast = new VariableDeclarationExpr(new VariableDeclarator(declaration.getRawType(), parameterName, new NameExpr(boxedParameterName)));
methodBody.addStatement(0, unboxedTypeDowncast);
} else {
boxedParameter = new Parameter(boxedType, parameterName);
}
parameters.add(boxedParameter);
}
return parameters;
}
use of com.github.javaparser.ast.expr.VariableDeclarationExpr in project drools by kiegroup.
the class CommonCodegenUtils method createMap.
private static void createMap(final BlockStmt body, final String mapName, final List<String> mapTypes, final Class<? extends Map> mapClass) {
final VariableDeclarator mapDeclarator = new VariableDeclarator(getTypedClassOrInterfaceTypeByTypeNames(Map.class.getName(), mapTypes), mapName);
final ObjectCreationExpr mapInitializer = new ObjectCreationExpr();
mapInitializer.setType(getTypedClassOrInterfaceTypeByTypeNames(mapClass.getName(), mapTypes));
mapDeclarator.setInitializer(mapInitializer);
final VariableDeclarationExpr mapDeclarationExpr = new VariableDeclarationExpr(mapDeclarator);
body.addStatement(mapDeclarationExpr);
}
use of com.github.javaparser.ast.expr.VariableDeclarationExpr in project drools by kiegroup.
the class KiePMMLNodeFactoryTest method populateEvaluateNodeWithScoreDistributions.
@Test
public void populateEvaluateNodeWithScoreDistributions() {
final BlockStmt toPopulate = new BlockStmt();
final VariableDeclarator variableDeclarator = new VariableDeclarator();
variableDeclarator.setType("List");
variableDeclarator.setName(SCORE_DISTRIBUTIONS);
toPopulate.addStatement(new VariableDeclarationExpr(variableDeclarator));
assertFalse(variableDeclarator.getInitializer().isPresent());
// Without probability
List<ScoreDistribution> scoreDistributions = getRandomPMMLScoreDistributions(false);
KiePMMLNodeFactory.populateEvaluateNodeWithScoreDistributions(toPopulate, scoreDistributions);
commonVerifyEvaluateNodeWithScoreDistributions(variableDeclarator, scoreDistributions);
// With probability
scoreDistributions = getRandomPMMLScoreDistributions(true);
KiePMMLNodeFactory.populateEvaluateNodeWithScoreDistributions(toPopulate, scoreDistributions);
commonVerifyEvaluateNodeWithScoreDistributions(variableDeclarator, scoreDistributions);
}
Aggregations