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

📄 basewaltztest.java

📁 jboss规则引擎
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
        rule.addPattern( lineColumn );
        final Declaration lineDeclaration = rule.getDeclaration( "line" );

        final Consequence consequence = new Consequence() {

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

                    Line line = (Line) drools.get( lineDeclaration );
                    drools.assertObject( new Edge( line.getP1(),
                                                   line.getP2(),
                                                   false,
                                                   Edge.NIL,
                                                   Edge.NIL ) );
                    drools.assertObject( new Edge( line.getP2(),
                                                   line.getP1(),
                                                   false,
                                                   Edge.NIL,
                                                   Edge.NIL ) );
                    drools.retractObject( tuple.get( lineDeclaration ) );

                    System.out.println( "draw " + line.getP1() + " " + line.getP2() );
                } catch ( Exception e ) {
                    e.printStackTrace();
                    throw new ConsequenceException( e );
                }
            }

        };
        rule.setConsequence( consequence );
        return rule;
    }

    // ;If the duplicating flag is set, and there are no more lines, then remove
    // the
    // ;duplicating flag and set the make junctions flag.
    // (p done_reversing
    // (stage ^value duplicate)
    // - (line)
    // -->
    // (modify 1 ^value detect_junctions))
    private Rule getDoneReversingRule() throws IntrospectionException,
                                       InvalidRuleException {
        final Rule rule = new Rule( "done_reversing" );

        final Column stageColumn = new Column( 0,
                                               this.stageType,
                                               "stage" );
        stageColumn.addConstraint( getLiteralConstraint( stageColumn,
                                                         "value",
                                                         new Integer( Stage.DUPLICATE ),
                                                         this.integerEqualEvaluator ) );
        rule.addPattern( stageColumn );
        final Declaration stageDeclaration = rule.getDeclaration( "stage" );

        final Column notLineColumn = new Column( 1,
                                                 this.lineType );
        final Not notLine = new Not();
        notLine.addChild( notLineColumn );
        rule.addPattern( notLine );

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

                    Stage stage = (Stage) drools.get( stageDeclaration );
                    stage.setValue( Stage.DETECT_JUNCTIONS );
                    drools.modifyObject( tuple.get( stageDeclaration ),
                                         stage );
                } catch ( Exception e ) {
                    e.printStackTrace();
                    throw new ConsequenceException( e );
                }
            }
        };
        rule.setConsequence( consequence );
        return rule;
    }

    // ;If three edges meet at a point and none of them have already been joined
    // in
    // ;a junction, then make the corresponding type of junction and label the
    // ;edges joined. This production calls make-3_junction to determine
    // ;what type of junction it is based on the angles inscribed by the
    // ;intersecting edges
    // (p make-3_junction
    // (stage ^value detect_junctions)
    // (edge ^p1 <base_point> ^p2 <p1> ^joined false)
    // (edge ^p1 <base_point> ^p2 {<p2> <> <p1>} ^joined false)
    // (edge ^p1 <base_point> ^p2 {<p3> <> <p1> <> <p2>} ^joined false)
    // -->
    // (make junction
    // ^type (make_3_junction <base_point> <p1> <p2> <p3>)
    // ^base_point <base_point>)
    // (modify 2 ^joined true)
    // (modify 3 ^joined true)
    // (modify 4 ^joined true))
    private Rule getMake3JunctionRule() throws IntrospectionException,
                                       InvalidRuleException {
        final Rule rule = new Rule( "make-3_junction" );

        final Column stageColumn = new Column( 0,
                                               this.stageType,
                                               "stage" );
        stageColumn.addConstraint( getLiteralConstraint( stageColumn,
                                                         "value",
                                                         new Integer( Stage.DETECT_JUNCTIONS ),
                                                         this.integerEqualEvaluator ) );
        rule.addPattern( stageColumn );
        //      final Declaration stageDeclaration = rule.getDeclaration("stage");

        final Column edgeColumn1 = new Column( 1,
                                               this.edgeType,
                                               "edge1" );
        edgeColumn1.addConstraint( getLiteralConstraint( edgeColumn1,
                                                         "joined",
                                                         new Boolean( false ),
                                                         this.booleanEqualEvaluator ) );
        setFieldDeclaration( edgeColumn1,
                             "p1",
                             "edge1p1" );
        setFieldDeclaration( edgeColumn1,
                             "p2",
                             "edge1p2" );
        rule.addPattern( edgeColumn1 );
        final Declaration edge1Declaration = rule.getDeclaration( "edge1" );
        final Declaration edge1P1Declaration = rule.getDeclaration( "edge1p1" );
        final Declaration edge1P2Declaration = rule.getDeclaration( "edge1p2" );

        final Column edgeColumn2 = new Column( 2,
                                               this.edgeType,
                                               "edge2" );
        edgeColumn2.addConstraint( getLiteralConstraint( edgeColumn2,
                                                         "joined",
                                                         new Boolean( false ),
                                                         this.booleanEqualEvaluator ) );
        setFieldDeclaration( edgeColumn2,
                             "p2",
                             "edge2p2" );
        rule.addPattern( edgeColumn2 );
        final Declaration edge2Declaration = rule.getDeclaration( "edge2" );
        final Declaration edge2P2Declaration = rule.getDeclaration( "edge2p2" );
        edgeColumn2.addConstraint( getBoundVariableConstraint( edgeColumn2,
                                                               "p1",
                                                               edge1P1Declaration,
                                                               this.integerEqualEvaluator ) );
        edgeColumn2.addConstraint( getBoundVariableConstraint( edgeColumn2,
                                                               "p2",
                                                               edge1P2Declaration,
                                                               this.integerNotEqualEvaluator ) );

        final Column edgeColumn3 = new Column( 3,
                                               this.edgeType,
                                               "edge3" );
        edgeColumn3.addConstraint( getLiteralConstraint( edgeColumn3,
                                                         "joined",
                                                         new Boolean( false ),
                                                         this.booleanEqualEvaluator ) );
        rule.addPattern( edgeColumn3 );
        final Declaration edge3Declaration = rule.getDeclaration( "edge3" );
        edgeColumn3.addConstraint( getBoundVariableConstraint( edgeColumn3,
                                                               "p1",
                                                               edge1P1Declaration,
                                                               this.integerEqualEvaluator ) );
        edgeColumn3.addConstraint( getBoundVariableConstraint( edgeColumn3,
                                                               "p2",
                                                               edge1P2Declaration,
                                                               this.integerNotEqualEvaluator ) );
        edgeColumn3.addConstraint( getBoundVariableConstraint( edgeColumn3,
                                                               "p2",
                                                               edge2P2Declaration,
                                                               this.integerNotEqualEvaluator ) );

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

                    Edge edge1 = (Edge) drools.get( edge1Declaration );
                    edge1.setJoined( true );
                    Edge edge2 = (Edge) drools.get( edge2Declaration );
                    edge2.setJoined( true );
                    Edge edge3 = (Edge) drools.get( edge3Declaration );
                    edge3.setJoined( true );

                    drools.assertObject( WaltzUtil.make_3_junction( edge1.getP1(),
                                                                    edge1.getP2(),
                                                                    edge2.getP2(),
                                                                    edge3.getP2() ) );

                    drools.modifyObject( tuple.get( edge1Declaration ),
                                         edge1 );
                    drools.modifyObject( tuple.get( edge2Declaration ),
                                         edge2 );
                    drools.modifyObject( tuple.get( edge3Declaration ),
                                         edge3 );

                } catch ( Exception e ) {
                    e.printStackTrace();
                    throw new ConsequenceException( e );
                }
            }
        };
        rule.setConsequence( consequence );
        return rule;
    }

    // ;If two, and only two, edges meet that have not already been joined, then
    // ;the junction is an "L"
    // (p make_L
    // (stage ^value detect_junctions)
    // (edge ^p1 <base_point> ^p2 <p2> ^joined false)
    // (edge ^p1 <base_point> ^p2 {<p3> <> <p2>} ^joined false)
    // - (edge ^p1 <base_point> ^p2 {<> <p2> <> <p3>})
    // -->
    // (make junction
    // ^type L
    // ^base_point <base_point>
    // ^p1 <p2>
    // ^p2 <p3>)
    // (modify 2 ^joined true)
    // (modify 3 ^joined true))
    private Rule getMakeLRule() throws IntrospectionException,
                               InvalidRuleException {
        final Rule rule = new Rule( "make_L" );

        final Column stageColumn = new Column( 0,
                                               this.stageType,
                                               "stage" );
        stageColumn.addConstraint( getLiteralConstraint( stageColumn,
                                                         "value",
                                                         new Integer( Stage.DETECT_JUNCTIONS ),
                                                         this.integerEqualEvaluator ) );
        rule.addPattern( stageColumn );
        //      final Declaration stageDeclaration = rule.getDeclaration("stage");

        final Column edgeColumn1 = new Column( 1,
                                               this.edgeType,
                                               "edge1" );
        edgeColumn1.addConstraint( getLiteralConstraint( edgeColumn1,

⌨️ 快捷键说明

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