[rules-users] Advice on organizing rules

glasscat aburasheed at studentmasjid.com
Thu Jul 30 13:04:32 EDT 2009


I'm using drools 5.0.1.

I've written the following rules to retrieve objects from the working
memory. Questions:
1) whether there is a better way to organize the rules. For instance the
rule "checkvalidresponse" right now checks for both successful retrieval and
unsuccessful retrieval. Should I split it to 2?
2) When I do drools.update(response) in the "default" rule, it seems to be
in endless loop.
3) The response value set in "retrieve" rule is not updated when rule
"checkvalidresponse" is triggered. Do I need to do drools.update(response)
in the "default" rule?
4) Should I set the response code in the "retrieve" rule and remove the
"checkvalidresponse" rule?

Thanks


rule "default"
dialect "java"
agenda-group "retrieve"
salience 50
when
        Request(sfid : ProductIdentifier)
        trdata : Data(Identifier == sfid)
        response : Response(product == null)
        ServiceResponse(responseCode == 0) from
response.getServiceResponse()

then
        response.setProduct(trdata.getProduct());

        // should I set response code here or let "checkvalidresponse"
handle it?

        // do I need to update here?
        // drools.update(response);
        
end

# Shoud I split this to 2? 1) check for successful query and 2) check for
unsuccessful query?
rule "checkvalidresponse"
dialect "java"
agenda-group "retrieve"
salience 20
when
        Request()
        response : Response()
        sres : ServiceResponse(responseCode == 0) from
response.getServiceResponse()

then
        String rname = drools.getRule().getName();
        System.out.println("Rule: \"" + rname + "\" triggered");
        Product prod = response.getProduct();

        // Check if a valid Product found
        if ( prod == null ) {
                sres.setResponseCode(9);
                sres.setResponseMessage("Product not found");
                sres.setExceptionCode(9999);
                sres.setExceptionMessage("");
        } else {
                sres.setResponseCode(0);
                sres.setResponseMessage("The operation completed
successfully");
                sres.setExceptionCode(0);
                sres.setExceptionMessage("");
        }
        response.setServiceResponse(sres);
        drools.update(response);
end


# Credentials
# The only valid credential: User Id: User Password: Password
#
rule "checkcred"
dialect "java"
salience 50
when
        request : Request()
        response : Response()
        cred : Credentials(userName != "User" || password != "Password")
from request.getCredentials()
        sres : ServiceResponse(responseCode == 0) from
response.getServiceResponse()

then
        String rname = drools.getRule().getName();
        System.out.println("Rule: \"" + rname + "\" triggered");

        sres.setResponseCode(9);
        sres.setResponseMessage("Invalid credentials");
        sres.setExceptionCode(9000);
        sres.setExceptionMessage("");
        response.setServiceResponse(sres);
        drools.update(response);
end

# Transaction context
# Valid channel: channel01 or channel02
#
rule "checktrctx"
dialect "java"
salience 40
when
        request: Request()
        response: Response()
        ctx : TransactionContext(channel != "channel01" && channel !=
"channel02") from request.getTransactionContext()
        sres : ServiceResponse(responseCode == 0) from
response.getServiceResponse()

then
        String rname = drools.getRule().getName();
        System.out.println("Rule: \"" + rname + "\" triggered");

        System.out.println("Invalid transaction context: " +
ctx.getChannel());
        sres.setResponseCode(9);
        sres.setResponseMessage("Invalid transaction context");
        sres.setExceptionCode(9001);
        sres.setExceptionMessage("");
        res.setResponse(sres);
        drools.update(res);
end

rule "requestgood"
dialect "java"
salience 1
when
        Request()
        response : Response(product == null)
        ServiceResponse(responseCode == 0) from
response.getServiceResponse()

then
        String rname = drools.getRule().getName();
        System.out.println("Rule: \"" + rname + "\" triggered");

        System.out.println("Request is good");
        drools.setFocus("retrieve");
end

-- 
View this message in context: http://www.nabble.com/Advice-on-organizing-rules-tp24742646p24742646.html
Sent from the drools - user mailing list archive at Nabble.com.




More information about the rules-users mailing list