I agree, that it is not a beautiful solution.
But the problem is, that you can´t tell if two Objects are equal if you
dont define equals() , hashcode() on your own. How will your
dublication-preventing Rule look like ?
Ontology-optimized rule engines such as Jena or Sesame (openrdf) work
internaly with Triple Objects (Resource, Property, Object) using
Strings. Thus the framework can hande the hashcode transparently to the
user and you never get dublicated Facts after applying Rules on the
Model. But, you need to map your BusinessObjects into the RuleEngine by
calling Framework-specific creationMethods manually , which is currently
a pretty annoying task...reminds me on the days without Hibernate when
working with JDBC and SQL.....
Anyway...from my point of view, this is simmilar to Object-Rational
mapping. Here we *could* have Object-Fact mapping. An easier solution
would be to use Annotations..to mark the attributes who are considert to
define hashCode and thus the Objectidentity. Because we know that
Reflection is evil, you could generats a Proxy-Class using lets say
Javassist, which wrapps your JavaClass and generates a hashcode Method
based on the Annotations you have set.
I think that a mapping-file in XML should not be necessary.
Implementation-steps:
1) assertObject into workmemory
2) get Class of Object and check if Drools-related Annotations are set
3) Generate HashCodeProxy
4) have fun - without Dublications
Peter Van Weert schrieb:
This will probably work, but is no longer declarative, rule-based
programming. If you do not care about this semi-philosophical
argument, stop reading here...
The correct way to solve the problem in my opinion is to add an extra
rule that removes duplicates (in Drools you should probably give it a
higher salience...). So you simply add away, and /declare/ that
duplicates have to be removed. The responsibility of using a hash-set
(or any other efficient data-structure) to efficiently look up
duplicate facts should lie with the rule-engine, not with the user.
As always, I do not know the details of the internals of Drools, so
your solution might in fact be faster, but it remains a hack in my
opinion...
Greets,
Pé