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

📄 joinnodetest.java

📁 drools 一个开放源码的规则引擎
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package org.drools.reteoo;

import org.drools.AssertionException;
import org.drools.DroolsTestCase;
import org.drools.FactHandle;
import org.drools.RetractionException;
import org.drools.rule.BooleanConstraint;
import org.drools.rule.Declaration;
import org.drools.rule.Rule;
import org.drools.spi.BooleanExpressionConstraint;
import org.drools.spi.ClassObjectType;
import org.drools.spi.Extractor;
import org.drools.spi.ObjectType;
import org.drools.spi.PropagationContext;
import org.drools.spi.Tuple;

public class JoinNodeTest extends DroolsTestCase
{
    Rule               rule;
    PropagationContext context;
    WorkingMemoryImpl  workingMemory;
    MockObjectSource   objectSource;
    MockTupleSource    tupleSource;
    MockTupleSink      sink;
    BetaNode           node;
    BetaMemory         memory;

    /**
     * Setup the BetaNode used in each of the tests
     */
    public void setUp()
    {
        this.rule = new Rule( "test-rule" );
        this.context = new PropagationContextImpl( PropagationContext.ASSERTION,
                                               null,
                                               null );
        this.workingMemory = new WorkingMemoryImpl( new RuleBaseImpl( new Rete() ) );

        this.tupleSource = new MockTupleSource( 4 );
        this.objectSource = new MockObjectSource( 4 );
        this.sink = new MockTupleSink();

        this.node = new JoinNode( 15,
                                  tupleSource,
                                  objectSource,
                                  5 );

        node.addTupleSink( sink );

        this.memory = (BetaMemory) workingMemory.getNodeMemory( node );

        /* check memories are empty */
        assertEquals( 0,
                      memory.leftMemorySize() );
        assertEquals( 0,
                      memory.rightMemorySize() );

    }

    public void testAttach() throws Exception
    {
        assertEquals( 15,
                      node.getId() );

        assertLength( 0,
                      objectSource.getObjectSinks() );

        assertLength( 0,
                      tupleSource.getTupleSinks() );

        node.attach();

        assertLength( 1,
                      objectSource.getObjectSinks() );

        assertLength( 1,
                      tupleSource.getTupleSinks() );

        assertSame( node,
                    objectSource.getObjectSinks().get( 0 ) );

        assertSame( node,
                    tupleSource.getTupleSinks().get( 0 ) );
    }

    /**
     * Test just tuple assertions
     * 
     * @throws AssertionException
     */
    public void testAssertTuple() throws Exception
    {
        FactHandleImpl f0 = new FactHandleImpl( 0 );
        ReteTuple tuple1 = new ReteTuple( 0,
                                          f0,
                                          workingMemory );

        /* assert tuple, should add one to left memory */
        node.assertTuple( tuple1,
                          context,
                          workingMemory );
        assertEquals( 1,
                      memory.leftMemorySize() );
        assertEquals( 0,
                      memory.rightMemorySize() );

        /* tuple already exists, so memory shouldn't increase */
        node.assertTuple( tuple1,
                          context,
                          workingMemory );
        assertEquals( 1,
                      memory.leftMemorySize() );

        /* check new tuples still assert */
        FactHandleImpl f1 = new FactHandleImpl( 1 );
        ReteTuple tuple2 = new ReteTuple( 0,
                                          f1,
                                          workingMemory );
        node.assertTuple( tuple2,
                          context,
                          workingMemory );
        assertEquals( 2,
                      memory.leftMemorySize() );

        /* make sure there have been no matches or propagation */
        TupleMatches betaMemory1 = memory.getBetaMemory( tuple1.getKey() );
        TupleMatches betaMemory2 = memory.getBetaMemory( tuple2.getKey() );
        assertLength( 0,
                      betaMemory1.getMatches() );
        assertLength( 0,
                      betaMemory2.getMatches() );
        assertLength( 0,
                      sink.getAsserted() );
    }

