📄 logicalassertiontest.java
字号:
final Agenda agenda = workingMemory.getAgenda();
final Consequence consequence = new Consequence() {
/**
*
*/
private static final long serialVersionUID = -2467227987792388019L;
public void evaluate(KnowledgeHelper knowledgeHelper,
WorkingMemory workingMemory) {
// do nothing
}
};
// Create first justifier
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 );
// get the activation onto the agenda
node.assertTuple( tuple1,
context1,
workingMemory );
// Create the second justifer
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 );
final PropagationContext context2 = new PropagationContextImpl( 0,
PropagationContext.ASSERTION,
null,
null );
// get the activations onto the agenda
node2.assertTuple( tuple2,
context2,
workingMemory );
// Create the first justifieable relationship
final String logicalString1 = new String( "logical" );
final FactHandle logicalHandle1 = workingMemory.assertObject( logicalString1,
false,
true,
rule1,
tuple1.getActivation() );
// Create the second justifieable relationship
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() );
// Now lets cancel the first activation
node2.retractTuple( tuple2,
context2,
workingMemory );
workingMemory.propagateQueuedActions();
// because this logical fact has two relationships it shouldn't retract yet
assertLength( 0,
sink.getRetracted() );
// check "logical" is still in the system
assertLength( 1,
workingMemory.getTruthMaintenanceSystem().getJustifiedMap().values() );
// now remove that final justification
node.retractTuple( tuple1,
context1,
workingMemory );
workingMemory.propagateQueuedActions();
// Should cause the logical fact to be retracted
assertLength( 1,
sink.getRetracted() );
// "logical" fact should no longer be in the system
assertLength( 0,
workingMemory.getTruthMaintenanceSystem().getJustifiedMap().values() );
}
/**
* This tests that when multiple not identical, but equals facts, are asserted
* into WM, only when all are removed, a logical assert will succeed
*
* @throws Exception
*/
public void testMultipleAssert() 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 Agenda agenda = workingMemory.getAgenda();
final Consequence consequence = new Consequence() {
/**
*
*/
private static final long serialVersionUID = -8086689039438217146L;
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 );
// Assert multiple stated objects
node.assertTuple( tuple1,
context1,
workingMemory );
final String statedString1 = new String( "logical" );
final FactHandle statedHandle1 = workingMemory.assertObject( statedString1 );
final String statedString2 = new String( "logical" );
final FactHandle statedHandle2 = workingMemory.assertObject( statedString2 );
// This assertion is logical should fail as there is previous stated objects
final String logicalString3 = new String( "logical" );
FactHandle logicalHandle3 = workingMemory.assertObject( logicalString3,
false,
true,
rule1,
tuple1.getActivation() );
// Checks that previous LogicalAssert failed
assertNull( logicalHandle3 );
// If assert behavior in working memory is IDENTITY,
// we need to retract object 2 times before being able to
// succesfully logically assert a new fact
if ( RuleBaseConfiguration.WM_BEHAVIOR_IDENTITY.equals( ((ReteooRuleBase) ruleBase).getConfiguration().getProperty( RuleBaseConfiguration.PROPERTY_ASSERT_BEHAVIOR ) ) ) {
workingMemory.retractObject( statedHandle2 );
logicalHandle3 = workingMemory.assertObject( logicalString3,
false,
true,
rule1,
tuple1.getActivation() );
// Checks that previous LogicalAssert failed
assertNull( logicalHandle3 );
}
workingMemory.retractObject( statedHandle1 );
logicalHandle3 = workingMemory.assertObject( logicalString3,
false,
true,
rule1,
tuple1.getActivation() );
// Checks that previous LogicalAssert succeeded as there are no more
// stated strings in the working memory
assertNotNull( logicalHandle3 );
}
/**
* This test checks that truth maintenance is correctly maintained for modified objects
*/
public void testMutableObject() {
// 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 Agenda agenda = workingMemory.getAgenda();
final Consequence consequence = new Consequence() {
/**
*
*/
private static final long serialVersionUID = 2679144678701462458L;
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 Cheese cheese = new Cheese( "brie",
10 );
final FactHandle cheeseHandle = workingMemory.assertObject( cheese,
false,
true,
rule1,
tuple1.getActivation() );
cheese.setType( "cheddar" );
cheese.setPrice( 20 );
assertEquals( 1,
workingMemory.getTruthMaintenanceSystem().getJustifiedMap().size() );
assertEquals( 1,
workingMemory.getTruthMaintenanceSystem().getAssertMap().size() );
workingMemory.retractObject( cheeseHandle );
assertEquals( 0,
workingMemory.getTruthMaintenanceSystem().getJustifiedMap().size() );
assertEquals( 0,
workingMemory.getTruthMaintenanceSystem().getAssertMap().size() );
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -