Drools Commands
Runtime Commands in Drools
Drools supports runtime commands that you can use in combination with KIE API operations, such as executing all rules or inserting or retracting objects in a KIE session. The full list of supported runtime commands is located in the org.drools.core.command.runtime
package in your Drools instance.
Sample runtime Commands in Drools
The following are sample runtime commands that you can use in combination with KIE API operations:
-
BatchExecutionCommand
-
InsertObjectCommand
-
RetractCommand
-
ModifyCommand
-
GetObjectCommand
-
GetObjectsCommand
-
InsertElementsCommand
-
FireAllRulesCommand
-
QueryCommand
-
SetGlobalCommand
-
GetGlobalCommand
For the full list of supported runtime commands, see the org.drools.core.command.runtime
package in your Drools instance.
Each command in this section includes a Java command example, making use of an object org.drools.compiler.test.Person
with the fields name
(String) and age
(Integer).
BatchExecutionCommand
Contains multiple commands to be executed together.
Name | Description | Requirement |
---|---|---|
|
List of commands to be executed. |
Required |
|
Sets the KIE session ID on which the commands will be executed. For stateless KIE sessions, this attribute is required. For stateful KIE sessions, this attribute is optional and if not specified, the default KIE session is used. |
Required for stateless KIE session, optional for stateful KIE session |
KIE session IDs are in the kmodule.xml file of your Drools project.
|
InsertObjectCommand insertCommand = new InsertObjectCommand(new Person("john", 25));
FireAllRulesCommand fireCommand = new FireAllRulesCommand();
BatchExecutionCommand batch = new BatchExecutionCommandImpl(Arrays.asList(insertCommand, fireCommand), "ksession1");
InsertObjectCommand
Inserts an object into the KIE session.
Name | Description | Requirement |
---|---|---|
|
The object to be inserted |
Required |
|
ID of the |
Optional |
|
Boolean to determine whether the object must be returned in the execution results (default: |
Optional |
|
Entry point for the insertion |
Optional |
Command insertObjectCommand =
CommandFactory.newInsert(new Person("john", 25), "john", false, null);
ksession.execute(insertObjectCommand);
RetractCommand
Retracts an object from the KIE session.
Name | Description | Requirement |
---|---|---|
|
The |
Required |
FactHandleFromString
RetractCommand retractCommand = new RetractCommand();
retractCommand.setFactHandleFromString("123:234:345:456:567");
FactHandle
from inserted objectRetractCommand retractCommand = new RetractCommand(factHandle);
ModifyCommand
Modifies a previously inserted object in the KIE session.
Name | Description | Requirement |
---|---|---|
|
The |
Required |
|
List of setters for object modifications |
Required |
ModifyCommand modifyCommand = new ModifyCommand(factHandle);
List<Setter> setters = new ArrayList<Setter>();
setters.add(new SetterImpl("age", "25"));
modifyCommand.setSetters(setters);
GetObjectCommand
Retrieves an object from a KIE session.
Name | Description | Requirement |
---|---|---|
|
The |
Required |
|
ID of the |
Optional |
GetObjectCommand getObjectCommand = new GetObjectCommand();
getObjectCommand.setFactHandleFromString("123:234:345:456:567");
getObjectCommand.setOutIdentifier("john");
GetObjectsCommand
Retrieves all objects from the KIE session as a collection.
Name | Description | Requirement |
---|---|---|
|
Filter for the objects returned from the KIE session |
Optional |
|
Identifier to be used in the execution results |
Optional |
GetObjectsCommand getObjectsCommand = new GetObjectsCommand();
getObjectsCommand.setOutIdentifier("objects");
InsertElementsCommand
Inserts a list of objects into the KIE session.
Name | Description | Requirement |
---|---|---|
|
The list of objects to be inserted into the KIE session |
Required |
|
ID of the |
Optional |
|
Boolean to determine whether the object must be returned in the execution results. Default value: |
Optional |
|
Entry point for the insertion |
Optional |
List<Object> objects = new ArrayList<Object>();
objects.add(new Person("john", 25));
objects.add(new Person("sarah", 35));
Command insertElementsCommand = CommandFactory.newInsertElements(objects);
FireAllRulesCommand
Executes all rules in the KIE session.
Name | Description | Requirement |
---|---|---|
|
Maximum number of rules to be executed. The default is |
Optional |
|
ID to be used for retrieving the number of fired rules in execution results. |
Optional |
|
Agenda Filter to be used for rule execution. |
Optional |
FireAllRulesCommand fireAllRulesCommand = new FireAllRulesCommand();
fireAllRulesCommand.setMax(10);
fireAllRulesCommand.setOutIdentifier("firedActivations");
QueryCommand
Executes a query defined in the KIE base.
Name | Description | Requirement |
---|---|---|
|
Query name. |
Required |
|
ID of the query results. The query results are added in the execution results with this identifier. |
Optional |
|
List of objects to be passed as a query parameter. |
Optional |
QueryCommand queryCommand = new QueryCommand();
queryCommand.setName("persons");
queryCommand.setOutIdentifier("persons");
SetGlobalCommand
Sets an object to a global state.
Name | Description | Requirement |
---|---|---|
|
ID of the global variable defined in the KIE base |
Required |
|
Object to be set into the global variable |
Optional |
|
Boolean to exclude the global variable you set from the execution results |
Optional |
|
ID of the global execution result |
Optional |
SetGlobalCommand setGlobalCommand = new SetGlobalCommand();
setGlobalCommand.setIdentifier("helper");
setGlobalCommand.setObject(new Person("kyle", 30));
setGlobalCommand.setOut(true);
setGlobalCommand.setOutIdentifier("output");
GetGlobalCommand
Retrieves a previously defined global object.
Name | Description | Requirement |
---|---|---|
|
ID of the global variable defined in the KIE base |
Required |
|
ID to be used in the execution results |
Optional |
GetGlobalCommand getGlobalCommand = new GetGlobalCommand();
getGlobalCommand.setIdentifier("helper");
getGlobalCommand.setOutIdentifier("helperOutput");