// test kbase serialization
kbase = SerializationHelper.serializeObject( kbase );
// Retrieve the generated fact type
FactType cheeseFact = kbase.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" ).set( cheese,
// "stilton" );
cheeseFact.set( cheese,
"type",
"stilton" );
assertEquals( "stilton",
cheeseFact.get( cheese,
"type" ) );
FactType personType = kbase.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" ).get( cheese ) );
// creating a stateful session
StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
Object cg = cheeseFact.newInstance();
ksession.setGlobal( "cg",
cg );
List<Object> result = new ArrayList<Object>();
ksession.setGlobal( "list",
result );
// inserting fact
ksession.insert( cheese );
// firing rules
ksession.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 = kbase.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" ).set( person,
cheese );
// demonstrating primitive type support
personFact.getField( "age" ).set( person,
7 );
// just documenting toString() result:
// assertEquals( "Person( age=7, likes=Cheese( type=stilton ) )",
// person.toString() );