📄 evalconditionnodetest.java
字号:
package org.drools.reteoo;
/*
* 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 org.drools.DroolsTestCase;
import org.drools.FactException;
import org.drools.common.DefaultFactHandle;
import org.drools.common.PropagationContextImpl;
import org.drools.spi.PropagationContext;
import org.drools.util.LinkedList;
public class EvalConditionNodeTest extends DroolsTestCase {
private PropagationContext context;
private ReteooWorkingMemory workingMemory;
public EvalConditionNodeTest(final String name) {
super( name );
}
public void setUp() {
this.context = new PropagationContextImpl( 0,
PropagationContext.ASSERTION,
null,
null );
this.workingMemory = new ReteooWorkingMemory( new ReteooRuleBase() );
}
public void testAttach() throws Exception {
final MockTupleSource source = new MockTupleSource( 12 );
final EvalConditionNode node = new EvalConditionNode( 18,
source,
new MockEvalCondition( true ) );
assertEquals( 18,
node.getId() );
assertLength( 0,
source.getTupleSinks() );
node.attach();
assertLength( 1,
source.getTupleSinks() );
assertSame( node,
source.getTupleSinks().get( 0 ) );
}
public void testMemory() {
final ReteooWorkingMemory workingMemory = new ReteooWorkingMemory( new ReteooRuleBase() );
final MockTupleSource source = new MockTupleSource( 12 );
final EvalConditionNode node = new EvalConditionNode( 18,
source,
new MockEvalCondition( true ) );
final LinkedList memory = (LinkedList) workingMemory.getNodeMemory( node );
assertNotNull( memory );
}
/**
* If a eval allows an incoming Object, then the Object MUST be
* propagated. This tests that the memory is updated
*
* @throws FactException
*/
public void testAssertedAllowed() throws FactException {
final MockEvalCondition eval = new MockEvalCondition( true );
// Create a test node that always returns false
final EvalConditionNode node = new EvalConditionNode( 1,
new MockTupleSource( 15 ),
eval );
final MockTupleSink sink = new MockTupleSink();
node.addTupleSink( sink );
// Create the Tuple
final DefaultFactHandle f0 = new DefaultFactHandle( 0,
"stilton" );
final ReteTuple tuple0 = new ReteTuple( f0 );
// Tuple should pass and propagate
node.assertTuple( tuple0,
this.context,
this.workingMemory );
// Create the Tuple
final DefaultFactHandle f1 = new DefaultFactHandle( 1,
"cheddar" );
final ReteTuple tuple1 = new ReteTuple( f1 );
// Tuple should pass and propagate
node.assertTuple( tuple1,
this.context,
this.workingMemory );
// Check memory was populated
final LinkedList memory = (LinkedList) this.workingMemory.getNodeMemory( node );
assertEquals( 2,
memory.size() );
// Check list is in the correct order
assertEquals( tuple0,
memory.getFirst() );
assertEquals( tuple1,
tuple0.getNext() );
// make sure assertions were propagated
assertEquals( 2,
sink.getAsserted().size() );
}
public void testAssertedAllowedThenRetract() throws FactException {
final MockEvalCondition eval = new MockEvalCondition( true );
// Create a test node that always returns false
final EvalConditionNode node = new EvalConditionNode( 1,
new MockTupleSource( 15 ),
eval );
final MockTupleSink sink = new MockTupleSink();
node.addTupleSink( sink );
// Create the Tuple
final DefaultFactHandle f0 = new DefaultFactHandle( 0,
"stilton" );
final ReteTuple tuple0 = new ReteTuple( f0 );
// Tuple should pass and propagate
node.assertTuple( tuple0,
this.context,
this.workingMemory );
// we create and retract two tuples, checking the linkedtuples is null for JBRULES-246 "NPE on retract()"
// Create the Tuple
final DefaultFactHandle f1 = new DefaultFactHandle( 1,
"cheddar" );
final ReteTuple tuple1 = new ReteTuple( f1 );
// Tuple should pass and propagate
node.assertTuple( tuple1,
this.context,
this.workingMemory );
// Check memory was populated
final LinkedList memory = (LinkedList) this.workingMemory.getNodeMemory( node );
assertEquals( 2,
memory.size() );
assertEquals( tuple0,
memory.getFirst() );
assertEquals( tuple1,
tuple0.getNext() );
// make sure assertions were propagated
assertEquals( 2,
sink.getAsserted().size() );
// Now test that the fact is retracted correctly
node.retractTuple( tuple0,
this.context,
this.workingMemory );
// Now test that the fact is retracted correctly
assertEquals( 1,
memory.size() );
assertEquals( tuple1,
memory.getFirst() );
// make sure retractions were propagated
assertEquals( 1,
sink.getRetracted().size() );
// Now test that the fact is retracted correctly
node.retractTuple( tuple1,
this.context,
this.workingMemory );
// Now test that the fact is retracted correctly
assertEquals( 0,
memory.size() );
// make sure retractions were propagated
assertEquals( 2,
sink.getRetracted().size() );
}
public void testAssertedAllowedThenModifyAllowed() throws FactException {
final MockEvalCondition eval = new MockEvalCondition( true );
// Create a test node that always returns false
final EvalConditionNode node = new EvalConditionNode( 1,
new MockTupleSource( 15 ),
eval );
final MockTupleSink sink = new MockTupleSink();
node.addTupleSink( sink );
// Create the Tuple
final DefaultFactHandle f0 = new DefaultFactHandle( 0,
"stilton" );
final ReteTuple tuple0 = new ReteTuple( f0 );
// Tuple should pass and propagate
node.assertTuple( tuple0,
this.context,
this.workingMemory );
// we create and retract two tuples, checking the linkedtuples is null for JBRULES-246 "NPE on retract()"
// Create the Tuple
final DefaultFactHandle f1 = new DefaultFactHandle( 1,
"cheddar" );
final ReteTuple tuple1 = new ReteTuple( f1 );
// Tuple should pass and propagate
node.assertTuple( tuple1,
this.context,
this.workingMemory );
// Check memory was populated
final LinkedList memory = (LinkedList) this.workingMemory.getNodeMemory( node );
assertEquals( 2,
memory.size() );
assertEquals( tuple0,
memory.getFirst() );
assertEquals( tuple1,
tuple0.getNext() );
// make sure assertions were propagated
assertEquals( 2,
sink.getAsserted().size() );
// Now test that the fact is modified correctly
node.modifyTuple( tuple0,
this.context,
this.workingMemory );
assertEquals( 2,
memory.size() );
// notice the order is reversed, as tuple0 was modified last and is more recent
assertEquals( tuple1,
memory.getFirst() );
assertEquals( tuple0,
tuple1.getNext() );
// make sure modifications were propagated
assertEquals( 1,
sink.getModified().size() );
// Now test that the fact is modified correctly
node.modifyTuple( tuple1,
this.context,
this.workingMemory );
// notice the order is reversed, as tuple0 was modified last and is more recent
assertEquals( tuple0,
memory.getFirst() );
assertEquals( tuple1,
tuple0.getNext() );
// make sure modifications were propagated
assertEquals( 2,
sink.getModified().size() );
}
public void testAssertedAllowedThenModifyNotAllowed() throws FactException {
final MockEvalCondition eval = new MockEvalCondition( true );
// Create a test node that always returns false
final EvalConditionNode node = new EvalConditionNode( 1,
new MockTupleSource( 15 ),
eval );
final MockTupleSink sink = new MockTupleSink();
node.addTupleSink( sink );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -