JBoss Drools Introduction
JBoss Drools Introduction
JBoss Drools Introduction
iii
Drools Introduction and Gener...
iv
v
vi
Chapter 1.
Chapter 1. Welcome
I've always stated that end business users struggle understanding the differences between rules
and processes, and more recently rules and event processing. For them they have this problem
in their mind and they just want to model it using some software. The traditional way of using
two vendor offerings forces the business user to work with a process oriented or rules oriented
approach which just gets in the way, often with great confusion over which tool they should be
using to model which bit.
PegaSystems and Microsoft have done a great job of showing that the two can be combined
and a behavioural modelling approach can be used. This allows the business user to work more
naturally where the full range of approaches is available to them, without the tools getting in the
way. From being process oriented to rule oriented or shades of grey in the middle - whatever
suites the problem being modelled at that time.
Drools 5.0 takes this one step further by not only adding BPMN2 based workflow with Drools Flow
but also adding event processing with Drools Fusion, creating a more holistic approach to software
development. Where the term holistic is used for emphasizing the importance of the whole and
the interdependence of its parts.
Drools 5.0 is now split into 5 modules, each with their own manual - Guvnor (BRMS/BPMS),
Expert (Rules), Fusion (CEP), Flow (Process/Workflow) and Planner. Guvnor is our web based
governance system, traditionally referred to in the rules world as a BRMS. We decided to move
away from the BRMS term to a play on governance as it's not rules specific. Expert is the traditional
rules engine. Fusion is the event processing side, it's a play on data/sensor fusion terminology.
Flow is our workflow module, Kris Verlaenen leads this and has done some amazing work; he's
currently moving flow to be incorporated into jBPM 5. The fith module called Planner, authored by
Geoffrey De Smet, solves allocation and scheduling type problem and while still in the early stage
of development is showing a lot of promise. We hope to add Semantics for 2011, based around
description logc, and that is being work on as part of the next generaion Drools designs.
I've been working in the rules field now for around 7 years and I finally feel like I'm getting to grips
with things and ideas are starting to gel and the real innovation is starting to happen. To me It feels
like we actually know what we are doing now, compared to the past where there was a lot of wild
guessing and exploration. I've been working hard on the next generation Drools Expert design
document with Edson Tirelli and Davide Sottara. I invite you to read the document and get involved,
http://community.jboss.org/wiki/DroolsLanguageEnhancements. The document takes things to
the next level pushing Drools forward as a hybrid engine, not just a capable production rule system,
but also melding in logic programming (prolog) with functional programming and description logic
along with a host of other ideas for a more expressive and modern feeling language.
I hope you can feel the passion that my team and I have while working on Drools, and that some
of it rubs off on you during your adventures.
1
Chapter 1. Welcome
2
Chapter 2.
A simple way to get started is to download and install the Eclipse plug-in - this will also require the
Eclipse GEF framework to be installed (see below, if you don't have it installed already). This will
provide you with all the dependencies you need to get going: you can simply create a new rule
project and everything will be done for you. Refer to the chapter on the Rule Workbench and IDE
for detailed instructions on this. Installing the Eclipse plug-in is generally as simple as unzipping
a file into your Eclipse plug-in directory.
Use of the Eclipse plug-in is not required. Rule files are just textual input (or spreadsheets as the
case may be) and the IDE (also known as the Rule Workbench) is just a convenience. People
have integrated the rule engine in many ways, there is no "one size fits all".
Alternatively, you can download the binary distribution, and include the relevant jars in your
projects classpath.
The following is a description of the important libraries that make up JBoss Drools
• knowledge-api.jar - this provides the interfaces and factories. It also helps clearly show what is
intended as a user api and what is just an engine api.
• drools-core.jar - this is the core engine, runtime component. Contains both the RETE engine
and the LEAPS engine. This is the only runtime dependency if you are pre-compiling rules (and
deploying via Package or RuleBase objects).
• drools-compiler.jar - this contains the compiler/builder components to take rule source, and build
executable rule bases. This is often a runtime dependency of your application, but it need not
be if you are pre-compiling your rules. This depends on drools-core.
3
Chapter 2. Installation and S...
• drools-jsr94.jar - this is the JSR-94 compliant implementation, this is essentially a layer over
the drools-compiler component. Note that due to the nature of the JSR-94 specification, not all
features are easily exposed via this interface. In some cases, it will be easier to go direct to the
Drools API, but in some environments the JSR-94 is mandated.
• drools-decisiontables.jar - this is the decision tables 'compiler' component, which uses the
drools-compiler component. This supports both excel and CSV input formats.
There are quite a few other dependencies which the above components require, most of which
are for the drools-compiler, drools-jsr94 or drools-decisiontables module. Some key ones to note
are "POI" which provides the spreadsheet parsing ability, and "antlr" which provides the parsing
for the rule language itself.
NOTE: if you are using Drools in J2EE or servlet containers and you come across classpath issues
with "JDT", then you can switch to the janino compiler. Set the system property "drools.compiler":
For example: -Ddrools.compiler=JANINO.
For up to date info on dependencies in a release, consult the released poms, which can be found
on the maven repository.
2.1.2. Runtime
The "runtime" requirements mentioned here are if you are deploying rules as their binary form
(either as KnowledgePackage objects, or KnowledgeBase objects etc). This is an optional feature
that allows you to keep your runtime very light. You may use drools-compiler to produce rule
packages "out of process", and then deploy them to a runtime system. This runtime system only
requires drools-core.jar and knowledge-api for execution. This is an optional deployment pattern,
and many people do not need to "trim" their application this much, but it is an ideal option for
certain environments.
Another option is to use the JBoss IDE, which comes with all the plug-in requirements pre
packaged, as well as a choice of other tools separate to rules. You can choose just to install rules
from the "bundle" that JBoss IDE ships with.
GEF is the Eclipse Graphical Editing Framework, which is used for graph viewing components
in the plug-in.
If you don't have GEF installed, you can install it using the built in update mechanism (or
downloading GEF from the Eclipse.org website not recommended). JBoss IDE has GEF already,
as do many other "distributions" of Eclipse, so this step may be redundant for some people.
4
Installing IDE (Rule Workbench)
Open the Help->Software updates...->Available Software->Add Site... from the help menu.
Location is:
http://download.eclipse.org/tools/gef/updates/releases/
Press next, and agree to install the plug-in (an Eclipse restart may be required). Once this is
completed, then you can continue on installing the rules plug-in.
To install from the zip file, download and unzip the file. Inside the zip you will see a plug-in directory,
and the plug-in jar itself. You place the plug-in jar into your Eclipse applications plug-in directory,
and restart Eclipse.
5
Chapter 2. Installation and S...
Download the Drools Eclipse IDE plugin from the link below. Unzip the downloaded file in your
main eclipse folder (do not just copy the file there, extract it so that the feature and plugin jars end
up in the features and plugin directory of eclipse) and (re)start Eclipse.
http://www.jboss.org/drools/downloads.html
To check that the installation was successful, try opening the Drools perspective: Click the
'Open Perspective' button in the top right corner of your Eclipse window, select 'Other...' and
pick the Drools perspective. If you cannot find the Drools perspective as one of the possible
perspectives, the installation probably was unsuccessful. Check whether you executed each of
the required steps correctly: Do you have the right version of Eclipse (3.4.x)? Do you have
Eclipse GEF installed (check whether the org.eclipse.gef_3.4.*.jar exists in the plugins directory
in your eclipse root folder)? Did you extract the Drools Eclipse plugin correctly (check whether the
org.drools.eclipse_*.jar exists in the plugins directory in your eclipse root folder)? If you cannot
find the problem, try contacting us (e.g. on irc or on the user mailing list), more info can be found
no our homepage here:
http://www.jboss.org/drools/
A Drools runtime is a collection of jars on your file system that represent one specific release of
the Drools project jars. To create a runtime, you must point the IDE to the release of your choice.
If you want to create a new runtime based on the latest Drools project jars included in the plugin
itself, you can also easily do that. You are required to specify a default Drools runtime for your
Eclipse workspace, but each individual project can override the default and select the appropriate
runtime for that project specifically.
You are required to define one or more Drools runtimes using the Eclipse preferences view.
To open up your preferences, in the menu Window select the Preferences menu item. A new
preferences dialog should show all your preferences. On the left side of this dialog, under the
Drools category, select "Installed Drools runtimes". The panel on the right should then show the
currently defined Drools runtimes. If you have not yet defined any runtimes, it should like something
like the figure below.
6
Installing IDE (Rule Workbench)
To define a new Drools runtime, click on the add button. A dialog as shown below should pop up,
requiring the name for your runtime and the location on your file system where it can be found.
7
Chapter 2. Installation and S...
1. If you simply want to use the default jars as included in the Drools Eclipse plugin, you can create
a new Drools runtime automatically by clicking the "Create a new Drools 5 runtime ..." button. A
file browser will show up, asking you to select the folder on your file system where you want this
runtime to be created. The plugin will then automatically copy all required dependencies to the
specified folder. After selecting this folder, the dialog should look like the figure shown below.
2. If you want to use one specific release of the Drools project, you should create a folder on
your file system that contains all the necessary Drools libraries and dependencies. Instead of
creating a new Drools runtime as explained above, give your runtime a name and select the
location of this folder containing all the required jars.
8
Installing IDE (Rule Workbench)
After clicking the OK button, the runtime should show up in your table of installed Drools runtimes,
as shown below. Click on checkbox in front of the newly created runtime to make it the default
Drools runtime. The default Drools runtime will be used as the runtime of all your Drools project
that have not selected a project-specific runtime.
You can add as many Drools runtimes as you need. For example, the screenshot below shows
a configuration where three runtimes have been defined: a Drools 4.0.7 runtime, a Drools 5.0.0
9
Chapter 2. Installation and S...
runtime and a Drools 5.0.0.SNAPSHOT runtime. The Drools 5.0.0 runtime is selected as the
default one.
Note that you will need to restart Eclipse if you changed the default runtime and you want to make
sure that all the projects that are using the default runtime update their classpath accordingly.
Whenever you create a Drools project (using the New Drools Project wizard or by converting an
existing Java project to a Drools project using the "Convert to Drools Project" action that is shown
when you are in the Drools perspective and you right-click an existing Java project), the plugin
will automatically add all the required jars to the classpath of your project.
When creating a new Drools project, the plugin will automatically use the default Drools runtime for
that project, unless you specify a project-specific one. You can do this in the final step of the New
Drools Project wizard, as shown below, by deselecting the "Use default Drools runtime" checkbox
and selecting the appropriate runtime in the drop-down box. If you click the "Configure workspace
settings ..." link, the workspace preferences showing the currently installed Drools runtimes will
be opened, so you can add new runtimes there.
10
Installing IDE (Rule Workbench)
You can change the runtime of a Drools project at any time by opening the project properties
(right-click the project and select Properties) and selecting the Drools category, as shown below.
Check the "Enable project specific settings" checkbox and select the appropriate runtime from the
drop-down box. If you click the "Configure workspace settings ..." link, the workspace preferences
showing the currently installed Drools runtimes will be opened, so you can add new runtimes
there. If you deselect the "Enable project specific settings" checkbox, it will use the default runtime
as defined in your global preferences.
11
Chapter 2. Installation and S...
The source code of each maven artifact is available in the JBoss maven repository as a source
jar. The same source jars are also included in the download zips. However, if you want to build
from source, it's highly recommended to get our sources from our source control.
Drools and jBPM use Git [http://git-scm.com/] for source control. The blessed git repositories are
hosted on Github [https://github.com]:
• https://github.com/droolsjbpm
Git allows you to fork our code, independently make personal changes on it, yet still merge in our
latest changes regularly and optionally share your changes with us. To learn more about git, read
the free book Git Pro [http://progit.org/book/].
In essense, building from source is very easy, for example if you want to build the guvnor project:
12
Eclipse
However, there are a lot potential pitfalls, so if you're serious about building from source and
possibly contributing to the project, follow the instructions in the README file in droolsjbpm-
build-bootstrap [https://github.com/droolsjbpm/droolsjbpm-build-bootstrap/blob/master/
README.md].
2.3. Eclipse
With the Eclipse project files generated they can now be imported into Eclipse. When starting
Eclipse open the workspace in the root of your subversion checkout.
13
Chapter 2. Installation and S...
14
Importing Eclipse Projects
15
Chapter 2. Installation and S...
When calling mvn install all the project dependencies were downloaded and added to the local
Maven repository. Eclipse cannot find those dependencies unless you tell it where that repository
is. To do this setup an M2_REPO classpath variable.
16
Importing Eclipse Projects
17
Chapter 2. Installation and S...
18
Importing Eclipse Projects
19
20
Chapter 3.
https://cla.jboss.org/
21
Chapter 3. Getting Involved
Minor code submissions, like format or documentation fixes do not need an associated JIRA issue
created.
https://issues.jboss.org/browse/JBRULES [???](Drools)
https://issues.jboss.org/browse/JBPM
https://issues.jboss.org/browse/GUVNOR
22
Fork Github
https://github.com/droolsjbpm
23
Chapter 3. Getting Involved
then using a String is not practical so then by all means place them in separate drl files instead
to be loaded from the classpath. If your tests need to use a model, please try to use those that
already exist for other unit tests; such as Person, Cheese or Order. If no classes exist that have
the fields you need, try and update fields of existing classes before adding a new class.
There are a vast number of tests to look over to get an idea, MiscTest is a good place to start.
https://github.com/droolsjbpm/drools/blob/master/drools-compiler/src/test/java/org/drools/
integrationtests/MiscTest.java [https://github.com/droolsjbpm]
24
Commit with Correct Conventions
25
Chapter 3. Getting Involved
related to this commit. Use an additional new line and dash for each separate point you wish to
make. You may add additional JIRA cross references to the same commit, if it's appropriate. In
general try to avoid combining unrelated issues in the same commit.
Don't forget to rebase your local fork from the original master and then push your commits back
to your fork.
26
Submit Pull Requests
The pull request then goes into a queue for everyone to see and comment on. Below you can see
a typical pull request. The pull requests allow for discussions and it shows all associated commits
and the diffs for each commit. The discussions typically involve code reviews which provide helpful
suggestions for improvements, and allows for us to leave inline comments on specific parts of the
code. Don't be disheartened if we don't merge straight away, it can often take several revisions
before we accept a pull request. Luckily github makes it very trivial to go back to your code, do
some more commits and then update your pull request to your latest and greatest.
It can take time for us to get round to responding to pull requests, so please be patient. Submitted
tests that come with a fix will generally be applied quite quickly, where as just tests will often way
until we get time to also submit that with a fix. Don't forget to rebase and resubmit your request
from time to time, otherwise over time it will have merge conflicts and core developers will general
ignore those.
27
28
Chapter 4.
We already allow nested accessors to be used as follows, where address is the nested object:
Now these accessors to nested objects can be grouped with a '.(...)' syntax providing more
readable rules as it follows:
Note the '.' prefix, this is necessary to differentiate the nested object constraints from a method call.
When dealing with nested objects, we may need to cast to a subtype. Now it is possible to do
that via the # symbol as in:
This example casts Address to LongAddress, making its getters available. If the cast is not possible
(instanceof returns false), the evaluation will be considered false. Also fully qualified names are
supported:
29
Chapter 4. Drools Release Notes
moreover, since we also support the instanceof operator, if that is used we will infer its results for
further uses of that field, within that pattern:
Sometimes the constraint of having one single consequence for each rule can be somewhat
limiting and leads to verbose and difficult to be maintained repetitions like in the following example:
It is already possible to partially overcome this problem by making the second rule extending the
first one like in:
30
Drools Expert
Anyway this feature makes it possible to define more labelled consequences other than the default
one in a single rule, so, for example, the 2 former rules can be compacted in only one like it follows:
rule "Give 10% discount and free parking to customers older than 60"
when
$customer : Customer( age > 60 )
do[giveDiscount]
$car : Car ( owner == $customer )
then
modify($car) { setFreeParking( true ) };
then[giveDiscount]
modify($customer) { setDiscount( 0.1 ) };
end
This last rule has 2 consequences, the usual default one, plus another one named "giveDiscount"
that is activated, using the keyword do, as soon as a customer older than 60 is found in the
knowledge base, regardless of the fact that he owns a car or not. The activation of a named
consequence can be also guarded by an additional condition like in this further example:
rule "Give free parking to customers older than 60 and 10% discount to golden
ones among them"
when
$customer : Customer( age > 60 )
if ( type == "Golden" ) do[giveDiscount]
$car : Car ( owner == $customer )
then
modify($car) { setFreeParking( true ) };
then[giveDiscount]
modify($customer) { setDiscount( 0.1 ) };
end
The condition in the if statement is always evaluated on the pattern immediately preceding it. In
the end this last, a bit more complicated, example shows how it is possible to switch over different
conditions using a nested if/else statement:
rule "Give free parking and 10% discount to over 60 Golden customer and 5% to
Silver ones"
when
$customer : Customer( age > 60 )
if ( type == "Golden" ) do[giveDiscount10]
else if ( type == "Silver" ) break[giveDiscount5]
$car : Car ( owner == $customer )
then
modify($car) { setFreeParking( true ) };
31
Chapter 4. Drools Release Notes
then[giveDiscount10]
modify($customer) { setDiscount( 0.1 ) };
then[giveDiscount5]
modify($customer) { setDiscount( 0.05 ) };
end
Here the purpose is to give a 10% discount AND a free parking to Golden customers over 60, but
only a 5% discount (without free parking) to the Silver ones. This result is achieved by activating
the consequence named "giveDiscount5" using the keyword break instead of do. In fact do just
schedules a consequence in the agenda, allowing the remaining part of the LHS to continue of
being evaluated as per normal, while break also blocks any further pattern matching evaluation.
Note, of course, that the activation of a named consequence not guarded by any condition with
break doesn't make sense (and generates a compile time error) since otherwise the LHS part
following it would be never reachable.
4.1.2. Guvnor
The left-hand side (WHEN) part of the rule is now authored in a similar manner to the right-hand
side (THEN).
32
Guvnor
• Date selectors are consistent with Decision Tables and Rule Templates
If you import a Java POJO model be prepared to see more members listed than you may have
had previously.
33
Chapter 4. Drools Release Notes
34
Guvnor
The guided rule editor now supports nesting entry-points in "from accumulate" and "from collect"
constructs. Define a "from accumulate" or "from collect" in the usual manner and then choose
"from entry-point" when defining the inner pattern. This feature is also available in Rule Templates
and the guided web-based Decision Table's BRL fragment columns.
An audit log has been added to the web-guided Decision Table editor to track additions, deletions
and modifications.
By default the audit log is not configured to record any events, however, users can easily select
the events in which they are interested.
35
Chapter 4. Drools Release Notes
Once the capture of events has been enabled all subsequent operations are recorded. Users are
able to perform the following:
• Delete an event from the log. Event details remain in the underlying repository.
36
Guvnor
Sections of the Guided Rule Editor can become "frozen" if the Fact Type on which they rely is
deleted from the available models, or the entire model is removed. Up until now the frozen section
could not be removed and the whole rule had to be deleted and re-created from scratch. With this
release frozen sections can be deleted.
Additionally, Drools Scorecards will allows for reason codes to be set, which help in identifying
the specific rules (buckets) that have contributed to the overall score. Drools Scorecards will be
based on the PMML 4.1 Standard.
The New Rule Wizard now allows for creation of scorecard assets.
37
Chapter 4. Drools Release Notes
38
Planner
4.1.3. Planner
The new selector architecture is far more flexible and powerful. It works great out-of-the-box, but
it can also be easily tailored to your use case for additional gain.
39
Chapter 4. Drools Release Notes
<unionMoveSelector>
<cacheType>JUST_IN_TIME</cacheType>
<selectionOrder>RANDOM</selectionOrder>
<changeMoveSelector/>
<swapMoveSelector/>
...
</unionMoveSelector>
Custom move factories are often no longer needed (but still supported). For now, the new selector
architecture is only applied on local search (tabu search, simulated annealing, ...), but it will be
applied on construction heuristics too.
The new selector architecture allows for Just In Time random selection. For very large use cases,
this configuration uses far less memory and is several times faster in performance than the older
configurations. Every move is generated just in time and there's no move list that needs shuffling:
40
Planner
Filtered selection allows you to discard specific moves easily, such as a pointless move that swaps
2 lectures of the same course:
41
Chapter 4. Drools Release Notes
Probability selection allows you to favor certain moves more than others.
42
Drools Integration
• Every chart that shows a score, now exists for every score level, so there is now a hard score
chart too.
• Each chart now has a table with the data too. This is especially useful for matrix benchmarks
when the graphs become cluttered due to too much data.
And if you have multiple CPU's in your computer, the benchmarker can now take advantage of
them thanks to parallel benchmarking.
@PlanningEntity(movableEntitySelectionFilter =
MovableShiftAssignmentSelectionFilter.class)
public class ShiftAssignment {
...
}
43
Chapter 4. Drools Release Notes
<drools:environment id="...">
<drools:entity-manager-factory ref=".."/>
<drools:transaction-manager ref=".."/>
<drools:globals ref=".."/>
<drools:object-marshalling-strategies>
...
</drools:object-marshalling-strategies>
<drools:scoped-entity-manager scope="app|cmd" ref="..."/>
</drools:environment>
4.2.1.1. Traits
Traits were introduced in the 5.3 release, and details on them can be found in the N&N for
there. This release adds an example so that people have something simple to run, to help them
understand. In the drools-examples source project open the classes and drl for the namespace
"/org/drools/examples/traits". There you will find an example around classifications of students
and workers.
44
Drools Expert
s.setSchool( "SomeSchool" );
update( s );
end
A two part detailed article has been written up at a blog, which will later be improved and rolled
into the main documentation. For now you can read them here.
• http://blog.athico.com/2011/12/new-feature-spotlight-traits-part-1.html
• http://blog.athico.com/2011/12/dynamic-typing-in-rules-traits-part-2.html
45
Chapter 4. Drools Release Notes
46
Drools Expert
The Simulator runs the Simulation. The Simulation is your scenario definition. The Simulation
consists of 1 to n Paths, you can think of a Path as a sort of Thread. The Path is a chronological
line on which Steps are specified at given temporal distances from the start. You don't specify
a time unit for the Step, say 12:00am, instead it is always a relative time distance from the start
of the Simulation (note: in Beta2 this will be relative time distance from the last step in the same
path). Each Step contains one or more Commands, i.e. create a StatefulKnowledgeSession or
insert an object or start a process. These are the very same commands that you would use to
script a knowledge session using the batch execution, so it's re-using existing concepts.
• 1.1 Simulation
• 1..n Paths
• 1..n Steps
• 1..n Commands
All the steps, from all paths, are added to a priority queue which is ordered by the temporal
distance, and allows us to incrementally execute the engine using a time slicing approach. The
simulator pops of the steps from the queue in turn. For each Step it increments the engine clock
and then executes all the Step's Commands.
Here is an example Command (notice it uses the same Commands as used by the
CommandExecutor):
Commands can be grouped together, especially Assertion commands, via test groups. The test
groups are mapped to JUnit "test methods", so as they pass or fail using a specialised JUnit
Runner the Eclipse GUI is updated - as illustrated in the above image, showing two passed test
groups named "test1" and "test2".
Using the JUnit integration is trivial. Just annotate the class with
@RunWith(JUnitSimulationRunner.class). Then any method that is annotated with @Test and
returns a Simulation instance will be invoked executing the returned Simulation instance in the
Simulator. As test groups are executed the JUnit GUI is updated.
47
Chapter 4. Drools Release Notes
cmds.add( new
KnowledgeBuilderAddCommand( ResourceFactory.newByteArrayResource( str.getBytes() ),
ResourceType.DRL, null ) );
Notice the set command. "path1" is the context, each path has it's own variable context. All paths
inherit from a "root" context. "KnowledgeBuilder.class.getName() " is the name that we are setting
the return value of the last command. As mentioned before we consider the class names of those
classes as registers, any further commands that attempt to operate on a knowledge builder will
use what ever is assigned to that, as in the case of KnowledgeBuilderAddCommand. This allows
multiple kbuilders, kbases and ksessions to exist in one context under different variable names,
but only the one assigned to the register name is the one that is currently executed on.
We know the above looks quite verbose. SimulationTest just shows our low level canonical model,
the idea is that high level representations are built ontop of this. As this is a builder API we
are currently focusing on two sets of fluents, compact and standard. We will also work on a
spreadsheet UI for building these, and eventually a dedicated textual dsl.
48
Drools Expert
Note that the test is not executing at build time, it's building a script to be executed later. The
script underneath matches what you saw in SimulationTest. Currently the way to run a simulation
manually is shown below. Although you already saw in SimulationTest that JUnit will execute
these automatically. We'll improve this over time.
49
Chapter 4. Drools Release Notes
.end()
.newPath( "path1" )
.newStep( 1000 )
.newStatefulKnowledgeSession()
.insert( new Person( "yoda", 150 ) ).set( "y" )
.fireAllRules()
.test( "y.name == 'yoda'" )
.test( "y.age == 160" )
.end()
.end()
.newPath( "path2" )
.newStep( 800 )
.newStatefulKnowledgeSession()
.insert( new Person( "darth", 70 ) ).set( "d" )
.fireAllRules()
.test( "d.name == 'darth'" )
.test( "d.age == 80" )
.end()
.end()
.end
There is still an awful lot to do, this is designed to eventually provide a unified simulation and
testing environment for rules, workflow and event processing over time, and eventually also over
distributed architectures.
• Flesh out the api to support more commands, and also to encompass jBPM commands
• Improve out of the box usability, including moving interfaces to knowledge-api and hiding "new"
constructors with factory methods
• Commands are already marshallable to json and xml. They should be updated to allow full round
tripping from java api commands and json/xml documents.
• What rule(s) fired, including optionally what data was used with the executing rule (Drools)
• Design and build tabular authoring tools via spreadsheet, targeting the web with round tripping
to excel.
• Design and develop textual DSL for authoring - maybe part of DRL (long term task).
50
Drools Expert
E.g.:
In essence the slot provides a discrete location to add type declarations where, previously, they
may have been added to a Queries or Functions definition.
51
Chapter 4. Drools Release Notes
In this way it is no longer necessary to build the DRLs files in the right order (e.g. first the DRLs
containing the type declarations and then the ones with the rules using them) and it will also be
possible to have circular references among them.
Moreover the KnowledgeBuilder (regardless if you are using the batch mode or not) also allows
to discard what has been added with the last DRL(s) building. This can be useful to recover from
having added a wrong DRL to the KnowledgeBuilder as it follows:
kbuilder.add(ResourceFactory.newByteArrayResource(wrongDrl.getBytes()),
ResourceType.DRL);
52
Drools Expert
if ( kbuilder.hasErrors() ) {
kbuilder.undo();
}
Currently when in a RHS you invoke update() or modify() on a given object it will trigger a
revaluation of all patterns of the matching object type in the knowledge base. As some have
experienced, this can be a problem that often can lead to unwanted and useless evaluations and in
the worst cases to infinite recursions. The only workaround to avoid it was to split up your objects
into smaller ones having a 1 to 1 relationship with the original object.
This new feature allows the pattern matching to only react to modification of properties actually
constrained or bound inside of a given pattern. That will help with performance and recursion and
avoid artificial object splitting. The implementation is bit mask based, so very efficient. When the
engine executes a modify statement it uses a bit mask of fields being changed, the pattern will
only respond if it has an overlapping bit mask. This does not work for update(), and is one of the
reason why we promote modify() as it encapsulates the field changes within the statement.
By default this feature is off in order to make the behavior of the rule engine backward compatible
with the former releases. When you want to activate it on a specific bean you have to annotate it
with @propertyReactive. This annotation works both on drl type declarations:
declare Person
@propertyReactive
firstName : String
lastName : String
end
@PropertyReactive
public static class Person {
private String firstName;
private String lastName;
}
In this way, for instance, if you have a rule like the following:
53
Chapter 4. Drools Release Notes
end
you won't have to add the no-loop attribute to it in order to avoid an infinite recursion because the
engine recognizes that the pattern matching is done on the 'firstName' property while the RHS of
the rule modifies the 'male' one. Note that this feature does not work for update(), and this is one of
the reasons why we promote modify() since it encapsulates the field changes within the statement.
Moreover, on Java classes, you can also annotate any method to say that its invocation actually
modifies other properties. For instance in the former Person class you could have a method like:
it will correctly recognize that the values of both properties 'firstName' and 'lastName' could
have potentially been modified and act accordingly, not missing of reevaluating the patterns
constrained on them. At the moment the usage of @Modifies is not allowed on fields but only on
methods. This is coherent with the most common scenario where the @Modifies will be used for
methods that are not related with a class field as in the Person.setName() in the former example.
Also note that @Modifies is not transitive, meaning that if another method internally invokes
the Person.setName() one it won't be enough to annotate it with @Modifies( { "name" } ), but it
is necessary to use @Modifies( { "firstName", "lastName" } ) even on it. Very likely @Modifies
transitivity will be implemented in the next release.
For what regards nested accessors, the engine will be notified only for top level fields. In other
words a pattern matching like:
will be reevaluated only for modification of the 'address' property of a Person object. In the same
way the constraints analysis is currently strictly limited to what there is inside a pattern. Another
example could help to clarify this. An LHS like the following:
$p : Person( )
54
Drools Expert
will not listen on modifications of the person's name, while this one will do:
Indeed, annotating a pattern with @watch allows you to modify the inferred set of properties for
which that pattern will react. Note that the properties named in the @watch annotation are actually
added to the ones automatically inferred, but it is also possible to explicitly exclude one or more
of them prepending their name with a ! and to make the pattern to listen for all or none of the
properties of the type used in the pattern respectively with the wildcards * and !*. So, for example,
you can annotate a pattern in the LHS of a rule like:
// listens for changes on all the properties except the age one
Person( firstName == $expectedFirstName ) @watch( *, !age )
Since doesn't make sense to use this annotation on a pattern using a type not annotated with
@PropertyReactive the rule compiler will raise a compilation error if you try to do so. Also the
duplicated usage of the same property in @watch (for example like in: @watch( firstName, !
firstName ) ) will end up in a compilation error. In a next release we will make the automatic
detection of the properties to be listened smarter by doing analysis even outside of the pattern.
It also possible to enable this feature by default on all the types of your model or to completely
disallow it by using on option of the KnowledgeBuilderConfiguration. In particular this new
PropertySpecificOption can have one of the following 3 values:
55
Chapter 4. Drools Release Notes
- DISABLED => the feature is turned off and all the other related annotations
are just ignored
- ALLOWED => this is the default behavior: types are not property reactive unless
they are not annotated with @PropertySpecific
- ALWAYS => all types are property reactive by default
So, for example, to have a KnowledgeBuilder generating property reactive types by default you
could do:
KnowledgeBuilderConfiguration config =
KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration();
config.setOption(PropertySpecificOption.ALWAYS);
KnowledgeBuilder kbuilder =
KnowledgeBuilderFactory.newKnowledgeBuilder(config);
In this last case it will be possible to disable the property reactivity feature on a specific type by
annotating it with @ClassReactive.
Using the new fluent simulation testing, you can test your rules in unit tests more easily:
@Test
public void rejectMinors() {
SimulationFluent simulationFluent = new DefaultSimulationFluent();
simulationFluent
.newKnowledgeBuilder()
.add(ResourceFactory.newClassPathResource("org/drools/examples/
carinsurance/rule/policyRequestApprovalRules.drl"),
ResourceType.DRL)
.end()
.newKnowledgeBase()
.addKnowledgePackages()
.end()
.newStatefulKnowledgeSession()
56
Drools Expert
.insert(john).set("john")
.insert(mini).set("mini")
.insert(johnMiniPolicyRequest).set("johnMiniPolicyRequest")
.fireAllRules()
.test("johnMiniPolicyRequest.automaticallyRejected == true")
.test("johnMiniPolicyRequest.rejectedMessageList.size() == 1")
.end()
.runSimulation();
}
You can even test your CEP rules in unit tests without suffering from slow tests:
@Test
public void lyingAboutAge() {
SimulationFluent simulationFluent = new DefaultSimulationFluent();
simulationFluent
.newStep(0)
.newKnowledgeBuilder()
.add(ResourceFactory.newClassPathResource("org/drools/examples/
carinsurance/cep/policyRequestFraudDetectionRules.drl"),
ResourceType.DRL)
.end()
.newKnowledgeBase()
.addKnowledgePackages()
.end(World.ROOT, KnowledgeBase.class.getName())
.newStatefulKnowledgeSession()
.end()
.newStep(1000)
.getStatefulKnowledgeSession()
.insert(realJohn).set("realJohn")
57
Chapter 4. Drools Release Notes
.insert(realMini).set("realMini")
.insert(realJohnMiniPolicyRequest).set("realJohnMiniPolicyRequest")
.fireAllRules()
.test("realJohnMiniPolicyRequest.requiresManualApproval == false")
.end()
.newStep(5000)
.getStatefulKnowledgeSession()
.insert(fakeJohn).set("fakeJohn")
.insert(fakeMini).set("fakeMini")
.insert(fakeJohnMiniPolicyRequest).set("fakeJohnMiniPolicyRequest")
.fireAllRules()
.test("fakeJohnMiniPolicyRequest.requiresManualApproval == true")
.end()
.runSimulation();
}
It is now possible to register an UnMatch listener on the Agenda in order to be notified when a
given activation gets unmatched and take the appropriate compensation actions, as in the follwing
example:
Note that, at the moment, this is an internal impelmentation, that will be eventually moved to a
stable API in the future. This is why it is only present in our internal API and so you need to
explicitly cast to AgendaItem in order to be able to use it.
The engine now supports the declaration of entry-points. This allow tools and the application to
inspect, select and restrict the use of entry-points in rules.
58
Drools Fusion
Example:
The engine now supports the declaration of Windows. This promotes a clear separation between
what are the filters applied to the window and what are the constraints applied to the result of
window. It also allows easy reuse of windows among multiple rules.
Another benefit is a new implementation of the basic window support in the engine, increasing the
overall performance of the rules that use sliding windows.
Example:
Rules can then use the declared window by referencing using a FROM CE. Example:
59
Chapter 4. Drools Release Notes
end
4.2.3. Guvnor
The Guided Decision Table editor and wizard now support the creation of "Limited Entry" tables.
60
Guvnor
61
Chapter 4. Drools Release Notes
62
Guvnor
63
Chapter 4. Drools Release Notes
With the introduction of support for Limited Entry, the decision table format was improved to better
differentiate between Condition and Action columns. Furthermore the table header was improved
to show more information for Action columns.
The ability to rearrange whole patterns as well as individual conditions in the constraints section
of the table has been added. This allows the table author to arrange constraints to maximise
performance of the resulting rules, by placing generalised constraints before more specific. Action
columns can also be re-arranged. Both patterns and columns are re-arranged by dragging and
dropping.
64
Guvnor
This release brings the ability to define Action columns to retract Facts.
If you are authoring an Extended Entry decision table the column definition contains basic
information and the fact being retracted is held in the table itself.
If however you are authoring a Limited Entry decision table the Fact being retracted is defined
in the column definition.
65
Chapter 4. Drools Release Notes
You can now bind fields in Conditions to variables. These variables can then be used in Predicate
or Formula conditions, and Work Item actions.
66
Guvnor
jBPM Work Items can now be used as Actions; and the corresponding Work Item Handler invoked
at runtime. Work Item Handlers should be added to the runtime session as normal.
Work Item input parameters can either be defined as a literal value in the column definition or as
a Fact or Fact Field binding.
• Set the value of a field on an existing Fact to the value of a Work Item output (result) parameter.
• Set the value of a field on a new Fact to the value of a Work Item output (result) parameter.
67
Chapter 4. Drools Release Notes
Figure 4.29. Setting a field from a Work Item output (result) parameter
On the bottom of Web decision tables, there's now a button Analyze... which will check if there
issues in your decision table, such as impossible matches and conflicting matches.
An impossible match is a row in a decision table that can never match. For example:
68
Guvnor
In the decision table above, row 2 is an impossible match, because there is no Person with a
minimum age of 21 and a maximum age of 20.
A conflicting match are 2 rows in a decision table that can both match.
69
Chapter 4. Drools Release Notes
In the decision table above, row 2 and 3 are a conflicting match, because a Person of age 67 will
match both rows: it's unclear whether that Person has to pay a fee of 200 or 100.
• Added support to create (PUT) and delete (DELETE) a Category with rest/categories/
{categoryName}.
• Removed buggy category property from Package: it was null upon GET and ignored upon
PUT.
• The REST resource to get the assets for a certain category has been moved from rest/
categories/{categoryName} to rest/categories/{categoryName}/assets.
• The REST resources now properly encode and decode the URL's. Note that URL path encoding
is different than URL query encoding: the former does not accept + as encoding for space.
This does not mean that Guvnor can not be made to work in AS 6. We are just focusing on JBoss
AS 7 and Tomcat.
When using Rule Templates you can now define interpolation variables in free-form DRL blocks.
Template Keys are formatted as documented in Drools Expert User Guide; i.e. @{keyName}. All
70
Guvnor
Template Keys in 'free-form' LHS or RHS elements are considered 'Text' (i.e. you'll get a TextBox
in the Template data screen). Data-types should be correctly escaped in the 'free-form' entry. For
example: System.out.println("@{key}");
All guided rule editors (BRL, Rule Templates and Decision Tables) now support relation operators
for String values.
Guvnor 5.4.x requires at least Java 6 to run. The Drools and jBPM will still run on Java 5. Guvnor
5.3.x hotfixes will still run on Java 5 too.
Any custom configuration in the guvnor war in WEB-INF/components.xml must now happen in
WEB-INF/beans.xml.
On the bottom of Web decision tables, the button Analyze... that checks for issues in your decision
table, now also checks for 2 new detections:
A duplicate match are 2 rows in a decision table that can both match but have the same actions.
For example: a person between 20 and 40 has to pay a fee of 400 and a person between 30 and
50 has to pay a fee of 400 too. These 2 rows duplicate each other. If the fee would be different
between them, then they would conflict each other.
A multiple values for one action match is 1 row in a decision table who's actions contradict itself.
This is a common problem in limited entry tables, but rare in non-limited entry tables.
For example: a person below 40 has to pay the standard fee (400) and also has to pay the
youngster fee (200). Since fee can only be set once, that's a problem.
BRL fragments can now be used for Condition and/or Action columns.
71
Chapter 4. Drools Release Notes
A BRL fragment is a section of a rule created using Guvnor's (BRL) Guided Rule Editor: Condition
columns permit the definition of "WHEN" sections and Action columns the definition of "THEN"
sections. Fields defined therein as "Template Keys" become columns in the decision table.
72
Guvnor
Consequently any rule that could be defined with the (BRL) Guided Rule Editor can now be defined
with a decision table; including free-format DRL and DSL Sentences.
BRL fragments are fully integrated with other columns in the decision table, so that a Pattern or
field defined in a regular column can be referenced in the BRL fragments and vice-versa.
73
Chapter 4. Drools Release Notes
Figure 4.33. A decision table with BRL fragments and regular columns
You can now copy and paste rows in both the guided Decision Table and Template Data editors.
Simply right-click a row in the tables' left hand-side selector column and choose the appropriate
operation.
74
Guvnor
When creating new constraints or actions in the BRL guided (rule) editor it is simple to define a
value as literal, or formula or expression. However, up until now, changing the value type required
deletion of the whole constraint or action. This release brings the ability to remove the value
definition thus enabling you to change a literal value to a formula etc without needing to delete
the whole constraint or action.
A new Editor to create Change-Sets was added in Guvnor. Using this new editor you can create
change-sets referencing packages, snapshots and even particular assets inside a package and
expose them to external applications. For further information refer to the documentation.
75
Chapter 4. Drools Release Notes
Custom Forms is a feature that exists in Guvnor since 5.1.1. It basically allows you to define
external applications that will be invoked by Rule Editor when a particular field of a particular Fact
Type is being used in a rule.
This feature is now also available to be used in DSL sentences. When defining a variable in a
sentence you can now use this syntax for variable's definition:
{<varName>:CF:<factType.fieldName>}
If you have an active Working-Set defining a Custom Form configuration for factType.fieldName,
the Custom Form will be invoked by Rule Editor while setting the value of that variable.
Support has been added for the "timer" and "calendar" attributes.
76
Guvnor
Support has been added for the "timer" and "calendar" attributes.
77
Chapter 4. Drools Release Notes
Uploading a XLS decision table results in the creation of numerous new assets, including
(obviously) web-guided Decision Tables, functions, declarative types and modifications to
package globals and imports etc (Queries are not converted, although supported in the XLS form,
as Guvnor doesn't support them yet [https://issues.jboss.org/browse/GUVNOR-1532]).
78
Guvnor
79
Chapter 4. Drools Release Notes
This is the first stage of "round-tripping" decision tables. We still need to add the ability to export a
guided decision table back to XLS, plus we'd like to add tighter integration of updated XLS assets
to their original converted cousins - so if a new version of the XLS decision table is uploaded the
related assets' versions are updated (rather than creating new) upon conversion.
This is a powerful enhancement and as such your feedback is critical to ensure we implement
the feature as you'd like it to operate. Check it out, feedback your opinions and help guide the
future work.
Numerical "value editors" (i.e. the text boxes for numerical values) in the BRL, Rule Template, Test
Scenarios and Decision Table editors now support the types Byte, Short, Integer, Long, Double,
Float, BigDecimal and BigInteger (and their primitive counterparts) correctly. The generated DRL
is automatically appended with "B" or "I" type classifiers for BigDecimal and BigInteger values
respectively, as provided for by Drools Expert. The Right-hand Side generates applicable DRL for
BigDecimal and BigInteger values according to the rule's dialect.
80
Guvnor
Dependent enumerations can now be used in both the Web Guided Decision Table editor and the
Rule Template Data grid. Furthermore improvements were made to the operation of dependent
enumerations in the BRL Guided Rule editor for sub-fields and expressions.
• A default value editor is correct for the data-type of the column's Fact\.
• If a "Value List" is provided, the default value needs to be one of the values in the list.
81
Chapter 4. Drools Release Notes
• If the column represents a field with an enumeration the default value must be one of the
enumeration's members.
• If the column uses an operator that does not need a value (e.g. "is null") a default value cannot
be provided.
• If the column field is a "dependent enumeration" the default value must be one of the permitted
values based upon parent enumeration default values, if any.
82
Guvnor
Figure 4.44. Setting the default value of a cell with a Value List
A new editor to create services was added in Guvnor. Service is a special asset that enables
users configure KnowledgeBases and KSessions to be executed remotely for any sort of client
application (via REST or SOAP).
In order to expose those services, the editor generates automatically a war file that can be
deployed on most platforms on most containers. For further information refer to the documentation.
83
Chapter 4. Drools Release Notes
4.2.4. Planner
<localSearch>
<selector>
<selector>
moveFactoryClass>
</selector>
<selector>
moveFactoryClass>
</selector>
</selector>
...
</localSearch>
84
Planner
It's no longer required to write your own Move and MoveFactory implementations, but you still
can (and mix those in too).
The Benchmarker can now read and write the input and output files from any format, through the
ProblemIO interface. The default is still an XStream implementation.
This is an implementation of the classic Traveling Salesman Problem: given a list of cities, find
the shortest tour for a salesman that visits each city exactly once.
This is an implementation of capacitated vehicle routing: Using a fleet of vehicles, transport items
from the depot(s) to customers at different locations. Each vehicle can service multiple locations,
but it has a limited capacity for items.
85
Chapter 4. Drools Release Notes
In the screenshot below, there are 6 vehicles (the lines in different colors) that drop off 541 items
at 33 customer locations. Each vehicle can carry 100 items.
The employee rostering example's GUI has been reworked to show shift assignment more clearly.
Until now, implementing TSP or Vehicle Routing like problems in Planner was hard. The new
chaining support makes it easy.
86
Planner
...
@PlanningVariable(chained = true)
@ValueRanges({
@ValueRange(type = ValueRangeType.FROM_SOLUTION_PROPERTY, solutionProperty = "vehic
@ValueRange(type = ValueRangeType.FROM_SOLUTION_PROPERTY, solutionProperty = "custo
excludeUninitializedPlanningEntity = true)})
public VrpAppearance getPreviousAppearance() {
return previousAppearance;
}
...
}
87
Chapter 4. Drools Release Notes
• Every optimization algorithm: including construction heuristics (first fit, first fit decreasing, ...),
local search (tabu search, simulated annealing), ...
• The generic build-in move factories. Note: currently there are chained alternatives for each
move factory, but those will be unified with the originals soon.
Property tabu has been renamed to planning entity tabu. Planning value tabu has been added.
The generic moves support this out-of-the-box.
<acceptor>
<planningValueTabuSize>5</planningValueTabuSize>
</acceptor>
Planner can now alternatively, use a score calculation written in plain Java. Just implement this
interface:
In this way, Planner does not use Drools at all. This allows you to:
• Use Planner, even if your company forbids any other language than Java (including DRL).
• Hook Planner up to an existing score calculation system, which you don't want to migrate to
DRL at this time.
There is 2 Java ways implemented: SimpleScoreCalculator (which is simple and slow) and
IncrementalScoreCalculator (which is fast).
88
Eclipse plugin
4.2.5.1. Breakpoints in Rule RHS are not working with Java dialect
Debugging with Java is not working. Debugging with MVEL is not affected. This will be fixed in
the future releases before 5.4.0.Final
This feature is off by default and must be explicitly enabled, that is because it is considered highly
experimental for the moment and will be subject to change.
KnowledgeBaseConfiguration kconf =
KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
kconf.setOption( DeclarativeAgendaOption.ENABLED );
KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase( kconf );
89
Chapter 4. Drools Release Notes
• All matched rule's Activations are inserted into WorkingMemory as facts. So you can now
match against an Activation. The rule's metadata and declarations are available as fields on
the Activation object.
• You can use the kcontext.blockActivation( Activation match ) for the current rule to block the
selected activation. Only when that rule becomes false will the activation be eligible for firing.
If it is already eligible for firing and is later blocked, it will be removed from the agenda until
it is unblocked.
• An activation may have multiple blockers and a count is kept. All blockers must became false
for the counter to reach zero to enable the Activation to be eligible for firing.
• An unblocked Activation is added to the Agenda and obeys normal salience, agenda groups,
ruleflow groups etc.
• @activationListener('direct') allows a rule to fire as soon as it's matched, this is to be used for
rules that block/unblock activations, it is not desirable for these rules to have side effects that
impact else where. The name may change later, this is actually part of the pluggable terminal
node handlers I made, which is an "internal" feature for the moment.
Here is a basic example that will block all activations from rules that have metadata
@department('sales'). They will stay blocked until the blockerAllSalesRules rule becomes false,
i.e. "go2" is retracted.
90
Drools Expert
This example shows how you can use active property to count the number of active or inactive
(already fired) activations.
Thanks to many improvements and optimizations made on both MVEL library and Drools internals,
the DRL compilation is now at least 3 times faster for both MVEL and Java dialects.
91
Chapter 4. Drools Release Notes
Example use:
Example 4.5.
declare entry-point X
@doc( "This entry point is receives events from the message queue X" )
end
A Drools Trait is a bean interface which can be attached - and removed - to and from an individual
object at runtime. While an object wears a trait, a reference of the trait type is returned, so methods
defined in the trait interface can be called normally. A trait, then, adds a type and some fields
to an object. If a bean has a field of the given name and type, that field will be used to support
the interface. ''Virtual'' fields, instead, will be stored as entries in a map, or as triples in an in-
memory store.
Traits are declared with the notation declare trait. Unlike normal beans, declared traits will
generate interfaces instead of classes: the declared fields will be mapped to getters/setters. Notice
that multiple traits can be worn at the same time.
The operator isA can be used in patterns to check whether an object is wearing a trait or not
4.3.1.6.3. Usage
92
Guvnor
4.3.2. Guvnor
Following the enhancement to Drools Expert type declaration in Guvnor now support 'extends'
to inherit from super-classes. Sub-classes can extend either other types declared in the same
package or imported Java classes. In order to extend a type declared in Java by a declared
subtype, repeat the supertype in a declare statement without any fields.
Thanks to the help of a community member the tree-view shown in the Knowledge Bases view
has been improved.
• The view no longer repeats intermediate level sub-package names that are empty (community
led effort).
• The package view can be viewed hierarchically, as has been the default up to 5.2.0.Final.
• The package view can now also be viewed "flat" with no nesting
93
Chapter 4. Drools Release Notes
94
Guvnor
One of the more noticeable changes we are making to support this has been to de-couple
asset types and groups from the code. What was once previously static code is now defined by
compile-time configuration. Due to limitations in GWT (no client-side reflection), the technology
with which Guvnor is implemented, we are unfortunately not able to offer runtime configuration
at this moment.
<asseteditor>
<class>org.drools.guvnor.client.modeldriven.ui.RuleModeller</class>
<format>brl</format>
95
Chapter 4. Drools Release Notes
<icon>images.ruleAsset()</icon> <title>constants.BusinessRuleAssets()</
title>
</asseteditor>
To emphasis the separation, asset groups have become their own "editor" appearing as a tab in
Guvnor's main, central panel.
The format of the new screen is being tried for 5.3.0.Beta1. There has been some discussion
whether a single table containing all assets would be better - with collapsible rows to group different
96
Guvnor
types of asset. The immediate problem with this approach is however that finding different asset
types on a "paged table" becomes more cumbersome for the user; as they'd have to sort by type
and page through.
Previously Guvnor enumerations that had a "display value" and a "DRL value" (i.e. the value
substituted for the display value when DRL was generated) could be defined with "<DRL
value>=<Display value>". Various community users have been using Guvnor enumerations to
support complex rule definitions in both DSL and the guided Decision Table editor.
A couple of minor enhancements have been made to Guvnor's guided decision table editor:-
• Removal of sorting
You are now able to restore the original sort order of a column by clicking on the sort icon
through: ascending, descending and none.
Up until now only literal value columns could take advantage of value lists; either "Guvnor
enums" or the Decision Table's "Optional value list". This been rectified with this release bringing
the advantage of predefined choices for these types of fields to the business user.
97
Chapter 4. Drools Release Notes
Guvnor URLs point to what ever is currently open in the active tab. Because of this you can
bookmark the URL or email it to your colleague. Forward and next page directs you to the
previously opened tab or to the next tab.
98
Guvnor
A wizard has been created to assist with the construction of a new table.
The wizard takes the user through the definition process, from adding patterns to creating
constraints and generating an expanded form.
99
Chapter 4. Drools Release Notes
100
Guvnor
101
Chapter 4. Drools Release Notes
102
Guvnor
Figure 4.60. Choose how rows are created - select the columns you want
to expand upon
103
Chapter 4. Drools Release Notes
The Asset Viewer now only shows sections that contain Assets.
104
Drools Planner
The Guvnor distribution now includes a war that runs on JBoss AS 7.0.1 (not on JBoss AS 7.0.0).
105
Chapter 4. Drools Release Notes
<solver>
...
<constructionheuristic>
<constructionheuristicType>FIRST_FIT</constructionheuristicType>
</constructionheuristic>
<localSearch>
...
</localSearch>
</solver>
• First Fit
• Best Fit
4.3.3.2. Phasing
Planner can now run several solver phases sequentially: each phase is a different optimization
algorithm. For example: run First Fit Decreasing, then Simulated Annealing, then Tabu Search:
<solver>
...
<constructionheuristic>
... <!-- First Fit Decreasing -->
</constructionheuristic>
<localSearch>
... <!-- Simulated Annealing -->
</localSearch>
<localSearch>
... <!-- Tabu Search -->
</localSearch>
</solver>
Planner now supports real-time planning. Real-time planning means that the planning problem
can change up to a few milliseconds before a solution needs to be executed.
106
Drools Integration
If one of the planning facts change while a planning problem is being solved, Planner can now
process such a planning fact change and incrementally continue from the best solution found
before that planning fact change. In practice, this means it can find a good solution for a big
planning problem only a few milliseconds after a planning fact changes.
Planner's documentation now covers several common techniques such as backup planning,
continuous planning and real-time planning.
The NQueens examples has been refactored to feel more like a real-world example.
INFO and DEBUG logging is far less verbose. Use TRACE logging to see everything. The log is
also now easier to read.
drools-spring provides features to define the listeners as standalone (individual) listeners and also
to define them as a group.
107
Chapter 4. Drools Release Notes
4.4.1.1. Core
4.4.1.1.1. MVEL
The MVEL dialect has been improved. We have moved all variable lookups to the new indexed
factories, which should allow faster execution, as well as being simpler code. The build process
for mvel has been reviewed to streamline it to avoid wasteless object creation so that the build
time is faster. We still have some more improvements on this to share the ParserConfiguration
which will make each MVEL compilation unit faster to initalise and use less memory, as they will
share import information for each package.
It was always possible to execute with MVEL in both dynamic and strict mode, with strict mode
for static type safety the default. This is configurable via the MVEL dialect configuration:
drools.dialect.mvel.strict = <true|false>
However there were some places in execution when strict mode was not enforced. Effort has now
been done to ensure that type safety is enforced through out, unless "strict == false". This means
that some bad code that compiled before may not compile now, because it is not type safe. For
those cases where the type safety cannot be achieved at compile time we added the @typesafe
annotation, discussed in it's own section.
4.4.1.1.2. Classloader
The Classloader has been improved to use a CompositeClassLoader instead of the previous
hierarchical "parent" classloader. This was necessary for OSGi where each module needs it's
own classpath, but reflection will not work if classloader cannot be found. Modules now add
themselves to the composite classloader when first initialised. This is also exposed to the user
where the previous Classloader argument on the kbase and kbuilder configuration now takes
vararg of ClassLoaders, all of which are now searchable.
You no longer need to enable or disable truth maintenance, via the kbase configuration. It is
now handled automatically and turned on only when needed. This was done along with the code
changes so that all entry points use the same code, previous to this the default entry point and
named entry points used different code, to avoid TMS overhead for event processing.
108
Drools Expert and Fusion
declare Person
firstName : String @key
lastName : String @key
age : int
end
The compiler will implicitly generate 3 constructors: one without parameters, one with the @key
fields, and one with all fields.
In order to extend a type declared in Java by a DRL declared subtype, repeat the supertype in
a declare statement without any fields.
import org.people.Person
109
Chapter 4. Drools Release Notes
declare Person
end
The parser has been rewritten. We had reached the limitations of what we could achieve in pure
ANTLR and moved to a hybrid parser, that adds flexibility to the language.
The main benefit with the new parser is that the language now support free form expressions for
constraints and 'from' statements. So complex expressions on nested accessors, method calls etc
should now all be possible as simple constraints without wrapping them with an eval(.....). This was
also important for us to start to move towards a single consistent grammer for both the "when" left
hand side and "then" right hand side. As previously we had to document the restricted limitations
of a field constraint on the LHS compared to expressions used inside of an 'eval' or used on the
RHS. Complex expressions are still internally rewritten as evals, so it's just syntacic sugar.
Examples:
The new parser also support free form expressions on the "from" clause, allowing for instance,
new syntaxes, like inline creation for lists:
A fluent API was created to allow programmatic creation of rules as an alternative to the previously
suggested method of template creation.
110
Drools Expert and Fusion
Positional arguments are ones where you don't need to specify the field name, as the position
maps to a known named field. i.e. Person( name == "mark" ) can be rewritten as Person( "mark"; ).
The semicolon ';' is important so that the engine knows that everything before it is a positional
argument. Otherwise we might assume it was a boolean expression, which is how it could be
interpretted after the semicolon. You can mix positional and named arguments on a pattern by
using the semicolon ';' to separate them. Any variables used in a positional that have not yet been
bound will be bound to the field that maps to that position.
declare Cheese
name : String
shop : String
price : int
end
The default order is the declared order, but this can be overiden using @Position
declare Cheese
name : String @position(1)
shop : String @position(2)
price : int @position(0)
end
111
Chapter 4. Drools Release Notes
Example patterns, with two constraints and a binding. Remember semicolon ';' is used to
differentiate the positional section from the named argument section. Variables and literals and
expressions using just literals are supported in posional arguments, but not variables.
Drools now provides Prolog style derivation queries, as an experimental feature. What this means
is that a query or the 'when' part of a rule may call a query, via a query element. This is also
recursive so that a query may call itself. A query element may be prefixed with a question mark
'?' which indicuates that we have a pattern construct that will pull data, rather than the normal
reactive push nature of patterns. If the ? is ommitted the query will be executed as a live "open
query" with reactiveness, similar to how normal patterns work.
A key aspect of BC is unification. This is where a query parameter may be bound or unbound,
when unbound it is considered an output variable and will bind to each found value.
In the example below x and y are parameters. Unification is done by subsequent bindings inside
of patterns. If a value for x is passed in, it's as though the pattern says "thing == x". If a value for
x is not passed in it's as though "x: thing" and x will be bound to each found thing.
Because Drools does not allow multiple bindings on the same variable we introduce ':=' unification
symbol to support this.
declare Location
thing : String
location : String
end
declare Location
thing : String
112
Drools Expert and Fusion
location : String
end
Here is an example of query element inside of a rule using mixed positional/named arguments.
package org.drools.test
import java.util.List
import java.util.ArrayList
dialect "mvel"
declare Here
place : String
end
declare Door
fromLocation : String
toLocation : String
end
declare Location
thing : String
location : String
end
declare Edible
thing : String
end
113
Chapter 4. Drools Release Notes
whereFood(x, z;) )
end
rule reactiveLook
when
Here( $place : place)
?look($place, $things; $food := food, $exits := exits)
then
System.out.println( \"You are in the \" + $place);
System.out.println( \" You can see \" + $things );
System.out.println( \" You can eat \" + $food );
System.out.println( \" You can go to \" + $exits );
end
As previously mentioned you can use live "open" queries to reactively receive changes over time
from the query results, as the underlying data it queries against changes. Notice the "look" rule
calls the query without using '?'.
Literal expressions can passed as query arguments, but at this stage you cannot mix expressions
with variables.
It is possible to call queries from java leaving arguments unspecified using the static field
org.drools.runtime.rule.Variable.v - note you must use 'v' and not an alternative instanceof
Variable. The following example will return all objects contained in the office.
114
Drools and jBPM integration
The algorithm uses stacks to handle recursion, so the method stack will not blow up.
@typesafe( <boolean>) has been added to type declarations. By default all type declarations are
compiled with type safety enabled; @typesafe( false ) provides a means to override this behaviour
by permitting a fall-back, to type unsafe evaluation where all constraints are generated as MVEL
constraints and executed dynamically. This can be important when dealing with collections that
do not have any generics or mixed type collections.
Added experimental framework to inspect a session and generate a report, either based on a
predefined template or with a user created template.
// Creates an inspector
SessionInspector inspector = new SessionInspector( ksession );
// Collects the session info
StatefulKnowledgeSessionInfo info = inspector.getSessionInfo();
// Generate a report using the "simple" report template
String report = SessionReporter.generateReport( "simple", info, null );
Camel integration using the Drools EndPoint was improved with the creation of both
DroolsConsumer and DroolsProducer components. Configurations were added to support the
insertion of either Camel's Exchange, Message or Body into the Drools session, allowing for the
easy development of dynamic content based routing applications. Also, support to entry points
was added.
Examples of routes:
115
Chapter 4. Drools Release Notes
The Drools Flow project and the jBPM project have been merged into the the newest version of
the jBPM project, called jBPM5. jBPM5 combines the best of both worlds: merging the experience
that was built up with the jBPM project over several years in supporting stable, long-living business
processes together with the improvements that were prototyped as part of Drools Flow to support
more flexible and adaptive processes. Now that jBPM 5.0 has been released, the Drools project
will be using jBPM5 as the engine to support process capabilities. Drools Flow as a subproject will
no longer exist, but its vision will continue as part of the jBPM project, still allowing (optional but)
advanced integration between business rules, business processes and complex event processing
and a unified environment for all three paradigms.
The impact for the end user however should be minimal, as the existing (knowledge) API is still
supported, the underlying implementation has just been replaced with a newer version. All existing
features should still be supported, and many more.
4.4.5. Guvnor
4.4.5.1. Guvnor Look & Feel Moving Closer To Native GWT Look
We have removed GWT-Ext from Guvnor and now only use GWT.
Embed Guvnor Editor's in external applications. You can create or edit assets like Business Rules,
Technical Rules, DSL definitions and Decision Tables in your applications using Guvnor's specific
editors. You can even edit multiple assets at once.
The ability to add annotations in Guvnor to declarative models has been added. This allows Fact
Types to be defined as events.
116
Guvnor
The guided editors have been enhanced to allow full use of Drools Fusion's Complex Event
Processing operators, sliding windows and entry-points.
117
Chapter 4. Drools Release Notes
The existing Guided Decision Table has been replaced to provide a foundation on which to build
our future guided Decision Table toolset. The initial release largely provides an equivalent feature-
set to the obsolete Guided Decision Table with a few improvements, as explained in more detail
below. A change from the legacy table was essential for us to begin to realise our desire to
provide the number one web-based decision table available. With this foundation we will be able
to expand the capabilities of our Guided Decision Table toolset to provide a feature rich, user-
friendly environment.
118
Guvnor
Adjacent cells in the same column with the same value can be merged thus eliminating the need
for otherwise cluttered views of multiple rows containing the same value.
119
Chapter 4. Drools Release Notes
4.4.5.5.2. Typed-columns
The major basic data-types (numeric, date, text and Boolean) are handled as such and respond
as you'd expect to sorting.
120
Guvnor
The table header section has been improved to show more verbose information to facilitate design-
time understanding. The table has a fixed header that remains as you'd expect, at the top of the
table, whilst scrolling larger decision tables.
The decision table responds better to changes made to column definitions; so changes to column
Fact Type, Fact Type field, calculation type, value list etc are reflected in the table itself.
Fact Patterns in condition columns can be negated to match when the defined pattern does not
exist in WorkingMemory. In essence it is now possible to construct rules within the decision table
equivalent to the following DRL fragment:-
121
Chapter 4. Drools Release Notes
Entire rules can be negated, giving rise to DRL fragements such as:-
122
Guvnor
123
Chapter 4. Drools Release Notes
Condition columns containing literal values that use the equality (==, equal to) or inequality (!=,
not equal to) operators can now contain a meta-value for "otherwise" which represents all other
values not explicitly defined in the column. This feature gives rise to DRL fragments such as the
following:-
124
Guvnor
Templates Rules and Decision Tables rules are now included in the package's report.
Now it is possible to create and mange Spring Context files inside Guvnor. These Context Files
are exposed through URLs so external applications can use them.
125
Chapter 4. Drools Release Notes
We added a new task in drools-ant which helps with configuring multiple Guvnor instances to be
able to share their Jackrabbit content.
The default Guvnor repository configuration uses embedded Derby databases which writes the
workspace and version information to the local file system. This is not always optimal for a
production system where it makes sense to use an external RDBMS.
We added a new section under the "Administration" tab called "Repository Configuration" which
helps generate the repository.xml configuration file for a number of databases (Microsoft SQL
Server, MySQL, Oracle, PostgreSQL, Derby, H2)
We have completed big parts of the integration between Guvnor and the Oryx web-based business
process editor. Our primary use cases supported by this integration are:
Please note that we are still working on full round-tripping support between the web-based Oryx
editor and our BPMN2 support provided through the eclipse plugin
126
Eclipse
4.4.6. Eclipse
The BRL Guided Editor has been removed due to lack of interest and it falling behind. Its removal
allows more focus on the GWT based Guided Editor in Guvnor. The DRL, text-based, Guided
Editor remains unaffected.
127
Chapter 4. Drools Release Notes
For example: before, in your pom.xml files, you declared drools-api like this:
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-api</artifactId>
...
</dependency>
And now, afterwards, in your pom.xml files, you declare knowledge-api like this instead:
<dependency>
<groupId>org.drools</groupId>
<artifactId>knowledge-api</artifactId>
...
</dependency>
As in Drools 5.0 it is still possible to configure a KnowledgeBase using configuration, via a xml
change set, instead of programmatically. However the change-set namespace is now versioned.
This means that for Drools 5.1, the 1.0.0 xsd should be referenced.
128
Core
<change-set xmlns='http://drools.org/drools-5.0/change-set'
xmlns:xs='http://www.w3.org/2001/XMLSchema-instance'
xs:schemaLocation='http://drools.org/drools-5.0/change-set change-set-5.0.xsd
main/resources/change-set-1.0.0.xsd' >
<add>
<resource source='classpath:org/domain/someRules.drl' type='DRL' />
<resource source='classpath:org/domain/aFlow.drf' type='DRF' />
</add>
</change-set>
4.5.2. Core
drools.mbeans = <enabled|disabled>
kbaseConf.setOption( MBeansOption.ENABLED )
4.5.2.2. Spring
Drools now has extensive Spring support, the XSD can be found in the the drools-spring jar. The
namespace is "http://drools.org/schema/drools-spring"
129
Chapter 4. Drools Release Notes
<drools:kbase id="kbase1">
<drools:resources>
<drools:resource type="DRL" source="classpath:org/drools/container/spring/
testSpring.drl"/>
<drools:resource ref="resource1"/>
<drools:resource source="classpath:org/drools/container/spring/
IntegrationExampleTest.xls" type="DTABLE">
<drools:decisiontable-conf input-type="XLS" worksheet-name="Tables_2" />
</drools:resource>
</drools:resources>
<drools:configuration>
<drools:mbeans enabled="true" />
<drools:event-processing-mode mode="STREAM" />
</drools:configuration>
</drools:kbase>
</beans>
Like KnowledgeBases Knowlege sessions can take a number of configurations, including "work-
item-handlers, "keep-references", "clock-type", "jpa-persistence".
130
Core
</drools:configuration>
</drools:ksession>
Knowledge Sessions can support startup batch scripts, previous versions used the "script"
element name, this will be updated to "batch". The following commands are supported: "insert-
131
Chapter 4. Drools Release Notes
ExecutionNodes are supported in Spring , these provide a Context of registered ksessions; this
can be used with Camel to provide ksession routing.
<drools:ksession
id="ksession1"
type="stateless"
name="stateless1"
kbase="kbase1"
node="node1"/
>
4.5.2.3. Camel
Spring can be combined with Camel to provide declarative rule services. a Camel Policy is added
from Drools which provides magic for injecting the ClassLoader used by the ksession for any data
formatters, it also augments the Jaxb and XStream data formatters. In the case lf Jaxb it adds
additional Drools related path info and with XStream it registers Drools related converters and
aliases.
You can create as many endpoints as you require, using different addresses. The
CommandMessagBodyReader is needed to allow the payload to be handled by Camel.
<cxf:rsServer id="rsServer"
address="/kservice/rest"
serviceClass="org.drools.jax.rs.CommandExecutorImpl">
132
Core
<cxf:providers>
<bean class="org.drools.jax.rs.CommandMessageBodyReader"/>
</cxf:providers>
</cxf:rsServer>
Camel routes can then be attached to CXF endpoints, allowing you control over the payload for
things like data formatting and executing against Drools ksessions. The DroolsPolicy adds some
smarts to the route. If JAXB or XStream are used, it would inject custom paths and converters, it
can also set the classloader too on the server side, based on the target ksession. On the client
side it automatically unwrapes the Response object.
This example unmarshalls the payload using an augmented XStream DataFormat and executes
it against the ksession1 instance. The "node" there refers to the ExecutionContext, which is a
context of registered ksessions.
The Drools endpoint "drools:node/ksession1" consists of the execution node name followed by
a separator and optional knowledge session name. If the knowledge session is not specified the
route will look at the "lookup" attribute on the incoming payload instace or in the head attribute
"DroolsLookup" to find it.
Spring, Camel and CXF can be combined for declarative services, drools-server is a .war that
combines these with some sample xml that works out of the box to get you started, acting like a sort
of template. If you are using the war in JBoss container you'll need to add this component, http://
camel.apache.org/camel-jboss.html. The war includes a test.jsp showing an echo like example to
get you started. This example just executes a simple "echo" type application. It sends a message
to the rule server that pre-appends the word "echo" to the front and sends it back. By default
the message is "Hello World", different messages can be passed using the url parameter msg -
test.jsp?msg="My Custom Message".
133
Chapter 4. Drools Release Notes
When setting this property to false, the KnowledgeAgent uses the exisitng KnowledgeBase
references apply the incremental changes. Now KnowledgeAgent's can process monitored
resource modifications in an incremental way. Modified definitions are compiled and compared
against the original version. According to definition's type, the behaves in different ways:
• Rules: For rules, the Agent searches for modifications in its attributes, LHS and RHS.
• Queries: queries are always replaced on kbase wether they are modified or not.
• Other definitions: All other definitions are always replaced in kbase (like if they were modified).
We expect to add better support for definition's modification detection in further versions.
The current implementation only supports the deletion of rules, querries and functions definitions.
Type declarations cannot be deleted.
The StatefulKnowledgeSessionInfo instance will contain a lot of relevant data gathered during the
analysis of the session. A simple example report template is provided and can be generated with
the following API call:
134
Expert
4.5.3. Expert
Rete traditional does an update as a retract + assert, for a given fact this causes all partial matches
to be destroyed, however during the assert some of which will be recreated again; because they
were true before the update and still true after the update. This causes a lot of uneccessary
object destruction and creation which puts more load on the Garbage Collector. Now an update
is a single pass and inspects the partial matches in place avoiding the unecessary destruction of
partial matches. It also removes the need to under go a normalisation process for events and truth
maintenance; the normalisation process was where we would look at the activations retracted and
activations inserted to figure out what was truly added and what was truly inserted to determine
the "diff".
4.5.3.2. Channels
Exit Points have been replaced by the more aptly named channels, we felt this was more
appropriate as they may be used by more than juse the rule engine and not an exact oppposte if
Entry Points. Where entry points are explicitey related to entering a partition in the Rete network.
Drools has always had query support, but the result was returned as an iterable set; this makes
it hard to monitor changes over time.
We have now complimented this with Live Querries, which has a listener attached instead of
returning an iterable result set. These live querries stay open creating a view and publish change
events for the contents of this view. So now you can execute your query, with parameters and
listen to changes in the resulting view.
135
Chapter 4. Drools Release Notes
A Drools blog article contains an example of Glazed Lists integration for live queries,
http://blog.athico.com/2010/07/glazed-lists-examples-for-drools-live.html
Interval "int:" timers follow the JDK semantics for initial delay optionally followed by a repeat
interval. Cron "cron:" timers follow standard cron expressions:
Calendars can now controll when rules can fire. The Calendar api is modelled on Quartz http://
www.quartz-scheduler.org/ [http://www.quartz-scheduler.org/] :
136
Expert
They can be used in conjunction with normal rules and rules including timers. The rule calendar
attribute can have one or more comma calendar names.
It is now possible to have a comma separated list of values in a cell and render those with a forall
template
137
Chapter 4. Drools Release Notes
forall(<separator>?){<codesnippt>}
forall(,) {propertyName == $}
forall(&&) {propertyName == $}
forall(||) {propertyName == $}
forall(||) {propertyNameA == $} && forall(||){propertyNameB == $}
etc
4.5.4. Flow
4.5.4.1. BPMN2
As we already announced earlier, the Drools team has decided to support the use of the upcoming
BPMN 2.0 specification for specifying business processes using XML. This milestone includes a
significant extension of the BPMN2 parser to support more of the BPMN2 features using Drools
Flow. More specifically:
• more extensive event support: much more combinations of event types (start, intermediate and
end) and event triggers (including for example error, escalation, timer, conditional and signal
events), have been included, as well as (interrupting and non-interrupting) boundary events
• sub-process parameters
• etc.
BPMN2 processes have also been integrated in the entire Drools tool chain, to support the entire
life cycle of the business process. This includes
• The ability to use BPMN2 processes in combination with our Eclipse tooling
• domain-specific processes
• etc.
138
Guvnor
As a result, Drools Flow is not only the first open-source process engine that supports such a
significant set of BPMN2 constructs natively, our knowledge-oriented approach also allows you
to easily combine your BPMN2 processes with business rules and complex event processing, all
using the same APIs and tools.
Drools Flow processes can now also be managed through a web console. This includes features
like managing your process instances (starting/stopping/inspecting), inspecting your (human) task
list and executing those tasks, and generating reports.
This console is actually the (excellent!) work of Heiko Braun, who has created a generic BPM
console that can be used to support multiple process languages. We have therefore implemented
the necessary components to allow this console to communicate with the Drools Flow engine.
Drools Flow can persist the runtime state of the running processes to a database (so they
don't all need to be in memory and can be restored in case of failure). Our default persistence
mechanism stores all the runtime information related to one process instance as a binary object
(with associated metadata). The data associated with this process instance (aka process instance
variables) were also stored as part of that binary object. This however could generate problem
(1) when the data was not Serializable, (2) when the objects were too large to persist as part of
the process instance state or (3) when they were already persisted elsewhere. We have therefor
implemented pluggable variable persisters where the user can define how variable values are
stored. This for example allows you to store variable values separately, and does support JPA
entities to be stored separately and referenced (avoiding duplication of state).
Over time, processes may evolve. Whenever a process is updated, it is important to determine
what should happen to the already running process instances. We have improved our support for
migrating running process instances to a newer version of the process definition. Check out the
Drools Flow documentation for more information.
The Drools build now exports an installer that simplifies installing the Eclipse plugin, Guvnor and
the gwt-console. It creates and copies the necessary jars and wars and deploys them to the JBoss
AS. It also includes a simple evaluation process example you can use to test your setup. For more
info, download the drools installer and take a look at the readme within.
4.5.5. Guvnor
Appearance has been cleaned, for example less pop ups. Reminders for save after changes in
assets and information about actions that were taken, also better error reporting if something goes
wrong.
139
Chapter 4. Drools Release Notes
4.5.5.1. Discussions
The comments are below the "documentation" section (and of course optional) (and there is an
Atom feed to them).
A "backchannel" type connection that is kept open from the browser to allow messages to push
back - this means (when enabled) that messages show up in real time (and other handy things
like if something is added to a list - the list is updated).
4.5.5.2. Inbox
The inbox feature provides the ability to track what you have opened, or edited - this shows up
under an "Inbox" item in the main navigator. http://blog.athico.com/2009/09/inbox-feature-to-track-
recent-changes.html
• Recently Opened
140
Guvnor
• Clicking on the recently opened item will open a listing of all items you have "recently" opened
(it tracks a few hundred items that you were last to look at).
• Recently Edited
• Any items that you save changes to, or comment on will show up here, once again.
• Incoming changes
• This tracks changes made by *other people* to items that are in *your* "Recently Edited" list.
When you open these items they then are removed from this list (but remain in your Recently
Edited list).
The Guvnor-Importer is a maven build tool that recurses your rules directory structure and
constructs an xml import file that can be manually imported into the Drools-Guvnor web interface
via the import/export administration feature.
4.5.5.4. DroolsDoc
PDF document containing information about the package and each DRL asset. DroolsDoc for
knowledge package can be downloaded from package view under "Information and important
URLs"
The built in selector allows user to choose what assets to build according to:
141
Chapter 4. Drools Release Notes
• Category
• Metadata
It is possible to verify just the asset you are working on ( ruleflow, rule, decision table ). Verification
finds issues like conflicting restrictions in a rule or redundant rows in decision tables.
Makes it possible to open more than one asset into one view. All the assets can be saved and
edited as a group.
Guided Editor has basic support for From, Accumulate and Collect Patterns. You can add any
of these structures as regular Patterns. New expression builder component was created to add
support for nested method calls of a variable. Using the “plus” button at the top of rule’s WHEN
section or using the new “Add after” button present in every Pattern will open the popup to add new
conditional elements to your rule. In the list of possible elements you will find three new entries:
”From”, “From Accumulate” and “From Collect”.
When you add a new “From” element, you will see something like the image below in the guided
editor. The left pattern of the “From” conditional element is a regular Pattern. You can add there
any type of conditional element you want.The right section of the “From” pattern is an expression
builder.
142
Guvnor
When using 'from collect' In the left pattern you can choose from “java.util.Collection”,
“java.util.List” or “java.util.Set” Fact Types. This Fact Types will be automatically included in the
package’s Fact Types list.
The right pattern of the collect conditional element could be one of this patterns:
• From Pattern
143
Chapter 4. Drools Release Notes
When using 'from accumulate' The left pattern could be any Fact Type Pattern. The right section
of this conditional element is splited in two:
The left pattern could be any Fact Type Pattern. The right section of this conditional element is
splited in two:
• Source Pattern: (Bed $n, in the screenshot) could be any Fact Type, From, Collect or
Accumulate pattern.
• Accumulate function: Here you will find a tabbed panel where you can enter an accumulate
function (sum() in the screenshot) or you can create an online custom function using the “Custom
Code” tab.
Rule Templates allow the Guided editor to be used to build complex rules that can then be authored
easily through a spreadsheet's tabular data metaphor. Instead of a field's value, simply mark it
as a named "Template Key" and that key is available as a column in the grid. Each row will be
applied to the rule template to generate a rule.
144
Guvnor
145
Chapter 4. Drools Release Notes
When modelling rules the user gets exposed to all the fact types which can be a bit over whelming.
Working Sets allow related fact types can be grouped together, provided a more managable view
of selecting fact types, when authoring rules
146
Guvnor
147
Chapter 4. Drools Release Notes
Figure 4.87. Without the Working Set all types are visible
148
Guvnor
Working Sets can be combined with fact consraints to provide additional design time validation.
For instance if you are authoring a rule on someone's age, we can know the valid ranges at design
time and use this to constrain the author. The fact constraints are part of the workingset and when
authoring a rule you must select the work set constraints that you wish to be applied as part of
the validation process.
149
Chapter 4. Drools Release Notes
150
Guvnor
Figure 4.93. Select the working set to validate the age field
151
Chapter 4. Drools Release Notes
Editing rules is made more explicit. Editor is less "boxy" and rules are written more as a normal
text. "contains" keyword was added and binding variables to restrictions is now easier.
Guided Editor supports Pattern reordering in LHS and RHS sections, as well as positional
inserting, new Patterns can be inserted in any order (and not always at the end).
152
Eclipse
Columns can be moved and location of a new row can be selected freely.
4.5.6. Eclipse
You can now use sorting and grouping for Agenda Groups.
153
Chapter 4. Drools Release Notes
It is now possible to drag and drop log files into the audit view.
There is a known issue with the experimental multi-thread execution mode as described in the
following JIRA.
https://jira.jboss.org/browse/JBRULES-2125
Drools now has complete api/implementation separation that is no longer rules oriented. This is
an important strategy as we move to support other forms of logic, such as workflow and event
processing. The main change is that we are now knowledge oriented, instead of rule oriented. The
154
Drools API
module drools-api provide the interfaces and factories and we have made pains to provide much
better javadocs, with lots of code snippets, than we did before. Drools-api also helps clearly show
what is intended as a user api and what is just an engine api, drools-core and drools-compiler did
not make this clear enough. The most common interfaces you will use are:
• org.drools.builder.KnowledgeBuilder
• org.drools.KnowledgeBase
• org.drools.agent.KnowledgeAgent
• org.drools.runtime.StatefulKnowledgeSession
• org.drools.runtime.StatelessKnowledgeSession
Factory classes, with static methods, provide instances of the above interfaces. A pluggable
provider approach is used to allow provider implementations to be wired up to the factories at
runtime. The Factories you will most commonly used are:
• org.drools.builder.KnowledgeBuilderFactory
• org.drools.io.ResourceFactory
• org.drools.KnowledgeBaseFactory
• org.drools.agent.KnowledgeAgentFactory
ksession.dispose();
155
Chapter 4. Drools Release Notes
ksession.dispose();
'kbuilder', 'kbase', 'ksession' are the variable identifiers often used, the k prefix is for 'knowledge'.
Example 4.27. We have uniformed how decision trees are loaded, and
they are now consistent with no need to pre generate the DRL with the
spreadsheet compiler
It is also possible to configure a KnowledgeBase using configuration, via a xml change set, instead
of programmatically.
<change-set xmlns='http://drools.org/drools-5.0/change-set'
156
Drools Guvnor
xmlns:xs='http://www.w3.org/2001/XMLSchema-instance'
xs:schemaLocation='http://drools.org/drools-5.0/change-set change-
set-5.0.xsd' >
<add>
<resource source='classpath:org/domain/someRules.drl' type='DRL' />
<resource source='classpath:org/domain/aFlow.drf' type='DRF' />
</add>
</change-set>
The other big change for the KnowledgeAgent, compared to the RuleAgent, is that polling scanner
is now a service. further to this there is an abstraction between the agent notification and the
resource monitoring, to allow other mechanisms to be used other than polling.
Example 4.30. These services currently are not started by default, to start
them do the following
ResourceFactory.getResourceChangeNotifierService().start();
ResourceFactory.getResourceChangeScannerService().start();
ResourceFactory.getResourceChangeNotifierService().addResourceChangeMonitor( myJmsMonitor);
157
Chapter 4. Drools Release Notes
158
Drools Guvnor
159
Chapter 4. Drools Release Notes
160
Drools Guvnor
This works with the new "declare" statement - you can now declare types in drl itself. You can
then populate these without using a pojo (if you like). These types are then available in the
rulebase.
• Fine grained security (lock down access to the app per package or per category). Users who
only have category permissions have limited UI capability (ideal for business users)
• Category rules allows you to set 'parent rules' for a category. Any rules appearing in the given
category will 'extend' the rule specified - ie inherit the conditions/LHS. The base rule for the
category can be set on package configuration tab. RHS is not inherited, only the LHS
• Scenario runner can show event trace that was recorded by audit logger
• DSL sentences in guided editor can now be set to show enums as a dropdown, dates as a
date picker, booleans as a checkbox and use regular expressions to validate the inputs (DSL
Widgets in Guvnor)
161
Chapter 4. Drools Release Notes
Shadow proxies are no longer needed. Shadow proxies protected the engine from information
change on facts, which if occurred outside of the engine's control it could not be modified or
retracted.
You no longer need to confine one PackageBuilder to one package namespace. Just keeping
adding your DRLs for any namespace and getPackages() returns an array of Packages for each
of the used namespaces.
It is now possible to attach a RuleBase to a PackageBuilder, this means that rules are built and
added to the rulebase at the same time. PackageBuilder uses the Package instances of the
actual RuleBase as it's source, removing the need for additional Package creation and merging
that happens in the existing approach.
Stateful sessions can now saved and resumed at a later date. Pre-loaded data sessions can now
be created. Pluggable strategies can be used for user object persistence, i.e. hibernate or identity
maps.
Drools now supports a new base construct called Type Declaration. This construct fulfils two
purposes: the ability to declare fact metadata, and the ability to dynamically generate new fact
types local to the rule engine. The Guvnor modelling tool uses this underneath. One example of
the construct is:
162
Drools Expert
declare StockTick
@role( event )
@timestamp( timestampAttr )
companySymbol : String
stockPrice : double
timestampAttr : long
end
To declare and associate fact metadata, just use the @ symbol for each metadata ID you want
to declare. Example:
declare StockTick
@role( event )
end
To activate the dynamic bean generation, just add fields and types to your type declaration:
declare Person
name : String
age : int
end
A series of DSL improvements were implemented, including a completely new parser and the
ability to declare matching masks for matching variables. For instance, one can constrain a phone
number field to a 2-digit country code + 3-digit area code + 8-digit phone number, all connected
by a "-" (dash), by declaring the DSL map like: The phone number is {number:\d{2}-\d{3}-\d{8}}
Any valid java regexp may be used in the variable mask.
163
Chapter 4. Drools Release Notes
4.6.3.9. fireUntilHalt()
Drools now supports "fireUntilHalt()" feature, that starts the engine in a reactive mode, where rules
will be continually fired, until a halt() call is made. This is specially useful for CEP scenarios that
require what is commonly known as "active queries".
164
Drools Expert
For options that don't have a predefined constant or can assume multiple values, a factory method
is provided. For instance, to configure the alpha threshold to 5, just use the "get" factory method:
config.setOption( AlphaThresholdOption.get(5) );
As you can see, the same setOption() method is used for the different possible configurations,
but they are still type safe.
There are times when it is necessary to collect sets or lists of values that are derived from the
facts attributes, but are not facts themselves. In such cases, it was not possible to use the collect
CE. So, Drools now has two accumulate functions for such cases: collectSet for collecting sets
of values (i.e., with no duplicate values) and collectList for collecting lists of values (i.e., allowing
duplicate values):
// collect the list of alarm codes from the alarms in the working memory
$codes : List() from accumulate( Alarm( $c : code, $s : severity ),
collectList( $c + $s ) )
Facts that implement support for property changes as defined in the Javabean(tm) spec, now can
be annotated so that the engine register itself to listen for changes on fact properties. The boolean
parameter that was used in the insert() method in the Drools 4 API is deprecated and does not
exist in the drools-api module.
declare Person
@propertyChangeSupport
165
Chapter 4. Drools Release Notes
end
Typically though you will want to execute a batch of commands, this can be achieved via
the composite Command BatchExecution. BatchExecutionResults is now used to handle
the results, some commands can specify "out" identifiers which it used to add the result to
the BatchExecutionResult. Handily querries can now be executed and results added to the
BatchExecutionResult. Further to this results are scoped to this execute call and return via the
BatchExecutionResults:
The CommandFactory details the supported commands, all of which can marshalled using
XStream and the BatchExecutionHelper. This can be combined with the pipeline to automate
the scripting of a session.
166
Drools Expert
Using the above for a rulset that updates the price of a Cheese fact, given the following xml to
insert a Cheese instance using an out-identifier:
<batch-execution>
<insert out-identifier='outStilton'>
<org.drools.Cheese>
<type>stilton</type>
<price>25</price>
<oldPrice>0</oldPrice>
</org.drools.Cheese>
</insert>
</batch-execution>
<batch-execution-results>
<result identifier='outStilton'>
<org.drools.Cheese>
<type>stilton</type>
<oldPrice>0</oldPrice>
<price>30</price>
</org.drools.Cheese>
</result>
</batch-execution-results>
4.6.3.17. Marshalling
167
Chapter 4. Drools Release Notes
However with marshalling you need more flexibility when dealing with referenced
user data. To achieve this we have the ObjectMarshallingStrategy interface.
Two implementations are provided, but the user can implement their own. The
two supplied are IdentityMarshallingStrategy and SerializeMarshallingStrategy.
SerializeMarshallingStrategy is the default, as used in the example above and it just calls the
Serializable or Externalizable methods on a user instance. IdentityMarshallingStrategy
instead creates an int id for each user object and stores them in a Map the id is written to the stream.
When unmarshalling it simply looks to the IdentityMarshallingStrategy map to retrieve the
instance. This means that if you use the IdentityMarshallingStrategy it's stateful for the life
of the Marshaller instance and will create ids and keep references to all objects that it attempts
to marshal.
For added flexability we can't assume that a single strategy is suitable for this we have added
the ObjectMarshallingStrategyAcceptor interface that each ObjectMarshallingStrategy
has. The Marshaller has a chain of strategies and when it attempts to read or write a user
object it iterates the strategies asking if they accept responsability for marshalling the user
object. One one implementation is provided the ClassFilterAcceptor. This allows strings
and wild cards to be used to match class names. The default is "*.*", so in the above the
IdentityMarshallingStrategy is used which has a default "*.*" acceptor. But lets say we want
to serialise all classes except for one given package, where we will use identity lookup, we could
do the following:
168
Drools Expert
169
Chapter 4. Drools Release Notes
Drools 4.0 had simple "RuleFlow" which was for orchestrating rules. Drools 5.0 introduces a
powerful (extensible) workflow engine. It allows users to specify their business logic using both
rules and processes (where powerful interaction between processes and rules is possible) and
offers a unified enviroment.
170
Drools Flow
Timers:
A timer node can be added which causes the execution of the node to wait for a specific period.
Currently just uses JDK defaults of initial delay and repeat delay, more complex timers will be
available in further milestones.
Human Task:
Processes can include tasks that need to be executed by human actors. Human tasks
include parameters like taskname, priority, description, actorId, etc. The process engine can
easily be integrated with existing human task component (like for example a WS-HumanTask
implementation) using our pluggable work items (see below). Swimlanes and assignment rules
are also supported.
The palette in the screenshot shows the two new components, and the workflow itself shows the
human task in use. It also shows two "work items" which is explained in the next section:
171
Chapter 4. Drools Release Notes
172
Drools Flow
Domain Specific Work Items are pluggable nodes that users create to facilitate custom task
execution. They provide an api to specify a new icon in the palette and gui editor for the tasks
properties, if no editor gui is supplied then it defaults to a text based key value pair form. The api
then allows execution behaviour for these work items to be specified. By default the Email and Log
work items are provided. The Drools flow Manual has been updated on how to implement these.
The below image shows three different work items in use in a workflow, "Blood Pressure", "BP
Medication", "Notify GP":
173
Chapter 4. Drools Release Notes
Drools 4.0 used Xstream to store it's content, which was not easily human writeable. Drools 5.0
introduced the ePDL which is a XML specific to our process language, it also allows for domain
specific extensions which has been talked about in detail in this blog posting "Drools Extensible
Process Definition Language (ePDL) and the Semantic Module Framework (SMF)". An example
of the XML language, with a DSL extension in red, is shown below.
174
Drools Flow
<nodes>
<start id="0" />
<connections>
<connection from="0 to="1" />
<connection from="1" to="2" />
<connection from="2" to="3" />
</connections>
</process>
• Exception handlers and exception handler scopes to handle exceptions that could be thrown
• A ForEach node allows instantiating a section of your flow multiple times, for each element in
a collection
175
Chapter 4. Drools Release Notes
4.6.4.9. JPA
Improved support for persistence (JPA) and transactions (JTA).
// create a new JPA-based session and specify the JPA entity manager factory
Environment env = KnowledgeBaseFactory.newEnvironment();
env.set(
EnvironmentName.ENTITY_MANAGER_FACTORY,
Persistence.createEntityManagerFactory(
"emf-
name" ) );
env.set( EnvironmentName.TRANSACTION_MANAGER, TransactionManagerServices.getTransactionManager(
StatefulKnowledgeSession
ksession
JPAKnowledgeService.newStatefulKnowledgeSession(
= kbase,
null,
//
env
);
KnowledgeSessionConfiguration may be null, and a default will be used
int sessionId = ksession.getId();
176
Drools Flow
• Process instances can now listen for external events by marking the event
node property "external" as true. External events are signaled to the engine
using session.signalEvent(type, eventData) More information on how to use
events inside your processes can be found in the Drools Flow documentation
here: https://hudson.jboss.org/hudson/job/drools/lastSuccessfulBuild/artifact/trunk/target/docs/
drools-flow/html/ch03.html#d0e917
• Process instances are now safe for multi-threading (as multiple thread are blocked from working
on the same process instance)
• Process persistence / transaction support has been further improved. Check out the drools-
process/drools-process-enterprise project for more information.
• The human task component has been extended to support all kinds of data for input / output /
exceptions during task execution.
Example 4.52. As a result, the life cycle methods of the task client have
been extended to allow content data
• It is now possible to migrate old Drools4 RuleFlows (using the xstream format) to Drools5
processes (using readable xml) during compilation. Migration will automatically be performed
when adding the RuleFlow to the KnowledgeBase when the following system property is set:
drools.ruleflow.port = true
• The "Transform" work item allows you to easily transform data from one format to another inside
processes. The code and an example can be found in the drools-process/drools-workitems
directory.
177
Chapter 4. Drools Release Notes
• The history log - that keeps the history of all executed process instances in a database - has
been extended so it is now capable of storing more detailed information for one specific process
instance. It is now possible to find out exactly which nodes were triggered during the execution
of the process instance.
• A new type of join has been added, one that will wait until n of its m incoming connections have
been completed. This n could either be hardcoded in the process or based on the value of a
variable in the process.
• Improvements have been made to make persistence easier to configure. The persistence
approach is based on a command service that makes sure that all the client invocations are
executed inside a transaction and that the state is stored in the database after successful
execution of the command. While this was already possible in M4 using the commands directly,
we have extended this so that people can simply use the normal StatefulKnowledgeSession
interface but simply can configure the persistence using configuration files. For more details,
check out the chapter on persistence in the Drools Flow documentation.
• they may be transparently garbage collected after it's lifecycle window expires
Example 4.53. Both existing and generated beans support event semantics:
178
Drools Fusion
import org.drools.test.StockTick
declare StockTick
@role( event )
end
$st : StockTick( company == "ACME", price > 10 ) from entry-point "stock stream"
As seen in the test above, Drools supports both: primitive events, that are point in time occurrences
with no duration, and compound events, that are events with distinct start and end timestamps.
• coincides
• before
• after
• meets
• metby
179
Chapter 4. Drools Release Notes
• overlaps
• overlappedby
• during
• includes
• starts
• startedby
• finishes
• finishedby
Drools 5.0 adds support for reasoning over sliding windows of events. For instance:
The above example will only pattern match the RHAT stock ticks that happened in the last 60
clock ticks, discarding any event older than that.
Enabling full event processing capabilities requires the ability to configure and interact with a
session clock. Drools adds support for time reasoning and session clock configuration, allowing
it to not only run real time event processing but also simulations, what-if scenarios and post-
processing audit by replaying a scenario.
Since events usually have strong temporal relationships, it is possible to infer a logical time window
when events can possibly match. The engine uses that capability to calculate when an event is
no longer capable of matching any rule anymore and automatically retracts that event.
180
Eclipse IDE
Drools adopted a simplified syntax for time units, based on the ISO 8601 syntax for durations.
This allows users to easily add temporal constraints to the rules writing time in well known units.
Example:
The above pattern will match if SomeEvent happens between 1 minute (1m) and 1 hour and 30
minutes after $anotherEvent.
added the ability to define a per-type event expiration policy. In the example below, the StockTick
events will expire 10 minutes after they enter the system:
Example 4.56. added the ability for point-in-time operators (before, after and
coincides) to be used with any arbitrary date field
• Support multiple runtimes: The IDE now supports multiple runtimes. A Drools runtime is a
collection of jars on your file system that represent one specific release of the Drools project
jars. To create a runtime, you must either point the IDE to the release of your choice, or you
can simply create a new runtime on your file system from the jars included in the Drools Eclipse
plugin. Drools runtimes can be configured by opening up the Eclipse preferences and selecting
the Drools -> Installed Drools Runtimes category, as shown below.
181
Chapter 4. Drools Release Notes
• Process Skins allow you to define how the different RuleFlow nodes are visualized. We now
support two skins: the default one which existed before and a BPMN skin that visualizes
the nodes using a BPMN-like representation: http://blog.athico.com/2008/10/drools-flow-and-
bpmn.html
• An (X)OR split now shows the name of the constraint as the connection label
• Custom work item editors now signal the process correctly that it has been changed
182
Language Expressiveness Enhancements
• New Field Constraint operators: not matches, not contains, in, not in, memberOf, not memberOf
• Full support for Conditional Elements nesting, for First Order Logic completeness.
• Parser improvements to remove previous language limitations, like character escaping and
keyword conflicts
• Support for pluggable dialects and full support for MVEL scripting language
• Fact attributes auto-vivification for return value restrictions and inline-eval constraints
• Support for nested accessors, property navigation and simplified collection, arrays and maps
syntax
• Support for Stateful and Stateless working memories (rule engine sessions)
183
Chapter 4. Drools Release Notes
• User friendly web interface with nice WEB 2.0 ajax features
• Package configuration
• Rule Authoring easy to edit rules both with guided editor ( drop-down menus ) and text editor
• Versioning enabled, you can easily replace yours assets with previously saved
This section of the manual is a work in progress and will document a simple how-to on upgrading
from Drools 3.0.x to Drools 4.0.x.
184
Rule Language Changes
WorkingMemory wm = rulebase.newWorkingMemory();
StatefulSession wm = rulebase.newStatefulSession();
The StatefulSession object has the same behavior as the Drools 3.0.x WorkingMemory (it even
extends the WorkingMemory interface), so there should be no other problems with this fix.
185
Chapter 4. Drools Release Notes
assert() insert()
assertLogical() insertLogical()
modify() update()
Drools 3.0.x did not had native support for primitive types and consequently, it auto-boxed all
primitives in it's respective wrapper classes. That way, any use of a boxed variable binding
required a manual unbox.
Drools 4.0.x has full support for primitive types and does not wrap values anymore. So, all previous
unwrap method calls must be removed from the DRL.
At this point, its main objective is to upgrade the memory action calls from 3.0.x to 4.0.x, but expect
it to grow over the next few weeks covering additional scenarios. It is important to note that it does
not make a dumb text search and replace in rules file, but it actually parses the rules file and try
to make sure it is not doing anything unexpected, and as so, it is a safe tool to use for upgrade
large sets of rule files.
186
DSL Grammars in Drools 4.0
The Drools update tool can be found as a maven project in the following source repository http://
anonsvn.labs.jboss.com/labs/jbossrules/trunk/experimental/drools-update/ you just need to check
it out, and execute the maven clean install action with the project's pom.xml file. After resolve all
the class path dependencies you are able to run the toll with the following command:
• -f pattern for the files to be updated. The format is the same as used by ANT: * = single file,
directory ** = any level of subdirectories EXAMPLE: src/main/resources/**/*.drl = matches all
DRL files inside any subdirectory of /src/main/resources
Now, you need to escape '[' and ']' characters, as they have special meaning in regexps. So, the
same mapping in Drools 4.0 would be:
187
Chapter 4. Drools Release Notes
188
Chapter 5.
189
Chapter 5. Drools compatibili...
If you combine the versions specified in any single row of this table, they are compatible.
190