[rules-users] Problem when using multiple rule files (Drools 5)

Wolfgang Laun wolfgang.laun at gmail.com
Thu Jul 23 08:42:45 EDT 2009


Consider adapting to the new API using KnowledgeBuilder etc. You can use a
ObjectOutput/InputStream with write/readObject on
Collection<KnowledgePackage>, which simplifies matters somewhat.

See below, two remarks.


On 7/23/09, Olaf Raether <o.raether at epro.de> wrote:

>
> I create *.pkg file with the following app:
>
>
> --------------------------------------------------------------------------------------------------
> import java.io.File;
> import java.io.FileOutputStream;
> import java.io.InputStreamReader;
> import java.io.ObjectOutputStream;
> import java.net.URL;
>
> import org.drools.RuleBase;
> import org.drools.RuleBaseFactory;
> import org.drools.WorkingMemory;
> import org.drools.rule.Package;
> import org.drools.compiler.PackageBuilder;
>
> public class DroolsPackageWriter {
>
>        String[] fileNames = { "rulefile1", "rulefile2"};
>
>        public void readWritePackages() {
>
>                for(int i = 0; i < fileNames.length; i++) {
>                        try {
>
>                        RuleBase ruleBase = RuleBaseFactory.newRuleBase();
>                    PackageBuilder builder = new PackageBuilder();
>
>                    String ruleFile = fileNames[i] + ".drl";
>                    System.out.println("Lese:"+ ruleFile);
>
>                    builder.addPackageFromDrl(new
>
> InputStreamReader(DroolsPackageWriter.class.getResourceAsStream(ruleFile)));


Here, call builder.getErrors() or at least builder.hasErrors() and check the
result.



>                    Package pkg = builder.getPackage();
>                    ruleBase.addPackage(pkg);
>                    WorkingMemory workingMemory =
> ruleBase.newStatefulSession();
>                    workingMemory.fireAllRules();
>
>                    URL url =
> DroolsPackageWriter.class.getResource(ruleFile);
>                    String file = url.getPath().replaceAll("%20","
> ").replaceAll(".drl",
> ".pkg");
>                    File f = new File(file);
>
>                    pkg.writeExternal(new ObjectOutputStream(new
> FileOutputStream(f)));
>                    System.out.println("geschrieben:"+ file);
>                        }
>                        catch(Exception ex) {
>                                ex.printStackTrace();
>                        }
>                }
>        }
>
>        /**
>         * @param args
>         */
>        public static void main(String[] args) {
>                DroolsPackageWriter dpw = new DroolsPackageWriter();
>                dpw.readWritePackages();
>
>        }
>
> }
>
> --------------------------------------------------------------------------------------------------
>
>
> I the app where i read the *.pkg files the code looks like this:
>
> ---------------------------------------------------------------
> String[] rules = new String[]{"rule1.pkg","rule2.pkg" };



Above, we had rulefile1, rulefile2?

RuleBase ruleBase = RuleBaseFactory.newRuleBase();
> for (int i = 0; i < rules.length; i++) {
>        String ruleFile = rules[i];
>        log.debug("Loading file: " + ruleFile);
>        pkg = new Package();
>        pkg.readExternal(new
> ObjectInputStream(this.getClass().getResourceAsStream(ruleFile)));
>        ruleBase.addPackage(pkg);
> }
>
> WorkingMemory workingMemory = ruleBase.newStatefulSession();
> setGlobals(workingMemory);
>
> Object[] facts = { /* anything you want */};
> for (int i = 0; i < facts.length; i++) {
> Object fact = facts[i];
> log.debug("Inserting fact: " + fact);
> try {
>          workingMemory.insert(fact);
> } catch(NullPointerException npe) {
>          System.out.println("NPE bei Fact:"+fact);
>          npe.printStackTrace();
> }
> }
> workingMemory.fireAllRules();
> log.debug("END: runRules()");
> }
> ---------------------------------------------------------------
>
> Hope this helps - to help me !
>
>
>
>
>
> Wolfgang Laun-2 wrote:
> >
> > On 7/23/09, Olaf Raether <o.raether at epro.de> wrote:
> >>
> >> The code looks  like this:
> >>
> >> RuleBase ruleBase = RuleBaseFactory.newRuleBase();
> >> for (int i = 0; i < rules.length; i++) {
> >>       Package pkg = getPackage(i);
> >>       ruleBase.addPackage(pkg);
> >> }
> >
> >
> > I'd expect to find "Package pkg = rules[i];" or similar in the loop.
> > Anyway, this doesn't tell how the Package objects were created from your
> > two
> > rules files, which might be significant.
> >
> > Do you check for package build errors?
> >
> > -W
> >
> >
> > for (int i = 0; i < facts.length; i++) {
> >>        Object fact = facts[i];
> >>        workingMemory.insert(fact);   // <<<<< Exception, when multiple
> >> rule
> >> files
> >> }
> >>
> >>
> >
> > _______________________________________________
> > rules-users mailing list
> > rules-users at lists.jboss.org
> > https://lists.jboss.org/mailman/listinfo/rules-users
> >
> >
>
> --
> View this message in context:
> http://www.nabble.com/Problem-when-using-multiple-rule-files-%28Drools-5%29-tp24623347p24623763.html
> Sent from the drools - user mailing list archive at Nabble.com.
>
> _______________________________________________
> rules-users mailing list
> rules-users at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/rules-users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/rules-users/attachments/20090723/8a98595d/attachment.html 


More information about the rules-users mailing list