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

📄 logicalassertiontest.java

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

import org.drools.DroolsTestCase;
import org.drools.FactException;
import org.drools.FactHandle;
import org.drools.RuleBase;
import org.drools.rule.Rule;
import org.drools.spi.Activation;
import org.drools.spi.ClassObjectType;
import org.drools.spi.Consequence;
import org.drools.spi.PropagationContext;

public class LogicalAssertionTest extends DroolsTestCase
{

    public void testSingleLogicalRelationship() throws Exception
    {
        /*
         * create a RuleBase with a single ObjectTypeNode we attach a MockObjectSink so we can detect assertions and retractions
         */
        Rete rete = new Rete();
        ObjectTypeNode objectTypeNode = rete.getOrCreateObjectTypeNode( new ClassObjectType( String.class ) );
        MockObjectSink sink = new MockObjectSink();
        objectTypeNode.addObjectSink( sink );
        RuleBase ruleBase = new RuleBaseImpl( rete );
        WorkingMemoryImpl workingMemory = (WorkingMemoryImpl) ruleBase.newWorkingMemory();

        final Agenda agenda = workingMemory.getAgenda();

        Consequence consequence = new Consequence() {
            public void invoke(Activation activation)
            {
                // do nothing
            }
        };
        final Rule rule1 = new Rule( "test-rule1" );
        rule1.setConsequence( consequence );

        FactHandleImpl handle1 = new FactHandleImpl( 1 );
        ReteTuple tuple1 = new ReteTuple( 0,
                                          handle1,
                                          workingMemory );

        final PropagationContext context1 = new PropagationContextImpl( PropagationContext.ASSERTION,
                                                                    null,
                                                                    null );

        /* Test single activation for a single logical assertions */
        agenda.addToAgenda( tuple1,
                            context1,
                            rule1 );
        ModuleImpl main = (ModuleImpl) agenda.getFocus();
        Activation activation1 = (Activation) main.getActivationQueue().get();

        String logicalString = new String ( "logical" );
        FactHandle logicalHandle = workingMemory.assertObject( logicalString,
                                                               false,
                                                               true,
                                                               rule1,
                                                               activation1 );
        agenda.removeFromAgenda( tuple1.getKey(),
                                 context1,
                                 rule1 );

        assertLength( 1,
                      sink.getRetracted() );

        Object[] values = (Object[]) sink.getRetracted().get( 0 );

        assertSame( logicalHandle,
                    values[0] );

        /*
         * Test single activation for a single logical assertions. This also tests that logical assertions live on after the related Activation has fired.
         */
        agenda.addToAgenda( tuple1,
                            context1,
                            rule1 );
        activation1 = (Activation) main.getActivationQueue().get();

        logicalHandle = workingMemory.assertObject( logicalString,
                                                    false,
                                                    true,
                                                    rule1,
                                                    activation1 );

        agenda.fireNextItem( null );

        agenda.removeFromAgenda( tuple1.getKey(),
                                 context1,
                                 rule1 );

        assertLength( 2,
                      sink.getRetracted() );

        values = (Object[]) sink.getRetracted().get( 1 );

        assertSame( logicalHandle,
                    values[0] );
    }

