⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mannersnative.java

📁 rule engine drools-2.0-beta-18
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            {                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, WorkingMemory workingMemory ) 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, WorkingMemory workingMemory ) 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 );        // ==================        // Build the RuleSet.        // ==================        RuleBaseBuilder builder = new RuleBaseBuilder( );        builder.addRuleSet( ruleSet );        RuleBase ruleBase = builder.build( );        workingMemory = ruleBase.newWorkingMemory( );    }    protected void tearDown() throws Exception    {        workingMemory = null;    }    protected List test(List inList) throws Exception    {        for ( Iterator i = inList.iterator( ); i.hasNext( ); )        {            workingMemory.assertObject( i.next( ) );        }        workingMemory.fireAllRules( );        return workingMemory.getObjects( );    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -