@Test
public void testBinaryPackageCompileAndExecuteWithBRXML() throws Exception {
RulesRepository repo = rulesRepository;
// create our package
ModuleItem pkg = repo.createModule( "testBinaryPackageCompileBRL",
"" );
DroolsHeader.updateDroolsHeader( "import org.drools.Person",
pkg );
AssetItem rule2 = pkg.addAsset( "rule2",
"" );
rule2.updateFormat( AssetFormats.BUSINESS_RULE );
RuleModel model = new RuleModel();
model.name = "rule2";
FactPattern pattern = new FactPattern( "Person" );
SingleFieldConstraint con = new SingleFieldConstraint();
con.setConstraintValueType( BaseSingleFieldConstraint.TYPE_PREDICATE );
con.setValue( "name soundslike 'foobar'" );
pattern.addConstraint( con );
pattern.setBoundName( "p" );
ActionSetField action = new ActionSetField( "p" );
ActionFieldValue value = new ActionFieldValue( "age",
"42",
SuggestionCompletionEngine.TYPE_NUMERIC_INTEGER );
action.addFieldValue( value );
model.addLhsItem( pattern );
model.addRhsItem( action );
rule2.updateContent( BRXMLPersistence.getInstance().marshal( model ) );
rule2.checkin( "" );
repo.save();
BuilderResult result = repositoryPackageService.buildPackage( pkg.getUUID(),
true );
if ( result != null ) {
for ( int i = 0; i < result.getLines().size(); i++ ) {
System.err.println( result.getLines().get( i ).getMessage() );
}
}
assertFalse( result.hasLines() );
pkg = repo.loadModule( "testBinaryPackageCompileBRL" );
byte[] binPackage = pkg.getCompiledBinaryBytes();
assertNotNull( binPackage );
Package[] binPkgs = (Package[]) DroolsStreamUtils.streamIn( binPackage );
assertNotNull( binPkgs );
assertEquals( 1,
binPkgs.length );
Package binPkg = binPkgs[0];
assertNotNull( binPkg );
assertTrue( binPkg.isValid() );
// and this shows off the "soundex" thing...
Person p = new Person( "fubar" );
KnowledgeBuilder kb = KnowledgeBuilderFactory.newKnowledgeBuilder();
kb.add( new InputStreamResource( new ByteArrayInputStream( binPackage ) ),
ResourceType.PKG );
KnowledgeBase kbase = kb.newKnowledgeBase();
StatelessKnowledgeSession sess = kbase.newStatelessKnowledgeSession();
sess.execute( p );
assertEquals( 42,
p.getAge() );
repositoryPackageService.createModuleSnapshot( "testBinaryPackageCompileBRL",
"SNAP1",
false,
"" ,false,"","","",false,"","",false,"");
pattern.setFactType( "PersonX" );
rule2.updateContent( BRXMLPersistence.getInstance().marshal( model ) );
rule2.checkin( "" );
result = repositoryPackageService.buildPackage( pkg.getUUID(),
true );
assertNotNull( result );
assertTrue( result.getLines().size() > 0 );
// assertEquals(2, results.length);
assertEquals( rule2.getName(),
result.getLines().get( 0 ).getAssetName() );
assertEquals( AssetFormats.BUSINESS_RULE,
result.getLines().get( 0 ).getAssetFormat() );
assertNotNull( result.getLines().get( 0 ).getMessage() );
assertEquals( rule2.getUUID(),
result.getLines().get( 0 ).getUuid() );
pkg = repo.loadModuleSnapshot( "testBinaryPackageCompileBRL",
"SNAP1" );
result = repositoryPackageService.buildPackage( pkg.getUUID(),
true );
assertFalse( result.hasLines() );
// check that the rule name in the model is being set
AssetItem asset2 = pkg.addAsset( "testSetRuleName",
"" );
asset2.updateFormat( AssetFormats.BUSINESS_RULE );
asset2.checkin( "" );
RuleModel model2 = new RuleModel();