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

📄 logicalassertiontest.java

📁 jboss规则引擎
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package org.drools.leaps;

/*
 * Copyright 2005 JBoss Inc
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.ArrayList;

import org.drools.DroolsTestCase;
import org.drools.FactException;
import org.drools.FactHandle;
import org.drools.WorkingMemory;
import org.drools.common.PropagationContextImpl;
import org.drools.rule.Rule;
import org.drools.spi.Activation;
import org.drools.spi.Consequence;
import org.drools.spi.KnowledgeHelper;
import org.drools.spi.PropagationContext;

/**
 * @author Alexander Bagerman
 */
public class LogicalAssertionTest extends DroolsTestCase {

    Consequence        consequence;

    LeapsRuleBase      ruleBase;

    LeapsWorkingMemory workingMemory;

    public void setUp() throws Exception {
        super.setUp();
        this.ruleBase = new LeapsRuleBase();
        this.workingMemory = (LeapsWorkingMemory) this.ruleBase.newWorkingMemory();
        this.consequence = new Consequence() {
            /**
             * 
             */
            private static final long serialVersionUID = -3536436180370109113L;

            public void evaluate(final KnowledgeHelper knowledgeHelper,
                                 final WorkingMemory workingMemory) {
                // do nothing
            }
        };
    }

    public void testEqualsMap() throws Exception {
        // create a RuleBase with a single ObjectTypeNode we attach a
        // MockObjectSink so we can detect assertions and retractions

        final Rule rule1 = new Rule( "test-rule1" );

        rule1.setConsequence( this.consequence );

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

        final LeapsFactHandle[] factHandles = new LeapsFactHandle[1];
        PropagationContext context1;
        LeapsTuple tuple1;

        final String logicalString1 = new String( "logical" );
        final FactHandle handle1 = ((LeapsFactHandleFactory) this.ruleBase.getFactHandleFactory()).newFactHandle( logicalString1 );
        context1 = new PropagationContextImpl( 1,
                                               PropagationContext.ASSERTION,
                                               rule1,
                                               null );
        factHandles[0] = (LeapsFactHandle) handle1;
        tuple1 = new LeapsTuple( factHandles,
                                 leapsRule1,
                                 context1 );
        this.workingMemory.assertTuple( tuple1 );
        FactHandle logicalHandle1 = this.workingMemory.assertObject( logicalString1,
                                                                     false,
                                                                     true,
                                                                     null,
                                                                     this.workingMemory.getAgenda().getActivations()[0] );

        final String logicalString2 = new String( "logical" );
        FactHandle logicalHandle2 = this.workingMemory.assertObject( logicalString2,
                                                                     false,
                                                                     true,
                                                                     rule1,
                                                                     this.workingMemory.getAgenda().getActivations()[0] );
        factHandles[0] = (LeapsFactHandle) logicalHandle2;
        tuple1 = new LeapsTuple( factHandles,
                                 leapsRule1,
                                 context1 );
        this.workingMemory.assertTuple( tuple1 );

        assertSame( logicalHandle1,
                    logicalHandle2 );

        // little sanity check using normal assert
        logicalHandle1 = this.workingMemory.assertObject( logicalString1 );
        logicalHandle2 = this.workingMemory.assertObject( logicalString2 );
        assertNotSame( logicalHandle1,
                       logicalHandle2 );

    }

    /**
     * This tests that Stated asserts always take precedent
     * 
     * @throws Exception
     */
    public void testStatedOverride() throws Exception {
        final Rule rule1 = new Rule( "test-rule1" );

        rule1.setConsequence( this.consequence );

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

        final LeapsFactHandle[] factHandles = new LeapsFactHandle[1];
        PropagationContext context1;
        LeapsTuple tuple1;

        String logicalString1 = new String( "logical" );
        final FactHandle handle1 = ((LeapsFactHandleFactory) this.ruleBase.getFactHandleFactory()).newFactHandle( logicalString1 );
        context1 = new PropagationContextImpl( 1,
                                               PropagationContext.ASSERTION,
                                               rule1,
                                               null );
        factHandles[0] = (LeapsFactHandle) handle1;
        tuple1 = new LeapsTuple( factHandles,
                                 leapsRule1,
                                 context1 );
        this.workingMemory.assertTuple( tuple1 );
        FactHandle logicalHandle1 = this.workingMemory.assertObject( logicalString1,
                                                                     false,
                                                                     true,
                                                                     null,
                                                                     this.workingMemory.getAgenda( )
                                                                                       .getActivations( )[0] );

        String logicalString2 = new String( "logical" );
        FactHandle logicalHandle2 = this.workingMemory.assertObject( logicalString2 );

        // 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 - not identity same - leaps can not store two objects if
        // handles are the same
        assertNotSame( logicalString1, this.workingMemory.getObject( logicalHandle2 ) );
        
        assertSame( logicalString2, this.workingMemory.getObject( logicalHandle2 ) );

        // Test that a logical assertion cannot override a STATED assertion
        factHandles[0] = (LeapsFactHandle) logicalHandle2;
        tuple1 = new LeapsTuple( factHandles,
                                 leapsRule1,
                                 context1 );
        this.workingMemory.assertTuple( tuple1 );

        logicalString2 = new String( "logical" );
        logicalHandle2 = this.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 + -