« July 2004 | Main | September 2004 »

August 21, 2004

JSR-94 Not Useless But Certainly Trivial

Listen to this articleListen to this article

I watch with interest as the Rule Engine chatter begins to increase. I truly believe it's an area much ignored by the great majority of developers.

If you're not aware, there is a JSR in the works to provide a common interface for integrating rule engines. In its current form, JSR-94 provides little more than a common interface for creating a context/rule engine and marshalling objects in and out. It is trivial to implement this yourself.

The fact is (pun intended) that the JSR provides little more than would result in developing a system that makes use of a business rules engine keeping in mind requirements for testability of rules and loose coupling (a.k.a. good design?). Only the JSR is considerably more verbose.

To illustrate, we have a large number of rules in our current system and a very small but useful set of interfaces:

public interface RuleEngineFactory {
    public RuleEngine createRuleEngine();
}

public interface RuleEngine {
    public void reset();
    public void addFact(Object fact);
    public void addFacts(Set facts);
    public void execute();
    public Set getFacts(Class type);
}

public interface RuleEnginePool {
    public RuleEngine getRuleEngine();
    public void returnRuleEngine(RuleEngine ruleEngine);
}

Add to these a few very light-weight implemenation classes (and some Dependency Injection for good measure) and you have pretty much everything you could need from an integration standpoint.

public class JRulesRuleEngineFactory implements RuleEngineFactory {
    private final IlrRuleset _ilrRuleset = new IlrRuleset();

    public JRulesRuleEngineFactory(Reader reader) {
        if (!_ilrRuleset.parseReader(reader)) {
            throw new RuntimeException("Error parsing rules");
        }
    }

    public RuleEngine createRuleEngine() {
        return new JRulesRuleEngine(new IlrContext(_ilrRuleset));
    }
}

public class ThreadLocalRuleEnginePool implements RuleEnginePool {
    private final ThreadLocal _engines = new ThreadLocal() {
        protected Object initialValue() {
            return _ruleEngineFactory.createRuleEngine();
        }
    };

    private final RuleEngineFactory _ruleEngineFactory;

    public ThreadLocalRuleEnginePool(RuleEngineFactory ruleEngineFactory) {
        assert ruleEngineFactory != null : "ruleEngineFactory can't be null";
        _ruleEngineFactory = ruleEngineFactory;
    }

    public RuleEngine getRuleEngine() {
        return (RuleEngine) _engines.get();
    }

    public void returnRuleEngine(RuleEngine ruleEngine) {
        assert ruleEngine == getRuleEngine() : "ruleEngine not allocated from the current thread";
        ruleEngine.reset();
    }
}

We theoretically have plugability of rule engines but to believe that this might be useful or even practical is naive at best.

Unfortunately, (or fortunately depending on your perspective) the biggest part of using a rule engine is in analysing and writing the rules themselves. Granted, many engines use a Rete Algorithm but to suggest that all Rete-based rule engines are the same is akin to saying that two Java applications are the same. JRules and JESS both use a Rete network and both are implemented in Java but the language, tools and behaviour (not to mention performance characteristics) of each differs sufficiently to render the conversion process rather less than trivial.

Surely, few of you would be imagine that a switch from using JSPs to say Velocity in a system of any significant size would be an overnight job. Similary, a switch from Struts to say Tapestry or JSF would be non-trivial. All of these technologies attempt to solve essentially the same problem but all come with a slightly different design philosphy. No matter how standardised the interface, if the behaviour of the system on the other side is different, the illusion breaks down.

Anything that lowers the barrier to entry for those wishing to explore the use of declarative rules is to be applauded however there are far more important problems for an organisation to solve than transparency of the underlying rule engine implementation. Atomicity, testing, managability, education, analysis, configuration, understandability, to list but a few. It is no coincidence that these are largely non-technical.

August 11, 2004

What Does Business Analysis Really Mean

Listen to this articleListen to this article

Many years ago now I read the first edition of About Face by Alan Cooper. At the time, Dave and I were working on an HR application which was subsequently sold to a another company and is still being sold and (I presume) used, today.

Not to ding my own chain (much!) but it still rates as one of the best apps I've ever built. I'm sure if I looked at the source code these days I would whince but I still think we made some pretty good technical achievements. However technical merit aside the one thing that really made an impression on me and continues to make an impression was the usability of the application. Not only in terms of business functionality but also the way it simplified the way users performed their day to day tasks.