    /**
     * Test just object assertions
     * 
     * @throws Exception
     */
    public void testAssertObject() throws Exception
    {
        FactHandleImpl f0 = new FactHandleImpl( 0 );

        /* assert tuple, should add one to left memory */
        node.assertObject( "test2",
                           f0,
                           context,
                           workingMemory );
        assertEquals( 0,
                      memory.leftMemorySize() );
        assertEquals( 1,
                      memory.rightMemorySize() );

        /* object/handle already exists, so memory shouldn't increase */
        node.assertObject( "test0",
                           f0,
                           context,
                           workingMemory );
        assertEquals( 1,
                      memory.rightMemorySize() );

        /* check new objects/handles still assert */
        FactHandleImpl f1 = new FactHandleImpl( 1 );
        node.assertObject( "test1",
                           f1,
                           context,
                           workingMemory );
        assertEquals( 2,
                      memory.rightMemorySize() );

        /* make sure there have been no left memory increases or propagation */
        assertEquals( 0,
                      memory.leftMemorySize() );
        assertLength( 0,
                      sink.getAsserted() );
    }

    /**
     * Test assertion with both Objects and Tuples
     * 
     * @throws Exception
     */
    public void testAssertPropagations() throws Exception
    {

        /* Assert first tuple */
        FactHandleImpl f0 = new FactHandleImpl( 0 );
        ReteTuple tuple1 = new ReteTuple( 0,
                                          f0,
                                          workingMemory );
        node.assertTuple( tuple1,
                          context,
                          workingMemory );
        TupleMatches betaMemory1 = memory.getBetaMemory( tuple1.getKey() );

        /* Assert second tuple */
        FactHandleImpl f1 = new FactHandleImpl( 0 );
        ReteTuple tuple2 = new ReteTuple( 1,
                                          f1,
                                          workingMemory );
        node.assertTuple( tuple2,
                          context,
                          workingMemory );
        TupleMatches betaMemory2 = memory.getBetaMemory( tuple2.getKey() );

        /* Assert an object and make sure we get matches and propogations */
        FactHandleImpl f2 = new FactHandleImpl( 2 );
        node.assertObject( "test1",
                           f2,
                           context,
                           workingMemory );
        assertLength( 1,
                      betaMemory1.getMatches() );
        assertLength( 1,
                      betaMemory2.getMatches() );
        assertLength( 2,
                      sink.getAsserted() );

        /* Assert another tuple and make sure there was one propagation */
        FactHandleImpl f3 = new FactHandleImpl( 3 );
        ReteTuple tuple3 = new ReteTuple( 0,
                                          f3,
                                          workingMemory );

        node.assertTuple( tuple3,
                          context,
                          workingMemory );
        assertLength( 3,
                      sink.getAsserted() );

        /* Assert another object and make sure there were three propagations */
        FactHandleImpl f4 = new FactHandleImpl( 4 );
        node.assertObject( "test1",
                           f4,
                           context,
                           workingMemory );
        TupleMatches betaMemory3 = memory.getBetaMemory( tuple3.getKey() );

        assertLength( 2,
                      betaMemory1.getMatches() );
        assertLength( 2,
                      betaMemory2.getMatches() );
        assertLength( 2,
                      betaMemory3.getMatches() );
        assertLength( 6,
                      sink.getAsserted() );
    }

    /**
     * Test Tuple retraction
     * 
     * @throws Exception
     * @throws RetractionException
     */
    public void testRetractTuple() throws Exception,
                                  RetractionException
    {
        FactHandleImpl f0 = new FactHandleImpl( 0 );
        ReteTuple tuple1 = new ReteTuple( 0,
                                          f0,
                                          workingMemory );

        /* Shouldn't propagate as there are no matched tuple/facts */
        node.retractTuples( tuple1.getKey(),
                            context,
                            workingMemory );
        assertLength( 0,
                      sink.getRetracted() );

        /* assert tuple, should add one to left memory */
        node.assertTuple( tuple1,
                          context,
                          workingMemory );
        assertEquals( 1,
                      memory.leftMemorySize() );

        /*
         * Shouldn't propagate as still no matched tuple/facts. Although it will remove the asserted tuple
         */
        node.retractTuples( tuple1.getKey(),
                            context,
                            workingMemory );
        assertLength( 0,
                      sink.getRetracted() );
        assertEquals( 0,
                      memory.leftMemorySize() );
        assertEquals( 0,
                      memory.rightMemorySize() );
    }

    /**

⌨️ 快捷键说明

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