📄 basemannerstest.java
字号:
/**
* <pre>
* rule continue() {
* Context context;
* when {
* context : Context( state == Context.CHECK_DONE )
* } then {
* context.setState( Context.ASSIGN_SEATS );
* }
* }
* </pre>
* @return
* @throws IntrospectionException
* @throws InvalidRuleException
*/
private Rule getContinueProcessing() throws IntrospectionException,
InvalidRuleException {
final Rule rule = new Rule( "continueProcessng" );
// -----------
// context : Context( state == Context.CHECK_DONE )
// -----------
final Column contextColumn = new Column( 0,
this.contextType,
"context" );
contextColumn.addConstraint( getLiteralConstraint( contextColumn,
"state",
new Integer( Context.CHECK_DONE ),
this.integerEqualEvaluator ) );
rule.addPattern( contextColumn );
final Declaration contextDeclaration = rule.getDeclaration( "context" );
// ------------
// context.setName( Context.ASSIGN_SEATS );
// ------------
final Consequence consequence = new Consequence() {
public void evaluate(KnowledgeHelper drools,
WorkingMemory workingMemory) throws ConsequenceException {
try {
Rule rule = drools.getRule();
Tuple tuple = drools.getTuple();
Context context = (Context) drools.get( contextDeclaration );
context.setState( Context.ASSIGN_SEATS );
drools.modifyObject( tuple.get( contextDeclaration ),
context );
//System.out.println( "continue processing" );
} catch ( Exception e ) {
e.printStackTrace();
throw new ConsequenceException( e );
}
}
};
rule.setConsequence( consequence );
return rule;
}
/**
* <pre>
* rule all_done() {
* Context context;
* when {
* context : Context( state == Context.PRINT_RESULTS )
* } then {
* }
* }
* </pre>
*
* @return
* @throws IntrospectionException
* @throws InvalidRuleException
*/
private Rule getAllDone() throws IntrospectionException,
InvalidRuleException {
final Rule rule = new Rule( "alldone" );
// -----------
// context : Context( state == Context.PRINT_RESULTS )
// -----------
final Column contextColumn = new Column( 0,
this.contextType );
contextColumn.addConstraint( getLiteralConstraint( contextColumn,
"state",
new Integer( Context.PRINT_RESULTS ),
this.integerEqualEvaluator ) );
rule.addPattern( contextColumn );
final Declaration contextDeclaration = rule.getDeclaration( "context" );
// ------------
//
// ------------
final Consequence consequence = new Consequence() {
public void evaluate(KnowledgeHelper drools,
WorkingMemory workingMemory) throws ConsequenceException {
try {
System.err.println( "all done" );
} catch ( Exception e ) {
throw new ConsequenceException( e );
}
}
};
rule.setConsequence( consequence );
return rule;
}
/**
* Convert the facts from the <code>InputStream</code> to a list of
* objects.
*/
protected List getInputObjects(final InputStream inputStream) throws IOException {
final List list = new ArrayList();
final BufferedReader br = new BufferedReader( new InputStreamReader( inputStream ) );
String line;
while ( (line = br.readLine()) != null ) {
if ( line.trim().length() == 0 || line.trim().startsWith( ";" ) ) {
continue;
}
final StringTokenizer st = new StringTokenizer( line,
"() " );
final String type = st.nextToken();
if ( "guest".equals( type ) ) {
if ( !"name".equals( st.nextToken() ) ) {
throw new IOException( "expected 'name' in: " + line );
}
final String name = st.nextToken();
if ( !"sex".equals( st.nextToken() ) ) {
throw new IOException( "expected 'sex' in: " + line );
}
final String sex = st.nextToken();
if ( !"hobby".equals( st.nextToken() ) ) {
throw new IOException( "expected 'hobby' in: " + line );
}
final String hobby = st.nextToken();
final Guest guest = new Guest( name,
Sex.resolve( sex ),
Hobby.resolve( hobby ) );
list.add( guest );
}
if ( "last_seat".equals( type ) ) {
if ( !"seat".equals( st.nextToken() ) ) {
throw new IOException( "expected 'seat' in: " + line );
}
list.add( new LastSeat( new Integer( st.nextToken() ).intValue() ) );
}
if ( "context".equals( type ) ) {
if ( !"state".equals( st.nextToken() ) ) {
throw new IOException( "expected 'state' in: " + line );
}
list.add( new Context( st.nextToken() ) );
}
}
inputStream.close();
return list;
}
private InputStream generateData() {
final String LINE_SEPARATOR = System.getProperty( "line.separator" );
final StringWriter writer = new StringWriter();
final int maxMale = this.numGuests / 2;
final int maxFemale = this.numGuests / 2;
int maleCount = 0;
int femaleCount = 0;
// init hobbies
final List hobbyList = new ArrayList();
for ( int i = 1; i <= this.maxHobbies; i++ ) {
hobbyList.add( "h" + i );
}
final Random rnd = new Random();
for ( int i = 1; i <= this.numGuests; i++ ) {
char sex = rnd.nextBoolean() ? 'm' : 'f';
if ( sex == 'm' && maleCount == maxMale ) {
sex = 'f';
}
if ( sex == 'f' && femaleCount == maxFemale ) {
sex = 'm';
}
if ( sex == 'm' ) {
maleCount++;
}
if ( sex == 'f' ) {
femaleCount++;
}
final List guestHobbies = new ArrayList( hobbyList );
final int numHobbies = this.minHobbies + rnd.nextInt( this.maxHobbies - this.minHobbies + 1 );
for ( int j = 0; j < numHobbies; j++ ) {
final int hobbyIndex = rnd.nextInt( guestHobbies.size() );
final String hobby = (String) guestHobbies.get( hobbyIndex );
writer.write( "(guest (name n" + i + ") (sex " + sex + ") (hobby " + hobby + "))" + LINE_SEPARATOR );
guestHobbies.remove( hobbyIndex );
}
}
writer.write( "(last_seat (seat " + this.numSeats + "))" + LINE_SEPARATOR );
writer.write( LINE_SEPARATOR );
writer.write( "(context (state start))" + LINE_SEPARATOR );
return new ByteArrayInputStream( writer.getBuffer().toString().getBytes() );
}
public static int getIndex(final Class clazz,
final String name) throws IntrospectionException {
final PropertyDescriptor[] descriptors = Introspector.getBeanInfo( clazz ).getPropertyDescriptors();
for ( int i = 0; i < descriptors.length; i++ ) {
if ( descriptors[i].getName().equals( name ) ) {
return i;
}
}
return -1;
}
private FieldConstraint getLiteralConstraint(final Column column,
final String fieldName,
final Object fieldValue,
final Evaluator evaluator) throws IntrospectionException {
final Class clazz = ((ClassObjectType) column.getObjectType()).getClassType();
final FieldExtractor extractor = new ClassFieldExtractor( clazz,
fieldName );
final FieldValue field = new MockField( fieldValue );
return new LiteralConstraint( field,
extractor,
evaluator );
}
private void setFieldDeclaration(final Column column,
final String fieldName,
final String identifier) throws IntrospectionException {
final Class clazz = ((ClassObjectType) column.getObjectType()).getClassType();
final FieldExtractor extractor = new ClassFieldExtractor( clazz,
fieldName );
column.addDeclaration( identifier,
extractor );
}
private FieldConstraint getBoundVariableConstraint(final Column column,
final String fieldName,
final Declaration declaration,
final Evaluator evaluator) throws IntrospectionException {
final Class clazz = ((ClassObjectType) column.getObjectType()).getClassType();
final FieldExtractor extractor = new ClassFieldExtractor( clazz,
fieldName );
return new BoundVariableConstraint( extractor,
declaration,
evaluator );
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -