use of com.github.javaparser.ast.type.ClassOrInterfaceType 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.type.ClassOrInterfaceType in project drools by kiegroup.
the class QueryGenerator method processQueryDef.
public static void processQueryDef(PackageModel packageModel, RuleContext context) {
QueryDescr queryDescr = ((QueryDescr) context.getRuleDescr());
packageModel.registerQueryName(queryDescr.getName());
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 = createDslTopLevelMethod(QUERY_CALL);
if (!queryDescr.getNamespace().isEmpty()) {
queryCall.addArgument(toStringLiteral(queryDescr.getNamespace()));
}
queryCall.addArgument(toStringLiteral(queryName));
for (QueryParameter qp : context.getQueryParameters()) {
queryCall.addArgument(new ClassExpr(toClassOrInterfaceType(qp.getType())));
queryCall.addArgument(toStringLiteral(qp.getName()));
}
packageModel.getQueryDefWithType().put(queryDefVariableName, new QueryDefWithType(queryDefType, queryCall, context));
}
use of com.github.javaparser.ast.type.ClassOrInterfaceType in project drools by kiegroup.
the class QueryGenerator method getQueryType.
private static ClassOrInterfaceType getQueryType(List<QueryParameter> queryParameters) {
ClassOrInterfaceType queryType = toClassOrInterfaceType(QueryDef.getQueryClassByArity(queryParameters.size()));
Type[] genericType = queryParameters.stream().map(e -> e.getType()).map(DrlxParseUtil::classToReferenceType).toArray(Type[]::new);
if (genericType.length > 0) {
queryType.setTypeArguments(genericType);
}
return queryType;
}
use of com.github.javaparser.ast.type.ClassOrInterfaceType in project drools by kiegroup.
the class CommonCodegenUtilsTest method getTypedClassOrInterfaceType.
@Test
public void getTypedClassOrInterfaceType() {
String className = "CLASS_NAME";
List<String> typesName = Arrays.asList("TypeA", "TypeB");
ClassOrInterfaceType retrieved = CommonCodegenUtils.getTypedClassOrInterfaceTypeByTypeNames(className, typesName);
assertNotNull(retrieved);
String expected = String.format("%1$s<%2$s,%3$s>", className, typesName.get(0), typesName.get(1));
assertEquals(expected, retrieved.asString());
}
use of com.github.javaparser.ast.type.ClassOrInterfaceType in project drools by kiegroup.
the class KiePMMLClusteringModelFactory method clusterCreationExprFrom.
private static ObjectCreationExpr clusterCreationExprFrom(Cluster cluster) {
NodeList<Expression> arguments = new NodeList<>();
arguments.add(literalExprFrom(cluster.getId()));
arguments.add(literalExprFrom(cluster.getName()));
if (cluster.getArray() != null && cluster.getArray().getType() == Array.Type.REAL) {
String arrayStringValue = (String) cluster.getArray().getValue();
String[] arrayStringChunks = arrayStringValue.split(" ");
try {
Arrays.stream(arrayStringChunks).map(Double::parseDouble).map(DoubleLiteralExpr::new).forEach(arguments::add);
} catch (NumberFormatException e) {
logger.error("Can't parse \"real\" cluster with value \"" + arrayStringValue + "\"", e);
}
}
return new ObjectCreationExpr(null, new ClassOrInterfaceType(null, KiePMMLCluster.class.getCanonicalName()), arguments);
}
Aggregations