// test rulebase serialization
ruleBase = SerializationHelper.serializeObject( ruleBase );
// Retrieve the generated fact type
FactType cheeseFact = ruleBase.getFactType( "org.drools.generatedbeans.Cheese" );
// Create a new Fact instance
Object cheese = cheeseFact.newInstance();
// Set a field value using the more verbose method chain...
// should we add short cuts?
// cheeseFact.getField( "type" ).getFieldAccessor().setValue( cheese,
// "stilton" );
cheeseFact.set( cheese,
"type",
"stilton" );
assertEquals( "stilton",
cheeseFact.get( cheese,
"type" ) );
FactType personType = ruleBase.getFactType( "org.drools.generatedbeans.Person" );
Object ps = personType.newInstance();
personType.set( ps,
"age",
42 );
Map<String, Object> personMap = personType.getAsMap( ps );
assertEquals( 42,
personMap.get( "age" ) );
personMap.put( "age",
43 );
personType.setFromMap( ps,
personMap );
assertEquals( 43,
personType.get( ps,
"age" ) );
// just documenting toString() result:
// assertEquals( "Cheese( type=stilton )",
// cheese.toString() );
// reading the field attribute, using the method chain
assertEquals( "stilton",
cheeseFact.getField( "type" ).getFieldAccessor().getValue( cheese ) );
// creating a stateful session
StatefulSession wm = ruleBase.newStatefulSession();
Object cg = cheeseFact.newInstance();
wm.setGlobal("cg", cg);
List result = new ArrayList();
wm.setGlobal( "list",
result );
// inserting fact
wm.insert( cheese );
// firing rules
wm.fireAllRules();
// checking results
assertEquals( 1,
result.size() );
assertEquals( new Integer( 5 ),
result.get( 0 ) );
// creating a person that likes the cheese:
// Retrieve the generated fact type
FactType personFact = ruleBase.getFactType( "org.drools.generatedbeans.Person" );
// Create a new Fact instance
Object person = personFact.newInstance();
// Set a field value using the more verbose method chain...
// should we add short cuts?
personFact.getField( "likes" ).getFieldAccessor().setValue( person,
cheese );
// demonstrating primitive type support
personFact.getField( "age" ).getFieldAccessor().setIntValue( person,
7 );
// just documenting toString() result:
// assertEquals( "Person( age=7, likes=Cheese( type=stilton ) )",
// person.toString() );