Dieser Inhalt ist in der von Ihnen ausgewählten Sprache nicht verfügbar.
Chapter 3. Quick Start
3.1. Rule Basics Link kopierenLink in die Zwischenablage kopiert!
3.1.1. Stateless Knowledge Sessions Link kopierenLink in die Zwischenablage kopiert!
3.1.2. Configuring Rules in a Stateless Session Link kopierenLink in die Zwischenablage kopiert!
Procedure 3.1. Task
- Create a data model like the driver's license example below:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Write the first rule. In this example, a rule is added to disqualify any applicant younger than 18:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - When the
Applicant
object is inserted into the rule engine, each rule's constraints evaluate it and search for a match. (There is always an implied constraint of "object type" after which there can be any number of explicit field constraints.)In theIs of valid age
rule there are two constraints:- The fact being matched must be of type Applicant
- The value of Age must be less than eighteen.
$a
is a binding variable. It exists to make possible a reference to the matched object in the rule's consequence (from which place the object's properties can be updated).Note
Use of the dollar sign ($
) is optional. It helps to differentiate between variable names and field names.Note
If the rules are in the same folder as the classes, the classpath resource loader can be used to build the first knowledge base. - Use the KnowledgeBuilder to to compile the list of rules into a knowledge base as shown:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow The above code snippet looks on the classpath for thelicenseApplication.drl
file, using the methodnewClassPathResource()
. (The resource type is DRL, short for "Drools Rule Language".) - Check the KnowledgeBuilder for any errors. If there are none, you can build the session.
- Execute the data against the rules. (Since the applicant is under the age of eighteen, their application will be marked as "invalid.")
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
The preceding code executes the data against the rules. Since the applicant is under the age of 18, the application is marked as invalid.
3.1.3. Configuring Rules with Multiple Objects Link kopierenLink in die Zwischenablage kopiert!
Procedure 3.2. Task
- To execute rules against any object-implementing
iterable
(such as a collection), add another class as shown in the example code below:Copy to Clipboard Copied! Toggle word wrap Toggle overflow - In order to check that the application was made within a legitimate time-frame, add this rule:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Use the JDK converter to implement the iterable interface. (This method commences with the line
Arrays.asList(...)
.) The code shown below executes rules against an iterable list. Every collection element is inserted before any matched rules are fired:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Note
Theexecute(Object object)
andexecute(Iterable objects)
methods are actually "wrappers" around a further method calledexecute(Command command)
which comes from theBatchExecutor
interface. - Use the
CommandFactory
to create instructions, so that the following is equivalent toexecute( Iterable it )
:ksession.execute( CommandFactory.newInsertIterable( new Object[] { application, applicant } ) );
ksession.execute( CommandFactory.newInsertIterable( new Object[] { application, applicant } ) );
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Use the
BatchExecutor
andCommandFactory
when working with many different commands or result output identifiers:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Note
CommandFactory
supports many other commands that can be used in theBatchExecutor
. Some of these areStartProcess
,Query
andSetGlobal
.
3.1.4. Stateful Sessions Link kopierenLink in die Zwischenablage kopiert!
StatelessKnowledgeSession
, the StatefulKnowledgeSession
supports the BatchExecutor
interface. The only difference is the FireAllRules
command is not automatically called at the end.
Warning
dispose()
method is called after running a stateful session. This is to ensure that there are no memory leaks. This is due to the fact that knowledge bases will obtain references to stateful knowledge sessions when they are created.
3.1.5. Common Use Cases for Stateful Sessions Link kopierenLink in die Zwischenablage kopiert!
- Monitoring
- For example, you can monitor a stock market and automate the buying process.
- Diagnostics
- Stateful sessions can be used to run fault-finding processes. They could also be used for medical diagnostic processes.
- Logistical
- For example, they could be applied to problems involving parcel tracking and delivery provisioning.
- Ensuring compliance
- For example, to validate the legality of market trades.
3.1.6. Stateful Session Monitoring Example Link kopierenLink in die Zwischenablage kopiert!
Procedure 3.3. Task
- Create a model of what you want to monitor. In this example involving fire alarms, the rooms in a house have been listed. Each has one sprinkler. A fire can start in any of the rooms:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - The rules must express the relationships between multiple objects (to define things such as the presence of a sprinkler in a certain room). To do this, use a binding variable as a constraint in a pattern. This results in a cross-product.
- Create an instance of the
Fire
class and insert it into the session.The rule below adds a binding toFire
object's room field to constrain matches. This so that only the sprinkler for that room is checked. When this rule fires and the consequence executes, the sprinkler activates:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Whereas the stateless session employed standard Java syntax to modify a field, the rule above uses themodify
statement. (It acts much like a "with" statement.)
3.1.7. First Order Logic Link kopierenLink in die Zwischenablage kopiert!
3.1.8. Configuring Rules with First Order Logic Link kopierenLink in die Zwischenablage kopiert!
Procedure 3.4. Task
- Configure a pattern featuring the keyword
Not
. First order logic ensures rules will only be matched when no other keywords are present. In this example, the rule turns the sprinkler off when the fire is extinguished:Copy to Clipboard Copied! Toggle word wrap Toggle overflow - An
Alarm
object is created when there is a fire, but only oneAlarm
is needed for the entire building no matter how many fires there might be.Not
's complement,exists
can now be introduced. It matches one or more instances of a category:Copy to Clipboard Copied! Toggle word wrap Toggle overflow - If there are no more fires, the alarm must be deactivated. To turn it off, use
Not
again:Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Use this code to print a general health status message when the application first starts and also when the alarm and all of the sprinklers have been deactivated:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Store the rules in a file called
fireAlarm.drl
. Save this file in a sub-directory on the class-path. - Finally, build a
knowledge base
, using the new name,fireAlarm.drl
:Copy to Clipboard Copied! Toggle word wrap Toggle overflow
3.1.9. Rule System Sample Configuration Link kopierenLink in die Zwischenablage kopiert!
Procedure 3.5. Task
- Insert
ksession.fireAllRules()
. This grants the matched rules permission to run but, since there is no fire in this example, they will merely produce the health message:Copy to Clipboard Copied! Toggle word wrap Toggle overflow The resulting message reads:> Everything is okay
> Everything is okay
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Create and insert two fires. (A fact handle will be kept.)
- With the fires now in the engine, call
fireAllRules()
. The alarm will be raised and the respective sprinklers will be turned on:Copy to Clipboard Copied! Toggle word wrap Toggle overflow The resulting message reads:> Raise the alarm > Turn on the sprinkler for room kitchen > Turn on the sprinkler for room office
> Raise the alarm > Turn on the sprinkler for room kitchen > Turn on the sprinkler for room office
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - When the fires are extinguished, the fire objects are retracted and the sprinklers are turned off. At this point in time, the alarm is canceled and the health message displays once more:
ksession.retract( kitchenFireHandle ); ksession.retract( officeFireHandle ); ksession.fireAllRules();
ksession.retract( kitchenFireHandle ); ksession.retract( officeFireHandle ); ksession.fireAllRules();
Copy to Clipboard Copied! Toggle word wrap Toggle overflow The resulting message reads:Copy to Clipboard Copied! Toggle word wrap Toggle overflow