📄 queryterminalnodetest.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 java.lang.reflect.Field;
import java.util.Iterator;
import java.util.Map;
import junit.framework.Assert;
import junit.framework.TestCase;
import org.drools.FactHandle;
import org.drools.QueryResult;
import org.drools.QueryResults;
import org.drools.WorkingMemory;
import org.drools.base.ClassFieldExtractor;
import org.drools.base.ClassObjectType;
import org.drools.base.DroolsQuery;
import org.drools.base.EvaluatorFactory;
import org.drools.rule.LiteralConstraint;
import org.drools.rule.Query;
import org.drools.spi.Evaluator;
import org.drools.spi.FieldValue;
import org.drools.spi.MockField;
public class QueryTerminalNodeTest extends TestCase {
public void testQueryTerminalNode() {
final ReteooRuleBase ruleBase = new ReteooRuleBase();
final Rete rete = ruleBase.getRete();
final ClassObjectType queryObjectType = new ClassObjectType( DroolsQuery.class );
final ObjectTypeNode queryObjectTypeNode = new ObjectTypeNode( 1,
queryObjectType,
rete );
queryObjectTypeNode.attach();
ClassFieldExtractor extractor = new ClassFieldExtractor( DroolsQuery.class,
"name" );
FieldValue field = new MockField( "query-1" );
final Evaluator evaluator = EvaluatorFactory.getEvaluator( Evaluator.STRING_TYPE,
Evaluator.EQUAL );
LiteralConstraint constraint = new LiteralConstraint( field,
extractor,
evaluator );
AlphaNode alphaNode = new AlphaNode( 2,
constraint,
queryObjectTypeNode );
alphaNode.attach();
final LeftInputAdapterNode liaNode = new LeftInputAdapterNode( 3,
alphaNode );
liaNode.attach();
final ClassObjectType cheeseObjectType = new ClassObjectType( Cheese.class );
final ObjectTypeNode cheeseObjectTypeNode = new ObjectTypeNode( 4,
cheeseObjectType,
rete );
cheeseObjectTypeNode.attach();
extractor = new ClassFieldExtractor( Cheese.class,
"type" );
field = new MockField( "stilton" );
constraint = new LiteralConstraint( field,
extractor,
evaluator );
alphaNode = new AlphaNode( 5,
constraint,
cheeseObjectTypeNode );
alphaNode.attach();
final JoinNode joinNode = new JoinNode( 6,
liaNode,
alphaNode );
joinNode.attach();
final Query query = new Query( "query-1" );
final QueryTerminalNode queryNode = new QueryTerminalNode( 7,
joinNode,
query );
queryNode.attach();
final org.drools.rule.Package pkg = new org.drools.rule.Package("com.drools.test");
pkg.addRule( query );
try {
Field pkgField = ruleBase.getClass().getSuperclass().getDeclaredField( "pkgs" );
pkgField.setAccessible( true );
Map pkgs = (Map) pkgField.get( ruleBase );
pkgs.put( pkg.getName(), pkg );
} catch ( Exception e ) {
Assert.fail("Should not throw any exception: "+e.getMessage());
}
final WorkingMemory workingMemory = ruleBase.newWorkingMemory();
QueryResults results = workingMemory.getQueryResults( "query-1" );
assertEquals( 0,
results.size() );
final Cheese stilton1 = new Cheese( "stilton",
100 );
final FactHandle handle1 = workingMemory.assertObject( stilton1 );
results = workingMemory.getQueryResults( "query-1" );
assertEquals( 1,
results.size() );
final Cheese cheddar = new Cheese( "cheddar",
55 );
workingMemory.assertObject( cheddar );
results = workingMemory.getQueryResults( "query-1" );
assertEquals( 1,
results.size() );
final Cheese stilton2 = new Cheese( "stilton",
5 );
final FactHandle handle2 = workingMemory.assertObject( stilton2 );
results = workingMemory.getQueryResults( "query-1" );
assertEquals( 2,
results.size() );
QueryResult result = results.get( 0 );
assertEquals( 1,
result.size() );
assertSame( stilton1,
result.get( 0 ) );
result = results.get( 1 );
assertEquals( 1,
result.size() );
assertSame( stilton2,
result.get( 0 ) );
int i = 0;
for ( final Iterator it = results.iterator(); it.hasNext(); ) {
result = (QueryResult) it.next();
assertEquals( 1,
result.size() );
if ( i == 0 ) {
assertSame( stilton1,
result.get( 0 ) );
} else {
assertSame( stilton2,
result.get( 0 ) );
}
i++;
}
workingMemory.retractObject( handle1 );
results = workingMemory.getQueryResults( "query-1" );
assertEquals( 1,
results.size() );
workingMemory.retractObject( handle2 );
results = workingMemory.getQueryResults( "query-1" );
assertEquals( 0,
results.size() );
}
public class Cheese {
private String type;
private int price;
public Cheese(final String type,
final int price) {
super();
this.type = type;
this.price = price;
}
public int getPrice() {
return this.price;
}
public String getType() {
return this.type;
}
public String toString() {
return "[Cheese type='" + this.type + "' price='" + this.price + "']";
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -