📄 basemannerstest.java
字号:
package org.drools.examples.manners;
/*
* 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.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.StringTokenizer;
import junit.framework.TestCase;
import org.drools.WorkingMemory;
import org.drools.base.ClassFieldExtractor;
import org.drools.base.ClassObjectType;
import org.drools.base.EvaluatorFactory;
import org.drools.rule.BoundVariableConstraint;
import org.drools.rule.Column;
import org.drools.rule.Declaration;
import org.drools.rule.InvalidRuleException;
import org.drools.rule.LiteralConstraint;
import org.drools.rule.Not;
import org.drools.rule.Package;
import org.drools.rule.Rule;
import org.drools.spi.Consequence;
import org.drools.spi.ConsequenceException;
import org.drools.spi.Evaluator;
import org.drools.spi.FieldConstraint;
import org.drools.spi.FieldExtractor;
import org.drools.spi.FieldValue;
import org.drools.spi.KnowledgeHelper;
import org.drools.spi.MockField;
import org.drools.spi.Tuple;
public abstract class BaseMannersTest extends TestCase {
/** Number of guests at the dinner (default: 16). */
private final int numGuests = 16;
/** Number of seats at the table (default: 16). */
private final int numSeats = 16;
/** Minimum number of hobbies each guest should have (default: 2). */
private final int minHobbies = 2;
/** Maximun number of hobbies each guest should have (default: 3). */
private final int maxHobbies = 3;
protected Package pkg;
private ClassObjectType contextType;
private ClassObjectType guestType;
private ClassObjectType seatingType;
private ClassObjectType lastSeatType;
private ClassObjectType countType;
private ClassObjectType pathType;
private ClassObjectType chosenType;
private Evaluator objectEqualEvaluator;
private Evaluator objectNotEqualEvaluator;
private Evaluator integerEqualEvaluator;
private Evaluator integerNotEqualEvaluator;
private Evaluator booleanEqualEvaluator;
private Evaluator booleanNotEqualEvaluator;
protected void setUp() throws Exception {
this.contextType = new ClassObjectType( Context.class );
this.guestType = new ClassObjectType( Guest.class );
this.seatingType = new ClassObjectType( Seating.class );
this.lastSeatType = new ClassObjectType( LastSeat.class );
this.countType = new ClassObjectType( Count.class );
this.pathType = new ClassObjectType( Path.class );
this.chosenType = new ClassObjectType( Chosen.class );
this.integerEqualEvaluator = EvaluatorFactory.getInstance().getEvaluator( Evaluator.INTEGER_TYPE,
Evaluator.EQUAL );
this.integerNotEqualEvaluator = EvaluatorFactory.getInstance().getEvaluator( Evaluator.INTEGER_TYPE,
Evaluator.NOT_EQUAL );
this.objectEqualEvaluator = EvaluatorFactory.getInstance().getEvaluator( Evaluator.OBJECT_TYPE,
Evaluator.EQUAL );
this.objectNotEqualEvaluator = EvaluatorFactory.getInstance().getEvaluator( Evaluator.OBJECT_TYPE,
Evaluator.NOT_EQUAL );
this.booleanEqualEvaluator = EvaluatorFactory.getInstance().getEvaluator( Evaluator.BOOLEAN_TYPE,
Evaluator.EQUAL );
this.booleanNotEqualEvaluator = EvaluatorFactory.getInstance().getEvaluator( Evaluator.BOOLEAN_TYPE,
Evaluator.NOT_EQUAL );
this.pkg = new Package( "Miss Manners" );
this.pkg.addRule( getAssignFirstSeatRule() );
this.pkg.addRule( getFindSeating() );
this.pkg.addRule( getPathDone() );
this.pkg.addRule( getMakePath() );
this.pkg.addRule( getContinueProcessing() );
this.pkg.addRule( getAreWeDone() );
this.pkg.addRule( getAllDone() );
}
/**
* <pre>
* rule assignFirstSeat() {
* Context context;
* Guest guest;
* Count count;
* when {
* context : Context( state == Context.START_UP )
* guest : Guest()
* count : Count()
* } then {
* String guestName = guest.getName();
* drools.assert( new Seating( count.getValue(), 1, true, 1, guestName, 1, guestName) );
* drools.assert( new Path( count.getValue(), 1, guestName ) );
* count.setCount( count.getValue() + 1 );
*
* System.out.println( "seat 1 " + guest.getName() + " );
*
* context.setPath( Context.ASSIGN_SEATS );
* }
* }
* </pre>
*
*
* @return
* @throws IntrospectionException
* @throws InvalidRuleException
*/
private Rule getAssignFirstSeatRule() throws IntrospectionException,
InvalidRuleException {
final Rule rule = new Rule( "assignFirstSeat" );
// -----------
// context : Context( state == Context.START_UP )
// -----------
final Column contextColumn = new Column( 0,
this.contextType,
"context" );
contextColumn.addConstraint( getLiteralConstraint( contextColumn,
"state",
new Integer( Context.START_UP ),
this.integerEqualEvaluator ) );
rule.addPattern( contextColumn );
final Declaration contextDeclaration = rule.getDeclaration( "context" );
// -----------
// guest: Guest()
// -----------
final Column guestColumn = new Column( 1,
this.guestType,
"guest" );
rule.addPattern( guestColumn );
final Declaration guestDeclaration = rule.getDeclaration( "guest" );
// ------------
// count : Count()
// ------------
final Column countColumn = new Column( 2,
this.countType,
"count" );
rule.addPattern( countColumn );
final Declaration countDeclaration = rule.getDeclaration( "count" );
final Consequence consequence = new Consequence() {
public void evaluate(KnowledgeHelper drools,
WorkingMemory workingMemory) throws ConsequenceException {
try {
Rule rule = drools.getRule();
Tuple tuple = drools.getTuple();
Guest guest = (Guest) drools.get( guestDeclaration );
Context context = (Context) drools.get( contextDeclaration );
Count count = (Count) drools.get( countDeclaration );
String guestName = guest.getName();
Seating seating = new Seating( count.getValue(),
0,
true,
1,
guestName,
1,
guestName );
drools.assertObject( seating );
Path path = new Path( count.getValue(),
1,
guestName );
drools.assertObject( path );
count.setValue( count.getValue() + 1 );
drools.modifyObject( tuple.get( countDeclaration ),
count );
context.setState( Context.ASSIGN_SEATS );
drools.modifyObject( tuple.get( contextDeclaration ),
context );
System.out.println( "assign first seat : " + seating + " : " + path );
} catch ( Exception e ) {
e.printStackTrace();
throw new ConsequenceException( e );
}
}
};
rule.setConsequence( consequence );
return rule;
}
/**
* <pre>
* rule findSeating() {
* Context context;
* int seatingId, seatingPid;
* String seatingRightGuestName, leftGuestName;
* Sex rightGuestSex;
* Hobby rightGuestHobby;
* Count count;
*
* when {
* context : Context( state == Context.ASSIGN_SEATS )
* Seating( seatingId:id, seatingPid:pid, pathDone == true
* seatingRightSeat:rightSeat seatingRightGuestName:rightGuestName )
* Guest( name == seatingRightGuestName, rightGuestSex:sex, rightGuestHobby:hobby )
* Guest( leftGuestName:name , sex != rightGuestSex, hobby == rightGuestHobby )
*
* count : Count()
*
* not ( Path( id == seatingId, guestName == leftGuestName) )
* not ( Chosen( id == seatingId, guestName == leftGuestName, hobby == rightGuestHobby) )
* } then {
* int newSeat = rightSeat + 1;
* drools.assert( new Seating( coung.getValue(), rightSeat, rightSeatName, leftGuestName, newSeat, countValue, id, false );
* drools.assert( new Path( countValue, leftGuestName, newSeat );
* drools.assert( new Chosen( id, leftGuestName, rightGuestHobby ) );
*
* System.out.println( "seat " + rightSeat + " " + rightSeatName + " " + leftGuestName );
*
* count.setCount( countValue + 1 );
* context.setPath( Context.MAKE_PATH );
* }
* }
* </pre>
*
* @return
* @throws IntrospectionException
* @throws InvalidRuleException
*/
private Rule getFindSeating() throws IntrospectionException,
InvalidRuleException {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -