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

📄 basemannerstest.java

📁 jboss规则引擎
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
        final Rule rule = new Rule( "findSeating" );

        // ---------------
        // context : Context( state == Context.ASSIGN_SEATS )
        // ---------------
        final Column contextColumn = new Column( 0,
                                                 this.contextType,
                                                 "context" );

        contextColumn.addConstraint( getLiteralConstraint( contextColumn,
                                                           "state",
                                                           new Integer( Context.ASSIGN_SEATS ),
                                                           this.integerEqualEvaluator ) );

        rule.addPattern( contextColumn );

        final Declaration contextDeclaration = rule.getDeclaration( "context" );

        // -------------------------------
        // Seating( seatingId:id, seatingPid:pid, pathDone == true
        // seatingRightSeat:rightSeat seatingRightGuestName:rightGuestName )
        // -------------------------------
        final Column seatingColumn = new Column( 1,
                                                 this.seatingType );

        setFieldDeclaration( seatingColumn,
                             "id",
                             "seatingId" );

        setFieldDeclaration( seatingColumn,
                             "pid",
                             "seatingPid" );

        seatingColumn.addConstraint( getLiteralConstraint( seatingColumn,
                                                           "pathDone",
                                                           new Boolean( true ),
                                                           this.booleanEqualEvaluator ) );

        setFieldDeclaration( seatingColumn,
                             "rightSeat",
                             "seatingRightSeat" );

        setFieldDeclaration( seatingColumn,
                             "rightGuestName",
                             "seatingRightGuestName" );

        rule.addPattern( seatingColumn );

        final Declaration seatingIdDeclaration = rule.getDeclaration( "seatingId" );
        final Declaration seatingPidDeclaration = rule.getDeclaration( "seatingPid" );
        final Declaration seatingRightGuestNameDeclaration = rule.getDeclaration( "seatingRightGuestName" );
        final Declaration seatingRightSeatDeclaration = rule.getDeclaration( "seatingRightSeat" );
        // --------------
        // Guest( name == seatingRightGuestName, rightGuestSex:sex,
        // rightGuestHobby:hobby )
        // ---------------
        final Column rightGuestColumn = new Column( 2,
                                                    this.guestType );

        rightGuestColumn.addConstraint( getBoundVariableConstraint( rightGuestColumn,
                                                                    "name",
                                                                    seatingRightGuestNameDeclaration,
                                                                    this.objectEqualEvaluator ) );

        setFieldDeclaration( rightGuestColumn,
                             "sex",
                             "rightGuestSex" );

        setFieldDeclaration( rightGuestColumn,
                             "hobby",
                             "rightGuestHobby" );

        rule.addPattern( rightGuestColumn );

        final Declaration rightGuestSexDeclaration = rule.getDeclaration( "rightGuestSex" );
        final Declaration rightGuestHobbyDeclaration = rule.getDeclaration( "rightGuestHobby" );

        // ----------------
        // Guest( leftGuestName:name , sex != rightGuestSex, hobby ==
        // rightGuestHobby )
        // ----------------
        final Column leftGuestColumn = new Column( 3,
                                                   this.guestType );

        setFieldDeclaration( leftGuestColumn,
                             "name",
                             "leftGuestName" );

        leftGuestColumn.addConstraint( getBoundVariableConstraint( leftGuestColumn,
                                                                   "sex",
                                                                   rightGuestSexDeclaration,
                                                                   this.objectNotEqualEvaluator ) );

        leftGuestColumn.addConstraint( getBoundVariableConstraint( rightGuestColumn,
                                                                   "hobby",
                                                                   rightGuestHobbyDeclaration,
                                                                   this.objectEqualEvaluator ) );
        rule.addPattern( leftGuestColumn );
        final Declaration leftGuestNameDeclaration = rule.getDeclaration( "leftGuestName" );

        // ---------------
        // count : Count()
        // ---------------
        final Column count = new Column( 4,
                                         this.countType,
                                         "count" );

        rule.addPattern( count );

        final Declaration countDeclaration = rule.getDeclaration( "count" );

        // --------------
        // not ( Path( id == seatingId, guestName == leftGuestName) )
        // --------------
        final Column notPathColumn = new Column( 5,
                                                 this.pathType );

        notPathColumn.addConstraint( getBoundVariableConstraint( notPathColumn,
                                                                 "id",
                                                                 seatingIdDeclaration,
                                                                 this.integerEqualEvaluator ) );

        notPathColumn.addConstraint( getBoundVariableConstraint( notPathColumn,
                                                                 "guestName",
                                                                 leftGuestNameDeclaration,
                                                                 this.objectEqualEvaluator ) );
        final Not notPath = new Not();
        notPath.addChild( notPathColumn );
        rule.addPattern( notPath );
        // ------------
        // not ( Chosen( id == seatingId, guestName == leftGuestName, hobby ==
        // rightGuestHobby ) )
        // ------------
        final Column notChosenColumn = new Column( 6,
                                                   this.chosenType );

        notChosenColumn.addConstraint( getBoundVariableConstraint( notChosenColumn,
                                                                   "id",
                                                                   seatingIdDeclaration,
                                                                   this.integerEqualEvaluator ) );

        notChosenColumn.addConstraint( getBoundVariableConstraint( notChosenColumn,
                                                                   "guestName",
                                                                   leftGuestNameDeclaration,
                                                                   this.objectEqualEvaluator ) );

        notChosenColumn.addConstraint( getBoundVariableConstraint( notChosenColumn,
                                                                   "hobby",
                                                                   rightGuestHobbyDeclaration,
                                                                   this.objectEqualEvaluator ) );

        final Not notChosen = new Not();
        notChosen.addChild( notChosenColumn );

        rule.addPattern( notChosen );

        // ------------
        // int newSeat = rightSeat + 1;
        // drools.assert( new Seating( coung.getValue(), 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 );
        // ------------
        final Consequence consequence = new Consequence() {

            public void evaluate(KnowledgeHelper drools,
                                 WorkingMemory workingMemory) throws ConsequenceException {
                try {
                    Rule rule = drools.getRule();
                    Tuple tuple = drools.getTuple();

                    Context context = (Context) drools.get( contextDeclaration );
                    Count count = (Count) drools.get( countDeclaration );
                    int seatId = ((Integer) drools.get( seatingIdDeclaration )).intValue();
                    int seatingRightSeat = ((Integer) drools.get( seatingRightSeatDeclaration )).intValue();

                    String leftGuestName = (String) drools.get( leftGuestNameDeclaration );
                    String rightGuestName = (String) drools.get( seatingRightGuestNameDeclaration );
                    Hobby rightGuestHobby = (Hobby) drools.get( rightGuestHobbyDeclaration );

                    Seating seating = new Seating( count.getValue(),
                                                   seatId,
                                                   false,
                                                   seatingRightSeat,
                                                   rightGuestName,
                                                   seatingRightSeat + 1,
                                                   leftGuestName );
                    drools.assertObject( seating );

                    Path path = new Path( count.getValue(),
                                          seatingRightSeat + 1,
                                          leftGuestName );

                    drools.assertObject( path );

                    Chosen chosen = new Chosen( seatId,
                                                leftGuestName,
                                                rightGuestHobby );

                    drools.assertObject( chosen );
                    count.setValue( count.getValue() + 1 );

                    //                    if ( count.getValue() == 5 ) {
                    //                        drools.retractObject( tuple.getFactHandleForDeclaration( countDeclaration ) );
                    //                    } else {
                    //                        drools.modifyObject( tuple.getFactHandleForDeclaration( countDeclaration ),
                    //                                             count );                        
                    //                    }

                    drools.modifyObject( tuple.get( countDeclaration ),
                                         count );

                    context.setState( Context.MAKE_PATH );
                    drools.modifyObject( tuple.get( contextDeclaration ),
                                         context );

                    System.err.println( "find seating : " + seating + " : " + path + " : " + chosen );

                } catch ( Exception e ) {
                    e.printStackTrace();
                    throw new ConsequenceException( e );
                }
            }

        };

        rule.setConsequence( consequence );

        return rule;
    }

    /**
     * <pre>
     *    rule makePath() {
     *        Context context;
     *        int seatingId, seatingPid, pathSeat;
     *        String pathGuestName;
     *   
     *        when {
     *            Context( state == Context.MAKE_PATH )
     *            Seating( seatingId:id, seatingPid:pid, pathDone == false )
     *            Path( id == seatingPid, pathGuestName:guest, pathSeat:seat )
     *            (not Path( id == seatingId, guestName == pathGuestName )
     *        } else {
     *            drools.assert( new Path( seatingId, pathSeat, pathGuestName ) );
     *   
     *        }
     *    } 
     * </pre>
     * 
     * @return
     * @throws IntrospectionException
     * @throws InvalidRuleException
     */
    private Rule getMakePath() throws IntrospectionException,
                              InvalidRuleException {
        final Rule rule = new Rule( "makePath" );

        // -----------
        // context : Context( state == Context.MAKE_PATH )
        // -----------
        final Column contextColumn = new Column( 0,
                                                 this.contextType );

        contextColumn.addConstraint( getLiteralConstraint( contextColumn,
                                                           "state",
                                                           new Integer( Context.MAKE_PATH ),
                                                           this.integerEqualEvaluator ) );

        rule.addPattern( contextColumn );

        // ---------------
        // Seating( seatingId:id, seatingPid:pid, pathDone == false )
        // ---------------
        final Column seatingColumn = new Column( 1,
                                                 this.seatingType );

⌨️ 快捷键说明

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