[rules-dev] AlphaNodeCompiler

Mark Proctor mproctor at codehaus.org
Sun Jan 13 17:40:50 EST 2008


Currently each literal constraint is a node, so something like:
Guest( name ==  "mark", sex != Sex.M, hobby ==  Hobby.H1)

Has three chained alpha nodes. To evaluate a pattern we have to 
propagate to each alpha node in turn.

I can now generate a compiled alpha network per object type 
JBRULES-1418, currently  java as a string, for prototyping purposes. We 
can change the java code to generate a class with  ASM bytecode  - so 
that's one class per one  ObjectType. This means each ObjectType has all 
it's literal constraints, for all rules, evaluated in a single method; 
that method will propagate full matched facts to the alpha network via 
sink variable references .So the main thing is it shows  the algorithm 
works, including node sharing and hashing via the case statement. 
currently it just empbeds each alpahconstraint with an isAllowed 
constraint, we could probably  look at inlining some of that logic too 
to have even less indirection.

Anyone want to finish this of for me? :)
i.e. generating ASM, writting  unit tests, and wiring it up to the Rete 
builder.

package org.drools.benchmark.manners

rule rule1
    when
        context : Context( state == Context.START_UP )
        guest : Guest( name ==  "mark", sex != Sex.M, hobby ==  Hobby.H1)
        count : Count()
    then
end

rule rule2
   when
        context : Context( state == Context.START_UP )
        guest : Guest( name ==  "mark", sex != Sex.M, hobby == Hobby.H2)
        count : Count()
   then
end

rule rule3
   when
        context : Context( state == Context.START_UP )
        guest : Guest( name ==  "luke", sex != Sex.M, hobby ==  Hobby.H3)
        count : Count()
   then
end

rule rule4
   when
        context : Context( state == Context.START_UP )
        guest : Guest( name ==  "mark", sex != Sex.M, hobby ==  Hobby.H4)
        count : Count()
   then
end

The following is produced for above (obviously doesn't compile,  but 
shows  the algo works):
public class Compiled[ClassObjectType 
class=org.drools.benchmark.manners.Count]AlphaNetwork implements 
RightTupleSink {
    RightTupleSink sink10;
    RightTupleSink sink17;
    RightTupleSink sink26;
    RightTupleSink sink33;
    public void assertObject(....) {
        sink10.assertRight(handle, wm, context);
        sink17.assertRight(handle, wm, context);
        sink26.assertRight(handle, wm, context);
        sink33.assertRight(handle, wm, context);
    }
}

public class Compiled[ClassObjectType 
class=org.drools.benchmark.manners.Context]AlphaNetwork implements 
RightTupleSink {
    AlphaNodeFieldConstraint alphaNodeConstraint2;
    RightTupleSink sink3;
    public void assertObject(....) {
        if ( alphaNodeConstraint2.isAllowed(handle, wm) ) {
            sink3.assertRight(handle, wm, context);
        }
    }
}

public class Compiled[ClassObjectType 
class=org.drools.benchmark.manners.Guest]AlphaNetwork implements 
RightTupleSink {
    AlphaNodeFieldConstraint alphaNodeConstraint5;
    AlphaNodeFieldConstraint alphaNodeConstraint6;
    AlphaNodeFieldConstraint alphaNodeConstraint30;
    RightTupleSink sink31;
    AlphaNodeFieldConstraint alphaNodeConstraint7;
    RightTupleSink sink8;
    AlphaNodeFieldConstraint alphaNodeConstraint14;
    RightTupleSink sink15;
    AlphaNodeFieldConstraint alphaNodeConstraint21;
    AlphaNodeFieldConstraint alphaNodeConstraint22;
    AlphaNodeFieldConstraint alphaNodeConstraint23;
    RightTupleSink sink24;
    public void assertObject(....) {
        if ( alphaNodeConstraint5.isAllowed(handle, wm) ) {
            if ( alphaNodeConstraint6.isAllowed(handle, wm) ) {
                HashKey key = new HashKey(handle);
                swtich ((RightTupleSink)this.hashedSinkedMap(key)).getId() {
                    case 7: {
                        sink8.assertRight(handle, wm, context);
                    };
                    case 14: {
                        sink15.assertRight(handle, wm, context);
                    };
                    case 30: {
                        sink31.assertRight(handle, wm, context);
                    };
                };
            }
        }
        if ( alphaNodeConstraint21.isAllowed(handle, wm) ) {
            if ( alphaNodeConstraint22.isAllowed(handle, wm) ) {
                if ( alphaNodeConstraint23.isAllowed(handle, wm) ) {
                    sink24.assertRight(handle, wm, context);
                }
            }
        }
    }
}






More information about the rules-dev mailing list