How to access working memory objects in DRL file

1,675 views
Skip to first unread message

Ravi

unread,
Dec 6, 2016, 7:06:53 AM12/6/16
to Drools Usage
Hi

I am trying to access the working memory objects to compare the newly inserted object in the DRL file, but I am not finding way to do so, 

Is there anyway where I can access the working memory objects of desired class type and then to compare that whether the object is already present in the working memory or not in drl file. Your help be grateful.

For example : 

package com.rule

import com.emp.Employee

rule "Employee Rule "
when 
empl: Employee(name=="ABC") // Here I want to compare the working memory objects which have been already present
             
then
empl.isExist(true);
end

Thanks
Ravi

Tibor Zimányi

unread,
Dec 6, 2016, 8:29:48 AM12/6/16
to Drools Usage
Hi Ravi,

you can use modify() method in "then" part of the rule. Check the docs [1], chapter 8.8.3.5. Fine grained property change listeners. 

Regards,

Tibor


Dňa utorok, 6. decembra 2016 13:06:53 UTC+1 Ravi napísal(-a):

Ravi

unread,
Dec 6, 2016, 9:29:01 AM12/6/16
to Drools Usage
But the problem which I am having is 

1st Insert
I inserted one object of type Employee in working memory with attributes id = 1, name = "ABC" and profession_type = "Salaried".

2nd Insert. 2nd Object
now if i try to insert one more object of type Employee with identical attribute values  id = 1, name = "ABC" and profession_type = "Salaried", I do not want to insert the object of same attribute values in working memory, I want to validate against the working memory in drl file. 

If there is change in attribute values, then I would like to insert into working memory.

Please let me know how modify() will help in this case. If you get me any code snippet which will be helpful.

Thanks
Ravi

Tibor Zimányi

unread,
Dec 6, 2016, 9:49:33 AM12/6/16
to Drools Usage
If the problem is that you don't want to insert duplicate facts into working memory, you can use "equality" mode of the engine. For more info see few last paragraphs in section "7.1.1.1. Insert" of documentation I linked before. Basically you set the equality mode for KieBase (e.g. see docs section 4.2.2.4. Defining a KieModule programmatically or it can be set in kmodule.xml) and then you define equals() and hashCode() methods on fact type classes (in your case Employee) according to your equality needs. In this case when inserting a fact, the engine will find out based on equals() and hashCode() methods, if there isn't already the same fact inserted. If there is, it doesn't insert new fact and returns FactHandle of the fact that is already in working memory. Using equality modes is the preferred method of handling duplicates. You can do it in DRL, but that just waste work of the engine, because you must fire rules.

I hope this helps.

Tibor

Dňa utorok, 6. decembra 2016 15:29:01 UTC+1 Ravi napísal(-a):

Ravi

unread,
Dec 6, 2016, 10:11:22 AM12/6/16
to Drools Usage
Thanks for the reply Tibor,

But my intention is to provide response back to the user that you are inserting duplicate object after firing rule, which is already present in the working memory. If it is returning the same fact handle of the existing object, I would not know what happened while firing the rule. Don't mind if you find this question as bit odd and asking you questions back and forth. How could I show a message to user that it is duplicate by setting any attribute as alreadyExist in the drl file while returning the response.

Thanks
Ravi

Ravi

unread,
Dec 7, 2016, 2:45:09 AM12/7/16
to Drools Usage
Tibor Can we access all the working memory objects in DRL file?? Do we have API call to get all the working memory objects directly in DRL file.

Thanks

Tibor Zimányi

unread,
Dec 7, 2016, 4:36:03 AM12/7/16
to Drools Usage
Hi Ravi,

I talked about this with my colleague and you can use method kieSession.getFactHandle(Object object) for this. See JavaDoc here[1] (session implements EntryPoint interface). You can check for duplicates before insert this way. If it returns null, you know that the fact is not in working memory. If it returns a FactHandle, the object is already there and you can provide output for user that he/she is inserting duplicate fact. This used with equality mode is the best option I think. I really wouldn't use DRL rules for this. 

Regards,

Tibor

Ravi

unread,
Dec 7, 2016, 4:49:04 AM12/7/16
to Drools Usage
Thanks Tibor, 

Even I was of the fact that I wanted to this in this fashion which you are suggesting. Thanks for all the feedback from you Tibor. I have the code to fetch all facts from the working memory.
Reply all
Reply to author
Forward
0 new messages