Dynamic fact classes in JBoss Drools Saturday, Sep 26 2009 

When using dynamically created fact classes at run-time in JBoss Drools, always remember to import them before you can use them in your rules.

e.g.

import com.sample.Customer;

But remember that you have your dynamic classes created & loaded (depends which library you are using for creating dynamic classes). I used Javassist to create dynamic classes.

So using Javassist you just need to load the dynamically generated classes using:

ClassPool pool = ClassPool.getDefault();
CtClass cc = pool.get("Point");

If you try to load the rule file (.drl) before creating & loading dynamic class, the rules engine doesn’t
load the rules, and no rules are fired.

ASM vs. Javassist Tuesday, Sep 1 2009 

Javassist:

• Javassist (Java Programming Assistant) makes Java bytecode manipulation simple. It is a class library for editing bytecodes in Java; it enables Java programs to define a new class at runtime and to modify a class file when the JVM loads it. Unlike other similar bytecode editors
• Javassist provides two levels of API: source level and bytecode level. If the users use the source-level API, they can edit a class file without knowledge of the specifications of the Java bytecode. The whole API is designed with only the vocabulary of the Java language. You can even specify inserted bytecode in the form of source text; Javassist compiles it on the fly. On the other hand, the bytecode-level API allows the users to directly edit a class file as other editors.
• Javassist lets you inspect, edit, and create Java binary classes.
• Javassist isn’t the only library for working with bytecode, but it does have one feature in particular that makes it a great starting point for experimenting with classworking: you can use Javassist to alter the bytecode of a Java class without actually needing to learn anything about bytecode or the Java virtual machine (JVM) architecture.
• Aspect Oriented Programming: Javassist can be a good tool for adding new methods into a class and for inserting before/after/around advice at the both caller and callee sides.
• Reflection: One of applications of Javassist is runtime reflection; Javassist enables Java programs to use a metaobject that controls method calls on base-level objects. No specialized compiler or virtual machine are needed.
• Javassist also provides lower-level API for directly editing a class file. To use this level of API, you need detailed knowledge of the Java bytecode and the class file format while this level of API allows you any kind of modification of class files.

ASM:

• ASM is an all purpose Java bytecode manipulation and analysis framework. It can be used to modify existing classes or dynamically generate classes, directly in binary form. Provided common transformations and analysis algorithms allow to easily assembling custom complex transformations and code analysis tools.
• ASM offer similar functionality as other bytecode frameworks, but it is focused on simplicity of use and performance. Because it was designed and implemented to be as small and as fast as possible, it makes it very attractive for using in dynamic systems*.
• ASM is a Java class manipulation tool designed to dynamically generate and manipulate Java classes, which are useful techniques to implement adaptable systems. ASM is based on a new approach, compared to equivalent existing tools, which consists in using the “visitor” design pattern without explicitly representing the visited tree with objects. This new approach gives much better performances than those of existing tools, for most of practical needs.

Comparison between Javassist & ASM:

• Javassist source level API is much easier to use than the actual bytecode manipulation in ASM
• Javassist provides a higher level abstraction layer over complex bytecode level operations. Javassist source level API requires very less or no knowledge of actual bytecodes, so much easier & faster to implement.
• Javassist uses reflection mechanism which makes it slower compared to ASM which uses Classworking techniques at runtime.
• Overall ASM is much faster & gives better performance than Javassist. Javassist uses a simplified version of Java source code, which it then compiles into bytecode. That makes Javassist very easy to use, but it also limits the use of bytecode to what can be expressed within the limits of the Javassist source code.
• In conclusion, if someone needs easier way of dynamically manipulating or creating Java classes Javassist API should be used & where the performance is the key issue ASM library should be used.

Table 1. Class construction times

Framework        First time       Later times
Javassist             257                  5.2
BCEL                     473                  5.5
ASM                      62.4                 1.1
The Table 1 results show that ASM does live up to its billing as faster than the other frameworks, and this advantage applies both to startup time and to repeated uses.

References:

Classworking toolkit: ASM classworking, http://www.ibm.com/developerworks/java/library/j-cwt05125/index.html

Class transformation with Javassist, http://www.ibm.com/developerworks/java/library/j-dyn0916.html

ASM: a code manipulation tool to implement adaptable systems, http://asm.ow2.org/current/asm-eng.pdf

Javassist Website, http://www.csg.is.titech.ac.jp/~chiba/javassist/

