[rules-users] Web client-side rule testing - best practices

ronalbury ronalbury at gmail.com
Mon Dec 5 16:12:42 EST 2011


Here's what I'm coming up with on my own.

I have a dozen PoJo's for my server record structure, and I am subclassing
them all off of an abstract class that has: 
/    private HashSet<String> modified = new HashSet<String>();
    private boolean allFields = false;

    public boolean fieldWasModified(String name) {
        boolean result;

        result = allFields || modified.contains(name);
        return result;
    }

    protected void markModified(String name) {
        String fieldName = name;

        if (name.startsWith("this.")) {
            fieldName = name.substring(5);
        }
        modified.add(fieldName);
    }

    public void markAllModified() {
        allFields = true;
    }/


I have modified the 'setter' template in the PoJo project to generate calls
to markModified:
/${field} = ${param};
setModified("${field}");/

My rules now test if a field is modified:
/rule "XmAddress city RegExp"
    when
        $a : XmAddress(fieldWasModified("city") 
            && city not matches "^[A-Za-z ]{1,25}$")  
    then
    	$a.addError(drools.getRule().getName(), "Invalid city");
end/

The Web client will use Ajax to create a new object and set record
attributes, as appropriate, and then call the rules.
At the end when I submit the entire record for testing on the server, I
first call markAllModified.

I understand that this will not handle cross-field rules (unless I include
both fields in the Ajax call), but it seems like it will handle most of my
standard data entry edits in a consistent manner across the UI and the
server.

Please provide feedback ... remember I'm a newbie so please make your
criticism constructive.

Thanks


--
View this message in context: http://drools.46999.n3.nabble.com/rules-users-Web-client-side-rule-testing-best-practices-tp3561732p3562649.html
Sent from the Drools: User forum mailing list archive at Nabble.com.



More information about the rules-users mailing list