1
votes

I'm looking for a way to store large objects within Drools for long periods of time (i.e. not like facts which are added and removed from within a session).

I've read that Drools works using KnowledgeBases and Sessions (Stateless & Stateful) and that KnowledgeBases contain application knowledge definitions but no runtime data.

In a case where I need to store, for example, a large dictionary (that won't change but will be referenced by more than one successive session), and have objects added to working memory and checked against this dictionary to have rules fired, where would it be best to have this stored?

Does everything just go into working memory (in which case, would I need to load the dictionary into memory each time I open a new session?) or am I just missing a crucial Drools basic principle? Would global variables be a good fix for this?

1
Stateful sessions can last for as long as your application is running, so I'm not sure what you mean by wanting something that lasts longer than a session. If you wish for your sessions to persist between application runs, then you should probably read the section of the manual on session persistence.Steve
Right you are...After some further reading up on the subject I realised I had the concept of Drools sessions and runtime memory wrong to begin with...Modified my question somewhat. Now I'm just not sure where to store large reference objects as opposed to having to add them to the kieSession each time it's created.GroomedGorilla
Cool. However, based on your updates, I'm not sure that there's a definite best way to do it. A global variable is a perfectly good way of doing it, and relatively simple. However, depending on how your rules are written, you may get better performance by inserting the dictionary (or perhaps its values) as a fact into a stateful session. You can then insert a request fact, fire rules and retract it.Steve
Interesting approach. I'm looking to use Stateless sessions for asynchronous calls. If I add the dictionary and my other facts to the session, would I be able to retract the other facts and keep firing rules once new ones are added (keeping the dictionary in memory)? Also, posted a secondary question on SO related to globals: stackoverflow.com/questions/28201629/…GroomedGorilla
If you go stateless, then you're probably going to have to go with globals, as you would not be able to preserve the inserted data between invocations. If you go stateful, you could bind stateful sessions to each HTTP session or synchronise access to a shared stateful session (or a pool of them). If it takes the rules engine a long time to process the 'static' data, that last option can work quite effectively.Steve

1 Answers

2
votes

Not sure how large "large" is (of course there's always a performance tradeoff), but you could also use an inserted object to pull from a database (/cache) and have the rules access the values via method.

 when 
    $y : AnObject (name == "car", lookupPrice > 10000 );

where AnObject.getLookupPrice() is a method that would pull a value out of the cached / stored dictionary.

If the object isn't too big you could codify as well (as an object) and use it the same way.