depending on your problem, it may be best to have N seperate working memories, and just move between them rather then load/unload.

On 10/25/06, Tomasz Cichocki <[EMAIL PROTECTED]> wrote:

Hi,

I'm using jboss rules 3.0.4 and I have simillar problem. I have huge
collection of facts in the working memory and those facts should be
processed in stages. There are packages of rules for each stage. So I
thought of following procedure:
0. get working memory from the rule base
1. assign all the facts to the working memory
2. add package 1 to the rule base (should automatically fire rules)
3. remove package 1 from the rule base and add package 2
4. remove package 2 from the rule base and add package 3
...

The problem is that not all of the rules from package 1 that can be fired
are fired. The same for package 2.
Surprisingly, adding package 2 before package 1 was removed changed the
behaviour: rules from package 2
got fired.

I was thinking of using agenda-groups instead of packages, but I don't know
how to completly disable all agenda groups but one (e.g.
in stage 1. only rules from agenda-group GROUP1 should be fired, but there
are no activations in GROUP1 and there are activations
in GROUP2 then GROUP2 will become active).

Anyway, here is simple example of the code:
>>>>>>>>>>>>>> SimplifiedExample.java >>>>>>>>>>
package org.drools.examples;

import java.io.*;

import org.drools.RuleBase;
import org.drools.RuleBaseFactory;
import org.drools.WorkingMemory;
import org.drools.compiler.PackageBuilder ;
import org.drools.rule.Package;

/**
* This is a sample file to launch a rule package from a rule source file.
*/
public class SimplifiedExample {

    public static final void main(final String[] args) {
        try {

            //load up the rulebase
                final Package package1 = readRule("simple_package1.drl");
//              final Package package2 = readRule("test_package2.drl");

                final RuleBase ruleBase = RuleBaseFactory.newRuleBase();
            final WorkingMemory workingMemory = ruleBase.newWorkingMemory();

            //assert
            final Message message = new Message();
            message.setMessage( "Hello World" );
            message.setStatus( Message.HELLO );
            workingMemory.assertObject( message );

            ruleBase.addPackage(package1);
            //"Hello World" is printed here
            //following line doesn't help "Goodbye crules world!" isn't
printed
            workingMemory.fireAllRules();

        } catch ( final Throwable t ) {
            t.printStackTrace();
        }
    }

    /**
     * Please note that this is the "low level" rule assembly API.
     */
    private static Package readRule(String drlName) throws Exception {
        final Reader source = new InputStreamReader(
SimplifiedExample.class.getResourceAsStream(drlName) );
        final PackageBuilder builder = new PackageBuilder();
        builder.addPackageFromDrl( source );
        return builder.getPackage();
    }

    public static class Message {
        public static final int HELLO   = 0;
        public static final int GOODBYE = 1;

        private String          message;

        private int             status;

        public String getMessage() {
            return this.message;
        }

        public void setMessage(final String message) {
            this.message = message;
        }

        public int getStatus() {
            return this.status;
        }

        public void setStatus(final int status) {
            this.status = status;
        }
    }

}
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

>>>>>>>>>>>>>> simple_package1.drl >>>>>>>>>>>>>>>>>
package org.drools.examples

import org.drools.examples.SimplifiedExample.Message;

rule "Hello World1"
        when
                m : Message( status == Message.HELLO, message : message )
        then
                System.out.println( message );
                m.setMessage( "Goodbye cruel world" );
                m.setStatus( Message.GOODBYE );
                modify( m );
end

rule "GoodBye1"
        no-loop true
        when
                m : Message( status == Message.GOODBYE, message : message )
        then
                System.out.println( message );
                m.setMessage( message );
end
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<


Michael Neale-2 wrote:
>
> yes it is a valid thing to do in theory, but not normally *all* the rules,
> *all* the time ;)
>
> Usually its adding or removing a rule from time to time.
>
> Can you simplify your code a bit, start by just removing 1 or 2 rules, and
> adding them in etc... add complexity until it breaks.
>
> Michael.
>
> On 8/4/06, Corey M. Ellis < [EMAIL PROTECTED]> wrote:
>>
>> Geoffrey Wiseman <geoffrey.wiseman <at> gmail.com> writes:
>>
>> >
>> >
>> > Wouldn't it be simpler to load a new rule base and transfer the objects
>> from
>> your old working memory to a new one?  If there's a reason why you don't
>> want to
>> do this, I'd like to understand that reason.  :)
>> >
>> >
>> > On 8/2/06, Corey Ellis <corey.ellis <at> comverse.com > wrote:
>> > Hi,I am using 3.0.3.  I would like to reload my rules
>> dynamicallywithout
>> having to recreate the working memory.  I have writtenthe following
>> method,
>> which I thought would do the trick.I get no acceptions, but after
>> reloading the
>> new rule file.  My
>> > originial rules don't get hit, and my new rule doesn't get appliedas
>> well.  Any ideas?  I have not been able to write a test for this,this
>> code
>> is
>> from my production code.  I want to make sure that theapproach I am
>> taking
>> for
>> reloading the rules is correct.
>> > Code snippets:public void reloadRules() {    lock.lock();    try
>> {        InitialContext ctx = new InitialContext();        WorkingMemory
>> wm
>> =(WorkingMemory)ctx.lookup(Resource.EVENT_TABLE_WM.getResourceName
>> > ());        RuleBase rb = wm.getRuleBase();        /* get the packages
>> */        Package pkg[] = rb.getPackages();        for (int i=0; i<
>> pkg.length;
>> i++) {          /* remove the old packages from rulebase */
>> >           rb.removePackage(pkg[i].getName());        }        /* add
>> the
>> new
>> packages to rulebase */        if (!addRulePkgs(rb))
>> {          System.setProperty("correlation.enabled", "false");
>> >         }    } catch (NamingException e)
>> {        myLog.error(TraceUtil.captureStackTrace(e));    } finally
>> {        lock.unlock();    }}Note:  the addRulePkgs method calls this
>> method to
>> build the rule packages
>> > public static boolean buildRulePackages(RuleBase ruleBase, String
>> ruleFileDir,Logger myLog) {  boolean flag = false;  String fs =
>> SystemUtils.FILE_SEPARATOR;  try {    if (ruleBase == null) {
>> >       return flag;    }    // Create a rule package using
>> PackageBuilder    PackageBuilderConfiguration cfg =
>> new  PackageBuilderConfiguration();    PackageBuilder builder = new
>> PackageBuilder(null, cfg);
>> >     File ruleDir = new File(ruleFileDir);    FilenameFilter fileFilter
>> =
>> new
>> SuffixFileFilter(".drl");    String[] ruleFiles =
>> ruleDir.list(fileFilter);    if (ruleFiles != null && ruleFiles.length
>> >  > 0) {      for (int i=0; i< ruleFiles.length; i++) {      StringBuffer
>> ruleFile = new
>>
>> StringBuffer();ruleFile.append(ruleFileDir).append(fs).append(ruleFiles[i]);
>>   FileReader
>> rf = new FileReader(
>> > ruleFile.toString());      if (rf != null)
>> {        builder.addPackageFromDrl(rf);      }    }    // Get the
>> compiled
>> package (which is serializable)    Package pkg = builder.getPackage();
>> > ruleBase.addPackage(pkg);    /* packages successfully added */    flag
>> =
>> true;  }  ... removed the catch blocks
>> ...}---------------------------------------------------------------------
>> > To unsubscribe from this list please
>> visit:    http://xircles.codehaus.org/manage_email
>> >
>> >
>> > -- Geoffrey Wiseman
>> >
>> >
>> I don't really have a preference, but the functionality was exposed so I
>> just
>> went this approach.  For me either approach is ok, as long as gives the
>> wanted
>> results.
>>
>>
>>
>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe from this list please visit:
>>
>>     http://xircles.codehaus.org/manage_email
>>
>>
>
>
Quoted from:
http://www.nabble.com/Reloading-of-rule-files-dynamically-tf2042324.html#a5646409
--
View this message in context: http://www.nabble.com/Reloading-of-rule-files-dynamically-tf2042324.html#a6990429
Sent from the drools - user mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email


Reply via email to