The Artima Developer Community
Sponsored Link

Java Answers Forum
Modelling a business rule

12 replies on 1 page. Most recent reply: Aug 30, 2003 6:27 AM by James Patterson

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 12 replies on 1 page
Mmutla

Posts: 10
Nickname: mmutla
Registered: Aug, 2003

Modelling a business rule Posted: Aug 15, 2003 3:19 AM
Reply to this message Reply
Advertisement
I need some ideas on how to model a simple business rule. At the moment I can't afford to buy a sophisticated business rule engine. My question is how can I create a business rule class where everytime I need to validate a business rule, I can just call this class to ensure that the rule is followed?

An example would be of a user whose job is to give access to other users to use a particular system. I want to have something like, the user object(which authorises) calling the business rule object to determine if it is qualified to give access. If the business rule object returns say, a true, then I know that this user object can authorise.

How to structure this business rule object is what I am looking for. Any help in this regard will be greatly appreciated.


Vincent O'Sullivan

Posts: 724
Nickname: vincent
Registered: Nov, 2002

Re: Modelling a business rule Posted: Aug 15, 2003 4:27 AM
Reply to this message Reply
Is this the sort of starting point your looking for...

class User
{
    /**
     * Returns <code>true</code>, if the user is an administrator
     * otherwise <code>false</code>.
     * @return <code>true</code>, if the user is an administrator
     *          otherwise <code>false</code>.
     */ 
    public boolean isAdministrator()
    {
       // ToDo: Insert code that identifies 
       // ToDo: this User instance as an administrator. 
    }
}
Usage:
   if u.isAdministrator()
   {
      // Do stuff.
   }


A better alternative (because it encapsulates the implementation better) might be to implement User as an interface and, when you create a User instance on your system (using a suitable factory method) then the object returned might be a User implemtation such as a Prole or an Administrator depending on their defined rights. The Administrator would come with additional capabilites (e.g. createNewUser(...) or editUser(...)) compared to the Prole.

Vince

Mmutla

Posts: 10
Nickname: mmutla
Registered: Aug, 2003

Re: Modelling a business rule Posted: Aug 18, 2003 4:27 AM
Reply to this message Reply
I am looking for something like this :
class Administrator {
//check if administrator is authorised to give access
BusinessRule br = ...
if (br.authorised(this)) {
...
}
}
My biggest problem at the moment is when to call this business object, how to structure it, how to represent conditional logic etc. Alternatively should I perhaps write a class with lots of "if" condiftions encapsulated in methods?

Mmutla

Vincent O'Sullivan

Posts: 724
Nickname: vincent
Registered: Nov, 2002

Re: Modelling a business rule Posted: Aug 18, 2003 2:21 PM
Reply to this message Reply
Well, the logic sounds a bit circular (Admin calls BusRule which tests Admin fr Authority) but my first cut would be something alomg the lines of:
/**
 * Have all business rules  implement a common interface.
 * To see if a business rule is being followed call <code>isValid</code>().
 */
public interface BusinessRule
{
    /**
     * Returns <code>true</code>, if the <code>BusinessRule</code> conditions
     * are fulfilled, otherwise false.
     */
    public boolean isValid();
}
A couple of possible implementations:
public class AccessAuthority1
implements BusinessRule
{
    boolean hasAuthority = false;
 
    public void setAdministrator(Administrator a)
    {
        hasAuthority = // Test a for authority.
    }
 
    public boolean isValid()
    {
        return hasAuthority;
    }
}
 
public class AccessAuthority2
implements BusinessRule
{
    Administrator a;
 
    public void setAdministrator(Administrator a)
    {
        this.a = a;
    }
 
    public boolean isValid()
    {
        return // Test a for authority.
    }
}


Thus, a BusinessRule is always tested by calling the isValid() method.

Vince.

Mmutla

Posts: 10
Nickname: mmutla
Registered: Aug, 2003

Re: Modelling a business rule Posted: Aug 19, 2003 4:07 AM
Reply to this message Reply
Thanks Vincent.

