Jess Information

Jess Home
Jess 7 Features
Download Now!
Online Demo

Documentation
FAQ
Manual
Mailing List
Jess Wiki

More information Related Web Sites
User Contributions
JSR94 Info
Developer's Log
About This Site

JESS ®, the Rule Engine for the JavaTM Platform

Jess Wiki: Facts Are Not Objects Pattern

Let’s change our model from FactsAreNotObjectsAntiPattern so that it follows a database-like entity-relational model rather than the object-oriented model. We’ll keep our Employee fact exactly the same except that we’ll add one field: the department to which the employee belongs.

public class Employee {
    private int departmentId;
    public int getDepartmentId() { return departmentId; }
    public void setDepartmentId(int departmentId) {
        this.departmentId = departmentId;
    }

    private int employeeId;
    public int getEmployeeId() { return employeeId; }
    public void setEmployeeId(int employeeId) {
        this.employeeId = employeeId;
    }

    private String name;
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }

    private String address;
    public String getAddress() { return address; }
    public void setAddress(String addr) { address = addr; }

    ...
}

Note that this ER model is not the same as that used in the anti-pattern, here an Employee can only be a member of one department, this simplification means we're cheating somewhat when comparing this "pattern" to the "anti-pattern".

We can now simplify our Department class so that it contains only information about the department itself and doesn’t try to keep track of the many employees that belong to it. Also, instead of referencing the manager directly, we’ll just refer to his/her employee ID number, which should be unique to an employee.

public class Department {
    private int departmentId;
    public int getDepartmentId() { return departmentId; }
    public void setDepartmentId(int departmentId) {
        this.departmentId = departmentId;
    }

    private String name;
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }

    private int managerEmployeeId;
    public int getManagerEmployeeId() { return managerEmployeeId; }
    public void setManagerEmployeeId(int managerEmployeeId) {
        this.managerEmployeeId = managerEmployeeId;
    }

    ...
}

The "promote-to-manager" and "fire-employee" facts will also refer to employee Id and department Id numbers rather than the corresponding objects directly. Now, let’s rewrite our rules:

(defrule promote-employee
    (promote-to-manager
        (employeeId     ?EMPLOYEE_ID)
        (departmentId   ?DEPARTMENT_ID_EMPLOYEE_IS_MOVING_TO))
    ?DEPARTMENT_FACT <- (department
        (departmentId   ?DEPARTMENT_ID_EMPLOYEE_IS_MOVING_TO))
    ?EMPLOYEE_FACT <- (employee
        (employeeId     ?EMPLOYEE_ID))
    =>
    (modify ?DEPARTMENT_FACT
        (managerEmployeeId ?EMPLOYEE_ID))
    (modify ?EMPLOYEE_FACT
        (departmentId ?DEPARTMENT_ID_EMPLOYEE_IS_MOVING_TO)))

(defrule fire-employee
    (fire-employee
        (employee       ?EMPLOYEE_ID))
    ?EMPLOYEE_FACT <- (employee
        (employeeId     ?EMPLOYEE_ID))
    =>
    (retract ?EMPLOYEE_FACT))
I hope this looks a lot cleaner and easier to understand to you. It is 100% pure Jess. It has no external Java method calls. The representation problems and bugs I’ve mentioned in FactsAreNotObjectsAntiPattern have been fixed.

Submitted by:
GeorgeWilliamson
Union Pacific Railroad
gawillia@up.com



FrontPage, JavaPitfalls, FactsAreNotObjects


Front Page | Sandbox | Recent Changes | Powered by Friki | Last Edited: 27 January 2008