Version 11

    This describes a simple declarative language for the rule engine to use instead of semantics.

     

    It will be executed off an AST, predominantly via reflection. It does not aim to replace full semantics, but for most rule activities, the idea would be to use this language alone. No classes will need to be generated.

     

    There is some debate as to how complete this language should be. Should it do everything that CLIPS has for instance? In any case, semantics (java) can be used to provide turing complete-ness.

     

    Examples

    Lets start with some examples, the first 2 rules in manners:

     

    original

    rule assignFirstSeat
        when
            context : Context( state == Context.START_UP )
            guest : Guest()
            count : Count()
        then
            String guestName = guest.getName();
            Seating seating =  new Seating( count.getValue(), 1, true, 1, guestName, 1, guestName);
            assert( seating );
            Path path = new Path( count.getValue(), 1, guestName );
            assert( path );
            count.setValue(  count.getValue() + 1 );        
            modify( count );
         System.out.println( "assign first seat :  " + seating + " : " + path );
            context.setState( Context.ASSIGN_SEATS );        
            modify( context );
    end
    

    with SDL

    rule assignFirstSeat
        when
            context : Context( state == Context.START_UP )
            guest : Guest()
            count : Count()
        then
            guestName: guest.name #binds a variable
         assert( new Seating(count.value, 1, guestName, 1, guestName ) )
            assert ( new Path( count.value, 1, guestName) )
         count.value = count.value + 1 #use field access rules
         modify(count) #is this really necessary? we can infer from above
         println( "assign first seat :  " + seating + " : " + path)     
         context.state = Context.ASSIGN_SEATS
         modify (context)
    end
    

     

     

     

    So whats to notice?

    The most important thing is that this language will have a drools specific grammar, not be a semantic module. It will also be implemented by invoking functions (both rule defined, built in, and methods) reflectively from an AST (with ASM optimisations only if needed).

    Some points:

    • Declare variables just like on LHS, no type info needed (ie boundVar: object)

    • access fields using dot notation foo.bar (using getters, if available etc.. just like on LHS)

    • can set fields using "="

    • Everything is autoboxed

    • allow arithemtic operators on boxed types just like they were primitive

    • some built in functions: assert, modify, println, plus user can extend.

     

     

    What about while/control flow

    • Haven't thought about it as yet. Need some more examples, and why it should be done in SDL.

     

     

    Another manners example follows:

     

     

    next rule original

    rule findSeating
       when 
           context : Context( state == Context.ASSIGN_SEATS )
           Seating( seatingId:id, seatingPid:pid, pathDone == true, seatingRightSeat:rightSeat, seatingRightGuestName:rightGuestName )
           Guest( name == seatingRightGuestName, rightGuestSex:sex, rightGuestHobby:hobby )
           Guest( leftGuestName:name , sex != rightGuestSex, hobby == rightGuestHobby )
    
           count : Count()
    
           not ( Path( id == seatingId, guestName == leftGuestName) )
           not ( Chosen( id == seatingId, guestName == leftGuestName, hobby == rightGuestHobby) )
       then
           int rightSeat = seatingRightSeat.intValue();
           int seatId = seatingId.intValue();
           int countValue = count.getValue();
           
           Seating seating = new Seating( countValue, seatId, false, rightSeat, seatingRightGuestName, rightSeat + 1, leftGuestName );
           assert( seating );     
                                
           Path path = new Path( countValue, rightSeat + 1, leftGuestName  );
           assert( path );
           
           Chosen chosen = new Chosen( seatId, leftGuestName, rightGuestHobby );
           assert( chosen  );
    
            System.out.println( "find seating : " + seating + " : " + path + " : " + chosen);
    
           count.setValue(  countValue + 1 );
           modify( count );
    
           context.setState( Context.MAKE_PATH );
           modify( context );
    end
    

    And with SDL

    rule findSeating
       when 
           context : Context( state == Context.ASSIGN_SEATS )
           Seating( seatingId:id, seatingPid:pid, pathDone == true, seatingRightSeat:rightSeat, seatingRightGuestName:rightGuestName )
           Guest( name == seatingRightGuestName, rightGuestSex:sex, rightGuestHobby:hobby )
           Guest( leftGuestName:name , sex != rightGuestSex, hobby == rightGuestHobby )
    
           count : Count()
    
           not ( Path( id == seatingId, guestName == leftGuestName) )
           not ( Chosen( id == seatingId, guestName == leftGuestName, hobby == rightGuestHobby) )
       then
           
           Seating seating = new Seating( countValue, seatingId, false, seatingRightSeat, 
                              seatingRightGuestName, seatingRightSeat + 1, leftGuestName )
           assert( seating )     
                                
           Path path = new Path( countValue, seatingRightSeat + 1, leftGuestName  )
           assert( path )
           
           Chosen chosen = new Chosen( seatingId, leftGuestName, rightGuestHobby )
           assert( chosen  )
    
           println( "find seating : " + seating + " : " + path + " : " + chosen);
    
           count.value =  countValue + 1
           modify( count )
    
           context.state = Context.MAKE_PATH
           modify( context )
    end
    

     

    Referenced by: