Tom's Blog
Koen Aers jBPM EclipseWorld sneak preview
Published by Tom |
November 08, 2007 09:05 AM EST |
Even though
Koen Aers
from JBoss had to be up early Thursday to give a
jBPM presentation
at EclipseWorld 2007,
he kindly stopped by our Northern Virginia Java Users Group
(NovaJUG)
meeting Wednesday night to
talk about business process management in general and JBoss's
jBPM platform specifically.
Koen Aers presents on jBPM at
the NovaJUG meeting Nov. 7.
[photo from my phone]
Business process engines can make applications easier to write, but they have received a bad reputation, Aers said. The reputation stems from the fact that most business process management systems are behemoths that take up half your hard disk and come with a steep learning curve, he said. JBPM is about a 500KB core library, not counting its Hibernate database persistence layer, and developers can learn and use only a small part of the whole platform.
BPM engines don't need to be complex. At their core, he said, business process engines boil down to the management of state: what state is each instance of a business process in at the moment, and what internal or human activities trigger a transition to a new state.
Here are my notes from Koen Aers's jBPM presentation.
Why use a process language?
- Simplify an application by extracting the state-management logic.
- Improves communication: Process languages should support graphical modeling that maps to executable notation.
- Automatic persistence history can be used for business intelligence.
- A tool that allows an analyst to model workflows (business processes) and hand over results to a developer, who will add the details to make it executable.
- With modeling, the more expressive the modeling notation, the harder it is to make the model executable.
- Thus the choice of modeling notation is important.
Popular modeling notations:
BPMN: a pure modeling notation. No automatic translations to code.
BPEL: The purpose is to orchestrate web services and publish result as a new web service.
XPDL: A format for storing process models. - A big repository that holds executable processes, persists the execution state of the processes, and records history of what happened during the process executions.
JPDL is an XML language defined by a schema. The language is extensible to support custom business processes. The language also supports defining Java actions that can be invoked at numerous points as the business process changes states.
Aers showed a demo of coding a business process using JBoss's visual process designer, an Eclipse plugin. The plugin lets you edit the jPDL both as XML and visually. Aers is a developer for the designer tool.
Before he started working on the jBPM designer tool, Aers said, he would code jPDL using straight XML with an editor that supported auto-completion from the XSD. The designer is primarily a marketing tool, he said, to support people's expectations of what a powerful BPMS must provide. "If you go to a presentation and you don't have a [graphical] designer, then you suck" in the customer's view, he said.
Thursday November 08, 2007 Permalink
Comments [3]
Grails with Jason Rudolph
Published by Tom |
October 11, 2007 04:12 PM EDT |
Grails committer Jason Rudolph showed off the power of Grails at last night's
Northern Virginia Java Users Group
(NovaJUG)
using a technique guaranteed to impress.
He started with a JDK, a Grails installation, and an empty directory.
In a few minutes,
he had a skeletal Java web application created and functioning in a web browser,
ready for enhancement.
Audience members literally oohed and aahed.
Grails is a rapid web application development framework for Groovy that allows Java developers to hold their heads higher when Rails advocates gloat about their Ruby framework. Grails provides both a development environment to make coding easier by generating code, and an MVC web framework with a servlet front controller, domain objects that easily tie into a database, and tag libraries for scripting Groovy Server Pages. "It's absolutely everything you need, soup to nuts, to start building a web application," he said.
Grails is in release 0.6, with a 1.0 release candidate currently in development. Since code is written in Groovy, Grails provides syntax enhancements and features unavailable directly in Java, but provides for full Java integration and reuse of existing Java libraries. Like Rails, Grails provides convention-over-configuration to minimize configuration files and tedious coding. It builds upon Spring and Hibernate.
During his presentation, Jason showed how to create domain classes, controller classes, and to customize GSP pages. He added constraints to domain values with simple declarations in the domain class, and showed how to change the default error messages that are displayed on the web page when constraint validation fails. It was refreshing to watch him make a simple change to a Groovy source file then see the result just by reloading the web page. No build step. No deployment step. In development mode, Grails watches for file changes and performs the build and deployment steps for you, he said. Grails writes regular messages to the log file when it performs this hidden work to remind you not to use this feature in production.
Jason touched upon some of the similarities and differences between Grails and Ruby on Rails. The philosophy of persistence is different. Grails considers your domain class to be the source of record. It talks to the database to create and update tables during development. Rails considers the database the source of record for domain entities, and creates domain classes appropriately. You can switch the database auto-create features off if you're using an existing database, he said. The Grails development team is looking at using Middlegen in a future release to generate domain objects from an existing database schema, he said. Jason said a quality shared by Grails and Rails is they both work best when used on greenfield projects without an existing database or codebase.
Other features provided directly by Grails or through plugins:
- Custom URL mapping
- Alternate domain implementations, such as mapping domain objects to EJB3 entity beans
- Expose actions as web services
- Web page flow
- Many taglibs
- Authentication and authorization
- Integrating a search engine (e.g. Lucene).
- An object-relational mapping language
- Using JSP custom tag libraries inside GSP pages
- JPA integration
- Generating the domain classes from the database, as mentioned above.
Slides and code from his presentation are available on his website.
Thursday October 11, 2007 Permalink
Comments [0]
Creating a simple rules engine using the Java scripting API
Published by Tom |
September 12, 2007 08:49 AM EDT |
Part 2
of my IBM developerWorks article,
Invoke dynamic
languages dynamically,
creates a simple rules engine using the Java scripting API.
Business rules,
written in a combination of Ruby, Groovy, and JavaScript,
determine whether a borrower qualifies for a variety of home loans.
I used a rules engine as a sample application because it seemed more compelling
than another hello-world application,
and it also seemed like an interesting use of the scripting API.
The Java scripting API, also known as JSR-223, works as a viable basis for a rules engine when a full-blown business rules engine isn't needed because it offers several of the benefits you get from using a regular rules engine. For instance, when business rules are stored as external scripts, the scripting API:
- Allows you to work easily with large sets of rapidly changing rules
- Allows frequent and flexible additions and changes to rules
- Separates rules from processing logic
- Centralizes rules and makes them easier to manage
- Easy to program: Use a scripting language -- or several -- of your choice
- Free and easy to set up (partially built into Java SE 6)
- Small number of required external dependencies
- No need to learn a complex declarative business-rules language.
For example,
here's a
sample
from Drools:
rule "Approve if not rejected" salience -100 agenda-group "approval" when not Rejection() p : Policy(approved == false, policyState:status) exists Driver(age > 25) Process(status == policyState) then log("APPROVED: due to no objections."); p.setApproved(true); end
ScriptMortgageQualifier
class in part 2 of my article shows one such design.
It stores business objects that the external rules will use in decision-making in the
ScriptEngine's context,
and receives rule execution results in a separate shared Java object stored in the ScriptEngine context.
Rules (scripts) are responsible for storing results of their decisions in the shared Java object,
which the main Java code inspects after the rules are run to determine what action to take.
In my sample application, I use individual files to store the rules. The application scans the rules directory on each pass and executes whatever rule scripts it finds there. An advantage of using the Java scripting API to find the rule scripts is the rules can be written in any of dozens of languages supported by script-engine implementations. The rules engine doesn't care what language the rules are written in as long as the applicable script engine and interpreter can be loaded at runtime, such as being supplied by JARs in the classpath. In my sample, I coded rules in Groovy, JavaScript, and Ruby.
Another possible way of structuring rule logic would be to have the rules themselves set additional attributes that other rules could then use (that is, learn from). For instance, say one set of rules runs and determines that the prospective home purchaser has a bank balance of $10 million. The rule could set a property (a global script variable) called
VIP
(very important person)
to true.
As a global variable,
the property would be available in the ScriptEngine context and passed along to the next rule to be run.
That next rule could use different logic based on the fact that this borrower is a VIP.
The above example begins to reveal the shortcomings of designing a rules engine around the scripting API. Most formal rules engines have the notion that all rules are considered to be in effect at all time. Setting a fact such as "customer has VIP status" in one rule should be taken into consideration by all rules to determine if that new fact changes other facts. But satisfying that feature by invoking external rules stored as scripts would require script writers to order the rules in the proper sequence. Trying to sequence your business rules correctly to account for fact-dependencies is error prone -- and impossible when the rules have mutual dependencies. This limitation of requiring rules to be run in a proper sequence is certainly where you would want to consider using a better rules engine.
Rule sequencing isn't the only disadvantage to executing rules stored as external scripts. Writing business rules in Groovy, Ruby or another scripting language has the disadvantage of:
- Rules in scripting languages are written imperatively rather than declaratively
- Complex business logic written imperatively might require deeply nested conditional statements, which makes the rules hard to read and prone to error
- To avoid the above problem of coding deeply nested if-then statements in your script, you might be tempted to write code that processes a decision table -- reinventing the wheel built by better rules engines
- The temptation to write your business rules in multiple scripting languages could become a maintenance headache
If you're trying to decide whether your application calls for a dedicated rules engine, the Jess website has a good article, Some Guidelines For Deciding Whether To Use A Rules Engine.
Wednesday September 12, 2007 Permalink
Comments [3]
Returning from Ruby or JavaScript called from the Java Scripting API
Published by Tom |
August 02, 2007 08:45 AM EDT |
Since the Java Scripting API makes it easy to execute external scripts
written in a variety of dynamic languages,
I tried to find a consistent way to return early from top-level code
written in JavaScript and Ruby.
My goal was to be able to structure short Ruby and JavaScript scripts
by coding everything at the "top level,"
that is,
outside of any defined function, method, or class.
That way,
the Ruby or JavaScript scriptlets would be easier to write and
I could eval them from Java without having to
call a specific function or method by name.
After hunting around, I found no simple or easy way a JavaScript or Ruby script could return early from being evaluated when the scripting code is outside of a function or method. A
return statement is not allowed outside a function in
JavaScript,
nor is it allowed outside a method in Ruby.
The only consistent language feature I found that guaranteed early script exit
was for the code to throw an exception.
If you're unfamiliar with the Java Scripting API (JSR-223, Scripting for the Java Platform), it was added in Java Standard Edition 6 to provide a consistent way to embed scripting-language interpreters into a Java application. The API's
javax.script
package contains classes and interfaces
that let you call and share data with an external script written in
dozens
of scripting languages,
including powerful dynamic languages like Ruby and Groovy.
The Java Scripting API is based primarily on the Apache Jakarta
Bean Scripting Framework
project,
but provides extra features and is now built into the Java language.
You can use the Scripting API in Java 1.5 by adding the new packages,
available by downloading the JSR-223
reference
implementation.
Here is what I set out to accomplish.
I wanted to be able to pass Java objects to scripts written in Ruby and JavaScript and let those scripts process the shared Java objects. The goal was to take advantage of the cleaner, more concise syntax these languages offer and allow end-users the ability to supply the Ruby and JavaScript code. That was why I didn't want to require script providers to code their logic inside a method or function. But by placing all code at the top level, the script writer would have no language feature available to return early from script processing.
For example, the Java code that called the script would look something like:
// Java objects to share with the scripts:
String textToProcess = ... // Text for scripts to process
int myStatus = ... // Some type of status indicator
// etc.
ScriptEngineManager scriptEngineMgr = new ScriptEngineManager();
ScriptEngine rubyEngine = scriptEngineMgr.getEngineByName("ruby");
rubyEngine.put("textToProcess", textToProcess);
rubyEngine.put("status", Integer.valueOf(myStatus));
// ...
// Put a shared object the script will use to return results.
ResultsObject result = new ResultsObject();
rubyEngine.put("result", result);
// Read Ruby script from external source and execute it
String rubyScript = ...
rubyEngine.eval(rubyScript);
// Read results set by the script.
Long resultCode = result.getResultCode();
// etc...
The Ruby script would look something like:
# Don't process the text if the status is greater than 200
if $status > 200
return # <-- This is illegal Ruby!
end
# Process the $textToProcess text...
...
although the conditions in which the script writer would want to exit
could be a lot more complicated and couldn't be structured around
an if-else statement.
The problem here is the Ruby script has no simple, clear way to prevent the entire script from being run, short of raising an exception. It is possible to work around the problem by requiring the script to be coded inside of a method. You also could require script writers to code around the problem by wrapping all code inside a needless outer loop and using a
break
statement to serve the purpose of a return statement.
The above code could thus be replaced by:
1.times do
# Don't process the text if the status is greater than 200
if $status > 200
break # This does work.
end
# Process the $textToProcess text...
...
end
An extra outer loop should work for JavaScript, too.
The problem with using an outer loop to provide a script return is that it requires the script writer to code the loop. That solution violates my goal of making the scripts as easy as possible to write -- and read.
My eventual solution, which I'm not satisfied with, was to allow the script to perform the equivalent of a top-level return statement by throwing an exception. To make the solution more palatable and cleaner for the script writer, I created a Java class that would throw the actual exception. The Java class also permits the script to return an optional reason message when exiting.
Here is the revised Java code that would call the scripts:
// Java objects to share with the scripts:
String textToProcess = ... // Text for scripts to process
int myStatus = ... // Some type of status indicator
// etc.
ScriptEngineManager scriptEngineMgr = new ScriptEngineManager();
ScriptEngine rubyEngine = scriptEngineMgr.getEngineByName("ruby");
rubyEngine.put("textToProcess", textToProcess);
rubyEngine.put("status", Integer.valueOf(myStatus));
// ...
// Put a shared object the script will use to return results.
ResultsObject result = new ResultsObject();
rubyEngine.put("result", result);
// Add an object scripts can call to exit early from processing.
rubyEngine.put("scriptExit", new ScriptEarlyExit());
// Read Ruby script from external source and execute it
String rubyScript = ...
rubyEngine.eval(rubyScript);
// Read results of the script.
Long resultCode = result.getResultCode();
// etc...
The Java code now supplies all scripts with a
ScriptEarlyExit
object they can use to invoke the
equivalent of a return
statement.
Here is the
ScriptEarlyExit
class:
/** Object passed to all scripts so they can indicate an early exit. */
public class ScriptEarlyExit {
public void withMessage(String msg) throws ScriptEarlyExitException {
throw new ScriptEarlyExitException(msg);
}
public void noMessage() throws ScriptEarlyExitException {
throw new ScriptEarlyExitException(null);
}
}
The
ScriptEarlyExitException
class is a simple
Exception
subclass:
/** Internal exception so ScriptEarlyExit methods can exit scripts early */
public class ScriptEarlyExitException extends Exception {
public ScriptEarlyExitException(String msg) {
super(msg);
}
}
With the
ScriptEarlyExit
object made available to scripts by the call to
rubyEngine.put("scriptExit", new ScriptEarlyExit()),
any script in any language should now be able to exit early.
The Ruby script revised to use the new object would be coded like:
# Don't process the text if the status is greater than 200
if $status > 200
$scriptExit.with_message 'Not processing because of invalid status'
end
# Continue processing
...
The Java method call from the script provides a consistent,
fairly clean way to return early from script processing.
I tested calling this
ScriptEarlyExit
object from Ruby using
JRuby 1.0,
from JavaScript using the
Rhino
interpreter built into Sun's Java 1.6,
and from
Groovy 1.0.
It worked well with them all.
This solution did require solving another problem. Using a Java exception to end script processing means the script engine is going to bubble up a
javax.script.ScriptException
back to Java.
I needed a way to determine whether that exception was a real
ScriptException
or my fake
ScriptEarlyExitException.
The solution was to check the script exception message to see if my special exception was embedded in the string. The coded ended up looking like:
try {
rubyEngine.eval(rubyScript);
} catch (ScriptException se) {
// Re-throw exception unless it's our early-exit exception.
if (se.getMessage() == null ||
!se.getMessage().contains("ScriptEarlyExitException")
) {
throw se; // a real ScriptException
}
// Set script result message if early-exit exception embedded.
// Will not work with Java 6's included JavaScript engine.
Throwable t = se.getCause();
while (t != null) {
if (t instanceof ScriptEarlyExitException) {
result.setExitMessage(t.getMessage());
break;
}
t = t.getCause();
}
}
The
catch
block examines the exception's message for the "ScriptEarlyExitException" string,
and ignores the
ScriptException
if found.
The code in the
catch
block then looks to see if one of the causes of the
ScriptException
was the
ScriptEarlyExitException.
If so,
the
ScriptEarlyExitException
exception's message string will hold the value set when the script called the
withMessage
method on the shared
ScriptEarlyExit
object.
That is,
when Ruby calls:
$scriptExit.with_message 'Not processing because of invalid status'
the
ScriptEarlyExitException.getMessage()will contain the string "Not processing because of invalid status". The
catch
clause sets that string to the
ResultsObject
object's exitMessage
property using the code:
result.setExitMessage(t.getMessage());
As the comment in the above code indicates,
retrieving the "exit" message from the Rhino JavaScript engine doesn't work.
Or at least finding and parsing the exit string out of the resulting
ScriptException
is more tedious.
That's because the Rhino script engine does not wrap caught Java exceptions
into the resulting stack trace.
With Rhino,
the loop:
Throwable t = se.getCause();
while (t != null) {
if (t instanceof ScriptEarlyExitException) {
result.setExitMessage(t.getMessage());
break;
}
t = t.getCause();
}
never finds a
ScriptEarlyExitException.
As I mentioned, this solution of having scripts call a method on a shared Java object in order to exit script processing early by throwing an exception isn't elegant. But it does work to let scripts execute the equivalent of a top-level "return" statement. This solution likely will work with other JSR-223 scripting engines besides the ones I tested. It seems, though, that there must be a better way. Groovy, by the way, permits a
return
statement in top-level code.
That's pretty nice.
Are you a Ruby or JavaScript pro with a better solution? Is there an easier way for Ruby or JavaScript to return from a script even when the script code is outside a method/function? If you would like to share better techniques, please post a comment here or email me at the address shown in the right-hand column under the "Feedback" heading. If you post a comment on this blog, I ask your forgiveness in that comments are moderated before appearing, but there is no indication of that when you click the "Post" button.
Thursday August 02, 2007 Permalink
Comments [5]
Still using StringBuffer? That's sooo Java 1.4
Published by Tom |
July 17, 2007 06:53 AM EDT |
Pop quiz:
Hashtable is to HashMap as StringBuffer is to ...
<fill in the blank>
Answer: StringBuilder.
I recently worked on a Java project where the target environment was Java 1.5. Although Java 1.5 has been out for almost three years, the client was just upgrading to it to take advantage of its language features and APIs.
While working on the project, I noticed most developers continued to use the StringBuffer class when StringBuilder would have been the better choice. In asking around, most developers said they were unaware of StringBuilder.
In case you're using Java 1.5 or 1.6 but not yet using StringBuilder, StringBuilder is an unsynchronized version of the tried-and-true StringBuffer class. Most of StringBuffer's public methods are synchronized to allow multiple threads to read and modify the string simultaneously. But since StringBuffer is almost always used to build up a string within a method, or to build a string over several method calls within a single-threaded environment, the synchronized nature of StringBuffer is overkill. An article in Dr. Dobb's Journal in June 2006 estimated switching from StringBuffer to StringBuilder could speed string building by 38%.
That's why Sun added StringBuilder to the language in JDK 5. None of StringBuilder's methods is synchronized, so the class is not meant to be used when multiple threads need to access the string. In multi-threaded contexts, you will want to use StringBuffer. But consider your own code. How many times have you needed to share a StringBuffer between multiple threads? You'll probably find that StringBuilder is often the better choice.
Tuesday July 17, 2007 Permalink
Comments [6]



