Package org.drools.spi

Examples of org.drools.spi.ConsequenceException


   }

   public void handleException(Activation activation,
                               WorkingMemory workingMemory,
                               Exception exception) {
       throw new ConsequenceException( exception,
                                       activation.getRule() );
   }
View Full Code Here


                                         getRule( ).getParameterDeclarations( ).iterator( ) ),
                        getGlobals( ) );
        }
        catch ( Exception e )
        {
            throw new ConsequenceException( e,
                                            getRule( ) );
        }
    }
View Full Code Here

        try {
            for (int i = 0; i < ruleMethods.length; i++) {
                ruleMethods[i].invokeMethod(tuple);
            }
        } catch (Exception e) {
            throw new ConsequenceException( e );
        }
    }
View Full Code Here

                    workingMemory.modifyObject( tuple.getFactHandleForObject( f ),
                                                f );
                }
                catch ( FactException e )
                {
                    throw new ConsequenceException( e );
                }
            }
        };
        bootstrap1Rule.setConsequence( bootstrapConsequence );
        ruleSet.addRule( bootstrap1Rule );

        // <rule name="Bootstrap 2">
        final Rule bootstrap2Rule = new Rule( "Bootstrap 2" );

        // Specify the declaration as a parameter of the Bootstrap2 Rule
        // <parameter identifier="f">
        // <class>org.drools.examples.fibonacci.Fibonacci</class>
        // </parameter>
        final Declaration fDeclaration2 = bootstrap2Rule.addParameterDeclaration( "f",
                                                                                  fibonacciType );

        // Build and Add the Conditions to the Bootstrap1 Rule
        // <java:condition>f.getSequence() == 2</java:condition>
        final Condition conditionBootstrap2A = new Condition( )
        {
            public boolean isAllowed(Tuple tuple)
            {
                Fibonacci f = (Fibonacci) tuple.get( fDeclaration2 );
                return f.getSequence( ) == 2;
            }

            public Declaration[] getRequiredTupleMembers()
            {
                return new Declaration[]{fDeclaration2};
            }

            public String toString()
            {
                return "f.getSequence() == 2";
            }
        };
        bootstrap2Rule.addCondition( conditionBootstrap2A );

        // <java:condition>f.getValue() == -1</java:condition>
        final Condition conditionBootstrap2B = new Condition( )
        {
            public boolean isAllowed(Tuple tuple)
            {
                Fibonacci f = (Fibonacci) tuple.get( fDeclaration2 );
                return f.getValue( ) == -1;
            }

            public Declaration[] getRequiredTupleMembers()
            {
                return new Declaration[]{fDeclaration2};
            }

            public String toString()
            {
                return "f.getValue() == -1";
            }
        };
        bootstrap2Rule.addCondition( conditionBootstrap2B );

        // Build and Add the Consequence to the Bootstrap2 Rule
        // <java:consequence>
        // f.setValue( 1 );
        // System.err.println( f.getSequence() + " == " + f.getValue() );
        // drools.modifyObject( f );
        // </java:consequence>
        final Consequence bootstrapConsequence2 = new Consequence( )
        {
            public void invoke(Tuple tuple) throws ConsequenceException
            {
                WorkingMemory workingMemory = tuple.getWorkingMemory( );

                Fibonacci f = (Fibonacci) tuple.get( fDeclaration2 );
                f.setValue( 1 );
                System.err.println( f.getSequence( ) + " == " + f.getValue( ) );

                try
                {
                    workingMemory.modifyObject( tuple.getFactHandleForObject( f ),
                                                f );
                }
                catch ( FactException e )
                {
                    throw new ConsequenceException( e );
                }
            }
        };
        bootstrap2Rule.setConsequence( bootstrapConsequence2 );
        ruleSet.addRule( bootstrap2Rule );

        // <rule name="Recurse" salience="10">
        final Rule recurseRule = new Rule( "Recurse" );
        recurseRule.setSalience( 10 );

        // <parameter identifier="f">
        // <class>org.drools.examples.fibonacci.Fibonacci</class>
        // </parameter>
        final Declaration fDeclarationRecurse = recurseRule.addParameterDeclaration( "f",
                                                                                     fibonacciType );

        // <java:condition>f.getValue() == -1</java:condition>
        final Condition conditionRecurse = new Condition( )
        {
            public boolean isAllowed(Tuple tuple)
            {
                Fibonacci f = (Fibonacci) tuple.get( fDeclarationRecurse );
                return f.getValue( ) == -1;
            }

            public Declaration[] getRequiredTupleMembers()
            {
                return new Declaration[]{fDeclarationRecurse};
            }

            public String toString()
            {
                return "f.getValue() == -1";
            }
        };
        recurseRule.addCondition( conditionRecurse );

        // <java:consequence>
        // System.err.println( "recurse for " + f.getSequence() );
        // drools.assertObject( new Fibonacci( f.getSequence() - 1 ) );
        // </java:consequence>
        final Consequence recurseConsequence = new Consequence( )
        {
            public void invoke(Tuple tuple) throws ConsequenceException
            {
                WorkingMemory workingMemory = tuple.getWorkingMemory( );

                Fibonacci f = (Fibonacci) tuple.get( fDeclarationRecurse );
                System.err.println( "recurse for " + f.getSequence( ) );
                try
                {
                    workingMemory.assertObject( new Fibonacci( f.getSequence( ) - 1 ) );
                }
                catch ( FactException e )
                {
                    throw new ConsequenceException( e );
                }
            }
        };
        recurseRule.setConsequence( recurseConsequence );
        ruleSet.addRule( recurseRule );

        // <rule name="Calculate">
        final Rule calculateRule = new Rule( "Calculate" );

        // <parameter identifier="f1">
        // <class>org.drools.examples.fibonacci.Fibonacci</class>
        // </parameter>
        final Declaration f1Declaration = calculateRule.addParameterDeclaration( "f1",
                                                                                 fibonacciType );

        // <parameter identifier="f2">
        // <class>org.drools.examples.fibonacci.Fibonacci</class>
        // </parameter>
        final Declaration f2Declaration = calculateRule.addParameterDeclaration( "f2",
                                                                                 fibonacciType );

        // <parameter identifier="f3">
        // <class>org.drools.examples.fibonacci.Fibonacci</class>
        // </parameter>
        final Declaration f3Declaration = calculateRule.addParameterDeclaration( "f3",
                                                                                 fibonacciType );

        // <java:condition>f2.getSequence() ==
        // (f1.getSequence()+1)</java:condition>
        final Condition conditionCalculateA = new Condition( )
        {
            public boolean isAllowed(Tuple tuple)
            {
                Fibonacci f1 = (Fibonacci) tuple.get( f1Declaration );
                Fibonacci f2 = (Fibonacci) tuple.get( f2Declaration );
                return f2.getSequence( ) == f1.getSequence( ) + 1;
            }

            public Declaration[] getRequiredTupleMembers()
            {
                return new Declaration[]{f1Declaration, f2Declaration};
            }

            public String toString()
            {
                return "f2.getSequence() == (f1.getSequence()+1)";
            }
        };
        calculateRule.addCondition( conditionCalculateA );

        // <java:condition>f3.getSequence() ==
        // (f2.getSequence()+1)</java:condition>
        final Condition conditionCalculateB = new Condition( )
        {
            public boolean isAllowed(Tuple tuple)
            {
                Fibonacci f2 = (Fibonacci) tuple.get( f2Declaration );
                Fibonacci f3 = (Fibonacci) tuple.get( f3Declaration );
                return f3.getSequence( ) == f2.getSequence( ) + 1;
            }

            public Declaration[] getRequiredTupleMembers()
            {
                return new Declaration[]{f2Declaration, f3Declaration};
            }

            public String toString()
            {
                return "f3.getSequence() == (f2.getSequence()+1)";
            }
        };
        calculateRule.addCondition( conditionCalculateB );

        // <java:condition>f1.getValue() != -1</java:condition>
        final Condition conditionCalculateC = new Condition( )
        {
            public boolean isAllowed(Tuple tuple)
            {
                Fibonacci f1 = (Fibonacci) tuple.get( f1Declaration );
                return f1.getValue( ) != -1;
            }

            public Declaration[] getRequiredTupleMembers()
            {
                return new Declaration[]{f1Declaration};
            }

            public String toString()
            {
                return "f1.getValue() != -1";
            }
        };
        calculateRule.addCondition( conditionCalculateC );

        // <java:condition>f2.getValue() != -1</java:condition>
        final Condition conditionCalculateD = new Condition( )
        {
            public boolean isAllowed(Tuple tuple)
            {
                Fibonacci f2 = (Fibonacci) tuple.get( f2Declaration );
                return f2.getValue( ) != -1;
            }

            public Declaration[] getRequiredTupleMembers()
            {
                return new Declaration[]{f2Declaration};
            }

            public String toString()
            {
                return "f2.getValue() != -1";
            }
        };
        calculateRule.addCondition( conditionCalculateD );

        // <java:condition>f3.getValue() == -1</java:condition>
        final Condition conditionCalculateE = new Condition( )
        {
            public boolean isAllowed(Tuple tuple)
            {
                Fibonacci f3 = (Fibonacci) tuple.get( f3Declaration );
                return f3.getValue( ) == -1;
            }

            public Declaration[] getRequiredTupleMembers()
            {
                return new Declaration[]{f3Declaration};
            }

            public String toString()
            {
                return "f3.getValue() == -1";
            }
        };
        calculateRule.addCondition( conditionCalculateE );

        // <java:consequence>
        // f3.setValue( f1.getValue() + f2.getValue() );
        // System.err.println( f3.getSequence() + " == " + f3.getValue() );
        // drools.modifyObject( f3 );
        // drools.retractObject( f1 );
        // </java:consequence>
        final Consequence calculateConsequence = new Consequence( )
        {
            public void invoke(Tuple tuple) throws ConsequenceException
            {
                WorkingMemory workingMemory = tuple.getWorkingMemory( );

                Fibonacci f1 = (Fibonacci) tuple.get( f1Declaration );
                Fibonacci f2 = (Fibonacci) tuple.get( f2Declaration );
                Fibonacci f3 = (Fibonacci) tuple.get( f3Declaration );

                f3.setValue( f1.getValue( ) + f2.getValue( ) );
                System.err.println( f3.getSequence( ) + " == " + f3.getValue( ) );
                try
                {
                    workingMemory.modifyObject( tuple.getFactHandleForObject( f3 ),
                                                f3 );
                    workingMemory.retractObject( tuple.getFactHandleForObject( f1 ) );
                }
                catch ( FactException e )
                {
                    throw new ConsequenceException( e );
                }
            }
        };
        calculateRule.setConsequence( calculateConsequence );
        ruleSet.addRule( calculateRule );
