Saturday, July 05, 2008

Drools Smooks Data Loader

Smooks is a powerful open source ETL tool, it can transform variety of data sources.



Drools now supports an internal model, so ideally you want to be able to load different payloads, such as XML, into this model. I've just added support for this and it'll be in M2 :)

Here is an example of the api loading an XML file into a drools session, the xml entries for OrderItem are mapped into the internal class and inserted into the given session. The matching rules simple do a print statement.

declare OrderItem
productId : long
quantity : Integer
price : double
end

rule someRule
when
$i : OrderItem()
then
System.out.println( $i );
end


PackageBuilder pkgBuilder = new PackageBuilder();
pkgBuilder.addPackageFromDrl( new InputStreamReader( getClass().getResourceAsStream( "test.drl" )) );

RuleBase ruleBase = RuleBaseFactory.newRuleBase();
ruleBase.addPackage( pkgBuilder.getPackage() );

StatefulSession session = ruleBase.newStatefulSession();

// Instantiate Smooks with the config...
Smooks smooks = new Smooks( "smooks-config.xml" );

// set rood id
DroolsSmooksConfiguration conf = new DroolsSmooksConfiguration( "root" );
DroolsSmooks loader = new DroolsSmooks( session, smooks, conf );
loader.insertFilter( new StreamSource( new ByteArrayInputStream( readInputMessage() ) ) );

session.fireAllRules();

7 comments:

  1. Hi Mark, nice work!
    How is this internal model implementing equals and hashCode methods? Is it just inheriting them from Object class?
    Thank you.
    Michal

    ReplyDelete
  2. I think so. We probably need to allow that to be configurable.

    ReplyDelete
  3. What library (jar) are the Smooks dataloaders in? I want to try using Smooks to load my facts from an XML file based on the code you've listed here) but can't resolve the references in my code.

    Dave

    ReplyDelete
  4. best to ask these things on the drools user mailing list:

    http://www.jboss.org/drools/lists.html

    You'll want the binaries zip from here:
    http://www.jboss.org/drools/downloads.html

    Where you'll find a drools-transformer-smooks.jar and also in lib directory you'll see the smooks dependency itself.

    If you still struggle maybe try using a maven build system, and add the jboss repository http://repository.jboss.com/maven2 from which the smooks dataloader and all it's dependencies will be managed for you http://repository.jboss.com/maven2/org/drools/drools-transformer-smooks/5.0.0.M5

    ReplyDelete
  5. Sorry...next time I'll hit the mailing list ;)

    I did download the M5 binaries from http://www.jboss.org/drools/downloads.html, but I don't see drools-transformer-smooks.jar as one of the jars in the download, nor do I see the Smooks dependency in the lib directory, which is why I was wondering where this stuff was ;)

    ReplyDelete
  6. I just checked in the -M5.zip and you are right, it's not there. I'm guessing our maven assembly needs updating. you can still get it from the maven repository though. I'll make sure that gets fixed for the next release, thanks for bringing it to my attention.

    http://repository.jboss.org/maven2/org/drools/drools-transformer-smooks/5.0.0.M5/

    ReplyDelete
  7. Hi Mark,
    Very helpful info.
    I am having a small problem working with smooks though.
    I uploaded all the reuired dependencies for Smooks from the site www.smooks.org. But I am unable to resolve DroolsSmooksConfiguration.
    Could you help with all the required dependencies needed to work with smooks?? I am working with JBossDeveloperStudio 3.0.1
    Thanks.

    ReplyDelete