To execute this process, right-click on RuleFlowTest.java
and select "Run As..." and "Java Application". When the process in executed, the
following output should appear in the Console window:
Hello World
If you look at the code of class RuleFlowTest
(see below),
you will see that executing a process requires a few steps:
You should first create a Knowledge Base. A Knowledge Base contains all the knowledge (i.e., processes, rules, etc.) that are relevant in your application. This Knowledge Base is usually created once, and then reused. In this case, the Knowledge Base only consists of our sample process.
Next, you should create a session to interact with the engine. Note that we also add a logger to the session to log execution events and make it easier to visualize what is going on.
Finally, you can start a new instance of the process by invoking the
startProcess(String processId)
method on the session. This starts
the execution of your process instance, resulting in the executions of the
Start node, the Action node, and the End node, in this order, after which the
process instance will be completed.
package com.sample; import org.drools.KnowledgeBase; import org.drools.KnowledgeBaseFactory; import org.drools.builder.KnowledgeBuilder; import org.drools.builder.KnowledgeBuilderError; import org.drools.builder.KnowledgeBuilderErrors; import org.drools.builder.KnowledgeBuilderFactory; import org.drools.builder.KnowledgeType; import org.drools.io.ResourceFactory; import org.drools.logger.KnowledgeRuntimeLogger; import org.drools.logger.KnowledgeRuntimeLoggerFactory; import org.drools.runtime.StatefulKnowledgeSession; /** * This is a sample file to launch a process. */ public class RuleFlowTest { public static final void main(String[] args) { try { // load up the knowledge base KnowledgeBase kbase = readKnowledgeBase(); StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession(); KnowledgeRuntimeLogger logger = KnowledgeRuntimeLoggerFactory.newFileLogger(ksession, "test"); // start a new process instance ksession.startProcess("com.sample.ruleflow"); logger.close(); } catch (Throwable t) { t.printStackTrace(); } } private static KnowledgeBase readKnowledgeBase() throws Exception { KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add(ResourceFactory.newClassPathResource("ruleflow.rf"), KnowledgeType.DRF); KnowledgeBuilderErrors errors = kbuilder.getErrors(); if (errors.size() > 0) { for (KnowledgeBuilderError error: errors) { System.err.println(error); } throw new IllegalArgumentException("Could not parse knowledge."); } KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); kbase.addKnowledgePackages(kbuilder.getKnowledgePackages()); return kbase; } }
Congratulations, you have successfully executed your first process!
Because we added a logger to the session, you can easily check what happened
internally by looking at the audit log. Select the "Audit View" tab on the
bottom right, next to the Console tab. Click on the "Open Log" button (the
first one one the right of the view) and navigate to the newly created
test.log
file in your project folder. (If you are not
sure where this project folder is located, right-click on the project folder
and you will find the location in the "Resource" section). An image like the
one below should be shown. It is a tree view of the events that
occurred at runtime. Events that were executed as the direct result of another
event are shown as the children of that event. This log shows that after
starting the process, the Start node, the Action node and the End node were
triggered, in that order, after which the process instance was completed.
You can now start experimenting and designing your own process by modifying our example. Note that you can validate your process by clicking on the "Check the ruleflow model" button, i.e., the green check box action in the upper toolbar that shows up if you are editing a process. Processes will also be validated upon save, and errors will be shown in the Error View.
Continue reading our documentation to learn about our more advanced features.