Search code examples
javajbossdroolsrule-enginecomplex-event-processing

write a rule to detect customer location in a department in a store based on visits


I am new to Drools and have some questions. I am working on creating rules which detects customers in departments in a store.

My model is as below:

Customer: Id, Point(x,y)

Department: Id, Rectangle

CustomerEvent

I currently have customers represented with x,y location or a point and a department is represented by rectangle.

1.) How do I write a rule to detect if a customer is in a rectangle?

2.) If a customer changes location within a department at least 2 times, trigger an event. How do I write a rule to detect if the customer is in a department and has changed location two times?

Please let me know.

Thanks!

Customer

public class CustomerObj {

public CustomerObj(String custId, Integer timestamp) {
    super();
    this.custId = custId;
    this.timestamp = timestamp;
}
private String custId;
private long timestamp;
private Integer classification;
private Point location;
private Department department;


public long getTimestamp() {
    return timestamp;
}
public void setTimestamp(Integer timestamp) {
    this.timestamp = timestamp;
}
public String getCustId() {
    return custId;
}
public void setCustId(String custId) {
    this.custId = custId;
}
public Integer getClassification() {
    return classification;
}
public void setClassification(Integer classification) {
    this.classification = classification;
}
public Point getLocation() {
    return location;
}
public void setLocation(Point location) {
    this.location = location;
}
public Department getDepartment() {
    return department;
}
public void setDepartment(Department department) {
    this.department = department;
}

}

Department

public class Department {

public Department(Integer departmentId) {
    super();
    this.departmentId = departmentId;
}

private Integer departmentId;
private Rectangle deptLocation;

public Integer getDepartmentId() {
    return departmentId;
}

public void setDepartmentId(Integer departmentId) {
    this.departmentId = departmentId;
}

public boolean containsCustomer(CustomerObj c) {
    if (this.deptLocation.contains(c.getLocation())) return true;
    else return false;
}

}


Solution

  • Assuming Rectangle and Point are from java.awt, things are rather simple.

    rule "customer in department"
    when
        CustomerObj( $cid: custId, $p: point )
        Department( $did: departmentId, $dloc: deptLocation,
                    $dloc.contains( $p ) )
    then
        System.out.println( "customer " + $cid + " in " + $did );
    end
    

    As for the movements, you'll need an CustomerEvent showing customer and department id and the location.

    rule "customer moves frequently in same department"
    when
        CustomerEvent( $cid: custId, $did: dptmtId, $p: Point )
        $dep: Department( departmentId == $did )
        $cust: CustomerObj( custId == $cid, department == $dep, moves >= 1 )
    then
        // whatever
    end
    

    Note that you'll need additional rules for changing the department reference in a customer and for registering the 1st move, and, possibly for counting the moves beyond the second move.