Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drools: storing rules in database

Currently I store all rules files on the file system (there are lots of versions of them) and load the different versions of them into memory at startup. I would like to change to storing my drools files in a database and was wondering if there is any solution or addon to Drools which facilitates this or should I craft my own?

Thanks.

like image 607
ghosttrain Avatar asked Dec 08 '10 21:12

ghosttrain


People also ask

How are rules stored in drools?

Methods for authoring Drools RulesDecision tables stored in an Excel/CSV spreadsheet (xsl, xslx) following a Drools template for defining Constraints and Facts. Domain-Specific Language (DSL) rule files (rdslr) using a “natural” programming language that mimics the domain specific language.

How can you improve drools performance of rule execution?

Preparing data before calling insert() would give you better performance. Converting drl/xls is done by KnowledgeBuilder for the first time. KnowledgeBase will be cached in an application so it wouldn't be a bottle-neck. Drools 5 evaluates rule condition during the insert stage (ksession.

What is DRL file in drools?

DRL (Drools Rule Language) rules are business rules that you define directly in . drl text files. These DRL files are the source in which all other rule assets in Business Central are ultimately rendered.

What is working memory in drools?

Working Memory – a storage with Facts, where they are used for pattern matching and can be modified, inserted and removed. Rule – represents a single rule which associates Facts with matching actions. It can be written in Drools Rule Language in the . drl files or as Decision Table in an excel spreadsheet.


2 Answers

Yes, it can be done. All you need is the ability to get InputStream.In my case I use my own JPA class RulePackage to persist rule source as byte[], but you could use direct JDBC connection to access BLOB/CLOB fields in your DB schema. Important thing is to save also type of stored rule source, it will be needed when building rule packages:

switch(rulePackage.getRuleSourceType()) {
  case DRL:
    kbuilder.add( ResourceFactory.newByteArrayResource(rulePackage.getSource()), ResourceType.DRL);
    break;
  case EXCEL:
    kbuilder.add( ResourceFactory.newByteArrayResource(rulePackage.getSource()), ResourceType.DTABLE, excelConfig);
    break;
  case CSV:
    kbuilder.add( ResourceFactory.newByteArrayResource(rulePackage.getSource()), ResourceType.DTABLE, csvConfig);
    break;
  default:
    throw new Exception("Rule package '" + rulePackage.getName() + "' has unknown type");
}

You could consider using newInputStreamResource method if more applicable in your case:

   case DRL:
    kbuilder.add( ResourceFactory.newInputStreamResource(new StringInputStream(myDrlAsString)), ResourceType.DRL);
    break;

or

   case DRL:
    kbuilder.add( ResourceFactory.newInputStreamResource(new ByteArrayInputStream(myDrlAsByteArr)), ResourceType.DRL);
    break;

Something like that.

like image 158
andbi Avatar answered Sep 20 '22 12:09

andbi


Yes you can do it by creating rule packages(.pkg files). This is the compiled/binary form of textual rules.

you can specify the name of the package file while creating the knowledgeBuilder and also how frequently the rules should be updated (say 30 seconds).

Use Guvnor to convert the textual rules to .pkg files. keep this file in a folder where the application can access it.

Please revert if you need code samples.

like image 28
Nicky Jaidev Avatar answered Sep 20 '22 12:09

Nicky Jaidev