I've just coded up in the Drools 3.0 rule language. Bindings are specified locally and application data is specified as arguments to the rule. This example shows a converted Miss Manners, so application data is not used:

public class Guest {
 private String  name;
 private char sex;
 private String hobby;
 ...
}

public class LastSeat {
   private int seat;
   ...
}

public class Seating {
   private int id;
   private int pid;
   private int leftSeat;
   private int rightSeat;
   private String leftName;
   private String rightName;
   private boolean pathDone;
   ...
}

public class Path {
   private int id;
   private String name;
   private int seat;
   ...
}

public class Chosen {
   private int id;
   private String name;
   private String hobby;
   ...
}

public class Count {
   private int value;
  ...
}

rule assignFirstSeat() {
   Context context;
   when {
       context : Context( state == Context.START_UP )
       Guest( guestName:name )
       count : Count(countValue:value)
   } then {
drools.assert( new Seating( 1, guestName, 2, guestName, countValue, 0, true ) );
       drools.assert( new Path( countValue, guestName, 1 ) );
       count.setCount(  countValue + 1 );

       ystem.out.println( "seat 1 " + guestName + " );

       context.setPath( Context.ASSIGN_SEATS );
   }
}

rule makePath() {
   Context context;
   int id, pid, leftSeat, rightSeat;
   String pathName;

   when {
       context : Context( state == Context.MAKE_PATH )
       Seat( id:id, pid:pid )
       Path( id == pid, pathName:name, pathSeat:seat )
       (not Path( id == id, name == pathName )
   } else {
       drools.assert( new Path( id, pathName, pathSeat ) );

   }
}

rule findSeating() {
   Context context;
   int id, pid, leftSeat, rightSeat;
   String leftSeatName, rightSeatName, leftGuestName, rightGuestHobby;

   when {
       context : Context( state == Context.START )
       Seating( id:id, pid:pid, leftSeat:leftSeat, leftSeatName:leftName,
rightSeat:rightSeat, rightSeatName:rightName, pathDone == true ) Guest( name == rightSeatName, rightGuestSex:sex, rightGuestHobby:hobby ) Guest( leftGuestName:name , sex != rightGuestSex, hobby == rightGuestHobby )

       count : Count(countValue:value)

       not ( Path( id == id, name == leftGuestName) )
not ( Chosen( id == id, name == leftGuestName, hobby == rightGuestHobby) )
   } then {
       int newSeat = rightSeat + 1;
drools.assert( new Seating( rightSeat, rightSeatName, leftGuestName, newSeat, countValue, id, false );
       drools.assert( new Path( countValue, leftGuestName, newSeat );
       drools.assert( new Chosen( id, leftGuestName, rightGuestHobby ) );

System.out.println( "seat " + rightSeat + " " + rightSeatName + " " + leftGuestName );

       count.setCount(  countValue + 1 );
       context.setPath( Context.MAKE_PATH );
   }
}

rule pathDone() {
   Context context;
   Seat seat;
   when {
       context : Context( state == Context.MAKE_PATH )
       seating : Seating( pathDone == false )
   } then {
       seating.setPathDone( true );
       context.setName( Context.CHECK_DONE );
   }
}

rule areWeDoneYet() {
   Context context;
   when {
       context : Context( state == Context.CHECK_DONE )
       LastSeat( lastSeat: seat )
       Seating( seat == lastSeat )
   } then {
       context.setName( Context.PRINT_RESULTS );
   }
}

rule continueProcessing {
   Context context;
   when {
       context : Context( state == Context.CHECK_DONE )
   } then {
       context.setName( Context.ASSIGN_SEATS);
   }
}

rule printResults() {
   Context context;
   when {
       context : Context( state == Context.PRINT_RESULTS )
       Seating( id: id, rightSeat:rightSeat )
       LastSeat( seat == rightSeat )
       path:Path( id == id, pathName:name, pathSeat:seat)
   } then {
System.out.println( "name : '" + pathName + "' seat : '" + pathSeat + "'" );
   }
}

rule allDone {
   Context context;
   when {
       context : Context( state == Context.PRINT_RESULTS )
   } then {
System.out.println( "rule all done: Condition context print_results" );
   }
}

Reply via email to