View Full Code Here

                                                                        tuple ),
                                            tuple.getWorkingMemory().getApplicationDataMap() );
        }
        catch ( Exception e )
        {
            throw new ConsequenceException( e,
                                            this.rule,
                                            this.block );
        }
    }
View Full Code Here

                                                             guest,
                                                             null ) );
                }
                catch ( FactException e )
                {
                    throw new ConsequenceException( e );
                }

                context.setState( "find_seating" );

                try
                {
                    workingMemory.modifyObject( tuple.getFactHandleForObject( context ),
                                                context );
                }
                catch ( FactException e )
                {
                    throw new ConsequenceException( e );
                }
            }
        };
        findFirstSeatRule.setConsequence( consequenceA );
        ruleSet.addRule( findFirstSeatRule );

        // ========================================
        // <rule name="find seating" salience="30">
        // ========================================
        final Rule findSeatingRule = new Rule( "find seating" );
        findSeatingRule.setSalience( 30 );

        // 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 contextDeclB = findSeatingRule.addParameterDeclaration( "context",
                                                                                  contextType );

        // <parameter identifier="guest">
        // <class>org.drools.examples.manners.model.Guest</class>
        // </parameter>
        final Declaration guestDeclB = findSeatingRule.addParameterDeclaration( "guest",
                                                                                guestType );

        // <parameter identifier="seating">
        // <class>org.drools.examples.manners.model.Seating</class>
        // </parameter>
        final Declaration seatingDeclB = findSeatingRule.addParameterDeclaration( "seating",
                                                                                  seatingType );

        // Build and Add the Condition to the Rule
        // <java:condition>context.isState("find_seating")</java:condition>
        final Condition conditionB1 = new Condition( )
        {
            public boolean isAllowed(Tuple tuple)
            {
                Context context = (Context) tuple.get( contextDeclB );
                return context.isState( "find_seating" );
            }

            public Declaration[] getRequiredTupleMembers()
            {
                return new Declaration[]{contextDeclB};
            }

            public String toString()
            {
                return "context.isState(\"find_seating\")";
            }
        };
        findSeatingRule.addCondition( conditionB1 );

        // <java:condition>seating.getGuest2() == null</java:condition>
        final Condition conditionB2 = new Condition( )
        {
            public boolean isAllowed(Tuple tuple)
            {
                Seating seating = (Seating) tuple.get( seatingDeclB );
                return seating.getGuest2( ) == null;
            }

            public Declaration[] getRequiredTupleMembers()
            {
                return new Declaration[]{seatingDeclB};
            }

            public String toString()
            {
                return "seating.getGuest2() == null";
            }
        };
        findSeatingRule.addCondition( conditionB2 );

        // <java:condition>!seating.getTabooList().contains(guest)</java:condition>
        final Condition conditionB3 = new Condition( )
        {
            public boolean isAllowed(Tuple tuple)
            {
                Seating seating = (Seating) tuple.get( seatingDeclB );
                Guest guest = (Guest) tuple.get( guestDeclB );
                return !seating.getTabooList( ).contains( guest );
            }

            public Declaration[] getRequiredTupleMembers()
            {
                return new Declaration[]{seatingDeclB, guestDeclB};
            }

            public String toString()
            {
                return "!seating.getTabooList().contains(guest)";
            }
        };
        findSeatingRule.addCondition( conditionB3 );

        // <java:condition>seating.getGuest1().hasOppositeSex(guest)</java:condition>
        final Condition conditionB4 = new Condition( )
        {
            public boolean isAllowed(Tuple tuple)
            {
                Seating seating = (Seating) tuple.get( seatingDeclB );
                Guest guest = (Guest) tuple.get( guestDeclB );
                return seating.getGuest1( ).hasOppositeSex( guest );
            }

            public Declaration[] getRequiredTupleMembers()
            {
                return new Declaration[]{seatingDeclB, guestDeclB};
            }

            public String toString()
            {
                return "seating.getGuest1().hasOppositeSex(guest)";
            }
        };
        findSeatingRule.addCondition( conditionB4 );

        // <java:condition>seating.getGuest1().hasSameHobby(guest)</java:condition>
        final Condition conditionB5 = new Condition( )
        {
            public boolean isAllowed(Tuple tuple)
            {
                Seating seating = (Seating) tuple.get( seatingDeclB );
                Guest guest = (Guest) tuple.get( guestDeclB );
                return seating.getGuest1( ).hasSameHobby( guest );
            }

            public Declaration[] getRequiredTupleMembers()
            {
                return new Declaration[]{seatingDeclB, guestDeclB};
            }

            public String toString()
            {
                return "seating.getGuest1().hasSameHobby(guest)";
            }
        };
        findSeatingRule.addCondition( conditionB5 );

        // Build and Add the Consequence to the Rule
        // <java:consequence>
        // System.out.println("FIRE: find seating: " + seating + " " + guest);
        //
        // Seating nextSeat = new Seating(seating.getSeat2(), guest, seating);
        // drools.assertObject(nextSeat);
        //
        // seating.setGuest2(guest);
        // seating.getTabooList().add(guest);
        // drools.modifyObject(seating);
        // </java:consequence>
        final Consequence consequenceB = new Consequence( )
        {
            public void invoke(Tuple tuple) throws ConsequenceException
            {
                Guest guest = (Guest) tuple.get( guestDeclB );
                Seating seating = (Seating) tuple.get( seatingDeclB );

                System.out.println( "FIRE: find seating: " + seating + " " + guest );

                Seating nextSeat = new Seating( seating.getSeat2( ),
                                                guest,
                                                seating );
                try
                {
                    workingMemory.assertObject( nextSeat );
                }
                catch ( FactException e )
                {
                    throw new ConsequenceException( e );
                }

                seating.setGuest2( guest );
                seating.getTabooList( ).add( guest );

                try
                {
                    workingMemory.modifyObject( tuple.getFactHandleForObject( seating ),
                                                seating );
                }
                catch ( FactException e )
                {
                    throw new ConsequenceException( e );
                }
            }
        };
        findSeatingRule.setConsequence( consequenceB );
        ruleSet.addRule( findSeatingRule );

        // ===========================================
        // <rule name="try another path" salience="20">
        // ===========================================
        final Rule tryAnotherPathRule = new Rule( "try another path" );
        tryAnotherPathRule.setSalience( 20 );

        // 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 contextDeclC = tryAnotherPathRule.addParameterDeclaration( "context",
                                                                                     contextType );

        // <parameter identifier="lastSeat">
        // <class>org.drools.examples.manners.model.LastSeat</class>
        // </parameter>
        final Declaration lastSeatDeclC = tryAnotherPathRule.addParameterDeclaration( "lastSeat",
                                                                                      lastSeatType );

        // <parameter identifier="seating">
        // <class>org.drools.examples.manners.model.Seating</class>
        // </parameter>
        final Declaration seatingDeclC = tryAnotherPathRule.addParameterDeclaration( "seating",
                                                                                     seatingType );

        // <java:condition>context.isState("find_seating")</java:condition>
        final Condition conditionC1 = new Condition( )
        {
            public boolean isAllowed(Tuple tuple)
            {
                Context context = (Context) tuple.get( contextDeclC );
                return context.isState( "find_seating" );
            }

            public Declaration[] getRequiredTupleMembers()
            {
                return new Declaration[]{contextDeclC};
            }

            public String toString()
            {
                return "context.isState(\"find_seating\")";
            }
        };
        tryAnotherPathRule.addCondition( conditionC1 );

        // <java:condition>lastSeat.getSeat() >
        // 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
            {
                Seating seating = (Seating) tuple.get( seatingDeclC );

                System.out.println( "FIRE: try another path: " + seating );

                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};
            }

            public String toString()
            {
                return "lastSeat.getSeat() == seating.getSeat1()";
            }
        };
        weAreDoneRule.addCondition( conditionD2 );

        // <java:consequence>
        // System.out.println("FIRE: we are done");
        //
        // import org.drools.examples.manners.model.Seat;
        // List list = new ArrayList();
        // while(seating != null) {
        // Seat seat = new Seat(seating.getSeat1(),
        // seating.getGuest1().getName());
        // seating = seating.getPrevSeat();
        // list.add(seat);
        // }
        //
        // for (int i = list.size(); i > 0; i--) {
        // Seat seat = (Seat)list.get(i-1);
        // System.out.println(seat);
        // drools.assertObject(seat);
        // }
        //
        // context.setState("all_done");
        // drools.modifyObject(context);
        // </java:consequence>
        final Consequence consequenceD = new Consequence( )
        {
            public void invoke(Tuple tuple) throws ConsequenceException
            {
                System.out.println( "FIRE: we are done" );

                Seating seating = (Seating) tuple.get( seatingDeclD );
                Context context = (Context) tuple.get( contextDeclD );

                List list = new ArrayList( );
                while ( seating != null )
                {
                    Seat seat = new Seat( seating.getSeat1( ),
                                          seating.getGuest1( ).getName( ) );
                    seating = seating.getPrevSeat( );
                    list.add( seat );
                }

                for ( int i = list.size( ); i > 0; i-- )
                {
                    Seat seat = (Seat) list.get( i - 1 );
                    System.out.println( seat );
                    try
                    {
                        workingMemory.assertObject( seat );
                    }
                    catch ( FactException e )
                    {
                        throw new ConsequenceException( e );
                    }
                }

                context.setState( "all_done" );
                try
                {
                    workingMemory.modifyObject( tuple.getFactHandleForObject( context ),
                                                context );
                }
                catch ( FactException e )
                {
                    throw new ConsequenceException( e );
                }
            }
        };
        weAreDoneRule.setConsequence( consequenceD );
        ruleSet.addRule( weAreDoneRule );
