This is an automated email from the ASF dual-hosted git repository.
tkobayas pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-kie.git
The following commit(s) were added to refs/heads/main by this push:
new 751dc357ac2 [incubator-kie#7231] Add public API to use ruleflow-group
(#7264)
751dc357ac2 is described below
commit 751dc357ac2d8abd88d6d2b2774ae7b1d77c5b16
Author: Toshiya Kobayashi <[email protected]>
AuthorDate: Wed Nov 18 16:45:30 2026 +0900
[incubator-kie#7231] Add public API to use ruleflow-group (#7264)
* [incubator-kie#7231] Add public API to activate ruleflow-group
Add RuleFlowGroup.activate(), drools.activateRuleFlowGroup() RHS method,
ActivateRuleFlowGroupCommand, and KieCommands.newActivateRuleFlowGroup()
so users can activate a ruleflow-group without casting to InternalAgenda.
Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
* adjust javadoc and add tests
* copilot suggestions
* follow public API conventions
* add to revapi-config.json
---------
Co-authored-by: Claude Opus 4.6 (1M context) <[email protected]>
---
.../base/rule/consequence/ConsequenceContext.java | 9 +
.../commands/fluent/KieSessionFluentImpl.java | 7 +
.../CommandBasedStatefulKnowledgeSessionImpl.java | 6 +
.../commands/impl/CommandFactoryServiceImpl.java | 5 +
.../runtime/BatchExecutionCommandImpl.java | 2 +
.../runtime/rule/ActivateRuleFlowGroupCommand.java | 64 ++++++
.../META-INF/native-image/reflect-config.json | 9 +
.../drools/core/common/AgendaGroupQueueImpl.java | 5 +
.../event/rule/impl/SerializableRuleFlowGroup.java | 4 +
.../consequence/DefaultKnowledgeHelper.java | 4 +
.../src/main/java/org/drools/model/Drools.java | 9 +
.../modelcompiler/consequence/DroolsImpl.java | 5 +
.../session/AgendaRuleFlowGroupsTest.java | 19 ++
.../impl/sessions/RuleUnitExecutorImpl.java | 5 +
.../integrationtests/drl/RuleFlowGroupTest.java | 235 +++++++++++++++++++++
.../java/org/drools/xml/support/XStreamHelper.java | 3 +
.../java/org/drools/xml/support/XStreamXML.java | 32 +++
.../xml/support/CommandSerializationTest.java | 5 +
.../org/drools/xml/support/XStreamXMLTest.java | 10 +
kie-api/src/build/revapi-config.json | 10 +
.../main/java/org/kie/api/command/KieCommands.java | 12 +-
.../org/kie/api/runtime/rule/RuleFlowGroup.java | 16 ++
22 files changed, 475 insertions(+), 1 deletion(-)
diff --git
a/drools-base/src/main/java/org/drools/base/rule/consequence/ConsequenceContext.java
b/drools-base/src/main/java/org/drools/base/rule/consequence/ConsequenceContext.java
index c5f37db2dbb..19013495a53 100644
---
a/drools-base/src/main/java/org/drools/base/rule/consequence/ConsequenceContext.java
+++
b/drools-base/src/main/java/org/drools/base/rule/consequence/ConsequenceContext.java
@@ -116,6 +116,15 @@ public interface ConsequenceContext extends RuleContext {
void setFocus(String focus);
+ /**
+ * Activates the given ruleflow-group.
+ * @param name the name of the ruleflow-group to activate
+ * @since 10.4.0
+ * @apiNote Equivalent to {@code
getKieRuntime().getAgenda().getRuleFlowGroup(name).activate()}.
+ * This is the ruleflow-group counterpart of {@link
#setFocus(String)}.
+ */
+ void activateRuleFlowGroup(String name);
+
EntryPoint getEntryPoint(String id);
Channel getChannel(String id);
diff --git
a/drools-commands/src/main/java/org/drools/commands/fluent/KieSessionFluentImpl.java
b/drools-commands/src/main/java/org/drools/commands/fluent/KieSessionFluentImpl.java
index f296063939d..0d93325ce57 100644
---
a/drools-commands/src/main/java/org/drools/commands/fluent/KieSessionFluentImpl.java
+++
b/drools-commands/src/main/java/org/drools/commands/fluent/KieSessionFluentImpl.java
@@ -66,6 +66,13 @@ public class KieSessionFluentImpl extends
BaseBatchWithProcessFluent<KieSessionF
return this;
}
+ /**
+ * {@inheritDoc}
+ *
+ * @implNote This method currently delegates to {@link
#setActiveAgendaGroup(String)}
+ * and does not fire {@code RuleFlowGroupActivated}/{@code
RuleFlowGroupDeactivated} events.
+ * A future version will align its behaviour with {@link
org.kie.api.runtime.rule.RuleFlowGroup#activate()}.
+ */
@Override
public KieSessionFluent setActiveRuleFlowGroup(String ruleFlowGroup) {
return setActiveAgendaGroup(ruleFlowGroup);
diff --git
a/drools-commands/src/main/java/org/drools/commands/impl/CommandBasedStatefulKnowledgeSessionImpl.java
b/drools-commands/src/main/java/org/drools/commands/impl/CommandBasedStatefulKnowledgeSessionImpl.java
index c595fcfd65f..d3599b0eaba 100644
---
a/drools-commands/src/main/java/org/drools/commands/impl/CommandBasedStatefulKnowledgeSessionImpl.java
+++
b/drools-commands/src/main/java/org/drools/commands/impl/CommandBasedStatefulKnowledgeSessionImpl.java
@@ -56,6 +56,7 @@ import
org.drools.commands.runtime.process.StartCorrelatedProcessCommand;
import org.drools.commands.runtime.process.StartProcessCommand;
import org.drools.commands.runtime.process.StartProcessFromNodeIdsCommand;
import org.drools.commands.runtime.process.StartProcessInstanceCommand;
+import org.drools.commands.runtime.rule.ActivateRuleFlowGroupCommand;
import org.drools.commands.runtime.rule.AgendaGroupSetFocusCommand;
import org.drools.commands.runtime.rule.ClearActivationGroupCommand;
import org.drools.commands.runtime.rule.ClearAgendaCommand;
@@ -398,6 +399,11 @@ public class CommandBasedStatefulKnowledgeSessionImpl
extends AbstractRuntime
public String getName() {
return name;
}
+
+ public void activate() {
+ ActivateRuleFlowGroupCommand command = new
ActivateRuleFlowGroupCommand( name );
+ runner.execute( command );
+ }
};
}
};
diff --git
a/drools-commands/src/main/java/org/drools/commands/impl/CommandFactoryServiceImpl.java
b/drools-commands/src/main/java/org/drools/commands/impl/CommandFactoryServiceImpl.java
index 8d64bcf52f1..fb75b08b40c 100644
---
a/drools-commands/src/main/java/org/drools/commands/impl/CommandFactoryServiceImpl.java
+++
b/drools-commands/src/main/java/org/drools/commands/impl/CommandFactoryServiceImpl.java
@@ -38,6 +38,7 @@ import
org.drools.commands.runtime.process.CompleteWorkItemCommand;
import org.drools.commands.runtime.process.RegisterWorkItemHandlerCommand;
import org.drools.commands.runtime.process.SignalEventCommand;
import org.drools.commands.runtime.process.StartProcessCommand;
+import org.drools.commands.runtime.rule.ActivateRuleFlowGroupCommand;
import org.drools.commands.runtime.rule.AgendaGroupSetFocusCommand;
import org.drools.commands.runtime.rule.ClearActivationGroupCommand;
import org.drools.commands.runtime.rule.ClearAgendaCommand;
@@ -296,6 +297,10 @@ public class CommandFactoryServiceImpl implements
ExtendedKieCommands {
return new AgendaGroupSetFocusCommand(name);
}
+ public Command newActivateRuleFlowGroup(String name) {
+ return new ActivateRuleFlowGroupCommand(name);
+ }
+
@Override
public Command newGetFactHandles() {
return new GetFactHandlesCommand();
diff --git
a/drools-commands/src/main/java/org/drools/commands/runtime/BatchExecutionCommandImpl.java
b/drools-commands/src/main/java/org/drools/commands/runtime/BatchExecutionCommandImpl.java
index e7cf9c4ea47..aa2c813cbf9 100644
---
a/drools-commands/src/main/java/org/drools/commands/runtime/BatchExecutionCommandImpl.java
+++
b/drools-commands/src/main/java/org/drools/commands/runtime/BatchExecutionCommandImpl.java
@@ -34,6 +34,7 @@ import
org.drools.commands.runtime.process.AbortWorkItemCommand;
import org.drools.commands.runtime.process.CompleteWorkItemCommand;
import org.drools.commands.runtime.process.SignalEventCommand;
import org.drools.commands.runtime.process.StartProcessCommand;
+import org.drools.commands.runtime.rule.ActivateRuleFlowGroupCommand;
import org.drools.commands.runtime.rule.AgendaGroupSetFocusCommand;
import org.drools.commands.runtime.rule.ClearActivationGroupCommand;
import org.drools.commands.runtime.rule.ClearAgendaCommand;
@@ -97,6 +98,7 @@ public class BatchExecutionCommandImpl implements Batch,
ExecutableCommand<Execu
@XmlElement(name = "complete-work-item", type =
CompleteWorkItemCommand.class),
@XmlElement(name = "get-objects", type =
GetObjectsCommand.class),
@XmlElement(name = "set-focus", type =
AgendaGroupSetFocusCommand.class),
+ @XmlElement(name = "activate-ruleflow-group", type =
ActivateRuleFlowGroupCommand.class),
@XmlElement(name = "clear-activation-group", type =
ClearActivationGroupCommand.class),
@XmlElement(name = "clear-agenda", type =
ClearAgendaCommand.class),
@XmlElement(name = "clear-agenda-group", type =
ClearAgendaGroupCommand.class),
diff --git
a/drools-commands/src/main/java/org/drools/commands/runtime/rule/ActivateRuleFlowGroupCommand.java
b/drools-commands/src/main/java/org/drools/commands/runtime/rule/ActivateRuleFlowGroupCommand.java
new file mode 100644
index 00000000000..6300e7cc617
--- /dev/null
+++
b/drools-commands/src/main/java/org/drools/commands/runtime/rule/ActivateRuleFlowGroupCommand.java
@@ -0,0 +1,64 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.drools.commands.runtime.rule;
+
+import jakarta.xml.bind.annotation.XmlAccessType;
+import jakarta.xml.bind.annotation.XmlAccessorType;
+import jakarta.xml.bind.annotation.XmlAttribute;
+import jakarta.xml.bind.annotation.XmlRootElement;
+
+import org.kie.api.command.ExecutableCommand;
+import org.kie.api.runtime.Context;
+import org.kie.api.runtime.KieSession;
+import org.kie.internal.command.RegistryContext;
+
+@XmlRootElement
+@XmlAccessorType(XmlAccessType.NONE)
+public class ActivateRuleFlowGroupCommand implements ExecutableCommand<Void> {
+
+ @XmlAttribute(required = true)
+ private String name;
+
+ public ActivateRuleFlowGroupCommand() { }
+
+ public ActivateRuleFlowGroupCommand(String name) {
+ this.name = name;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ @Override
+ public Void execute(Context context) {
+ KieSession ksession = ((RegistryContext) context).lookup(
KieSession.class );
+ ksession.getAgenda().getRuleFlowGroup( this.name ).activate();
+ return null;
+ }
+
+ @Override
+ public String toString() {
+ return "session.getAgenda().getRuleFlowGroup(" + name +
").activate();";
+ }
+
+}
diff --git
a/drools-commands/src/main/resources/META-INF/native-image/reflect-config.json
b/drools-commands/src/main/resources/META-INF/native-image/reflect-config.json
index 0e7203cac09..177cafd72b2 100644
---
a/drools-commands/src/main/resources/META-INF/native-image/reflect-config.json
+++
b/drools-commands/src/main/resources/META-INF/native-image/reflect-config.json
@@ -170,6 +170,15 @@
"allDeclaredClasses": true,
"allPublicClasses": true
},
+ {
+ "name": "org.drools.commands.runtime.rule.ActivateRuleFlowGroupCommand",
+ "allDeclaredConstructors": true,
+ "allPublicConstructors": true,
+ "allDeclaredMethods": true,
+ "allPublicMethods": true,
+ "allDeclaredClasses": true,
+ "allPublicClasses": true
+ },
{
"name": "org.drools.commands.runtime.rule.AgendaGroupSetFocusCommand",
"allDeclaredConstructors": true,
diff --git
a/drools-core/src/main/java/org/drools/core/common/AgendaGroupQueueImpl.java
b/drools-core/src/main/java/org/drools/core/common/AgendaGroupQueueImpl.java
index 7f57cc14663..cc66970f1a2 100644
--- a/drools-core/src/main/java/org/drools/core/common/AgendaGroupQueueImpl.java
+++ b/drools-core/src/main/java/org/drools/core/common/AgendaGroupQueueImpl.java
@@ -131,6 +131,11 @@ public class AgendaGroupQueueImpl
reteEvaluator.addPropagation( new SetFocusAction( this.name ) );
}
+ @Override
+ public void activate() {
+ ((InternalAgenda)
reteEvaluator.getActivationsManager()).activateRuleFlowGroup(this.name);
+ }
+
public class SetFocusAction extends
AbstractPropagationEntry<ReteEvaluator> {
private final String name;
diff --git
a/drools-core/src/main/java/org/drools/core/event/rule/impl/SerializableRuleFlowGroup.java
b/drools-core/src/main/java/org/drools/core/event/rule/impl/SerializableRuleFlowGroup.java
index f1d3562c153..a71c5e9e560 100644
---
a/drools-core/src/main/java/org/drools/core/event/rule/impl/SerializableRuleFlowGroup.java
+++
b/drools-core/src/main/java/org/drools/core/event/rule/impl/SerializableRuleFlowGroup.java
@@ -53,4 +53,8 @@ public class SerializableRuleFlowGroup implements
RuleFlowGroup, Externalizable
throw new UnsupportedOperationException();
}
+ public void activate() {
+ throw new UnsupportedOperationException();
+ }
+
}
diff --git
a/drools-kiesession/src/main/java/org/drools/kiesession/consequence/DefaultKnowledgeHelper.java
b/drools-kiesession/src/main/java/org/drools/kiesession/consequence/DefaultKnowledgeHelper.java
index 2ab10f1bb75..709d4d367bf 100644
---
a/drools-kiesession/src/main/java/org/drools/kiesession/consequence/DefaultKnowledgeHelper.java
+++
b/drools-kiesession/src/main/java/org/drools/kiesession/consequence/DefaultKnowledgeHelper.java
@@ -317,6 +317,10 @@ public class DefaultKnowledgeHelper implements
KnowledgeHelper, Externalizable {
toStatefulKnowledgeSession().setFocus( focus );
}
+ public void activateRuleFlowGroup(final String name) {
+ toStatefulKnowledgeSession().getAgenda().getRuleFlowGroup( name
).activate();
+ }
+
public Object get(final Declaration declaration) {
return declaration.getValue(reteEvaluator, tuple );
}
diff --git
a/drools-model/drools-canonical-model/src/main/java/org/drools/model/Drools.java
b/drools-model/drools-canonical-model/src/main/java/org/drools/model/Drools.java
index e15dacf2f94..d88d187b37b 100644
---
a/drools-model/drools-canonical-model/src/main/java/org/drools/model/Drools.java
+++
b/drools-model/drools-canonical-model/src/main/java/org/drools/model/Drools.java
@@ -30,6 +30,15 @@ public interface Drools extends DroolsEntryPoint {
void setFocus(String focus);
+ /**
+ * Activates the given ruleflow-group.
+ * @param name the name of the ruleflow-group to activate
+ * @since 10.4.0
+ * @apiNote Equivalent to {@code
getKieRuntime().getAgenda().getRuleFlowGroup(name).activate()}.
+ * This is the ruleflow-group counterpart of {@link
#setFocus(String)}.
+ */
+ void activateRuleFlowGroup(String name);
+
Channel getChannel(String name);
void logicalInsert(Object object);
diff --git
a/drools-model/drools-model-compiler/src/main/java/org/drools/modelcompiler/consequence/DroolsImpl.java
b/drools-model/drools-model-compiler/src/main/java/org/drools/modelcompiler/consequence/DroolsImpl.java
index 53942ba6cbe..aa5e824c786 100644
---
a/drools-model/drools-model-compiler/src/main/java/org/drools/modelcompiler/consequence/DroolsImpl.java
+++
b/drools-model/drools-model-compiler/src/main/java/org/drools/modelcompiler/consequence/DroolsImpl.java
@@ -164,6 +164,11 @@ public class DroolsImpl implements Drools,
org.kie.api.runtime.rule.RuleContext
knowledgeHelper.setFocus( focus );
}
+ @Override
+ public void activateRuleFlowGroup( String name ) {
+ knowledgeHelper.activateRuleFlowGroup( name );
+ }
+
@Override
public KieBase getKieBase() {
return (KieBase) reteEvaluator.getKnowledgeBase();
diff --git
a/drools-persistence/drools-persistence-jpa/src/test/java/org/drools/persistence/session/AgendaRuleFlowGroupsTest.java
b/drools-persistence/drools-persistence-jpa/src/test/java/org/drools/persistence/session/AgendaRuleFlowGroupsTest.java
index d818e7442a1..3f0b6063837 100644
---
a/drools-persistence/drools-persistence-jpa/src/test/java/org/drools/persistence/session/AgendaRuleFlowGroupsTest.java
+++
b/drools-persistence/drools-persistence-jpa/src/test/java/org/drools/persistence/session/AgendaRuleFlowGroupsTest.java
@@ -100,6 +100,25 @@ public class AgendaRuleFlowGroupsTest {
assertThat(groups[1].getName()).isEqualTo("ruleflow-group");
}
+ @ParameterizedTest
+ @MethodSource("parameters")
+ public void testRuleFlowGroupActivateViaPublicApi(boolean locking) throws
Exception {
+ //
CommandBasedStatefulKnowledgeSessionImpl.getAgenda().getRuleFlowGroup().activate()
goes through ActivateRuleFlowGroupCommand
+ CommandBasedStatefulKnowledgeSessionImpl ksession =
createSession(locking, -1, "ruleflow-groups.drl");
+ try {
+ List<String> list = new ArrayList<String>();
+ list.add("Test");
+ ksession.insert(list);
+
+ assertThat(ksession.fireAllRules()).isEqualTo(0);
+
+ ksession.getAgenda().getRuleFlowGroup("ruleflow-group").activate();
+ assertThat(ksession.fireAllRules()).isEqualTo(1);
+ } finally {
+ ksession.dispose();
+ }
+ }
+
@DisabledIfSystemProperty(named = "drools.drl.antlr4.parser.enabled",
matches = "true")
@ParameterizedTest
@MethodSource("parameters")
diff --git
a/drools-ruleunits/drools-ruleunits-impl/src/main/java/org/drools/ruleunits/impl/sessions/RuleUnitExecutorImpl.java
b/drools-ruleunits/drools-ruleunits-impl/src/main/java/org/drools/ruleunits/impl/sessions/RuleUnitExecutorImpl.java
index aac556f9e65..f326a93dd67 100644
---
a/drools-ruleunits/drools-ruleunits-impl/src/main/java/org/drools/ruleunits/impl/sessions/RuleUnitExecutorImpl.java
+++
b/drools-ruleunits/drools-ruleunits-impl/src/main/java/org/drools/ruleunits/impl/sessions/RuleUnitExecutorImpl.java
@@ -487,6 +487,11 @@ public class RuleUnitExecutorImpl implements ReteEvaluator
{
knowledgeHelper.setFocus(focus);
}
+ @Override
+ public void activateRuleFlowGroup(String name) {
+ knowledgeHelper.activateRuleFlowGroup(name);
+ }
+
@Override
public Object get(Declaration declaration) {
return knowledgeHelper.get(declaration);
diff --git
a/drools-test-coverage/test-compiler-integration/src/test/java/org/drools/compiler/integrationtests/drl/RuleFlowGroupTest.java
b/drools-test-coverage/test-compiler-integration/src/test/java/org/drools/compiler/integrationtests/drl/RuleFlowGroupTest.java
index 0574151c785..156d6dd3b98 100644
---
a/drools-test-coverage/test-compiler-integration/src/test/java/org/drools/compiler/integrationtests/drl/RuleFlowGroupTest.java
+++
b/drools-test-coverage/test-compiler-integration/src/test/java/org/drools/compiler/integrationtests/drl/RuleFlowGroupTest.java
@@ -18,6 +18,9 @@
*/
package org.drools.compiler.integrationtests.drl;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
import java.util.stream.Stream;
import org.drools.core.common.InternalAgenda;
@@ -29,6 +32,11 @@ import
org.drools.testcoverage.common.util.TestParametersUtil2;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.kie.api.KieBase;
+import org.kie.api.KieServices;
+import org.kie.api.command.KieCommands;
+import org.kie.api.event.rule.DefaultAgendaEventListener;
+import org.kie.api.event.rule.RuleFlowGroupActivatedEvent;
+import org.kie.api.runtime.ExecutionResults;
import org.kie.api.runtime.KieSession;
import static org.assertj.core.api.Assertions.assertThat;
@@ -75,4 +83,231 @@ public class RuleFlowGroupTest {
}
}
+ @ParameterizedTest(name = "KieBase type={0}")
+ @MethodSource("parameters")
+ public void testActivateFromJava(KieBaseTestConfiguration
kieBaseTestConfiguration) {
+ final String drl =
+ "import " + Person.class.getCanonicalName() + ";\n" +
+ "rule R1\n" +
+ "ruleflow-group \"group1\"\n" +
+ "when\n" +
+ " $p : Person()\n" +
+ "then\n" +
+ " $p.setName(\"activated\");\n" +
+ "end\n";
+
+ final KieBase kbase =
KieBaseUtil.getKieBaseFromKieModuleFromDrl("ruleflowgroup-test",
kieBaseTestConfiguration, drl);
+ final KieSession ksession = kbase.newKieSession();
+ try {
+ final Person person = new Person();
+ ksession.insert(person);
+ ksession.getAgenda().getRuleFlowGroup("group1").activate();
+ assertThat(ksession.fireAllRules()).isEqualTo(1);
+ assertThat(person.getName()).isEqualTo("activated");
+ } finally {
+ ksession.dispose();
+ }
+ }
+
+ @ParameterizedTest(name = "KieBase type={0}")
+ @MethodSource("parameters")
+ public void
testActivateFiresRuleFlowGroupActivatedEvent(KieBaseTestConfiguration
kieBaseTestConfiguration) {
+ final String drl =
+ "import " + Person.class.getCanonicalName() + ";\n" +
+ "rule R1\n" +
+ "ruleflow-group \"group1\"\n" +
+ "when\n" +
+ " $p : Person()\n" +
+ "then\n" +
+ "end\n";
+
+ final KieBase kbase =
KieBaseUtil.getKieBaseFromKieModuleFromDrl("ruleflowgroup-test",
kieBaseTestConfiguration, drl);
+ final KieSession ksession = kbase.newKieSession();
+ try {
+ final List<String> events = new ArrayList<>();
+ ksession.addEventListener(new DefaultAgendaEventListener() {
+ @Override
+ public void
beforeRuleFlowGroupActivated(RuleFlowGroupActivatedEvent event) {
+ events.add("before:" + event.getRuleFlowGroup().getName());
+ }
+ @Override
+ public void
afterRuleFlowGroupActivated(RuleFlowGroupActivatedEvent event) {
+ events.add("after:" + event.getRuleFlowGroup().getName());
+ }
+ });
+
+ ksession.insert(new Person());
+ ksession.getAgenda().getRuleFlowGroup("group1").activate();
+ assertThat(events).containsExactly("before:group1",
"after:group1");
+ } finally {
+ ksession.dispose();
+ }
+ }
+
+ @ParameterizedTest(name = "KieBase type={0}")
+ @MethodSource("parameters")
+ public void testActivateTwoGroupsStackOrder(KieBaseTestConfiguration
kieBaseTestConfiguration) {
+ final String drl =
+ "import " + Person.class.getCanonicalName() + ";\n" +
+ "global java.util.List results;\n" +
+ "rule R1\n" +
+ "ruleflow-group \"group1\"\n" +
+ "when\n" +
+ " $p : Person()\n" +
+ "then\n" +
+ " results.add(\"group1\");\n" +
+ "end\n" +
+ "rule R2\n" +
+ "ruleflow-group \"group2\"\n" +
+ "when\n" +
+ " $p : Person()\n" +
+ "then\n" +
+ " results.add(\"group2\");\n" +
+ "end\n";
+
+ final KieBase kbase =
KieBaseUtil.getKieBaseFromKieModuleFromDrl("ruleflowgroup-test",
kieBaseTestConfiguration, drl);
+ final KieSession ksession = kbase.newKieSession();
+ try {
+ final List<String> results = new ArrayList<>();
+ ksession.setGlobal("results", results);
+ ksession.insert(new Person());
+ ksession.getAgenda().getRuleFlowGroup("group1").activate();
+ ksession.getAgenda().getRuleFlowGroup("group2").activate();
+ ksession.fireAllRules();
+ // group2 was pushed last, so it fires first (stack order)
+ assertThat(results).containsExactly("group2", "group1");
+ } finally {
+ ksession.dispose();
+ }
+ }
+
+ @ParameterizedTest(name = "KieBase type={0}")
+ @MethodSource("parameters")
+ public void testActivateRuleFlowGroupFromRHS(KieBaseTestConfiguration
kieBaseTestConfiguration) {
+ final String drl =
+ "import " + Person.class.getCanonicalName() + ";\n" +
+ "global java.util.List results;\n" +
+ "rule R1\n" +
+ "ruleflow-group \"group1\"\n" +
+ "when\n" +
+ " $p : Person()\n" +
+ "then\n" +
+ " results.add(\"R1\");\n" +
+ " drools.activateRuleFlowGroup(\"group2\");\n" +
+ "end\n" +
+ "rule R2\n" +
+ "ruleflow-group \"group2\"\n" +
+ "when\n" +
+ " $p : Person()\n" +
+ "then\n" +
+ " results.add(\"R2\");\n" +
+ "end\n";
+
+ final KieBase kbase =
KieBaseUtil.getKieBaseFromKieModuleFromDrl("ruleflowgroup-test",
kieBaseTestConfiguration, drl);
+ final KieSession ksession = kbase.newKieSession();
+ try {
+ final List<String> results = new ArrayList<>();
+ ksession.setGlobal("results", results);
+ ksession.insert(new Person());
+ ksession.getAgenda().getRuleFlowGroup("group1").activate();
+ ksession.fireAllRules();
+ assertThat(results).containsExactly("R1", "R2");
+ } finally {
+ ksession.dispose();
+ }
+ }
+
+ @ParameterizedTest(name = "KieBase type={0}")
+ @MethodSource("parameters")
+ public void
testActivateRuleFlowGroupFromRHSWithMvelDialect(KieBaseTestConfiguration
kieBaseTestConfiguration) {
+ final String drl =
+ "import " + Person.class.getCanonicalName() + ";\n" +
+ "global java.util.List results;\n" +
+ "rule R1\n" +
+ "ruleflow-group \"group1\"\n" +
+ "dialect \"mvel\"\n" +
+ "when\n" +
+ " $p : Person()\n" +
+ "then\n" +
+ " results.add(\"R1\");\n" +
+ " drools.activateRuleFlowGroup(\"group2\");\n" +
+ "end\n" +
+ "rule R2\n" +
+ "ruleflow-group \"group2\"\n" +
+ "when\n" +
+ " $p : Person()\n" +
+ "then\n" +
+ " results.add(\"R2\");\n" +
+ "end\n";
+
+ final KieBase kbase =
KieBaseUtil.getKieBaseFromKieModuleFromDrl("ruleflowgroup-test",
kieBaseTestConfiguration, drl);
+ final KieSession ksession = kbase.newKieSession();
+ try {
+ final List<String> results = new ArrayList<>();
+ ksession.setGlobal("results", results);
+ ksession.insert(new Person());
+ ksession.getAgenda().getRuleFlowGroup("group1").activate();
+ ksession.fireAllRules();
+ assertThat(results).containsExactly("R1", "R2");
+ } finally {
+ ksession.dispose();
+ }
+ }
+
+ @ParameterizedTest(name = "KieBase type={0}")
+ @MethodSource("parameters")
+ public void
testActivateRuleFlowGroupCommandInBatch(KieBaseTestConfiguration
kieBaseTestConfiguration) {
+ final String drl =
+ "import " + Person.class.getCanonicalName() + ";\n" +
+ "rule R1\n" +
+ "ruleflow-group \"group1\"\n" +
+ "when\n" +
+ " $p : Person()\n" +
+ "then\n" +
+ " $p.setName(\"activated\");\n" +
+ "end\n";
+
+ final KieBase kbase =
KieBaseUtil.getKieBaseFromKieModuleFromDrl("ruleflowgroup-test",
kieBaseTestConfiguration, drl);
+ final KieSession ksession = kbase.newKieSession();
+ try {
+ final KieCommands commands = KieServices.get().getCommands();
+ final Person person = new Person();
+ final ExecutionResults results =
ksession.execute(commands.newBatchExecution(Arrays.asList(
+ commands.newInsert(person),
+ commands.newActivateRuleFlowGroup("group1"),
+ commands.newFireAllRules("fired"))));
+ assertThat(results.getValue("fired")).isEqualTo(1);
+ assertThat(person.getName()).isEqualTo("activated");
+ } finally {
+ ksession.dispose();
+ }
+ }
+
+ @ParameterizedTest(name = "KieBase type={0}")
+ @MethodSource("parameters")
+ public void testClearOnActivatedGroup(KieBaseTestConfiguration
kieBaseTestConfiguration) {
+ final String drl =
+ "import " + Person.class.getCanonicalName() + ";\n" +
+ "rule R1\n" +
+ "ruleflow-group \"group1\"\n" +
+ "when\n" +
+ " $p : Person()\n" +
+ "then\n" +
+ " $p.setName(\"fired\");\n" +
+ "end\n";
+
+ final KieBase kbase =
KieBaseUtil.getKieBaseFromKieModuleFromDrl("ruleflowgroup-test",
kieBaseTestConfiguration, drl);
+ final KieSession ksession = kbase.newKieSession();
+ try {
+ final Person person = new Person();
+ ksession.insert(person);
+ ksession.getAgenda().getRuleFlowGroup("group1").activate();
+ ksession.getAgenda().getRuleFlowGroup("group1").clear();
+ assertThat(ksession.fireAllRules()).isEqualTo(0);
+ assertThat(person.getName()).isNotEqualTo("fired");
+ } finally {
+ ksession.dispose();
+ }
+ }
+
}
diff --git
a/drools-xml-support/src/main/java/org/drools/xml/support/XStreamHelper.java
b/drools-xml-support/src/main/java/org/drools/xml/support/XStreamHelper.java
index 83198a5e78a..df2f13837d1 100644
--- a/drools-xml-support/src/main/java/org/drools/xml/support/XStreamHelper.java
+++ b/drools-xml-support/src/main/java/org/drools/xml/support/XStreamHelper.java
@@ -29,6 +29,7 @@ import
org.drools.commands.runtime.process.AbortWorkItemCommand;
import org.drools.commands.runtime.process.CompleteWorkItemCommand;
import org.drools.commands.runtime.process.SignalEventCommand;
import org.drools.commands.runtime.process.StartProcessCommand;
+import org.drools.commands.runtime.rule.ActivateRuleFlowGroupCommand;
import org.drools.commands.runtime.rule.AgendaGroupSetFocusCommand;
import org.drools.commands.runtime.rule.ClearActivationGroupCommand;
import org.drools.commands.runtime.rule.ClearAgendaCommand;
@@ -103,6 +104,8 @@ public class XStreamHelper {
DefaultFactHandle.class );
xstream.alias( "set-focus",
AgendaGroupSetFocusCommand.class );
+ xstream.alias( "activate-ruleflow-group",
+ ActivateRuleFlowGroupCommand.class );
xstream.alias( "clear-activation-group",
ClearActivationGroupCommand.class );
xstream.alias( "clear-agenda",
diff --git
a/drools-xml-support/src/main/java/org/drools/xml/support/XStreamXML.java
b/drools-xml-support/src/main/java/org/drools/xml/support/XStreamXML.java
index afeb9452700..091ac54caea 100644
--- a/drools-xml-support/src/main/java/org/drools/xml/support/XStreamXML.java
+++ b/drools-xml-support/src/main/java/org/drools/xml/support/XStreamXML.java
@@ -51,6 +51,7 @@ import
org.drools.commands.runtime.process.AbortWorkItemCommand;
import org.drools.commands.runtime.process.CompleteWorkItemCommand;
import org.drools.commands.runtime.process.SignalEventCommand;
import org.drools.commands.runtime.process.StartProcessCommand;
+import org.drools.commands.runtime.rule.ActivateRuleFlowGroupCommand;
import org.drools.commands.runtime.rule.AgendaGroupSetFocusCommand;
import org.drools.commands.runtime.rule.ClearActivationGroupCommand;
import org.drools.commands.runtime.rule.ClearAgendaCommand;
@@ -92,6 +93,7 @@ public class XStreamXML {
xstream.addImplicitCollection( BatchExecutionCommandImpl.class,
"commands" );
+ xstream.registerConverter( new ActivateRuleFlowGroupConverter( xstream
) );
xstream.registerConverter( new AgendaGroupSetFocusConverter( xstream )
);
xstream.registerConverter( new ClearActivationGroupConverter( xstream
) );
xstream.registerConverter( new ClearAgendaConverter( xstream ) );
@@ -1215,6 +1217,36 @@ public class XStreamXML {
}
}
+ public static class ActivateRuleFlowGroupConverter extends
AbstractCollectionConverter
+ implements
+ Converter {
+
+ public ActivateRuleFlowGroupConverter(XStream xstream) {
+ super( xstream.getMapper() );
+ }
+
+ public void marshal(Object object,
+ HierarchicalStreamWriter writer,
+ MarshallingContext context) {
+ ActivateRuleFlowGroupCommand cmd = (ActivateRuleFlowGroupCommand)
object;
+
+ writer.addAttribute( "name",
+ cmd.getName() );
+ }
+
+ public Object unmarshal(HierarchicalStreamReader reader,
+ UnmarshallingContext context) {
+ String name = reader.getAttribute( "name" );
+
+ ActivateRuleFlowGroupCommand cmd = new
ActivateRuleFlowGroupCommand( name );
+ return cmd;
+ }
+
+ public boolean canConvert(Class clazz) {
+ return clazz.equals( ActivateRuleFlowGroupCommand.class );
+ }
+ }
+
public static class AgendaGroupSetFocusConverter extends
AbstractCollectionConverter
implements
Converter {
diff --git
a/drools-xml-support/src/test/java/org/drools/xml/support/CommandSerializationTest.java
b/drools-xml-support/src/test/java/org/drools/xml/support/CommandSerializationTest.java
index be2e6b7c048..1234ada695a 100644
---
a/drools-xml-support/src/test/java/org/drools/xml/support/CommandSerializationTest.java
+++
b/drools-xml-support/src/test/java/org/drools/xml/support/CommandSerializationTest.java
@@ -51,6 +51,7 @@ import
org.drools.commands.runtime.process.AbortWorkItemCommand;
import org.drools.commands.runtime.process.CompleteWorkItemCommand;
import org.drools.commands.runtime.process.SignalEventCommand;
import org.drools.commands.runtime.process.StartProcessCommand;
+import org.drools.commands.runtime.rule.ActivateRuleFlowGroupCommand;
import org.drools.commands.runtime.rule.AgendaGroupSetFocusCommand;
import org.drools.commands.runtime.rule.ClearActivationGroupCommand;
import org.drools.commands.runtime.rule.ClearAgendaCommand;
@@ -341,6 +342,10 @@ public class CommandSerializationTest {
batchCmd.addCommand(cmd);
}
+ {
+ ActivateRuleFlowGroupCommand cmd = new
ActivateRuleFlowGroupCommand(randomString());
+ batchCmd.addCommand(cmd);
+ }
{
AgendaGroupSetFocusCommand cmd = new
AgendaGroupSetFocusCommand(randomString());
batchCmd.addCommand(cmd);
diff --git
a/drools-xml-support/src/test/java/org/drools/xml/support/XStreamXMLTest.java
b/drools-xml-support/src/test/java/org/drools/xml/support/XStreamXMLTest.java
index 4d452688aa0..9c9530a4f49 100644
---
a/drools-xml-support/src/test/java/org/drools/xml/support/XStreamXMLTest.java
+++
b/drools-xml-support/src/test/java/org/drools/xml/support/XStreamXMLTest.java
@@ -31,6 +31,7 @@ import com.thoughtworks.xstream.XStream;
import org.drools.commands.runtime.rule.ModifyCommand.SetterImpl;
import org.drools.core.base.RuleNameEndsWithAgendaFilter;
import org.drools.commands.runtime.process.StartProcessCommand;
+import org.drools.commands.runtime.rule.ActivateRuleFlowGroupCommand;
import org.drools.commands.runtime.rule.AgendaGroupSetFocusCommand;
import org.drools.commands.runtime.rule.ClearActivationGroupCommand;
import org.drools.commands.runtime.rule.ClearAgendaCommand;
@@ -88,6 +89,15 @@ public class XStreamXMLTest {
assertThat(cmd.getProcessId()).isEqualTo("some-process-id");
}
+ @Test
+ public void testMarshallActivateRuleFlowGroupCommand() {
+ ActivateRuleFlowGroupCommand cmd = new
ActivateRuleFlowGroupCommand("foo-group");
+ String xmlString = xstream.toXML( cmd );
+ assertThat(xmlString).isEqualTo("<activate-ruleflow-group
name=\"foo-group\"/>");
+ ActivateRuleFlowGroupCommand cmd2 = (ActivateRuleFlowGroupCommand)
xstream.fromXML( xmlString );
+ assertThat(cmd2.getName()).isEqualTo(cmd.getName());
+ }
+
@Test
public void testMarshallAgendaGroupSetFocusCommand() {
AgendaGroupSetFocusCommand cmd = new
AgendaGroupSetFocusCommand("foo-group");
diff --git a/kie-api/src/build/revapi-config.json
b/kie-api/src/build/revapi-config.json
index 528a7b4a5f8..a57b434ab3a 100644
--- a/kie-api/src/build/revapi-config.json
+++ b/kie-api/src/build/revapi-config.json
@@ -8,6 +8,16 @@
"code": "java.annotation.attributeAdded",
"annotationType": "java.lang.Deprecated",
"justification": "Adding version information (since/forRemoval
attributes) to existing @Deprecated annotations is a documentation improvement
that does not affect binary or source compatibility"
+ },
+ {
+ "code": "java.method.addedToInterface",
+ "new": "method void
org.kie.api.runtime.rule.RuleFlowGroup::activate()",
+ "justification": "incubator-kie#7231: Added public API to activate a
ruleflow-group. RuleFlowGroup is implemented only by Drools internal classes"
+ },
+ {
+ "code": "java.method.addedToInterface",
+ "new": "method org.kie.api.command.Command
org.kie.api.command.KieCommands::newActivateRuleFlowGroup(java.lang.String)",
+ "justification": "incubator-kie#7231: Added command factory method
to activate a ruleflow-group. KieCommands is implemented only by Drools
internal classes"
}
]
}
diff --git a/kie-api/src/main/java/org/kie/api/command/KieCommands.java
b/kie-api/src/main/java/org/kie/api/command/KieCommands.java
index 4573248386a..f5e2477c49d 100644
--- a/kie-api/src/main/java/org/kie/api/command/KieCommands.java
+++ b/kie-api/src/main/java/org/kie/api/command/KieCommands.java
@@ -143,7 +143,17 @@ public interface KieCommands extends KieService {
Command<FactHandle> fromExternalFactHandleCommand(String
factHandleExternalForm, boolean disconnected);
Command newAgendaGroupSetFocus(String name);
-
+
+ /**
+ * Creates a command that activates the given ruleflow-group.
+ * @param name the name of the ruleflow-group to activate
+ * @return the command
+ * @since 10.4.0
+ * @apiNote Equivalent to {@code
ksession.getAgenda().getRuleFlowGroup(name).activate()}.
+ * This is the ruleflow-group counterpart of {@link
#newAgendaGroupSetFocus(String)}.
+ */
+ Command newActivateRuleFlowGroup(String name);
+
Command newGetFactHandles();
Command newGetFactHandles(String outIdentifier);
diff --git a/kie-api/src/main/java/org/kie/api/runtime/rule/RuleFlowGroup.java
b/kie-api/src/main/java/org/kie/api/runtime/rule/RuleFlowGroup.java
index c3000a2c062..6fe8b49e06f 100644
--- a/kie-api/src/main/java/org/kie/api/runtime/rule/RuleFlowGroup.java
+++ b/kie-api/src/main/java/org/kie/api/runtime/rule/RuleFlowGroup.java
@@ -26,4 +26,20 @@ public interface RuleFlowGroup {
public String getName();
public void clear();
+
+ /**
+ * Activates this ruleflow-group by pushing it onto the focus stack,
+ * so that its rules become eligible to fire.
+ * <p>
+ * Before/after {@code RuleFlowGroupActivated} events are fired
synchronously
+ * on the calling thread. The corresponding {@code
RuleFlowGroupDeactivated}
+ * event fires only when the group was activated by a process node
instance;
+ * a plain {@code activate()} call never fires {@code
RuleFlowGroupDeactivated}.
+ *
+ * @since 10.4.0
+ * @apiNote This is the ruleflow-group counterpart of {@link
AgendaGroup#setFocus()}.
+ * In addition to pushing the group onto the focus stack, it fires
+ * {@code RuleFlowGroupActivated} events.
+ */
+ void activate();
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]