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

📄 joinnodetest.java

📁 drools 一个开放源码的规则引擎
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
     * Test Object retraction
     * 
     * @throws Exception
     * @throws RetractionException
     */
    public void testRetractObject() throws Exception,
                                   RetractionException
    {
        FactHandleImpl f0 = new FactHandleImpl( 0 );

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

        /* assert object, should add one to right memory */
        node.assertObject( "test0",
                           f0,
                           context,
                           workingMemory );
        assertEquals( 1,
                      memory.rightMemorySize() );

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

    /**
     * Test retractions with both tuples and objects
     * 
     * @throws Exception
     * @throws RetractionException
     */
    public void testRetractPropagations() throws Exception,
                                         RetractionException
    {
        /* assert tuple */
        FactHandleImpl f0 = new FactHandleImpl( 0 );
        ReteTuple tuple1 = new ReteTuple( 0,
                                          f0,
                                          workingMemory );
        node.assertTuple( tuple1,
                          context,
                          workingMemory );

        /* assert object */
        FactHandleImpl f1 = new FactHandleImpl( 1 );
        node.assertObject( "test1",
                           f1,
                           context,
                           workingMemory );

        /* assert object */
        FactHandleImpl f2 = new FactHandleImpl( 2 );
        node.assertObject( "test2",
                           f2,
                           context,
                           workingMemory );

        /* should have three asserted propagations */
        assertLength( 2,
                      sink.getAsserted() );

        /* check zero retracted propatations */
        assertLength( 0,
                      sink.getRetracted() );

        /* retract an object, should have one retracted propagations */
        node.retractObject( f2,
                            context,
                            workingMemory );
        assertLength( 1,
                      sink.getRetracted() );

        /* retract a tuple, should have one retracted propagations */
        node.retractTuples( tuple1.getKey(),
                            context,
                            workingMemory );
        assertLength( 2,
                      sink.getRetracted() );

        /* should be zero left tuples and 1 right handle in memory */
        assertEquals( 0,
                      memory.leftMemorySize() );
        assertEquals( 1,
                      memory.rightMemorySize() );

        /* clear out the final right memory item */
        node.retractObject( f1,
                            context,
                            workingMemory );

        assertEquals( 0,
                      memory.rightMemorySize() );

    }

    /**
     * Test that a correct join results from propatation
     */
    public void testJoin() throws Exception
    {
        /* assert tuple */
        FactHandleImpl f0 = new FactHandleImpl( 0 );
        workingMemory.putObject( f0,
                                 "test0" );
        ReteTuple tuple1 = new ReteTuple( 2,
                                          f0,
                                          workingMemory );
        node.assertTuple( tuple1,
                          context,
                          workingMemory );

        /* assert object */
        FactHandleImpl f1 = new FactHandleImpl( 1 );
        workingMemory.putObject( f1,
                                 "test1" );
        node.assertObject( "test1",
                           f1,
                           context,
                           workingMemory );

        Object[] list = (Object[]) sink.getAsserted().get( 0 );
        ReteTuple joinedTuple = (ReteTuple) list[0];

        assertNull( joinedTuple.get( 1 ) );

        assertEquals( "test0",
                      joinedTuple.get( 2 ) );

        assertNull( joinedTuple.get( 3 ) );
        assertNull( joinedTuple.get( 4 ) );

        assertEquals( "test1",
                      joinedTuple.get( 5 ) );
    }

    /**
     * While all the previous tests work with the DefaultJoinNodeBinder, ie joins always succeed. This tests joins with BooleanExpressionConstraint. We only use one constraint, as Constraints are tested more thorougly else where, likewise for this
     * reason we use a very simple constraint.
     * 
     * @throws Exception
     * 
     */
    public void testJoinNodeWithConstraint() throws Exception
    {
        ObjectType stringObjectType = new ClassObjectType( String.class );

        /* just return the object */
        Extractor stringExtractor = new Extractor() {
            public Object getValue(Object object)
            {
                return object;
            }
        };

        /* Bind the extractor to a decleration */
        Declaration string1Declaration = new Declaration( 0,
                                                          "string1",
                                                          stringObjectType,
                                                          stringExtractor,
                                                          3 );

        /* Bind the extractor to a decleration */
        Declaration string2Declaration = new Declaration( 0,
                                                          "string2",
                                                          stringObjectType,
                                                          stringExtractor,
                                                          9 );

        /* create the boolean expression check */
        BooleanExpressionConstraint checkString = new BooleanExpressionConstraint() {
            public boolean isAllowed(Object object,
                                     FactHandle handle,
                                     Declaration declaration, // ?string1
                                     Declaration[] declarations, // ?string2
                                     Tuple tuple)
            {
                String string1 = (String) object;
                String string2 = (String) tuple.get( declarations[0] );

                return "string1string2".equals( string1 + string2 );

            }
        };

        /* create the constraint */
        BooleanConstraint constraint = new BooleanConstraint( checkString,
                                                              string1Declaration,
                                                              new Declaration[]{string2Declaration} );

        /* string1Declaration is bound to column 3 */
        this.node = new JoinNode( 15,
                                  new MockTupleSource( 5 ),
                                  new MockObjectSource( 8 ),
                                  3,
                                  new BetaNodeBinder( constraint ) );

        node.addTupleSink( sink );

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

        /* assert tuple */
        FactHandleImpl f0 = new FactHandleImpl( 0 );
        workingMemory.putObject( f0,
                                 "string2" );
        ReteTuple tuple1 = new ReteTuple( 9,
                                          f0,
                                          workingMemory );
        node.assertTuple( tuple1,
                          context,
                          workingMemory );

        /* assert object */
        FactHandleImpl f1 = new FactHandleImpl( 1 );
        String string1 = "string1";
        workingMemory.putObject( f1,
                                 string1 );
        node.assertObject( string1,
                           f1,
                           context,
                           workingMemory );

        /* Join should work */
        assertLength( 1,
                      sink.getAsserted() );

        Object[] list = (Object[]) sink.getAsserted().get( 0 );
        ReteTuple joinedTuple = (ReteTuple) list[0];
        assertEquals( "string1",
                      joinedTuple.get( 3 ) );

        assertEquals( "string2",
                      joinedTuple.get( 9 ) );

        /*
         * now check that constraint blocks these assertions /* assert tuple
         */
        FactHandleImpl f2 = new FactHandleImpl( 2 );
        workingMemory.putObject( f1,
                                 "string22" );
        ReteTuple tuple2 = new ReteTuple( 9,
                                          f2,
                                          workingMemory );
        node.assertTuple( tuple2,
                          context,
                          workingMemory );
        /* nothing extra should be asserted */
        assertLength( 1,
                      sink.getAsserted() );

        /* Although it will remember the tuple for possible future matches */
        assertEquals( 2,
                      memory.leftMemorySize() );
        assertEquals( 1,
                      memory.rightMemorySize() );

        /* assert object */
        FactHandleImpl f3 = new FactHandleImpl( 3 );
        String stringx = "stringx";
        workingMemory.putObject( f3,
                                 stringx );
        node.assertObject( stringx,
                           f3,
                           context,
                           workingMemory );
        /* nothing extra should be asserted */
        assertLength( 1,
                      sink.getAsserted() );

        /* Although it will remember the tuple for possible future matches */
        assertEquals( 2,
                      memory.leftMemorySize() );
        assertEquals( 2,
                      memory.rightMemorySize() );

    }

}

⌨️ 快捷键说明

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