use of org.drools.javaparser.ast.expr.ClassExpr in project drools by kiegroup.
the class ModelGenerator method addUnitData.
private static void addUnitData(String unitVar, Class<?> type, BlockStmt ruleBlock) {
Type declType = classToReferenceType(type);
ClassOrInterfaceType varType = JavaParser.parseClassOrInterfaceType(UnitData.class.getCanonicalName());
varType.setTypeArguments(declType);
VariableDeclarationExpr var_ = new VariableDeclarationExpr(varType, toVar(unitVar), Modifier.FINAL);
MethodCallExpr unitDataCall = new MethodCallExpr(null, UNIT_DATA_CALL);
unitDataCall.addArgument(new ClassExpr(declType));
unitDataCall.addArgument(new StringLiteralExpr(unitVar));
AssignExpr var_assign = new AssignExpr(var_, unitDataCall, AssignExpr.Operator.ASSIGN);
ruleBlock.addStatement(var_assign);
}
use of org.drools.javaparser.ast.expr.ClassExpr 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.ClassExpr 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.ClassExpr in project drools by kiegroup.
the class FlowExpressionBuilder method buildIndexedBy.
private MethodCallExpr buildIndexedBy(DrlxParseSuccess drlxParseResult, MethodCallExpr exprDSL) {
if (!hasIndex(drlxParseResult)) {
return exprDSL;
}
TypedExpression left = drlxParseResult.getLeft();
TypedExpression right = drlxParseResult.getRight();
Class<?> indexType = Stream.of(left, right).map(TypedExpression::getType).filter(Objects::nonNull).findFirst().get();
ClassExpr indexedBy_indexedClass = new ClassExpr(JavaParser.parseType(indexType.getCanonicalName()));
// not 100% accurate as the type in "nameExpr" is actually parsed if it was JavaParsers as a big chain of FieldAccessExpr
FieldAccessExpr indexedBy_constraintType = new FieldAccessExpr(new NameExpr("org.drools.model.Index.ConstraintType"), drlxParseResult.getDecodeConstraintType().toString());
LambdaExpr indexedBy_leftOperandExtractor = new LambdaExpr();
indexedBy_leftOperandExtractor.addParameter(new Parameter(new UnknownType(), "_this"));
boolean leftContainsThis = left.getExpression().toString().contains("_this");
indexedBy_leftOperandExtractor.setBody(new ExpressionStmt(leftContainsThis ? left.getExpression() : right.getExpression()));
MethodCallExpr indexedByDSL = new MethodCallExpr(exprDSL, INDEXED_BY_CALL);
indexedByDSL.addArgument(indexedBy_indexedClass);
indexedByDSL.addArgument(indexedBy_constraintType);
indexedByDSL.addArgument("" + indexIdGenerator.getFieldId(drlxParseResult.getPatternType(), left.getFieldName()));
indexedByDSL.addArgument(indexedBy_leftOperandExtractor);
Collection<String> usedDeclarations = drlxParseResult.getUsedDeclarations();
if (isAlphaIndex(usedDeclarations)) {
indexedByDSL.addArgument(right.getExpression());
} else {
LambdaExpr indexedBy_rightOperandExtractor = new LambdaExpr();
indexedBy_rightOperandExtractor.addParameter(new Parameter(new UnknownType(), usedDeclarations.iterator().next()));
indexedBy_rightOperandExtractor.setBody(new ExpressionStmt(!leftContainsThis ? left.getExpression() : right.getExpression()));
indexedByDSL.addArgument(indexedBy_rightOperandExtractor);
}
return indexedByDSL;
}
use of org.drools.javaparser.ast.expr.ClassExpr in project drools by kiegroup.
the class PatternExpressionBuilder method buildIndexedBy.
private Optional<MethodCallExpr> buildIndexedBy(DrlxParseSuccess drlxParseResult) {
if (!hasIndex(drlxParseResult)) {
return Optional.empty();
}
IndexUtil.ConstraintType decodeConstraintType = drlxParseResult.getDecodeConstraintType();
TypedExpression left = drlxParseResult.getLeft();
TypedExpression right = drlxParseResult.getRight();
Class<?> indexType = Stream.of(left, right).map(TypedExpression::getType).filter(Objects::nonNull).findFirst().get();
ClassExpr indexedBy_indexedClass = new ClassExpr(JavaParser.parseType(indexType.getCanonicalName()));
// not 100% accurate as the type in "nameExpr" is actually parsed if it was JavaParsers as a big chain of FieldAccessExpr
FieldAccessExpr indexedBy_constraintType = new FieldAccessExpr(new NameExpr("org.drools.model.Index.ConstraintType"), decodeConstraintType.toString());
LambdaExpr indexedBy_leftOperandExtractor = new LambdaExpr();
indexedBy_leftOperandExtractor.addParameter(new Parameter(new UnknownType(), "_this"));
boolean leftContainsThis = left.getExpression().toString().contains("_this");
indexedBy_leftOperandExtractor.setBody(new ExpressionStmt(leftContainsThis ? left.getExpression() : right.getExpression()));
MethodCallExpr indexedByDSL = new MethodCallExpr(null, drlxParseResult.isBetaNode() ? BETA_INDEXED_BY_CALL : ALPHA_INDEXED_BY_CALL);
indexedByDSL.addArgument(indexedBy_indexedClass);
indexedByDSL.addArgument(indexedBy_constraintType);
indexedByDSL.addArgument("" + indexIdGenerator.getFieldId(drlxParseResult.getPatternType(), left.getFieldName()));
indexedByDSL.addArgument(indexedBy_leftOperandExtractor);
Collection<String> usedDeclarations = drlxParseResult.getUsedDeclarations();
if (isAlphaIndex(usedDeclarations)) {
indexedByDSL.addArgument(right.getExpression());
} else if (usedDeclarations.size() == 1) {
LambdaExpr indexedBy_rightOperandExtractor = new LambdaExpr();
indexedBy_rightOperandExtractor.addParameter(new Parameter(new UnknownType(), usedDeclarations.iterator().next()));
indexedBy_rightOperandExtractor.setBody(new ExpressionStmt(!leftContainsThis ? left.getExpression() : right.getExpression()));
indexedByDSL.addArgument(indexedBy_rightOperandExtractor);
}
return Optional.of(indexedByDSL);
}
Aggregations