ASM Website, http://asm.ow2.org/

JBoss Drools 5 dynamic rules creation Tuesday, Sep 1 2009 

In my previous post, i mentioned about creating dynamic rules using Drools API, but in Drools 5 version many classes have changed both structurally & conceptually.

So by the following way you can implement dynamic rules using Drools 5 API:


KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(kbase);

Resource resource = ResourceFactory.newClassPathResource("/Shipper.drl", DroolsTest.class);
kbuilder.add(resource, ResourceType.DRL);

// Rule as a string
String rule = "rule \"ss\"" +
"when " +
"$cust  : com.sample.Customer( region >= 1 ) " +
"$order : com.sample.Order( customer == $cust, totalAmount > limit ) " +
"then " +
"System.out.println(\"TotalAmount: \"+$order.getTotalAmount()+\" -> ship via FEDX \" ); end";

// Creating a Resource for string representation of the rule
Resource resource1 = ResourceFactory.newReaderResource((Reader) new StringReader(rule));
kbuilder.add(resource1, ResourceType.DRL);

// Adding rules dynamically
kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());

Javassist: Creating dynamic Java classes & methods on the fly!!! Friday, Jul 10 2009 

Presently working on an interesting scenario: creating java classes & methods dynamically..on the fly!!

The basic requirement is that ability to change the method signatures & business logic dynamically. Normally java developers don’t need to know what happens behind the scenes at bytecode level. But sometimes due to some business requirements it is necessary to modify the class definitions & it’s members at run-time.

For this there are many open source libraries (projects) available like: ASM, BCEL which directly talks to bytecodes generated by java compiler.

There is a sub-project of JBoss called Javassist which has two APIs available for same task:

1) Source level API (doesn’t need detailed knowledge of bytecodes..designed for java application developers)
2) Bytecode API (directly manipulates & does transformation into bytecodes…designed for expert programmers who know detailed structure of bytecodes).

Presently working on Source Level API as it is easier than the other one & practically does the same thing as Bytecode API.

The home page for Javassist is: http://www.csg.is.titech.ac.jp/~chiba/javassist/

IBM Developerworks has a great series dedicated to this area:

http://www.ibm.com/developerworks/java/library/j-dyn0429/

And those of you who are interested in having some knowledge of bytecodes must read this article:

http://www.ibm.com/developerworks/ibm/library/it-haggar_bytecode/

Cheers.

JBoss Drools dynamic fact classes Friday, Jul 3 2009 

I found this blog very useful in creating the facts dynamically in Drools engine.

http://blog.athico.com/2006/12/dynamically-generated-class-beans-as.html

The example used by this blog has a broken link. So i am giving you the updated link of web version of the JBoss dynamic rules example subversion:

http://anonsvn.jboss.org/repos/labs/labs/jbossrules/contrib/dynamic/src/

Cheers.

JBoss Drools dynamic rules at runtime Friday, Jul 3 2009 

While working on a research project which involves usage of JBoss Drools rule engine based on JSR-94 specifications, i needed to create rules dynamically.
Normally rules are defined in external files statically & they are read from the program. Creating rules dynamically is really easy with Drools.
e.g.


private static RuleBase readRule(PackageBuilder builder) throws Exception {

//read in the source
Reader source = new InputStreamReader( DroolsTest.class.getResourceAsStream( "/Shipper.drl" ) );

builder.addPackageFromDrl( source );

// Dynamic rule addition
String rule = "rule \"Shipper FEDX\" when $cust  : Customer( region >= 1 ) $order : Order( customer == $cust, totalAmount > limit ) then System.out.println(\"TotalAmount: \"+$order.getTotalAmount()+\" -> ship via FEDX \" ); end";

builder.addPackageFromDrl((Reader) new StringReader(rule));

//add the package to a rulebase (deploy the rule package).
RuleBase ruleBase = RuleBaseFactory.newRuleBase();

Package pkg = builder.getPackage();

ruleBase.addPackage(pkg);

return ruleBase;
}

Here main point is that:

Create Package instance after you add all of your rules and then use:

Package pkg = builder.getPackage();

WSO2 RemoteRegistry API security property settings Friday, Jun 26 2009 

Use:


System
.setProperty(
"javax.net.ssl.trustStore",
"C://client-truststore.jks");
System.setProperty("javax.net.ssl.trustStorePassword", "wso2carbon");
System.setProperty("javax.net.ssl.trustStoreType", "JKS");

