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

📄 evalconditionnodetest.java

📁 jboss规则引擎
💻 JAVA
📖 第 1 页 / 共 2 页
字号:

        // Create the Tuple
        final DefaultFactHandle f0 = new DefaultFactHandle( 0,
                                                            "stilton" );
        final ReteTuple tuple0 = new ReteTuple( f0 );

        // Tuple should pass and propagate 
        node.assertTuple( tuple0,
                          this.context,
                          this.workingMemory );

        // we create and retract two tuples, checking the linkedtuples is null for JBRULES-246 "NPE on retract()"        
        // Create the Tuple
        final DefaultFactHandle f1 = new DefaultFactHandle( 1,
                                                            "cheddar" );
        final ReteTuple tuple1 = new ReteTuple( f1 );

        // Tuple should pass and propagate 
        node.assertTuple( tuple1,
                          this.context,
                          this.workingMemory );

        // Check memory was populated
        final LinkedList memory = (LinkedList) this.workingMemory.getNodeMemory( node );

        assertEquals( 2,
                      memory.size() );
        assertEquals( tuple0,
                      memory.getFirst() );
        assertEquals( tuple1,
                      tuple0.getNext() );

        // make sure assertions were propagated
        assertEquals( 2,
                      sink.getAsserted().size() );

        eval.setIsAllowed( false );

        // Now test that the fact is modified correctly
        node.modifyTuple( tuple0,
                          this.context,
                          this.workingMemory );

        // tuple1 has now been removed
        assertEquals( 1,
                      memory.size() );
        assertEquals( tuple1,
                      memory.getFirst() );

        // make sure modifications were propagated as a retraction, as it is now no longer passing
        assertEquals( 1,
                      sink.getRetracted().size() );

        // Now test that the fact is modified correctly
        node.modifyTuple( tuple1,
                          this.context,
                          this.workingMemory );

        // tuple0 has now been removed
        assertEquals( 0,
                      memory.size() );

        // make sure modifications were propagated as a retraction
        assertEquals( 2,
                      sink.getRetracted().size() );
    }

    public void testAssertedNotAllowed() throws FactException {
        final MockEvalCondition eval = new MockEvalCondition( false );

        // Create a test node that always returns false 
        final EvalConditionNode node = new EvalConditionNode( 1,
                                                              new MockTupleSource( 15 ),
                                                              eval );

        final MockTupleSink sink = new MockTupleSink();
        node.addTupleSink( sink );

        // Create the Tuple
        final DefaultFactHandle f0 = new DefaultFactHandle( 0,
                                                            "stilton" );
        final ReteTuple tuple0 = new ReteTuple( f0 );

        // Tuple should fail and not propagate
        node.assertTuple( tuple0,
                          this.context,
                          this.workingMemory );

        // Create the Tuple
        final DefaultFactHandle f1 = new DefaultFactHandle( 1,
                                                            "cheddar" );
        final ReteTuple tuple1 = new ReteTuple( f1 );

        // Tuple should fail and not propagate 
        node.assertTuple( tuple1,
                          this.context,
                          this.workingMemory );

        // Check memory was not populated
        final LinkedList memory = (LinkedList) this.workingMemory.getNodeMemory( node );

        assertEquals( 0,
                      memory.size() );

        // test no propagations
        assertEquals( 0,
                      sink.getAsserted().size() );
        assertEquals( 0,
                      sink.getModified().size() );
        assertEquals( 0,
                      sink.getRetracted().size() );
    }

    public void testAssertedNotAllowedThenModifyNotAllowed() throws FactException {
        final MockEvalCondition eval = new MockEvalCondition( false );

        // Create a test node that always returns false 
        final EvalConditionNode node = new EvalConditionNode( 1,
                                                              new MockTupleSource( 15 ),
                                                              eval );

        final MockTupleSink sink = new MockTupleSink();
        node.addTupleSink( sink );

        // Create the Tuple
        final DefaultFactHandle f0 = new DefaultFactHandle( 0,
                                                            "stilton" );
        final ReteTuple tuple0 = new ReteTuple( f0 );

        // Tuple should fail and not propagate
        node.assertTuple( tuple0,
                          this.context,
                          this.workingMemory );

        // Create the Tuple
        final DefaultFactHandle f1 = new DefaultFactHandle( 1,
                                                            "chedddar" );
        final ReteTuple tuple1 = new ReteTuple( f1 );

        // Tuple should fail and not propagate 
        node.assertTuple( tuple1,
                          this.context,
                          this.workingMemory );

        // Check memory was not populated
        final LinkedList memory = (LinkedList) this.workingMemory.getNodeMemory( node );

        assertEquals( 0,
                      memory.size() );

        // Now test that the fact is modified correctly
        node.modifyTuple( tuple0,
                          this.context,
                          this.workingMemory );
        assertEquals( 0,
                      memory.size() );

        // Now test that the fact is modified correctly
        node.modifyTuple( tuple1,
                          this.context,
                          this.workingMemory );

        // make sure the memory wasn't populated
        assertEquals( 0,
                      memory.size() );

        // test no propagations
        assertEquals( 0,
                      sink.getAsserted().size() );
        assertEquals( 0,
                      sink.getModified().size() );
        assertEquals( 0,
                      sink.getRetracted().size() );
    }

    public void testAssertedNotAllowedThenModifyAllowed() throws FactException {
        final MockEvalCondition eval = new MockEvalCondition( false );

        // Create a test node that always returns false 
        final EvalConditionNode node = new EvalConditionNode( 1,
                                                              new MockTupleSource( 15 ),
                                                              eval );

        final MockTupleSink sink = new MockTupleSink();
        node.addTupleSink( sink );

        // Create the Tuple
        final DefaultFactHandle f0 = new DefaultFactHandle( 0,
                                                            "stilton" );
        final ReteTuple tuple0 = new ReteTuple( f0 );

        // Tuple should fail and not propagate
        node.assertTuple( tuple0,
                          this.context,
                          this.workingMemory );

        // Create the Tuple
        final DefaultFactHandle f1 = new DefaultFactHandle( 1,
                                                            "cheddar" );
        final ReteTuple tuple1 = new ReteTuple( f1 );

        // Tuple should fail and not propagate 
        node.assertTuple( tuple1,
                          this.context,
                          this.workingMemory );

        // Check memory was not populated
        final LinkedList memory = (LinkedList) this.workingMemory.getNodeMemory( node );

        assertEquals( 0,
                      memory.size() );

        eval.setIsAllowed( true );

        // Now test that the fact is modified correctly
        node.modifyTuple( tuple1,
                          this.context,
                          this.workingMemory );
        assertEquals( 1,
                      memory.size() );

        // As this this  wasn't asserted and remember before, will propagate as an assert
        assertEquals( 1,
                      sink.getAsserted().size() );

        assertSame( tuple1,
                    memory.getFirst() );

        // Now test that the fact is modified correctly
        node.modifyTuple( tuple0,
                          this.context,
                          this.workingMemory );
        assertEquals( 2,
                      memory.size() );

        // As this this  wasn't asserted and remember before, will propagate as an assert
        assertEquals( 2,
                      sink.getAsserted().size() );

        assertSame( tuple1,
                    memory.getFirst() );

        assertSame( tuple0,
                    tuple1.getNext() );

        // test no propagations
        assertEquals( 0,
                      sink.getModified().size() );
        assertEquals( 0,
                      sink.getRetracted().size() );
    }

    public void testUpdateWithMemory() throws FactException {
        // If no child nodes have children then we need to re-process the left
        // and right memories
        // as a joinnode does not store the resulting tuples
        final ReteooWorkingMemory workingMemory = new ReteooWorkingMemory( new ReteooRuleBase() );

        // Creat the object source so we can detect the alphaNode telling it to
        // propate its contents
        final MockTupleSource source = new MockTupleSource( 1 );

        /* Create a test node that always returns true */
        final EvalConditionNode node = new EvalConditionNode( 1,
                                                              new MockTupleSource( 15 ),
                                                              new MockEvalCondition( true ) );

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

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

        final ReteTuple tuple1 = new ReteTuple( f0 );

        node.assertTuple( tuple1,
                          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 );
        node.addTupleSink( sink2 );
        assertLength( 0,
                      sink2.getAsserted() );

        node.updateNewNode( workingMemory,
                            this.context );

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

⌨️ 快捷键说明

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