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

📄 joinnodetest.java

📁 jboss规则引擎
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        // check bi directional match to resulting TupleMatch
        final Map map = tuple1.getTupleMatches();
        final TupleMatch match = (TupleMatch) map.get( f0 );

        ObjectMatches matches = (ObjectMatches) this.memory.rightObjectIterator( this.workingMemory,
                                                                                 tuple1 ).next();
        assertSame( match,
                    matches.getFirstTupleMatch() );

        // check reference form TupleMatch to propgated ReteTuple
        assertSame( match.getJoinedTuples().get( 0 ),
                    ((Object[]) this.sink.getAsserted().get( 0 ))[0] );

        // check objectmatches correct references second asserted tuple
        final DefaultFactHandle f2 = new DefaultFactHandle( 2,
                                                            "cheese" );
        final ReteTuple tuple2 = new ReteTuple( f2 );
        this.node.assertTuple( tuple2,
                               this.context,
                               this.workingMemory );

        assertSame( (tuple2.getTupleMatches()).get( f0 ),
                    matches.getFirstTupleMatch().getNext() );

        final DefaultFactHandle f3 = (DefaultFactHandle) this.workingMemory.assertObject( "test2" );
        this.node.assertObject( f3,
                                this.context,
                                this.workingMemory );

        matches = getMatchesFor( tuple1,
                                 f3 );
        assertSame( (tuple1.getTupleMatches()).get( f3 ),
                    matches.getFirstTupleMatch() );
        assertSame( (tuple2.getTupleMatches()).get( f3 ),
                    matches.getFirstTupleMatch().getNext() );
    }

    /**
     * Test Tuple retraction
     * 
     * @throws Exception
     * @throws RetractionException
     */
    public void testRetractTuple() throws Exception {
        // setup 2 tuples 3 fact handles
        final DefaultFactHandle f0 = (DefaultFactHandle) this.workingMemory.assertObject( "test0" );
        this.node.assertObject( f0,
                                this.context,
                                this.workingMemory );

        final DefaultFactHandle f1 = (DefaultFactHandle) this.workingMemory.assertObject( "test1" );
        final ReteTuple tuple1 = new ReteTuple( f1 );
        this.node.assertTuple( tuple1,
                               this.context,
                               this.workingMemory );

        final DefaultFactHandle f2 = (DefaultFactHandle) this.workingMemory.assertObject( "test2" );
        final ReteTuple tuple2 = new ReteTuple( f2 );
        this.node.assertTuple( tuple2,
                               this.context,
                               this.workingMemory );

        final DefaultFactHandle f3 = (DefaultFactHandle) this.workingMemory.assertObject( "test3" );
        this.node.assertObject( f3,
                                this.context,
                                this.workingMemory );

        final DefaultFactHandle f4 = (DefaultFactHandle) this.workingMemory.assertObject( "test4" );
        this.node.assertObject( f4,
                                this.context,
                                this.workingMemory );

        assertLength( 6,
                      this.sink.getAsserted() );

        // Retract an object and make sure its removed from the previous
        // matching ReteTuples
        this.node.retractObject( f0,
                                 this.context,
                                 this.workingMemory );
        assertLength( 2,
                      this.sink.getRetracted() );

        //assertNull( this.memory.getRightFactHandleMemory().get( f0 ) );
        assertNull( tuple1.getTupleMatches().get( f0 ) );
        assertNull( tuple2.getTupleMatches().get( f0 ) );

        this.node.retractTuple( tuple2,
                                this.context,
                                this.workingMemory );
        ObjectMatches matches = this.getMatchesFor( tuple2,
                                                    f3 );
        for ( TupleMatch match = matches.getFirstTupleMatch(); match != null; match = (TupleMatch) match.getNext() ) {
            assertNotSame( tuple2,
                           match.getTuple() );
        }

        matches = this.getMatchesFor( tuple2,
                                      f4 );
        for ( TupleMatch match = matches.getFirstTupleMatch(); match != null; match = (TupleMatch) match.getNext() ) {
            assertNotSame( tuple2,
                           match.getTuple() );
        }
        assertLength( 4,
                      this.sink.getRetracted() );
    }

    public void testConstraintPropagations() throws Exception {
        this.constraint.isAllowed = false;
        // assert first right object
        final DefaultFactHandle f0 = (DefaultFactHandle) this.workingMemory.assertObject( "test0" );
        this.node.assertObject( f0,
                                this.context,
                                this.workingMemory );

        // assert tuple, should add left memory should be 2
        final DefaultFactHandle f1 = new DefaultFactHandle( 1,
                                                            "cheese" );
        final ReteTuple tuple1 = new ReteTuple( f1 );
        this.node.assertTuple( tuple1,
                               this.context,
                               this.workingMemory );

        assertLength( 0,
                      this.sink.getAsserted() );

        // check no matches
        final Map map = tuple1.getTupleMatches();
        assertLength( 0,
                      map.keySet() );

        //assertNull( ((ObjectMatches) this.memory.getRightFactHandleMemory().get( f0 )).getFirstTupleMatch() );
    }

    public void testUpdateWithMemory() {
        final ReteooWorkingMemory workingMemory = new ReteooWorkingMemory( new ReteooRuleBase() );

        final JoinNode joinNode = new JoinNode( 1,
                                                this.tupleSource,
                                                this.objectSource );

        // Add the first tuple sink and assert a tuple and object
        // The sink has no memory
        final MockTupleSink sink1 = new MockTupleSink( 2 );
        joinNode.addTupleSink( sink1 );

        final DefaultFactHandle f0 = new DefaultFactHandle( 0,
                                                            "string0" );

        final ReteTuple tuple1 = new ReteTuple( f0 );

        joinNode.assertTuple( tuple1,
                              this.context,
                              workingMemory );

        final String string1 = "string1";
        final DefaultFactHandle string1Handle = new DefaultFactHandle( 1,
                                                                       string1 );

        joinNode.assertObject( string1Handle,
                               this.context,
                               workingMemory );

        assertLength( 1,
                      sink1.getAsserted() );

        // Add the new sink, this should be updated from the re-processed
        // joinnode memory
        final MockTupleSink sink2 = new MockTupleSink( 3 );
        joinNode.addTupleSink( sink2 );
        assertLength( 0,
                      sink2.getAsserted() );

        joinNode.updateNewNode( workingMemory,
                                this.context );

        assertLength( 1,
                      sink2.getAsserted() );
    }

    /**
     * @param tuple1
     * @param matches
     * @param f3
     * @return
     */
    private ObjectMatches getMatchesFor(final ReteTuple tuple1,
                                        final DefaultFactHandle f3) {
        ObjectMatches matches = null;
        for ( final Iterator i = this.memory.rightObjectIterator( this.workingMemory,
                                                                  tuple1 ); i.hasNext(); ) {
            matches = (ObjectMatches) i.next();
            if ( matches.getFactHandle().equals( f3 ) ) {
                break;
            }
        }
        return matches;
    }

    public void testGetConstraints_ReturnsNullEvenWithEmptyBinder() {
        BetaNodeBinder nullBinder = null;
        JoinNode joinNode = new JoinNode( 1,
                                                this.tupleSource,
                                                this.objectSource, nullBinder);        
        FieldConstraint[] constraints = joinNode.getConstraints();
        assertNull(constraints);
    }
    
}

⌨️ 快捷键说明

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