[rules-users] Drools & "Query by Example"?

Michael Zimmermann list at incunabulum.de
Fri Sep 5 09:38:20 EDT 2008


Hi there,

I am not really sure what is the best approach to model this the drools
way... So, what is the best approach using drools to find all objects of
a known type that fulfil certain criterias? Caveat: The number of
criterias is flexible and not known?

I know, that such a selection process can be defined as simple case of
constraint programming. Afaik this could be a problem for any rulebased
system. Yet we do not want to introduce another compontent as this leads
to dependency and communication issues.

Example:
Assume a class book with properties "author", "year", "id" and
"publisher". As our problem is more complex I want to test book
instances for the properties "author", "year" etc. in individual rules.
Now, given a list of books in the working memory I want to get answer to
the following questions:

- List all books with author="myName", id="123"
- List all book with publisher="Frank"

Due to the closed world assumption in Java and hence Drools, what is not
possible is:

rule "MyRule"
when
  book(year==myYear, author==myAuthor ...)
then
  ....

This works only if all properties myYear, myAuthor ... are present. If
one is undefined, the rule does not match.

The System:
The system uses drools quite successfully for many tasks. All data
objects are persisted using db4o.

Currently, I can think of two different approaches:
1)
- Start with all objects in the working memory
- For each condition, if present, flag all objects, that do not
  fulfil this condition, as invalid
- Afterwards, list / process all objects that are not marked as invalid

2)
- Start with no object in the working memory
- Construct a blank object of the type analyzed
- If a certain condition is present, add this conidtion to
  the object
- Query the object database in db4o and add all valid matching
  object to the working memory. (db4o supports QueryByExample)

The double negotiation in the first approach is hard to understand and
difficult to maintain. The second approach works reasonably well yet
moves some of the main application logic to another component.

So, any clues, pointers or ideas how to solve this problem in a better way?

cu, Michael



More information about the rules-users mailing list