/** * @return The compiled package. The package may contain errors, which you * can report on by calling getErrors or printErrors. If you try to * add an invalid package (or rule) to a RuleBase, you will get a * runtime exception. * * Compiled packages are serializable. */ public Package getPackage() { PackageRegistry pkgRegistry = null; if (!this.pkgRegistryMap.isEmpty()) { pkgRegistry = (PackageRegistry) this.pkgRegistryMap.values().toArray()[ currentRulePackage ]; } Package pkg = null; if (pkgRegistry != null) { pkg = pkgRegistry.getPackage(); } if (hasErrors() && pkg != null) { pkg.setError( getErrors().toString() ); } return pkg; }
public Package[] getPackages() { Package[] pkgs = new Package[this.pkgRegistryMap.size()]; String errors = null; if (!getErrors().isEmpty()) { errors = getErrors().toString(); } int i = 0; for (PackageRegistry pkgRegistry : this.pkgRegistryMap.values()) { Package pkg = pkgRegistry.getPackage(); pkg.getDialectRuntimeRegistry().onBeforeExecute(); if (errors != null) { pkg.setError( errors ); } pkgs[i++] = pkg; } return pkgs; }
private RuleBase makeRuleBase(final PackageBuilder builder) throws CheckedDroolsException { if ( builder.hasErrors() ) { throw new CheckedDroolsException( "There were errors in the rule source: " + builder.getErrors().toString() ); } final Package binaryPackage = builder.getPackage(); final RuleBase rb = RuleBaseFactory.newRuleBase( RuleBaseLoader.engineType ); try { rb.addPackage( binaryPackage ); } catch ( final Exception e ) { throw new CheckedDroolsException( "Unable to add compiled package to rulebase. Nested error is: " + e.getMessage() ); } return rb; }
/** * Return a wm ready to go based on the rules in a drl at the specified uri (in the classpath). */ public StatefulSession getWorkingMemory(String uri) throws DroolsParserException, IOException, Exception { PackageBuilder builder = new PackageBuilder(); builder.addPackageFromDrl(new InputStreamReader(this.getClass() .getResourceAsStream(uri))); assertFalse(builder.getErrors().toString(), builder.hasErrors()); RuleBase rb = RuleBaseFactory.newRuleBase(); rb.addPackage(builder.getPackage()); SessionConfiguration conf = new SessionConfiguration(); conf.setClockType( ClockType.PSEUDO_CLOCK ); StatefulSession wm = rb.newStatefulSession( conf, null ); return wm; } }