    public void testEqualsMap() throws Exception
    {
        /*
         * create a RuleBase with a single ObjectTypeNode we attach a MockObjectSink so we can detect assertions and retractions
         */
        Rete rete = new Rete();
        ObjectTypeNode objectTypeNode = rete.getOrCreateObjectTypeNode( new ClassObjectType( String.class ) );
        MockObjectSink sink = new MockObjectSink();
        objectTypeNode.addObjectSink( sink );
        RuleBase ruleBase = new RuleBaseImpl( rete );
        WorkingMemoryImpl workingMemory = (WorkingMemoryImpl) ruleBase.newWorkingMemory();

        final Agenda agenda = workingMemory.getAgenda();

        Consequence consequence = new Consequence() {
            public void invoke(Activation activation)
            {
                // do nothing
            }
        };
        final Rule rule1 = new Rule( "test-rule1" );
        rule1.setConsequence( consequence );

        FactHandleImpl handle1 = new FactHandleImpl( 1 );
        ReteTuple tuple1 = new ReteTuple( 0,
                                          handle1,
                                          workingMemory );

        final PropagationContext context1 = new PropagationContextImpl( PropagationContext.ASSERTION,
                                                                    null,
                                                                    null );

        /* Test single activation for a single logical assertions */
        agenda.addToAgenda( tuple1,
                            context1,
                            rule1 );
        ModuleImpl main = (ModuleImpl) agenda.getFocus();
        Activation activation1 = (Activation) main.getActivationQueue().get();

        String logicalString1 = new String( "logical" );
        FactHandle logicalHandle1 = workingMemory.assertObject( logicalString1,
                                                                false,
                                                                true,
                                                                rule1,
                                                                activation1 );

        String logicalString2 = new String( "logical" );
        FactHandle logicalHandle2 = workingMemory.assertObject( logicalString2,
                                                                false,
                                                                true,
                                                                rule1,
                                                                activation1 );

        assertSame( logicalHandle1,
                    logicalHandle2 );
        
        /* little sanity check using normal assert */
        logicalHandle1 = workingMemory.assertObject( logicalString1 );
        logicalHandle2 = workingMemory.assertObject( logicalString2 );
        assertNotSame( logicalHandle1,
                       logicalHandle2 );        
    }
    
    /**
     * This tests that Stated asserts always take precedent
     * @throws Exception
     */
    public void testStatedOverride() throws Exception
    {
        /*
         * create a RuleBase with a single ObjectTypeNode we attach a MockObjectSink so we can detect assertions and retractions
         */
        Rete rete = new Rete();
        ObjectTypeNode objectTypeNode = rete.getOrCreateObjectTypeNode( new ClassObjectType( String.class ) );
        MockObjectSink sink = new MockObjectSink();
        objectTypeNode.addObjectSink( sink );
        RuleBase ruleBase = new RuleBaseImpl( rete );
        WorkingMemoryImpl workingMemory = (WorkingMemoryImpl) ruleBase.newWorkingMemory();

        final Agenda agenda = workingMemory.getAgenda();

        Consequence consequence = new Consequence() {
            public void invoke(Activation activation)
            {
                // do nothing
            }
        };
        final Rule rule1 = new Rule( "test-rule1" );
        rule1.setConsequence( consequence );

        FactHandleImpl handle1 = new FactHandleImpl( 1 );
        ReteTuple tuple1 = new ReteTuple( 0,
                                          handle1,
                                          workingMemory );

        final PropagationContext context1 = new PropagationContextImpl( PropagationContext.ASSERTION,
                                                                    null,
                                                                    null );

        /* Test that a STATED assertion overrides a logical assertion */
        agenda.addToAgenda( tuple1,
                            context1,
                            rule1 );
        ModuleImpl main = (ModuleImpl) agenda.getFocus();
        Activation activation1 = (Activation) main.getActivationQueue().get();

        String logicalString1 = new String( "logical" );
        FactHandle logicalHandle1 = workingMemory.assertObject( logicalString1,
                                                               false,
                                                               true,
                                                               rule1,
                                                               activation1 );
        
        /* This assertion is stated and should override any previous justified
         * "equals" objects. 
         */
        String logicalString2 = new String( "logical" );
        FactHandle logicalHandle2 = workingMemory.assertObject( logicalString2 );
        
        agenda.removeFromAgenda( tuple1.getKey(),
                                 context1,
                                 rule1 );
        
        assertLength( 0,
                      sink.getRetracted() );  
        
        /* Should keep the same handle when overriding */
        assertSame( logicalHandle1,
                    logicalHandle2 );               
        
        /* so while new STATED assertion is equal */
        assertEquals( logicalString1,
                      workingMemory.getObject( logicalHandle2 ) );
        /* they are not identity same */
        assertNotSame( logicalString1,
                       workingMemory.getObject( logicalHandle2 ) );
        
        
        /* Test that a logical assertion cannot override a STATED assertion */
        agenda.addToAgenda( tuple1,
                            context1,
                            rule1 );
        activation1 = (Activation) main.getActivationQueue().get();
        
        logicalString2 = new String( "logical" );
        logicalHandle2 = workingMemory.assertObject( logicalString2 );        

        /* This logical assertion will be ignored as there is already
         * an equals STATED assertion.

⌨️ 快捷键说明

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