Search in sources :

Example 1 with PredicateInformation

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);
    }
}
Also used : MVELConstraint(org.drools.mvel.MVELConstraint) BuildContext(org.drools.core.reteoo.builder.BuildContext) Resource(org.kie.api.io.Resource) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) PredicateInformation(org.drools.model.functions.PredicateInformation) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase)

Example 2 with PredicateInformation

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);
}
Also used : MaterializedLambdaTestUtils.verifyCreatedClass(org.drools.modelcompiler.util.lambdareplace.MaterializedLambdaTestUtils.verifyCreatedClass) ArrayList(java.util.ArrayList) PredicateInformation(org.drools.model.functions.PredicateInformation) Test(org.junit.Test)

Example 3 with PredicateInformation

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);
}
Also used : ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) VariableDeclarationExpr(com.github.javaparser.ast.expr.VariableDeclarationExpr) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) NameExpr(com.github.javaparser.ast.expr.NameExpr) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) DrlxParseUtil.toClassOrInterfaceType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.toClassOrInterfaceType) AssignExpr(com.github.javaparser.ast.expr.AssignExpr) Expression(com.github.javaparser.ast.expr.Expression) BooleanLiteralExpr(com.github.javaparser.ast.expr.BooleanLiteralExpr) PredicateInformation(org.drools.model.functions.PredicateInformation) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Aggregations

PredicateInformation (org.drools.model.functions.PredicateInformation)3 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)1 AssignExpr (com.github.javaparser.ast.expr.AssignExpr)1 BooleanLiteralExpr (com.github.javaparser.ast.expr.BooleanLiteralExpr)1 Expression (com.github.javaparser.ast.expr.Expression)1 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)1 NameExpr (com.github.javaparser.ast.expr.NameExpr)1 ObjectCreationExpr (com.github.javaparser.ast.expr.ObjectCreationExpr)1 VariableDeclarationExpr (com.github.javaparser.ast.expr.VariableDeclarationExpr)1 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)1 ReturnStmt (com.github.javaparser.ast.stmt.ReturnStmt)1 ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)1 ArrayList (java.util.ArrayList)1 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)1 ByteArrayResource (org.drools.core.io.impl.ByteArrayResource)1 BuildContext (org.drools.core.reteoo.builder.BuildContext)1 InternalKnowledgeBase (org.drools.kiesession.rulebase.InternalKnowledgeBase)1 DrlxParseUtil.toClassOrInterfaceType (org.drools.modelcompiler.builder.generator.DrlxParseUtil.toClassOrInterfaceType)1 MaterializedLambdaTestUtils.verifyCreatedClass (org.drools.modelcompiler.util.lambdareplace.MaterializedLambdaTestUtils.verifyCreatedClass)1 MVELConstraint (org.drools.mvel.MVELConstraint)1