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

📄 logicalassertiontest.java

📁 jboss规则引擎
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        logicalString1 = new String( "logical" );
        logicalHandle1 = this.workingMemory.assertObject( logicalString1,
                                                          false,
                                                          true,
                                                          null,
                                                          this.workingMemory.getAgenda().getActivations()[0] );
        // Already an equals object but not identity same, so will do nothing
        // and return null
        assertNull( logicalHandle1 );

        //        // Alreyad identify same so return previously assigned handle
        //        logicalHandle1 = this.workingMemory.assertObject( logicalString2,
        //                                                          false,
        //                                                          true,
        //                                                          null,
        //                                                          this.workingMemory.getAgenda().getActivations()[0] );
        //        // return the matched handle
        //        assertSame( logicalHandle2,
        //                    logicalHandle1 );
        //
        //        this.workingMemory.retractObject( handle1 );
        //
        //        // Should keep the same handle when overriding
        //        assertSame( logicalHandle1,
        //                    logicalHandle2 );
        //
        //        // so while new STATED assertion is equal
        //        assertEquals( logicalString1,
        //                      this.workingMemory.getObject( logicalHandle2 ) );
        //
        //        // they are not identity same
        //        assertNotSame( logicalString1,
        //                       this.workingMemory.getObject( logicalHandle2 ) );
        //
    }

    public void testRetract() throws Exception {
        final Rule rule1 = new Rule( "test-rule1" );
        // create the first agendaItem which will justify the fact "logical"
        rule1.setConsequence( this.consequence );

        final LeapsRule leapsRule1 = new LeapsRule( rule1,
                                                    new ArrayList(),
                                                    new ArrayList(),
                                                    new ArrayList(),
                                                    new ArrayList() );

        final LeapsFactHandle tuple1FactHandle = (LeapsFactHandle) this.workingMemory.assertObject( "tuple1 object" );
        final LeapsFactHandle tuple2FactHandle = (LeapsFactHandle) this.workingMemory.assertObject( "tuple2 object" );
        final LeapsFactHandle[] factHandlesTuple1 = new LeapsFactHandle[1];
        final LeapsFactHandle[] factHandlesTuple2 = new LeapsFactHandle[1];
        factHandlesTuple1[0] = tuple1FactHandle;
        factHandlesTuple2[0] = tuple2FactHandle;

        final PropagationContext context = new PropagationContextImpl( 0,
                                                                       PropagationContext.ASSERTION,
                                                                       rule1,
                                                                       null );
        LeapsTuple tuple1 = new LeapsTuple( factHandlesTuple1,
                                            leapsRule1,
                                            context );
        final LeapsTuple tuple2 = new LeapsTuple( factHandlesTuple2,
                                                  leapsRule1,
                                                  context );
        this.workingMemory.assertTuple( tuple1 );
        final Activation activation1 = this.workingMemory.getAgenda().getActivations()[0];

        // Assert the logical "logical" fact
        final String logicalString1 = new String( "logical" );
        final FactHandle logicalHandle1 = this.workingMemory.assertObject( logicalString1,
                                                                           false,
                                                                           true,
                                                                           rule1,
                                                                           activation1 );
        assertEquals( 3,
                      this.workingMemory.getObjects().size() );

        // create the second agendaItem to justify the "logical" fact
        final Rule rule2 = new Rule( "test-rule2" );
        rule2.setConsequence( this.consequence );
        final PropagationContext context2 = new PropagationContextImpl( 0,
                                                                        PropagationContext.ASSERTION,
                                                                        rule2,
                                                                        null );
        tuple1 = new LeapsTuple( factHandlesTuple2,
                                 leapsRule1,
                                 context2 );
        this.workingMemory.assertTuple( tuple1 );
        final Activation activation2 = this.workingMemory.getAgenda().getActivations()[1];
        //
        final String logicalString2 = new String( "logical" );
        final FactHandle logicalHandle2 = this.workingMemory.assertObject( logicalString2,
                                                                           false,
                                                                           true,
                                                                           rule1,
                                                                           activation2 );
        // "logical" should only appear once
        assertEquals( 3,
                      this.workingMemory.getObjects().size() );

        // retract the logical object
        this.workingMemory.retractObject( logicalHandle2 );

        // The logical object should never appear
        assertEquals( 2,
                      this.workingMemory.getObjects().size() );

    }

    public void testMultipleLogicalRelationships() throws FactException {
        final Rule rule1 = new Rule( "test-rule1" );
        // create the first agendaItem which will justify the fact "logical"
        rule1.setConsequence( this.consequence );

        final LeapsRule leapsRule1 = new LeapsRule( rule1,
                                                    new ArrayList(),
                                                    new ArrayList(),
                                                    new ArrayList(),
                                                    new ArrayList() );

        final LeapsFactHandle tuple1Fact = (LeapsFactHandle) this.workingMemory.assertObject( "tuple1 object" );
        final LeapsFactHandle tuple2Fact = (LeapsFactHandle) this.workingMemory.assertObject( "tuple2 object" );
        final LeapsFactHandle[] tuple1Handles = new LeapsFactHandle[1];
        final LeapsFactHandle[] tuple2Handles = new LeapsFactHandle[1];
        tuple1Handles[0] = tuple1Fact;
        tuple2Handles[0] = tuple2Fact;

        final PropagationContext context1 = new PropagationContextImpl( 0,
                                                                        PropagationContext.ASSERTION,
                                                                        rule1,
                                                                        null );
        final LeapsTuple tuple1 = new LeapsTuple( tuple1Handles,
                                                  leapsRule1,
                                                  context1 );
        this.workingMemory.assertTuple( tuple1 );
        final Activation activation1 = this.workingMemory.getAgenda().getActivations()[0];

        // Assert the logical "logical" fact
        final String logicalString1 = new String( "logical" );
        final FactHandle logicalHandle1 = this.workingMemory.assertObject( logicalString1,
                                                                           false,
                                                                           true,
                                                                           rule1,
                                                                           activation1 );

        // create the second agendaItem to justify the "logical" fact
        final Rule rule2 = new Rule( "test-rule2" );
        rule2.setConsequence( this.consequence );
        final PropagationContext context2 = new PropagationContextImpl( 0,
                                                                        PropagationContext.ASSERTION,
                                                                        rule2,
                                                                        null );
        final LeapsTuple tuple2 = new LeapsTuple( tuple2Handles,
                                                  leapsRule1,
                                                  context2 );
        this.workingMemory.assertTuple( tuple2 );
        // "logical" should only appear once
        final Activation activation2 = this.workingMemory.getAgenda().getActivations()[1];
        //
        final String logicalString2 = new String( "logical" );
        final FactHandle logicalHandle2 = this.workingMemory.assertObject( logicalString2,
                                                                           false,
                                                                           true,
                                                                           rule2,
                                                                           activation2 );

        assertEquals( 3,
                      this.workingMemory.getObjects().size() );
        //
        this.workingMemory.retractObject( tuple1Fact );
        // check "logical" is still in the system
        assertEquals( 2,
                      this.workingMemory.getObjects().size() );

        // now remove that final justification
        this.workingMemory.retractObject( tuple2Fact );
        // "logical" fact should no longer be in the system
        assertEquals( 0,
                      this.workingMemory.getObjects().size() );
    }
}

⌨️ 快捷键说明

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