最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Call from Drools Rule to Java and call to Query - Stack Overflow

programmeradmin1浏览0评论

Misterious error occurs when migrating from 5.5.0.Final version of Drools to latest Drools version 9.44.0.Final.

We have a rule that calls a Java method, and then this method calls a Query from Drools. This worked fine in older version, but now in the latest versions doesn't work. The execution hangs.

Debugging it like the current state of KieSessions' Agenda was FIRING and the Query call provokes a new item in this agenda that is blocked permanently and never returns to the code.

Sample code is the following one:

////
// DROOLS
////
query javaToDrools
  ves : Data()
end

rule "INSERT: New Item"
  when
    $data: MyData($id:id)
  then
    System.out.println("Insert Data: " + $id);
    insert(new Data($data));
    myJavaClass.droolsToJava();
end
////
// JAVA
////
public void droolsToJava() {
    QueryResults results = session.getQueryResults("javaToDrools");
    results.forEach(res -> {
        res.toString();
    });
    session.fireUntilHalt();
}

The problem is here:

StatefulKnowledgeSessionImpl.getQueryResults
-> StatefulKnowledgeSessionImpl.internalGetQueryResult
-> StatefulKnowledgeSessionImpl.evalQuery
-> StatefulKnowledgeSessionImpl.addPropagation -> It adds the Propagation

But then in the evalQuery method, the call to executeQuery.getResult() never retrieves the result. It hangs into an infinite loop.

It's like I can't insert a fact into the KieSession and call to a query at the same time...

Misterious error occurs when migrating from 5.5.0.Final version of Drools to latest Drools version 9.44.0.Final.

We have a rule that calls a Java method, and then this method calls a Query from Drools. This worked fine in older version, but now in the latest versions doesn't work. The execution hangs.

Debugging it like the current state of KieSessions' Agenda was FIRING and the Query call provokes a new item in this agenda that is blocked permanently and never returns to the code.

Sample code is the following one:

////
// DROOLS
////
query javaToDrools
  ves : Data()
end

rule "INSERT: New Item"
  when
    $data: MyData($id:id)
  then
    System.out.println("Insert Data: " + $id);
    insert(new Data($data));
    myJavaClass.droolsToJava();
end
////
// JAVA
////
public void droolsToJava() {
    QueryResults results = session.getQueryResults("javaToDrools");
    results.forEach(res -> {
        res.toString();
    });
    session.fireUntilHalt();
}

The problem is here:

StatefulKnowledgeSessionImpl.getQueryResults
-> StatefulKnowledgeSessionImpl.internalGetQueryResult
-> StatefulKnowledgeSessionImpl.evalQuery
-> StatefulKnowledgeSessionImpl.addPropagation -> It adds the Propagation

But then in the evalQuery method, the call to executeQuery.getResult() never retrieves the result. It hangs into an infinite loop.

It's like I can't insert a fact into the KieSession and call to a query at the same time...

Share Improve this question edited Nov 18, 2024 at 12:38 Marc Gil Sendra asked Nov 18, 2024 at 11:55 Marc Gil SendraMarc Gil Sendra 9312 gold badges10 silver badges24 bronze badges 2
  • Which JRE version are you using? – artiomi Commented Nov 18, 2024 at 12:11
  • I tried with Java 17 and Java 21 with same result – Marc Gil Sendra Commented Nov 18, 2024 at 12:15
Add a comment  | 

1 Answer 1

Reset to default 0

I found that the problem is that the RuleBaseConfiguration is set to be executed in SEQUENTIAL, and I can't set it to FULLY_PARALLEL due to Queries are not supported. See KnowledgeBaseImpl#checkParallelEvaluation

if (ruleBaseConfig.isParallelEvaluation()) {
  if (!rule.isMainAgendaGroup()) {
    disableParallelEvaluation( "Agenda-groups are not supported with parallel execution: disabling it" );
  } else if (rule.getActivationGroup() != null) {
    disableParallelEvaluation( "Activation-groups are not supported with parallel execution: disabling it" );
  } else if (!rule.getSalience().isDefault() && ruleBaseConfig.isParallelExecution()) {
    disableParallelEvaluation( "Salience is not supported with parallel execution: disabling it" );
  } else if (rule.isQuery()) {
    disableParallelEvaluation( "Queries are not supported with parallel execution: disabling it" );
  }
}

Latest if force to SEQUENTIAL execution.

发布评论

评论列表(0)

  1. 暂无评论