use of org.drools.compiler.lang.descr.PatternDescr in project drools by kiegroup.
the class XmlPackageReaderTest method testAccumulate.
@Test
public void testAccumulate() throws Exception {
final XmlPackageReader xmlPackageReader = getXmReader();
xmlPackageReader.read(new InputStreamReader(getClass().getResourceAsStream("test_ParseAccumulate.xml")));
final PackageDescr packageDescr = xmlPackageReader.getPackageDescr();
assertNotNull(packageDescr);
RuleDescr obj = (RuleDescr) packageDescr.getRules().get(0);
Object patternobj = obj.getLhs().getDescrs().get(0);
assertTrue(patternobj instanceof PatternDescr);
final PatternDescr patterncheese = (PatternDescr) patternobj;
assertEquals(patterncheese.getIdentifier(), "cheese");
assertEquals(patterncheese.getObjectType(), "Cheese");
AccumulateDescr accumulatedescr = (AccumulateDescr) patterncheese.getSource();
assertEquals("total += $cheese.getPrice();", accumulatedescr.getActionCode());
assertEquals("int total = 0;", accumulatedescr.getInitCode());
assertEquals("new Integer( total ) );", accumulatedescr.getResultCode());
patternobj = obj.getLhs().getDescrs().get(1);
assertTrue(patternobj instanceof PatternDescr);
final PatternDescr patternmax = (PatternDescr) patternobj;
assertEquals(patternmax.getIdentifier(), "max");
assertEquals(patternmax.getObjectType(), "Number");
accumulatedescr = (AccumulateDescr) patternmax.getSource();
assertTrue(accumulatedescr.isExternalFunction());
assertEquals("max", accumulatedescr.getFunctions().get(0).getFunction());
assertNull(accumulatedescr.getInitCode());
assertNull(accumulatedescr.getActionCode());
assertNull(accumulatedescr.getResultCode());
assertNull(accumulatedescr.getReverseCode());
}
use of org.drools.compiler.lang.descr.PatternDescr 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 = new MethodCallExpr(null, "window");
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());
final Type initType = JavaParser.parseType(initClass.getCanonicalName());
initializer.addArgument(new ClassExpr(initType));
parseConditions(kbuilder, packageModel, pattern, initClass).forEach(initializer::addArgument);
packageModel.addAllWindowReferences(windowName, initializer);
}
use of org.drools.compiler.lang.descr.PatternDescr in project drools by kiegroup.
the class FromCollectVisitor method trasformFromCollectToCollectList.
public void trasformFromCollectToCollectList(PatternDescr pattern, CollectDescr collectDescr) {
// The inner pattern of the "from collect" needs to be processed to have the binding
final PatternDescr collectDescrInputPattern = collectDescr.getInputPattern();
collectDescrInputPattern.accept(parentVisitor);
final AccumulateDescr accumulateDescr = new AccumulateDescr();
accumulateDescr.setInputPattern(collectDescrInputPattern);
accumulateDescr.addFunction("collectList", null, false, new String[] { collectDescrInputPattern.getIdentifier() });
final PatternDescr transformedPatternDescr = new PatternDescr(pattern.getObjectType());
for (BaseDescr o : pattern.getConstraint().getDescrs()) {
transformedPatternDescr.addConstraint(o);
}
transformedPatternDescr.setSource(accumulateDescr);
transformedPatternDescr.accept(parentVisitor);
}
use of org.drools.compiler.lang.descr.PatternDescr in project drools by kiegroup.
the class AccumulateVisitor method visit.
public void visit(AccumulateDescr descr, PatternDescr basePattern) {
final MethodCallExpr accumulateDSL = new MethodCallExpr(null, "accumulate");
context.addExpression(accumulateDSL);
final MethodCallExpr accumulateExprs = new MethodCallExpr(null, "and");
accumulateDSL.addArgument(accumulateExprs);
context.pushExprPointer(accumulateExprs::addArgument);
BaseDescr input = descr.getInputPattern() == null ? descr.getInput() : descr.getInputPattern();
boolean inputPatternHasConstraints = (input instanceof PatternDescr) && (!((PatternDescr) input).getConstraint().getDescrs().isEmpty());
input.accept(modelGeneratorVisitor);
if (accumulateExprs.getArguments().isEmpty()) {
accumulateDSL.remove(accumulateExprs);
} else if (accumulateExprs.getArguments().size() == 1) {
accumulateDSL.setArgument(0, accumulateExprs.getArguments().get(0));
}
if (!descr.getFunctions().isEmpty()) {
for (AccumulateDescr.AccumulateFunctionCallDescr function : descr.getFunctions()) {
final Optional<NewBinding> optNewBinding = visit(context, function, accumulateDSL, basePattern, inputPatternHasConstraints);
processNewBinding(optNewBinding);
}
} else if (descr.getFunctions().isEmpty() && descr.getInitCode() != null) {
// LEGACY: Accumulate with inline custom code
if (input instanceof PatternDescr) {
visitAccInlineCustomCode(context, descr, accumulateDSL, basePattern, (PatternDescr) input);
} else {
throw new UnsupportedOperationException("I was expecting input to be of type PatternDescr. " + input);
}
} else {
throw new UnsupportedOperationException("Unknown type of Accumulate.");
}
context.popExprPointer();
postVisit();
}
use of org.drools.compiler.lang.descr.PatternDescr in project drools by kiegroup.
the class AccumulateVisitor method visitAccInlineCustomCode.
/**
* By design this legacy accumulate (with inline custome code) visitor supports only with 1-and-only binding in the accumulate code/expressions.
*/
protected void visitAccInlineCustomCode(RuleContext context2, AccumulateDescr descr, MethodCallExpr accumulateDSL, PatternDescr basePattern, PatternDescr inputDescr) {
context.pushExprPointer(accumulateDSL::addArgument);
final MethodCallExpr functionDSL = new MethodCallExpr(null, "accFunction");
String code = null;
try {
code = new String(IoUtils.readBytesFromInputStream(this.getClass().getResourceAsStream("/AccumulateInlineFunction.java")));
} catch (IOException e1) {
e1.printStackTrace();
throw new RuntimeException("Unable to locate template.");
}
String targetClassName = StringUtil.toId(context2.getRuleDescr().getName()) + "Accumulate" + descr.getLine();
code = code.replaceAll("AccumulateInlineFunction", targetClassName);
CompilationUnit templateCU = JavaParser.parse(code);
ClassOrInterfaceDeclaration templateClass = templateCU.getClassByName(targetClassName).orElseThrow(() -> new RuntimeException("Template did not contain expected type definition."));
ClassOrInterfaceDeclaration templateContextClass = templateClass.getMembers().stream().filter(m -> m instanceof ClassOrInterfaceDeclaration && ((ClassOrInterfaceDeclaration) m).getNameAsString().equals("ContextData")).map(ClassOrInterfaceDeclaration.class::cast).findFirst().orElseThrow(() -> new RuntimeException("Template did not contain expected type definition."));
List<String> contextFieldNames = new ArrayList<>();
MethodDeclaration initMethod = templateClass.getMethodsByName("init").get(0);
BlockStmt initBlock = JavaParser.parseBlock("{" + descr.getInitCode() + "}");
for (Statement stmt : initBlock.getStatements()) {
if (stmt instanceof ExpressionStmt && ((ExpressionStmt) stmt).getExpression() instanceof VariableDeclarationExpr) {
VariableDeclarationExpr vdExpr = (VariableDeclarationExpr) ((ExpressionStmt) stmt).getExpression();
for (VariableDeclarator vd : vdExpr.getVariables()) {
contextFieldNames.add(vd.getNameAsString());
templateContextClass.addField(vd.getType(), vd.getNameAsString(), Modifier.PUBLIC);
if (vd.getInitializer().isPresent()) {
Expression initializer = vd.getInitializer().get();
Expression target = new FieldAccessExpr(new NameExpr("data"), vd.getNameAsString());
Statement initStmt = new ExpressionStmt(new AssignExpr(target, initializer, AssignExpr.Operator.ASSIGN));
initMethod.getBody().get().addStatement(initStmt);
}
}
} else {
// add as-is.
initMethod.getBody().get().addStatement(stmt);
}
}
Type singleAccumulateType = JavaParser.parseType("java.lang.Object");
MethodDeclaration accumulateMethod = templateClass.getMethodsByName("accumulate").get(0);
BlockStmt actionBlock = JavaParser.parseBlock("{" + descr.getActionCode() + "}");
Collection<String> allNamesInActionBlock = collectNamesInBlock(context2, actionBlock);
if (allNamesInActionBlock.size() == 1) {
String nameExpr = allNamesInActionBlock.iterator().next();
accumulateMethod.getParameter(1).setName(nameExpr);
singleAccumulateType = context2.getDeclarationById(nameExpr).get().getType();
} else {
new LegacyAccumulate(context, descr, basePattern).build();
return;
}
writeAccumulateMethod(contextFieldNames, singleAccumulateType, accumulateMethod, actionBlock);
// <result expression>: this is a semantic expression in the selected dialect that is executed after all source objects are iterated.
MethodDeclaration resultMethod = templateClass.getMethodsByName("getResult").get(0);
Type returnExpressionType = JavaParser.parseType("java.lang.Object");
Expression returnExpression = JavaParser.parseExpression(descr.getResultCode());
if (returnExpression instanceof NameExpr) {
returnExpression = new EnclosedExpr(returnExpression);
}
rescopeNamesToNewScope(new NameExpr("data"), contextFieldNames, returnExpression);
resultMethod.getBody().get().addStatement(new ReturnStmt(returnExpression));
MethodDeclaration getResultTypeMethod = templateClass.getMethodsByName("getResultType").get(0);
getResultTypeMethod.getBody().get().addStatement(new ReturnStmt(new ClassExpr(returnExpressionType)));
if (descr.getReverseCode() != null) {
MethodDeclaration supportsReverseMethod = templateClass.getMethodsByName("supportsReverse").get(0);
supportsReverseMethod.getBody().get().addStatement(JavaParser.parseStatement("return true;"));
MethodDeclaration reverseMethod = templateClass.getMethodsByName("reverse").get(0);
BlockStmt reverseBlock = JavaParser.parseBlock("{" + descr.getReverseCode() + "}");
Collection<String> allNamesInReverseBlock = collectNamesInBlock(context2, reverseBlock);
if (allNamesInReverseBlock.size() == 1) {
reverseMethod.getParameter(1).setName(allNamesInReverseBlock.iterator().next());
} else {
new LegacyAccumulate(context, descr, basePattern).build();
return;
}
writeAccumulateMethod(contextFieldNames, singleAccumulateType, reverseMethod, reverseBlock);
} else {
MethodDeclaration supportsReverseMethod = templateClass.getMethodsByName("supportsReverse").get(0);
supportsReverseMethod.getBody().get().addStatement(JavaParser.parseStatement("return false;"));
MethodDeclaration reverseMethod = templateClass.getMethodsByName("reverse").get(0);
reverseMethod.getBody().get().addStatement(JavaParser.parseStatement("throw new UnsupportedOperationException(\"This function does not support reverse.\");"));
}
// add resulting accumulator class into the package model
this.packageModel.addGeneratedPOJO(templateClass);
functionDSL.addArgument(new ClassExpr(JavaParser.parseType(targetClassName)));
functionDSL.addArgument(new NameExpr(toVar(inputDescr.getIdentifier())));
final String bindingId = basePattern.getIdentifier();
final MethodCallExpr asDSL = new MethodCallExpr(functionDSL, "as");
asDSL.addArgument(new NameExpr(toVar(bindingId)));
accumulateDSL.addArgument(asDSL);
context.popExprPointer();
}
Aggregations