View Full Code Here

        // add consequence
        rule.setConsequence( new org.drools.spi.Consequence( )
        {
            public void invoke(org.drools.spi.Tuple tuple) throws ConsequenceException
            {
                throw new ConsequenceException( "not enough cheese",
                                                rule );
            }
        } );

        // add condition
View Full Code Here

        // add consequence
        rule.setConsequence( new org.drools.spi.Consequence()
        {
            public void invoke(Activation activation) throws ConsequenceException
            {
                throw new ConsequenceException( "not enough cheese",
                                                rule );
            }
        } );

        /* 1/10th of a second */
 
View Full Code Here

                    workingMemory.modifyObject( tuple.getFactHandleForObject( f ),
                                                f );
                }
                catch ( FactException e )
                {
                    throw new ConsequenceException( e );
                }

                FibTotal total = (FibTotal) workingMemory.getApplicationData( "fibtotal" );
                total.setTotal( total.getTotal( ) + 1 );
            }
        };
        bootstrap1Rule.setConsequence( bootstrapConsequence );
        ruleSet.addRule( bootstrap1Rule );

        // <rule name="Bootstrap 2">
        final Rule bootstrap2Rule = new Rule( "Bootstrap 2" );

        // Specify the declaration as a parameter of the Bootstrap2 Rule
        // <parameter identifier="f">
        // <class>org.drools.examples.fibonacci.Fibonacci</class>
        // </parameter>
        final Declaration fDeclaration2 = bootstrap2Rule.addParameterDeclaration( "f",
                                                                                  fibonacciType );

        // Build and Add the Conditions to the Bootstrap1 Rule
        // <java:condition>f.getSequence() == 2</java:condition>
        final Condition conditionBootstrap2A = new Condition( )
        {
            public boolean isAllowed(Tuple tuple)
            {
                Fibonacci f = (Fibonacci) tuple.get( fDeclaration2 );
                return f.getSequence( ) == 2;
            }

            public Declaration[] getRequiredTupleMembers()
            {
                return new Declaration[]{fDeclaration2};
            }

            public String toString()
            {
                return "f.getSequence() == 2";
            }
        };
        bootstrap2Rule.addCondition( conditionBootstrap2A );

        // <java:condition>f.getValue() == -1</java:condition>
        final Condition conditionBootstrap2B = new Condition( )
        {
            public boolean isAllowed(Tuple tuple)
            {
                Fibonacci f = (Fibonacci) tuple.get( fDeclaration2 );
                return f.getValue( ) == -1;
            }

            public Declaration[] getRequiredTupleMembers()
            {
                return new Declaration[]{fDeclaration2};
            }

            public String toString()
            {
                return "f.getValue() == -1";
            }
        };
        bootstrap2Rule.addCondition( conditionBootstrap2B );

        // Build and Add the Consequence to the Bootstrap1 Rule
        // <java:consequence>
        // f.setValue( 1 );
        // System.err.println( f.getSequence() + " == " + f.getValue() );
        // drools.modifyObject( f );
        // </java:consequence>
        final Consequence bootstrap2Consequence = new Consequence( )
        {
            public void invoke(Tuple tuple) throws ConsequenceException
            {
                WorkingMemory workingMemory = tuple.getWorkingMemory( );
                Fibonacci f = (Fibonacci) tuple.get( fDeclaration2 );
                f.setValue( 1 );

                try
                {
                    workingMemory.modifyObject( tuple.getFactHandleForObject( f ),
                                                f );
                }
                catch ( FactException e )
                {
                    throw new ConsequenceException( e );
                }

                FibTotal total = (FibTotal) workingMemory.getApplicationData( "fibtotal" );
                total.setTotal( total.getTotal( ) + 1 );
            }
        };
        bootstrap2Rule.setConsequence( bootstrap2Consequence );
        ruleSet.addRule( bootstrap2Rule );

        // <rule name="Recurse" salience="10">
        final Rule recurseRule = new Rule( "Recurse" );
        recurseRule.setSalience( 10 );

        // <parameter identifier="f">
        // <class>org.drools.examples.fibonacci.Fibonacci</class>
        // </parameter>
        final Declaration fDeclarationRecurse = recurseRule.addParameterDeclaration( "f",
                                                                                     fibonacciType );

        // <java:condition>f.getValue() == -1</java:condition>
        final Condition conditionRecurse = new Condition( )
        {
            public boolean isAllowed(Tuple tuple)
            {
                Fibonacci f = (Fibonacci) tuple.get( fDeclarationRecurse );
                return f.getValue( ) == -1;
            }

            public Declaration[] getRequiredTupleMembers()
            {
                return new Declaration[]{fDeclarationRecurse};
            }

            public String toString()
            {
                return "f.getValue() == -1";
            }
        };
        recurseRule.addCondition( conditionRecurse );

        // <java:consequence>
        // System.err.println( "recurse for " + f.getSequence() );
        // drools.assertObject( new Fibonacci( f.getSequence() - 1 ) );
        // </java:consequence>
        final Consequence recurseConsequence = new Consequence( )
        {
            public void invoke(Tuple tuple) throws ConsequenceException
            {
                WorkingMemory workingMemory = tuple.getWorkingMemory( );

                Fibonacci f = (Fibonacci) tuple.get( fDeclarationRecurse );
                try
                {
                    workingMemory.assertObject( new Fibonacci( f.getSequence( ) - 1 ) );
                }
                catch ( FactException e )
                {
                    throw new ConsequenceException( e );
                }
            }
        };
        recurseRule.setConsequence( recurseConsequence );
        ruleSet.addRule( recurseRule );

        // <rule name="Calculate">
        final Rule calculateRule = new Rule( "Calculate" );

        // <parameter identifier="f1">
        // <class>org.drools.examples.fibonacci.Fibonacci</class>
        // </parameter>
        final Declaration f1Declaration = calculateRule.addParameterDeclaration( "f1",
                                                                                 fibonacciType );

        // <parameter identifier="f2">
        // <class>org.drools.examples.fibonacci.Fibonacci</class>
        // </parameter>
        final Declaration f2Declaration = calculateRule.addParameterDeclaration( "f2",
                                                                                 fibonacciType );

        // <parameter identifier="f3">
        // <class>org.drools.examples.fibonacci.Fibonacci</class>
        // </parameter>
        final Declaration f3Declaration = calculateRule.addParameterDeclaration( "f3",
                                                                                 fibonacciType );

        // <java:condition>f2.getSequence() ==
        // (f1.getSequence()+1)</java:condition>
        final Condition conditionCalculateA = new Condition( )
        {
            public boolean isAllowed(Tuple tuple)
            {
                Fibonacci f1 = (Fibonacci) tuple.get( f1Declaration );
                Fibonacci f2 = (Fibonacci) tuple.get( f2Declaration );
                return f2.getSequence( ) == f1.getSequence( ) + 1;
            }

            public Declaration[] getRequiredTupleMembers()
            {
                return new Declaration[]{f1Declaration, f2Declaration};
            }

            public String toString()
            {
                return "f2.getSequence() == (f1.getSequence()+1)";
            }
        };
        calculateRule.addCondition( conditionCalculateA );

        // <java:condition>f3.getSequence() ==
        // (f2.getSequence()+1)</java:condition>
        final Condition conditionCalculateB = new Condition( )
        {
            public boolean isAllowed(Tuple tuple)
            {
                Fibonacci f2 = (Fibonacci) tuple.get( f2Declaration );
                Fibonacci f3 = (Fibonacci) tuple.get( f3Declaration );
                return f3.getSequence( ) == f2.getSequence( ) + 1;
            }

            public Declaration[] getRequiredTupleMembers()
            {
                return new Declaration[]{f2Declaration, f3Declaration};
            }

            public String toString()
            {
                return "f3.getSequence() == (f2.getSequence()+1)";
            }
        };
        calculateRule.addCondition( conditionCalculateB );

        // <java:condition>f1.getValue() != -1</java:condition>
        final Condition conditionCalculateC = new Condition( )
        {
            public boolean isAllowed(Tuple tuple)
            {
                Fibonacci f1 = (Fibonacci) tuple.get( f1Declaration );
                return f1.getValue( ) != -1;
            }

            public Declaration[] getRequiredTupleMembers()
            {
                return new Declaration[]{f1Declaration};
            }

            public String toString()
            {
                return "f1.getValue() != -1";
            }
        };
        calculateRule.addCondition( conditionCalculateC );

        // <java:condition>f2.getValue() != -1</java:condition>
        final Condition conditionCalculateD = new Condition( )
        {
            public boolean isAllowed(Tuple tuple)
            {
                Fibonacci f2 = (Fibonacci) tuple.get( f2Declaration );
                return f2.getValue( ) != -1;
            }

            public Declaration[] getRequiredTupleMembers()
            {
                return new Declaration[]{f2Declaration};
            }

            public String toString()
            {
                return "f2.getValue() != -1";
            }
        };
        calculateRule.addCondition( conditionCalculateD );

        // <java:condition>f3.getValue() == -1</java:condition>
        final Condition conditionCalculateE = new Condition( )
        {
            public boolean isAllowed(Tuple tuple)
            {
                Fibonacci f3 = (Fibonacci) tuple.get( f3Declaration );
                return f3.getValue( ) == -1;
            }

            public Declaration[] getRequiredTupleMembers()
            {
                return new Declaration[]{f3Declaration};
            }

            public String toString()
            {
                return "f3.getValue() == -1";
            }
        };
        calculateRule.addCondition( conditionCalculateE );

        // <java:consequence>
        // f3.setValue( f1.getValue() + f2.getValue() );
        // System.err.println( f3.getSequence() + " == " + f3.getValue() );
        // drools.modifyObject( f3 );
        // drools.retractObject( f1 );
        // </java:consequence>
        final Consequence calculateConsequence = new Consequence( )
        {
            public void invoke(Tuple tuple) throws ConsequenceException
            {
                WorkingMemory workingMemory = tuple.getWorkingMemory( );

                Fibonacci f1 = (Fibonacci) tuple.get( f1Declaration );
                Fibonacci f2 = (Fibonacci) tuple.get( f2Declaration );
                Fibonacci f3 = (Fibonacci) tuple.get( f3Declaration );

                f3.setValue( f1.getValue( ) + f2.getValue( ) );
                try
                {
                    workingMemory.modifyObject( tuple.getFactHandleForObject( f3 ),
                                                f3 );
                    workingMemory.retractObject( tuple.getFactHandleForObject( f1 ) );
                }
                catch ( FactException e )
                {
                    throw new ConsequenceException( e );
                }

                FibTotal total = (FibTotal) workingMemory.getApplicationData( "fibtotal" );
                total.setTotal( total.getTotal( ) + 1 );
            }
View Full Code Here

                                                             guest,
                                                             null ) );
                }
                catch ( FactException e )
                {
                    throw new ConsequenceException( e );
                }

                context.setState( "find_seating" );

                try
                {
                    workingMemory.modifyObject( tuple.getFactHandleForObject( context ),
                                                context );
                }
                catch ( FactException e )
                {
                    throw new ConsequenceException( e );
                }
            }
        };
        findFirstSeatRule.setConsequence( consequenceA );
        ruleSet.addRule( findFirstSeatRule );

        // ========================================
        // <rule name="find seating" salience="30">
        // ========================================
        final Rule findSeatingRule = new Rule( "find seating" );
        findSeatingRule.setSalience( 30 );

        // 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 contextDeclB = findSeatingRule.addParameterDeclaration( "context",
                                                                                  contextType );

        // <parameter identifier="guest">
        // <class>org.drools.examples.manners.model.Guest</class>
        // </parameter>
        final Declaration guestDeclB = findSeatingRule.addParameterDeclaration( "guest",
                                                                                guestType );

        // <parameter identifier="seating">
        // <class>org.drools.examples.manners.model.Seating</class>
        // </parameter>
        final Declaration seatingDeclB = findSeatingRule.addParameterDeclaration( "seating",
                                                                                  seatingType );

        // Build and Add the Condition to the Rule
        // <java:condition>context.isState("find_seating")</java:condition>
        final Condition conditionB1 = new Condition( )
        {
            public boolean isAllowed(Tuple tuple)
            {
                Context context = (Context) tuple.get( contextDeclB );
                return context.isState( "find_seating" );
            }

            public Declaration[] getRequiredTupleMembers()
            {
                return new Declaration[]{contextDeclB};
            }

            public String toString()
            {
                return "context.isState(\"find_seating\")";
            }
        };
        findSeatingRule.addCondition( conditionB1 );

        // <java:condition>seating.getGuest2() == null</java:condition>
        final Condition conditionB2 = new Condition( )
        {
            public boolean isAllowed(Tuple tuple)
            {
                Seating seating = (Seating) tuple.get( seatingDeclB );
                return seating.getGuest2( ) == null;
            }

            public Declaration[] getRequiredTupleMembers()
            {
                return new Declaration[]{seatingDeclB};
            }

            public String toString()
            {
                return "seating.getGuest2() == null";
            }
        };
        findSeatingRule.addCondition( conditionB2 );

        // <java:condition>!seating.getTabooList().contains(guest)</java:condition>
        final Condition conditionB3 = new Condition( )
        {
            public boolean isAllowed(Tuple tuple)
            {
                Seating seating = (Seating) tuple.get( seatingDeclB );
                Guest guest = (Guest) tuple.get( guestDeclB );
                return !seating.getTabooList( ).contains( guest );
            }

            public Declaration[] getRequiredTupleMembers()
            {
                return new Declaration[]{seatingDeclB, guestDeclB};
            }

            public String toString()
            {
                return "!seating.getTabooList().contains(guest)";
            }
        };
        findSeatingRule.addCondition( conditionB3 );

        // <java:condition>seating.getGuest1().hasOppositeSex(guest)</java:condition>
        final Condition conditionB4 = new Condition( )
        {
            public boolean isAllowed(Tuple tuple)
            {
                Seating seating = (Seating) tuple.get( seatingDeclB );
                Guest guest = (Guest) tuple.get( guestDeclB );
                return seating.getGuest1( ).hasOppositeSex( guest );
            }

            public Declaration[] getRequiredTupleMembers()
            {
                return new Declaration[]{seatingDeclB, guestDeclB};
            }

            public String toString()
            {
                return "seating.getGuest1().hasOppositeSex(guest)";
            }
        };
        findSeatingRule.addCondition( conditionB4 );

        // <java:condition>seating.getGuest1().hasSameHobby(guest)</java:condition>
        final Condition conditionB5 = new Condition( )
        {
            public boolean isAllowed(Tuple tuple)
            {
                Seating seating = (Seating) tuple.get( seatingDeclB );
                Guest guest = (Guest) tuple.get( guestDeclB );
                return seating.getGuest1( ).hasSameHobby( guest );
            }

            public Declaration[] getRequiredTupleMembers()
            {
                return new Declaration[]{seatingDeclB, guestDeclB};
            }

            public String toString()
            {
                return "seating.getGuest1().hasSameHobby(guest)";
            }
        };
        findSeatingRule.addCondition( conditionB5 );

        // Build and Add the Consequence to the Rule
        // <java:consequence>
        // System.out.println("FIRE: find seating: " + seating + " " + guest);
        //
        // Seating nextSeat = new Seating(seating.getSeat2(), guest, seating);
        // drools.assertObject(nextSeat);
        //
        // seating.setGuest2(guest);
        // seating.getTabooList().add(guest);
        // drools.modifyObject(seating);
        // </java:consequence>
        final Consequence consequenceB = new Consequence( )
        {
            public void invoke(Tuple tuple) throws ConsequenceException
            {
                WorkingMemory workingMemory = tuple.getWorkingMemory( );

                Guest guest = (Guest) tuple.get( guestDeclB );
                Seating seating = (Seating) tuple.get( seatingDeclB );

                Seating nextSeat = new Seating( seating.getSeat2( ),
                                                guest,
                                                seating );
                try
                {
                    workingMemory.assertObject( nextSeat );
                }
                catch ( FactException e )
                {
                    throw new ConsequenceException( e );
                }

                seating.setGuest2( guest );
                seating.getTabooList( ).add( guest );

                try
                {
                    workingMemory.modifyObject( tuple.getFactHandleForObject( seating ),
                                                seating );
                }
                catch ( FactException e )
                {
                    throw new ConsequenceException( e );
                }
            }
        };
        findSeatingRule.setConsequence( consequenceB );
        ruleSet.addRule( findSeatingRule );

        // ===========================================
        // <rule name="try another path" salience="20">
        // ===========================================
        final Rule tryAnotherPathRule = new Rule( "try another path" );
        tryAnotherPathRule.setSalience( 20 );

        // 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 contextDeclC = tryAnotherPathRule.addParameterDeclaration( "context",
                                                                                     contextType );

        // <parameter identifier="lastSeat">
        // <class>org.drools.examples.manners.model.LastSeat</class>
        // </parameter>
        final Declaration lastSeatDeclC = tryAnotherPathRule.addParameterDeclaration( "lastSeat",
                                                                                      lastSeatType );

        // <parameter identifier="seating">
        // <class>org.drools.examples.manners.model.Seating</class>
        // </parameter>
        final Declaration seatingDeclC = tryAnotherPathRule.addParameterDeclaration( "seating",
                                                                                     seatingType );

        // <java:condition>context.isState("find_seating")</java:condition>
        final Condition conditionC1 = new Condition( )
        {
            public boolean isAllowed(Tuple tuple)
            {
                Context context = (Context) tuple.get( contextDeclC );
                return context.isState( "find_seating" );
            }

            public Declaration[] getRequiredTupleMembers()
            {
                return new Declaration[]{contextDeclC};
            }

            public String toString()
            {
                return "context.isState(\"find_seating\")";
            }
        };
        tryAnotherPathRule.addCondition( conditionC1 );

        // <java:condition>lastSeat.getSeat() >
        // 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};
            }

            public String toString()
            {
                return "lastSeat.getSeat() == seating.getSeat1()";
            }
        };
        weAreDoneRule.addCondition( conditionD2 );

        // <java:consequence>
        // System.out.println("FIRE: we are done");
        //
        // import org.drools.examples.manners.model.Seat;
        // List list = new ArrayList();
        // while(seating != null) {
        // Seat seat = new Seat(seating.getSeat1(),
        // seating.getGuest1().getName());
        // seating = seating.getPrevSeat();
        // list.add(seat);
        // }
        //
        // for (int i = list.size(); i > 0; i--) {
        // Seat seat = (Seat)list.get(i-1);
        // System.out.println(seat);
        // drools.assertObject(seat);
        // }
        //
        // context.setState("all_done");
        // drools.modifyObject(context);
        // </java:consequence>
        final Consequence consequenceD = new Consequence( )
        {
            public void invoke(Tuple tuple) throws ConsequenceException
            {
                WorkingMemory workingMemory = tuple.getWorkingMemory( );

                Seating seating = (Seating) tuple.get( seatingDeclD );
                Context context = (Context) tuple.get( contextDeclD );

                List list = new ArrayList( );
                while ( seating != null )
                {
                    Seat seat = new Seat( seating.getSeat1( ),
                                          seating.getGuest1( ).getName( ) );
                    seating = seating.getPrevSeat( );
                    list.add( seat );
                }

                for ( int i = list.size( ); i > 0; i-- )
                {
                    Seat seat = (Seat) list.get( i - 1 );
                    try
                    {
                        workingMemory.assertObject( seat );
                    }
                    catch ( FactException e )
                    {
                        throw new ConsequenceException( e );
                    }
                }

                context.setState( "all_done" );
                try
                {
                    workingMemory.modifyObject( tuple.getFactHandleForObject( context ),
                                                context );
                }
                catch ( FactException e )
                {
                    throw new ConsequenceException( e );
                }
            }
        };
        weAreDoneRule.setConsequence( consequenceD );
        ruleSet.addRule( weAreDoneRule );
View Full Code Here

TOP

Related Classes of org.drools.spi.ConsequenceException

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.