use of org.drools.model.functions.PredicateInformation in project drools by kiegroup.
the class ConstraintEvaluationExceptionTest method initConstraintTestField.
private void initConstraintTestField(String constraint, String ruleName, String ruleFileName) {
if (testRunType.isExecutableModel()) {
predicateInformation = new PredicateInformation(constraint, ruleName, ruleFileName);
} else {
mvelConstraint = new MVELConstraint("com.example", constraint, null, null, null, null, null);
InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
BuildContext buildContext = new BuildContext(kBase, Collections.emptyList());
RuleImpl ruleImpl = new RuleImpl(ruleName);
Resource resource = new ByteArrayResource();
resource.setSourcePath(ruleFileName);
ruleImpl.setResource(resource);
buildContext.setRule(ruleImpl);
mvelConstraint.registerEvaluationContext(buildContext);
}
}
use of org.drools.model.functions.PredicateInformation in project drools by kiegroup.
the class MaterializedLambdaPredicateTest method createClassWithOneParameter.
@Test
public void createClassWithOneParameter() {
PredicateInformation predicateInformation = new PredicateInformation("p.age > 35", "rule1", "rulefilename1.drl");
predicateInformation.addRuleNames("rule2", "rulefilename2.drl");
predicateInformation.addRuleNames("rule3", "rulefilename3.drl");
CreatedClass aClass = new MaterializedLambdaPredicate("org.drools.modelcompiler.util.lambdareplace", "rulename", predicateInformation).create("(org.drools.modelcompiler.domain.Person p) -> p.getAge() > 35", new ArrayList<>(), new ArrayList());
String classNameWithPackage = aClass.getClassNameWithPackage();
String expectedPackageName = classNameWithPackage.substring(0, classNameWithPackage.lastIndexOf('.'));
String expectedClassName = classNameWithPackage.substring(classNameWithPackage.lastIndexOf('.') + 1);
// language=JAVA
String expectedResult = "" + "package PACKAGE_TOREPLACE;\n" + "import static rulename.*; " + "import org.drools.modelcompiler.dsl.pattern.D; " + "" + "@org.drools.compiler.kie.builder.MaterializedLambda() " + "public enum CLASS_TOREPLACE implements org.drools.model.functions.Predicate1<org.drools.modelcompiler.domain.Person>, org.drools.model.functions.HashedExpression {\n" + " INSTANCE; \n" + "public static final String EXPRESSION_HASH = \"4DEB93975D9859892B1A5FD4B38E2155\";" + " public java.lang.String getExpressionHash() {\n" + " return EXPRESSION_HASH;\n" + " } " + " @Override()\n" + " public boolean test(org.drools.modelcompiler.domain.Person p) throws java.lang.Exception {\n" + " return p.getAge() > 35;\n" + " }\n" + " @Override()\n" + " public org.drools.model.functions.PredicateInformation predicateInformation() {\n" + " org.drools.model.functions.PredicateInformation info = new org.drools.model.functions.PredicateInformation(\"p.age > 35\");\n" + " info.addRuleNames(\"rule1\", \"rulefilename1.drl\", \"rule2\", \"rulefilename2.drl\", \"rule3\", \"rulefilename3.drl\");" + " return info;\n" + " }\n" + "" + " }\n";
expectedResult = expectedResult.replace("PACKAGE_TOREPLACE", expectedPackageName).replace("CLASS_TOREPLACE", expectedClassName);
verifyCreatedClass(aClass, expectedResult);
}
use of org.drools.model.functions.PredicateInformation in project drools by kiegroup.
the class MaterializedLambdaPredicate method createPredicateInformationMethod.
private void createPredicateInformationMethod(EnumDeclaration classDeclaration) {
MethodDeclaration methodDeclaration = classDeclaration.addMethod("predicateInformation", Modifier.Keyword.PUBLIC);
methodDeclaration.addAnnotation("Override");
ClassOrInterfaceType predicateInformationType = toClassOrInterfaceType(PredicateInformation.class);
methodDeclaration.setType(predicateInformationType);
BlockStmt block = new BlockStmt();
NameExpr infoExpr = new NameExpr("info");
VariableDeclarationExpr infoVar = new VariableDeclarationExpr(toClassOrInterfaceType(PredicateInformation.class), "info");
NodeList<Expression> newPredicateInformationArguments = NodeList.nodeList(toStringLiteral(StringEscapeUtils.escapeJava(predicateInformation.getStringConstraint())));
ObjectCreationExpr newPredicateInformation = new ObjectCreationExpr(null, predicateInformationType, newPredicateInformationArguments);
block.addStatement(new AssignExpr(infoVar, newPredicateInformation, AssignExpr.Operator.ASSIGN));
int i = 0;
NodeList<Expression> addRuleNamesArguments = null;
for (PredicateInformation.RuleDef ruleDef : predicateInformation.getRuleDefs()) {
if (i++ % 125 == 0) {
addRuleNamesArguments = NodeList.nodeList();
block.addStatement(new MethodCallExpr(infoExpr, "addRuleNames", addRuleNamesArguments));
}
addRuleNamesArguments.add(toStringLiteral(ruleDef.getRuleName()));
addRuleNamesArguments.add(toStringLiteral(ruleDef.getFileName()));
}
if (predicateInformation.isMoreThanMaxRuleDefs()) {
block.addStatement(new MethodCallExpr(infoExpr, "setMoreThanMaxRuleDefs", NodeList.nodeList(new BooleanLiteralExpr(true))));
}
block.addStatement(new ReturnStmt(infoExpr));
methodDeclaration.setBody(block);
}
Aggregations