Hi Dirk. When you are building up a package, if you re-used the package builder, you are adding a package each time, which means, it may merge the package (which is what you are doing if you are adding a rule at a time). Hence yes, you do need to add imports/packages each time, as it is a package you are adding.
Feel free to mess around with the guts of the package builder, I think it needs to be fine grained for the reasons you described (I need it to be as well). If you do anything, please submit as a patch as we could use it. We have removeRule() already, so perhaps addRule would be nice. On 12/6/06, Dirk Bergstrom <[EMAIL PROTECTED]> wrote:
I'm working on a system that will store rules in a database, and allow users to add new rules to the system. I want the following capabilities: *) Remove individual rules from the RuleBase. *) Add new rules on the fly. *) Test the validity (syntax, etc.) of rules created by users. I'm not clear on how to use PackageBuilder to accomplish this. I'd appreciate it if y'all could look over the code I've written (below), and let me know if I've got the right idea. The major questions are: *) Can I re-use a PackageBuilder for multiple rules, or do I need to create a new PB for every rule I want to add? *) The parseRule() method is intended to be used standalone, to test validity, and as part of the addRuleToRuleBase() method. Is this sensible? *) Do I need to include the "package ....;" and "import ....;" declarations in the text of each rule, or only once? Thanks. private void prepRuleBuilder() throws DashboardException { PackageBuilderConfiguration pkgBuilderCfg = new PackageBuilderConfiguration(); pkgBuilderCfg.setCompiler(PackageBuilderConfiguration.JANINO); pkgBuilderCfg.setJavaLanguageLevel("1.5"); rulePackageBuilder = new PackageBuilder(pkgBuilderCfg); /* Read in the DSL now, so we don't have to worry about IOExceptions * later.*/ try { dslReader = new StringReader(Repository.readFile (DSL_FILE).toString()); } catch (FileNotFoundException e) { throw new DashboardException("dsl file '" + DSL_FILE + "' not found."); } catch (IOException e) { throw new DashboardException("IOException reading dsl file '" + DSL_FILE +"': " + e.getMessage()); } } /** * Add a new Rule to the RuleBase. * * @param rule * @throws DashboardException */ public void addRuleToRuleBase(Rule rule) throws DashboardException { if (null == rulePackageBuilder) { prepRuleBuilder(); } try { parseRule(rule); Package pkg = rulePackageBuilder.getPackage(); ruleBase.addPackage(pkg); } catch (DashboardException e) { // Otherwise it gets caught below. throw e; } catch (Exception e) { /* Grumble. RuleBase.addPackage() declares "Exception", * which is waaaay to broad. */ throw new DashboardException("Exception adding pkg to ruleBase " + "for rule'" + rule.getName() + "': " + e.getMessage()); } } /** * Parse a Rule. * * @param rule * @throws DashboardException If the Rule is invalid. */ public void parseRule(Rule rule) throws DashboardException { if (null == rulePackageBuilder) { prepRuleBuilder(); } try { rulePackageBuilder.addPackageFromDrl( new StringReader(rule.getFullRuleText()), dslReader); if (rulePackageBuilder.getErrors().length > 0) { // Bad rule, no donut! throw new DashboardException("Errors parsing rule '" + rule.getName() + "': " + rulePackageBuilder.getErrors ()); } } catch (DroolsParserException e) { throw new DashboardException("Exception parsing rule '" + rule.getName() + "': " + e.getMessage()); } catch (IOException e) { // Shouldn't get here since we're using StringReader for "IO". throw new DashboardException("Surprising IOException during " + "processing of rule '" + rule.getName() + "': " + e.getMessage()); } } -- Dirk Bergstrom [EMAIL PROTECTED] _____________________________________________ Juniper Networks Inc., Computer Geek Tel: 408.745.3182 Fax: 408.745.8905 --------------------------------------------------------------------- To unsubscribe from this list please visit: http://xircles.codehaus.org/manage_email