猿问

在 PatternDescr 中 Drools AndDescr(和 OrDescr)

我以编程方式创建规则文件。这是一个简单的例子:


    PackageDescr pkg = DescrFactory.newPackage()

            .name(Constants.DRL_FILE_PACKAGE)

            .newImport().target(Product.class.getName()).end()

            .getDescr();

    RuleDescr testRule = new RuleDescr();


    RelationalExprDescr expr1 = new RelationalExprDescr("==", false, null, new ExprConstraintDescr("productId.id"), new ExprConstraintDescr(null));

    RelationalExprDescr expr2 = new RelationalExprDescr("==", false, null, new ExprConstraintDescr("operation"), new ExprConstraintDescr("5"));

    OrDescr or = new OrDescr();

    PatternDescr patternDescr = new PatternDescr();

    AndDescr and = new AndDescr();


    or.addDescr(expr1);

    or.addDescr(expr2);


    patternDescr.setObjectType(Product.class.getName());

    patternDescr.addConstraint(or);


    and.addDescr(patternDescr);


    testRule.setLhs(and);

    testRule.setConsequence("System.out.println(\"Hi\");");

    pkg.addRule(testRule);


    String drl = new DrlDumper().dump(pkg);

这会生成以下 DRL 规则:


rule "null"

when

    test.Product( [OR [productId.id == null, operation == 5] ] )  

then

System.out.println("Hi");

end

然后,当使用此规则创建 KieContainer 时,我遇到了一个例外:


[ERR 101] Line 7:53 no viable alternative at input 'OR' in rule "null"

PatternDescr 中包含 AndDescr 或 OrDescr 是否无效?


如果是这样,我是否可以覆盖 AndDescr 和 OrDescr 中的 toString 方法(这就是此处打印的内容),使其成为有效语法,或者是否有更好的方法来处理这种情况?


如果它不是无效的,那么我缺少什么才能成功构建 KieContainer?


慕无忌1623718
浏览 103回答 1
1回答

慕神8447489

您使用内部 API 来构建对用户来说并不友好的规则。即使我们回答了这个问题,你也会发现很多这样的问题。而且内部结构最终肯定会改变,所以我建议你不要使用它。相反,开始使用可执行模型以编程方式构建规则,这是一个完全根据您的需要创建的 Java DSL。这样你就不必担心描述符的内部结构。看一下org.drools.modelcompiler.PatternDSLTest或org.drools.model.FlowDSLTest,您可以找到一些与您正在做的事情非常相似的示例,例如&nbsp; &nbsp; public void testOr() {&nbsp; &nbsp; &nbsp; &nbsp; Result result = new Result();&nbsp; &nbsp; &nbsp; &nbsp; Variable<Person> personV = declarationOf( Person.class );&nbsp; &nbsp; &nbsp; &nbsp; Variable<Person> markV = declarationOf( Person.class );&nbsp; &nbsp; &nbsp; &nbsp; Variable<String> nameV = declarationOf( String.class );&nbsp; &nbsp; &nbsp; &nbsp; Rule rule = rule( "or" )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .build(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; or(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pattern( personV ).expr("exprA", p -> p.getName().equals("Mark")),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; and(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pattern( markV ).expr("exprA", p -> p.getName().equals("Mark")),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pattern( personV ).expr("exprB", markV, (p1, p2) -> p1.getAge() > p2.getAge())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pattern( nameV ).expr("exprC", personV, (s, p) -> s.equals( p.getName() )),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; on(nameV).execute( result::setValue )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; Model model = new ModelImpl().addRule( rule );&nbsp; &nbsp; &nbsp; &nbsp; KieBase kieBase = KieBaseBuilder.createKieBaseFromModel( model );&nbsp; &nbsp; &nbsp; &nbsp; KieSession ksession = kieBase.newKieSession();&nbsp; &nbsp; &nbsp; &nbsp; ksession.insert( "Mario" );&nbsp; &nbsp; &nbsp; &nbsp; ksession.insert(new Person("Mark", 37));&nbsp; &nbsp; &nbsp; &nbsp; ksession.insert(new Person("Edson", 35));&nbsp; &nbsp; &nbsp; &nbsp; ksession.insert(new Person("Mario", 40));&nbsp; &nbsp; &nbsp; &nbsp; ksession.fireAllRules();&nbsp; &nbsp; &nbsp; &nbsp; assertEquals("Mario", result.getValue());&nbsp; &nbsp; }希望这可以帮助
随时随地看视频慕课网APP

相关分类

Java
我要回答