Drools: storing rules in database

前端 未结 5 855
温柔的废话
温柔的废话 2021-01-31 11:51

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

相关标签:
5条回答
  • 2021-01-31 11:59

    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.

    0 讨论(0)
  • 2021-01-31 12:07

    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.

    0 讨论(0)
  • 2021-01-31 12:18

    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.

    0 讨论(0)
  • 2021-01-31 12:21

    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.

    0 讨论(0)
  • 2021-01-31 12:24

    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.

    0 讨论(0)
提交回复
热议问题