Suppose I have an Administrator with about three business rules to satisfy. Example,
rule 1 - the person must earn x income
rule 2 - the person must have reached grade x (from the company's grading system)
rule 3 - must be certified by some professional body


Once an administrator object has been created, I would like to see if this is indeed an administrator by checking the three rules. My main question is really how do I model all these three rules and many others of course. When do I check for them? Are there events that I need to consider that will trigger these rules? A typical example of what I want to have follows :

Say I have a class like this,
public class BusinessRule {
    boolean checkForAdminRules(Administrator a) {
        if (a.getSalary() < x) {
            return false;
        }
        if (a.getGrade().equals("x") {
           return false;
        }
        if (a.getQualification().equals("xx"){
           return false;
        } 
        return true;
    //underneath here I can have many other methods for all the other objects
    //that are governed by business rules 
    boolean checkAccessRules() {
        ...
    }
    //etc, etc
} 

This business rule class will be called everytime there is a need to validate the rules. This would be like normal "if statements" in a class but this time centralising them so that any other interested class can use them. This simple, basic design, by the way is just to show you the essence of what I am trying to do.

tushar

Posts: 3
Nickname: tusha0
Registered: Feb, 2003

Re: Modelling a business rule Posted: Aug 19, 2003 4:34 AM
Reply to this message Reply
Rather than defining your access control business rules in code as classes, you'll find it more flexible to define them in say a configuration file or your database.

Model the generic aspects of your access control business rules in your classes, and leave the specifics to be configured later.

The generic aspects are things like 'role name', 'numeric access level', or 'boolean authority' - which could be part of your User class.

The specific aspects like 'Mr Bloggs' has the 'admin role', access level 5', or 'authority to modify files' could be configured outside your classes in a configuration file or database.

You'll find that business rules like 'salary > x', or 'num. of years in company' are too fickle, and will change more often than the seasons - you don't want to change your code every time your boss changes his mind. Instead materialising the business rules as specific values that can be configured outside your code will make things more manageable.

Hope that makes sense.

Vincent O'Sullivan

Posts: 724
Nickname: vincent
Registered: Nov, 2002

Re: Modelling a business rule Posted: Aug 23, 2003 10:37 AM
Reply to this message Reply
> Rather than defining your access control business rules in
> code as classes, you'll find it more flexible to define
> them in say a configuration file or your database.

Tushar is quite right. If you can encode your business rules in your database then you will be able to simplify your application code (always a good thing) and create an application that is both more maintainable and robust.

Vince.

David

Posts: 150
Nickname: archangel
Registered: Jul, 2003

Re: Modelling a business rule Posted: Aug 26, 2003 2:36 AM
Reply to this message Reply
hmmm....my mult-tier design twitch is coming back at the words "encode your business rules in your database"....ouch....[twitch]....[twitch]....

David

Posts: 150
Nickname: archangel
Registered: Jul, 2003

Re: Modelling a business rule Posted: Aug 26, 2003 2:36 AM
Reply to this message Reply
Sorry, "multi-tier".

Mmutla

Posts: 10
Nickname: mmutla
Registered: Aug, 2003

Re: Modelling a business rule Posted: Aug 29, 2003 1:55 AM
Reply to this message Reply
Hi tushar

Can you share with me an example of how you would represent these rules in a database?

David

Posts: 150
Nickname: archangel
Registered: Jul, 2003

Re: Modelling a business rule Posted: Aug 29, 2003 3:39 AM
Reply to this message Reply
I'm assuming they're talking about using TRIGGERs to validate data when there is an attempt to INSERT or UPDATE data - it's kinda database specific.

Robert Kingan

Posts: 1
Nickname: rkingan
Registered: Aug, 2003

Re: Modelling a business rule Posted: Aug 29, 2003 9:10 AM
Reply to this message Reply
Assuming you are using a relational database as the back-end for your application, and that you have a table (say "User") containing info on each of the users, then you would have another table called, say, "Role", listing bundles of permissions associated with different types of people (like "Administrator"), a table called "Permission" which lists all of the permissions you'd need to check, and then a couple of tables to manage the many-to-many relationships between User and Role and between Role and Permission. The first, table, which you might call "User_Role", consists of keys to users and roles--each role that a user has is encoded in one record of the table, which has the user's id number (or whatever primary key you're using), and the role's key also.

The "Role_Permission" table does the same thing to indicate which permissions are associated with the different roles.

Then, to check whether a user can do something, you can do a join on User_Role and Role_Permission, filtering by the user's id, to get a list of permissions for the user. You could have one object whose job it is to check permissions for everybody else.

You also seemed to indicate that some of the permissions were dependent on other data; i.e. not simply "He can do this because the sys admin says he can", but "She can do this because she is a level 5 wizard making over $55K per year". I would include one or more classes to implement these rules, and have them called by the class you use to manage user info. Then just have that class update the user_role table based on the user info as the info is updated. That way those classes, and those classes only, worry about the relationship between user info and roles.

Just my $.02 worth.

James Patterson

Posts: 16
Nickname: jmep
Registered: Mar, 2003

Re: Modelling a business rule Posted: Aug 30, 2003 6:27 AM
Reply to this message Reply
I've worked with Rules Engine processing for nearly 2 years. Personally, I would stay away from database solutions - once you see the power and flexibility of Rules Based solutions you'll never go back:

Consider the Open Source drools project at
http://www.drools.org/

And Friedman-Hill just released his book about Jess
http://www.manning.com/friedman-hill/index.html

Good Luck!

Flat View: This topic has 12 replies on 1 page
Topic: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorExcept Previous Topic   Next Topic Topic: Stateful and Stateless Session Beans

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use