Alan Coopers most excellent insights were enlightening to me at the time and certainly influenced a lot of the design. But I can't take credit for the usability nor functionality of the application. No for that we have Dave to thank. Besides having a brain the size of a planet, he is an exceptional business analyst. "Oh we have really good business analysts" I hear you cry. I'm sure you do in which case you'll appreciate my definition of a business analyst.

Picking on Dave once again, he has an amazing ability to actually analyse a business. By this I mean try to really understand what the customer does; Why they do it; Determine if their current business practices even make sense; How their shiny new software might actually make their life easier; and; most importanty to convey to (AKA convince) the customer why his ideas will work. I've heard of CEOs walking away from meetings asking their staff how this guy knows so much about their business. And I know for a fact that he had very little prior knowledge. He just knows how to ask the right questions to get to the heart of their business.

Traditionally (though I have little data to suggest this isn't still the norm) business analysts will sit with the customers and essentially document what the customer does. Workflow, day to day tasks, etc. From this they then write story cards or use cases (whatever is flavour dejour) that form the basis of the application design. These then go to the developers who consult with the customers on what exactly needs to be done, screen designs, etc. and then off they go to build the software. Unfortunately, the net result is usually a computerised version of some ancient manual system that is barely better than what they had and in many cases worse!

Maybe it's because the skills of which I write are rare but I'm not sure where the notion that customers should design the software comes from . The idea that customers know what they need (or even want) is just plain ludicrous. In most cases, business people understand what drives their business. They understand what their competitive advantage is and where they could gain new business if only they could do X or had Y. Surely it is the BAs job, nay duty, to come up to speed with the business and from that explain to the customers what would make their life easier. Surely that is where BAs add value. They understand software AND the business.

Even as a developer, I see it as my responsibility to go and talk to the BAs and customers if I see inconsistencies or if I think the application flow or business rules can be improved. I'm sure their are those who wished Id shut up sometimes but I still think it's worth it. Which brings us right back to where we started. It's very rare that the end users can design a piece of software that actually does what they need but it's equally as likely that developers will, on their own, design totally unusable software. So go read the book :-).

Oh, and this paper if you have the time.

August 07, 2004

The Lost Art of Database Design

Listen to this articleListen to this article

Have you ever read any C. J. Date? No? Hardly suprising. In fact most of my colleages couldn't give two hoots when it comes to data modelling (aka ER modelling) as distinct from object modelling. I'll admit that for many it's not the sexiest of topics.

Some months ago, Dave and I were discussing object modelling and database design. Daves assertion was that you need to do a good data model before your object model. I countered that I believed a good object model necessarily produced a well formed and normalised data model. On reflection it seems we were both right.

You see I was under the illusion that a good understanding of relational theory and alegbra was ubiquitous. I naively believed that all software developers knew what a normal form was, what a primary key actually meant and why relational integrity enforced via foreign key constraints IS important! WRONG!

Relational database management systems (RDBMS) are based on mathematically provable theory. SQL for example is a bastardisation (though not Dates exact words) of the relational algebra which in turn is derived from predicate calculus. Interestingly RDBMS and Business Rule Engines have much in common so it's not suprising that Date, an amazing logician, seems to spend a bit of his time these days writing on business rules. But I digress.

Given this basis in rigourous theory and mathematics, I find it amazing that as an industry of self-proclaimed engineers so few of us seem to have any clues on the matter, preferring instead to stay in the relative adhocery of object modelling where we can all become pioneers by inventing and re-inventing this months rules of thumb or best practices for good OO design.

Many of the available ORM tools force you to either screw your object model or screw your database model and being the anal retentive person that I am when it comes to both topics I find this rather discouraging. Mostly because I'd be willing to bet my left nut that most developers out there don't even realise that they've ended up with either an object model that violates some pretty basic principles of good OO design or ended up with a data model that is just plain wrong.

Granted, DBA's are supposed to look at our data model and find the flaws but so far as I can tell, most DBAs these days are there to enforce BLLSHT NMG STNDRDS THT N N CN NDRSTND or to tell us what columns need indexing.

I'm all for surrogate keys over business keys, etc. but the fact remains that until the world chooses object oriented database management systems (OODBMS) in favour of the good old RDBMS don't delude yourself into believing that relational theory isn't important.