where client-truststore.jks is the file located at: \wso2registry-2.0.1\resources\security

Also just make sure that you use trusttorePassword as “wso2carbon” & trustStoreType as “JKS”

Otherwise it throws some strange exceptions.

.NET client of a BPEL process deployed in ActiveBPEL engine interop issues Friday, Jun 26 2009 

While working on a research project under Dr. Minh Tran, i was required to create a .NET client of a BPEL process deployed in ActiveBPEL engine in Tomcat container as a Web Service.

The issue is that if you ues document/literal style encoding for your BPEL process WSDL then it simply doesn’t work in VS 2005 / 08 version ;-)

You need to change it to RPC style encoding for interoperability between java & .NET. It is against the WS-I guidelines, but still couldn’t figure out a proper way of achieving it.

I found this article very useful from IBM developerworks regarding interoperability issues between .NET & Java WS:

http://www.ibm.com/developerworks/webservices/library/ws-tip-j2eenet1/

http://www.ibm.com/developerworks/webservices/library/ws-tip-j2eenet2/

http://www.ibm.com/developerworks/webservices/library/ws-tip-j2eenet3/

WSO2 RemoteRegistry API dependency location Friday, Jun 26 2009 

On WSO2 Registry RemoteRegistry API Page at: http://wso2.org/project/registry/2.0.1/docs/wso2registry-2.0.1-docs/remote_api.html

it’s not mentioned clearly that where actually required libraries are located.

They are located at: wso2registry-2.0.1\webapps\ROOT\WEB-INF\plugins It took me several hours to figure it out, thanks to a forum post by a WSO2 employee.

Cheers.

ActiveBPEL designer dynamic endpoint references Friday, Jun 26 2009 

Working on a Research Project at Swinburne Uni. under Dr. Minh Tran, i was assgined a responsibity to implement a BPEL process using ActiveBPEL designer & engine.

Though this tool is intened to help developers save their time to write BPEL & WSDL files manually, sometimes small problems takes days to solve the issue with limited help provided on the net.

As ActiveBPEL is now changed to ActiveVOS they have gone from open source to a commercial product, and that is too acceptable as open source should be used to earn some money.

As they went commercial they give access to only registered customers & new registrations have been blocked. So google searches returns the results but only registered members can view them. But there is a way to view the posts in registered forums. Just click on cached link of such results and voila you can see all the posts.. ;-)

Now turning to real issue of dynamic binding which bugged me for 2 days.

I was supposed to use dynamic binding for assigning service endpoints at run-time & avoid using static URIs.

I used ActiveBPEL User Guide. They have a heading  “Copy Operation Dynamic Endpoint Reference Example”. Under that heading they have given an example that doesn’t work:


<assign>
<copy>
<from>
<wsa:EndpointReference xmlns:s="http://www.activeendpoints.com/wsdl/bpr-epr" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing">
<wsa:Address>anyURI</wsa:Address>
<wsa:ServiceName>tns:Service</wsa:ServiceName>
</wsa:EndpointReference>
</from>
<to partnerLink=”mypartnerLink”/>
</copy>
</assign>

What you should be using in your copy operation is:


<wsa:EndpointReference xmlns:s="http://swin.edu.au/" xmlns:wsa="http://schemas.xmlsoap.org/ws/2003/03/addressing">
<wsa:Address>$processInput.drivabilityCheckingService</wsa:Address>
<wsa:ServiceName PortName="DrivabilityCheckingServiceSoap">s:DrivabilityCheckingService</wsa:ServiceName>
</wsa:EndpointReference>

Which is then converted to BPEL as:


<bpel:assign>
<bpel:copy ignoreMissingFromData="yes">
<bpel:from>
<bpel:literal>
<wsa:EndpointReference xmlns:s="http://swin.edu.au/" xmlns:wsa="http://schemas.xmlsoap.org/ws/2003/03/addressing">
<wsa:Address>$processInput.drivabilityCheckingService</wsa:Address>
<wsa:ServiceName PortName="DrivabilityCheckingServiceSoap">s:DrivabilityCheckingService</wsa:ServiceName>
</wsa:EndpointReference>
</bpel:literal>
</bpel:from>
<bpel:to partnerLink="drivabilityCheckingServicePLT"/>
</bpel:copy>

I intentionally kept the URIs i actually used to get you an idea of how exactly it should be done which is not mentioned in the official user’s guide.

Follow

Get every new post delivered to your Inbox.