简体   繁体   中英

Drools changing state in LHS using Stateless Knowledge session

I am listing my problem here.

  1. Persons can enter or exit a room.
  2. Person can be a student or a teacher or HOD.
  3. Students have an attribute called status which can be updated manually as well as automatically.

These are my events:

  1. People entering the room.
  2. Attribute status updated for each student.

These are my rules.

  1. If a person entering a room is Teacher then print teacher entered.
  2. If the number of persons entering the room has gone beyond 30 alert me.
  3. If a student has attended more than 20 hours of the class then update status to complete.

Now I want 1 and 3 combined together.
like: 4. Alert the Teacher if a student who has attribute status complete enters the room.

Now as I told earlier both events come separately. So handling it in 2 different rules was easier. But when I want to create a rule which is a combination of 1 and 3 as in rule 4 then I have to verify if a person enters a room is a student and if he has attribute "complete".

But, loading the status attribute even before validating if he is a student, sounds bad to me. So, I want to call a method for loading the attribute only if

"a person is a student and there is a rule trying to load the attribute status".

I am planning to do it by a method call in LHS, which is not straightforward.

Is there any other way, I could handle this?

Having small rules identifying specific situation in most of the times is better than having complex rules trying to identify mixed situations. What is not clear to me is whether your want to replace rule 3 with rule 4 or not. I would leave rule 3 and create rule 4 as follow:

when
    PersonEnteringRoom($p: person, $r: room, person.type == "Student", person.status == "Complete") // status was set by rule 3.
    Room(this == $r, $t: currentTeacher != null) //the relation could be stablished by rule 1
then
    Notifier.notify($t, "The student has completed the course", $p);
end

Of course, whether this rule works for you or not depends in other factors like your execution cycle.

Hope it helps,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM