Drools: storing rules in database

怎甘沉沦 提交于 2019-12-02 17:17:50

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.

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.

Well, you might want to look at Guvnor as a way of providing rule management.

But in short, no, you'll have to write your own.

Keep in mind you don't need a file in between, you can read the string representation of the rule out of the db and just compile that.

I also faced same scenario. I stored all Rule files in database.

When loading rules at application startup, I got the rules from database and stored them in temporary folder for compiling and putting them in Rule Base.

If your rule changes during running application, update the changed rule in Rule Base with the same way.

Once you get into the business of storing multiple version of rules in a data store you are really looking for the Business Rule Management (BRMS) features of Guvnor (in Drools 5.x) or the Drools-WB/KIE-WB workbenches in Drools 6.0.

Those solutions have solved for versioning, storage, and deployment of multiple rule versions out of the box. In short, they will be more robust than a homegrown solution.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!