📄 logicalassertiontest.java
字号:
// "equals" objects.
String logicalString2 = new String( "logical" );
FactHandle logicalHandle2 = workingMemory.assertObject( logicalString2 );
node.retractTuple( tuple1,
context1,
workingMemory );
assertLength( 0,
sink.getRetracted() );
// we override and discard the original logical object
assertSame( logicalHandle2,
logicalHandle1 );
// 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
node.assertTuple( tuple1,
context1,
workingMemory );
logicalString2 = new String( "logical" );
logicalHandle2 = workingMemory.assertObject( logicalString2 );
// This logical assertion will be ignored as there is already
// an equals STATED assertion.
logicalString1 = new String( "logical" );
logicalHandle1 = workingMemory.assertObject( logicalString1,
false,
true,
rule1,
tuple1.getActivation() );
assertNull( logicalHandle1 );
// Already identify same so return previously assigned handle
logicalHandle1 = workingMemory.assertObject( logicalString2,
false,
false,
rule1,
tuple1.getActivation() );
// return the matched handle
assertSame( logicalHandle2,
logicalHandle1 );
node.retractTuple( tuple1,
context1,
workingMemory );
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 ) );
}
/**
* This tests that Stated asserts always take precedent
*
* @throws Exception
*/
public void testStatedOverridePreserve() 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" );
final Rete rete = new Rete();
final ObjectTypeNode objectTypeNode = new ObjectTypeNode( 0,
new ClassObjectType( String.class ),
rete );
objectTypeNode.attach();
final MockObjectSink sink = new MockObjectSink();
objectTypeNode.addObjectSink( sink );
final TerminalNode node = new TerminalNode( 2,
new MockTupleSource( 2 ),
rule1 );
final RuleBaseConfiguration conf = new RuleBaseConfiguration();
conf.setProperty( RuleBaseConfiguration.PROPERTY_LOGICAL_OVERRIDE_BEHAVIOR,
RuleBaseConfiguration.WM_BEHAVIOR_PRESERVE );
final RuleBase ruleBase = new ReteooRuleBase( conf );
final ReteooWorkingMemory workingMemory = (ReteooWorkingMemory) ruleBase.newWorkingMemory();
final Agenda agenda = workingMemory.getAgenda();
final Consequence consequence = new Consequence() {
/**
*
*/
private static final long serialVersionUID = 4142527256796002354L;
public void evaluate(KnowledgeHelper knowledgeHelper,
WorkingMemory workingMemory) {
// do nothing
}
};
rule1.setConsequence( consequence );
final DefaultFactHandle handle1 = new DefaultFactHandle( 1,
"cheese" );
final ReteTuple tuple1 = new ReteTuple( handle1 );
final PropagationContext context1 = new PropagationContextImpl( 0,
PropagationContext.ASSERTION,
null,
null );
// Test that a STATED assertion overrides a logical assertion
node.assertTuple( tuple1,
context1,
workingMemory );
final String logicalString1 = new String( "logical" );
final FactHandle logicalHandle1 = workingMemory.assertObject( logicalString1,
false,
true,
rule1,
tuple1.getActivation() );
// This assertion is stated and should override any previous justified
// "equals" objects.
String logicalString2 = new String( "logical" );
FactHandle logicalHandle2 = workingMemory.assertObject( logicalString2 );
node.retractTuple( tuple1,
context1,
workingMemory );
assertLength( 0,
sink.getRetracted() );
assertNotSame( logicalHandle2,
logicalHandle1 );
// so while new STATED assertion is equal
assertEquals( workingMemory.getObject( logicalHandle1 ),
workingMemory.getObject( logicalHandle2 ) );
// they are not identity same
assertNotSame( workingMemory.getObject( logicalHandle1 ),
workingMemory.getObject( logicalHandle2 ) );
// Test that a logical assertion cannot override a STATED assertion
node.assertTuple( tuple1,
context1,
workingMemory );
logicalString2 = new String( "logical" );
logicalHandle2 = workingMemory.assertObject( logicalString2 );
}
public void testRetract() 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" );
final Rete rete = new Rete();
final ObjectTypeNode objectTypeNode = new ObjectTypeNode( 0,
new ClassObjectType( String.class ),
rete );
objectTypeNode.attach();
final MockObjectSink sink = new MockObjectSink();
objectTypeNode.addObjectSink( sink );
final TerminalNode node = new TerminalNode( 2,
new MockTupleSource( 2 ),
rule1 );
final RuleBase ruleBase = new ReteooRuleBase();
final ReteooWorkingMemory workingMemory = (ReteooWorkingMemory) ruleBase.newWorkingMemory();
final Consequence consequence = new Consequence() {
/**
*
*/
private static final long serialVersionUID = -3139752102258757978L;
public void evaluate(KnowledgeHelper knowledgeHelper,
WorkingMemory workingMemory) {
// do nothing
}
};
// create the first activation which will justify the fact "logical"
rule1.setConsequence( consequence );
final DefaultFactHandle handle1 = new DefaultFactHandle( 1,
"cheese" );
final ReteTuple tuple1 = new ReteTuple( handle1 );
final PropagationContext context = new PropagationContextImpl( 0,
PropagationContext.ASSERTION,
null,
null );
node.assertTuple( tuple1,
context,
workingMemory );
// Assert the logical "logical" fact
final String logicalString1 = new String( "logical" );
final FactHandle logicalHandle1 = workingMemory.assertObject( logicalString1,
false,
true,
rule1,
tuple1.getActivation() );
// create the second activation to justify the "logical" fact
final Rule rule2 = new Rule( "test-rule2" );
final TerminalNode node2 = new TerminalNode( 4,
new MockTupleSource( 3 ),
rule2 );
rule2.setConsequence( consequence );
final DefaultFactHandle handle2 = new DefaultFactHandle( 2,
"cheese" );
final ReteTuple tuple2 = new ReteTuple( handle2 );
node.assertTuple( tuple2,
context,
workingMemory );
node2.assertTuple( tuple2,
context,
workingMemory );
// Assert the logical "logical" fact
final String logicalString2 = new String( "logical" );
final FactHandle logicalHandle2 = workingMemory.assertObject( logicalString2,
false,
true,
rule2,
tuple2.getActivation() );
// "logical" should only appear once
assertLength( 1,
workingMemory.getTruthMaintenanceSystem().getJustifiedMap().values() );
// retract the logical object
workingMemory.retractObject( logicalHandle2 );
// The logical object should never appear
assertLength( 0,
workingMemory.getTruthMaintenanceSystem().getJustifiedMap().values() );
}
public void testMultipleLogicalRelationships() throws FactException {
final Rule rule1 = new Rule( "test-rule1" );
final ReteooRuleBase ruleBase = new ReteooRuleBase();
final Rete rete = ruleBase.getRete();
// Create a RuleBase with a single ObjectTypeNode we attach a
// MockObjectSink so we can detect assertions and retractions
final ObjectTypeNode objectTypeNode = new ObjectTypeNode( 1,
new ClassObjectType( String.class ),
rete );
objectTypeNode.attach();
final MockObjectSink sink = new MockObjectSink();
objectTypeNode.addObjectSink( sink );
final TerminalNode node = new TerminalNode( 2,
new MockTupleSource( 2 ),
rule1 );
final ReteooWorkingMemory workingMemory = (ReteooWorkingMemory) ruleBase.newWorkingMemory();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -