// seating.getSeat1()</java:condition>
final Condition conditionC2 = new Condition( )
{
public boolean isAllowed(Tuple tuple)
{
LastSeat lastSeat = (LastSeat) tuple.get( lastSeatDeclC );
Seating seating = (Seating) tuple.get( seatingDeclC );
return lastSeat.getSeat( ) > seating.getSeat1( );
}
public Declaration[] getRequiredTupleMembers()
{
return new Declaration[]{lastSeatDeclC, seatingDeclC};
}
public String toString()
{
return "lastSeat.getSeat() > seating.getSeat1()";
}
};
tryAnotherPathRule.addCondition( conditionC2 );
// <java:condition>seating.getGuest2() == null</java:condition>
final Condition conditionC3 = new Condition( )
{
public boolean isAllowed(Tuple tuple)
{
Seating seating = (Seating) tuple.get( seatingDeclC );
return seating.getGuest2( ) == null;
}
public Declaration[] getRequiredTupleMembers()
{
return new Declaration[]{seatingDeclC};
}
public String toString()
{
return "seating.getGuest2() == null";
}
};
tryAnotherPathRule.addCondition( conditionC3 );
// <java:consequence>
// System.out.println("FIRE: try another path: " + seating);
//
// Seating prevSeat = seating.getPrevSeat();
// prevSeat.setGuest2(null);
// drools.modifyObject(prevSeat);
//
// drools.retractObject(seating);
// </java:consequence>
final Consequence consequenceC = new Consequence( )
{
public void invoke(Tuple tuple) throws ConsequenceException
{
WorkingMemory workingMemory = tuple.getWorkingMemory( );
Seating seating = (Seating) tuple.get( seatingDeclC );
Seating prevSeat = seating.getPrevSeat( );
prevSeat.setGuest2( null );
try
{
workingMemory.modifyObject( tuple.getFactHandleForObject( prevSeat ),
prevSeat );
}
catch ( FactException e )
{
throw new ConsequenceException( e );
}
try
{
workingMemory.retractObject( tuple.getFactHandleForObject( seating ) );
}
catch ( FactException e )
{
throw new ConsequenceException( e );
}
}
};
tryAnotherPathRule.setConsequence( consequenceC );
ruleSet.addRule( tryAnotherPathRule );
// =======================================
// <rule name="we are done" salience="10">
// =======================================
final Rule weAreDoneRule = new Rule( "we are done" );
weAreDoneRule.setSalience( 10 );
// Build the declaration and specify it as a parameter of the Rule
// <parameter identifier="context">
// <class>org.drools.examples.manners.model.Context</class>
// </parameter>
final Declaration contextDeclD = weAreDoneRule.addParameterDeclaration( "context",
contextType );
// <parameter identifier="lastSeat">
// <class>org.drools.examples.manners.model.LastSeat</class>
// </parameter>
final Declaration lastSeatDeclD = weAreDoneRule.addParameterDeclaration( "lastSeat",
lastSeatType );
// <parameter identifier="seating">
// <class>org.drools.examples.manners.model.Seating</class>
// </parameter>
final Declaration seatingDeclD = weAreDoneRule.addParameterDeclaration( "seating",
seatingType );
// <java:condition>context.isState("find_seating")</java:condition>
final Condition conditionD1 = new Condition( )
{
public boolean isAllowed(Tuple tuple)
{
Context context = (Context) tuple.get( contextDeclD );
return context.isState( "find_seating" );
}
public Declaration[] getRequiredTupleMembers()
{
return new Declaration[]{contextDeclD};
}
public String toString()
{
return "context.isState(\"find_seating\")";
}
};
weAreDoneRule.addCondition( conditionD1 );
// <java:condition>lastSeat.getSeat() ==
// seating.getSeat1()</java:condition>
final Condition conditionD2 = new Condition( )
{
public boolean isAllowed(Tuple tuple)
{
LastSeat lastSeat = (LastSeat) tuple.get( lastSeatDeclD );
Seating seating = (Seating) tuple.get( seatingDeclD );
return lastSeat.getSeat( ) == seating.getSeat1( );
}
public Declaration[] getRequiredTupleMembers()
{
return new Declaration[]{lastSeatDeclD, seatingDeclD};