Skip to: Site menu | Main content

Drools

Java Rules Engine

Escalation Example Print

Introduction

This example illustrations temporal rules, which are Rules that fire after a given time if they are still true.

Running the Escallation Example

Change into the drools-examples directory and run the Escallation example:

D:\java\workspaces\drools-2.0>maven escalation-java
 __  __
|  \/  |__ _Apache__ ___
| |\/| / _` \ V / -_) ' \  ~ intelligent projects ~
|_|  |_\__,_|\_/\___|_||_|  v. 1.0

(bunch of output)

escalation-java:
    [java] Using drl: escalation.java.drl
    [java] ----------------------------------------
    [java]     PRE
    [java] ----------------------------------------
    [java] [TroubleTicket: submitter='bob'; status='NEW']
    [java] [TroubleTicket: submitter='dave'; status='NEW']
    [java] ----------------------------------------
    [java] ----------------------------------------
    [java]     POST ASSERT
    [java] ----------------------------------------
    [java] [TroubleTicket: submitter='bob'; status='NEW']
    [java] [TroubleTicket: submitter='dave'; status='NEW']
    [java] ----------------------------------------
    [java] [[ Sleeping 10 seconds ]]
    [java] ----------------------------------------
    [java] 02:03:39 CET
    [java] ** ticket == [TroubleTicket: submitter='dave'; status='NEW']
    [java] ** Escalating trouble ticket from NEW to NOTIFIED after 2 seconds
    [java] ** ticket == [TroubleTicket: submitter='dave'; status='NOTIFIED']
    [java] ----------------------------------------
    [java] ----------------------------------------
    [java] 02:03:39 CET
    [java] ** ticket == [TroubleTicket: submitter='bob'; status='NEW']
    [java] ** Escalating trouble ticket from NEW to NOTIFIED after 2 seconds
    [java] ** ticket == [TroubleTicket: submitter='bob'; status='NOTIFIED']
    [java] ----------------------------------------
    [java] ----------------------------------------
    [java] 02:03:44 CET
    [java] ** ticket == [TroubleTicket: submitter='dave'; status='NOTIFIED']
    [java] ** Escalating trouble ticket from NOTIFIED to /dev/null after 5 seconds
    [java] ** ticket == [TroubleTicket: submitter='dave'; status='/dev/null']
    [java] ----------------------------------------
    [java] ----------------------------------------
    [java] 02:03:44 CET
    [java] ** ticket == [TroubleTicket: submitter='bob'; status='NOTIFIED']
    [java] ** Escalating trouble ticket from NOTIFIED to /dev/null after 5 seconds
    [java] ** ticket == [TroubleTicket: submitter='bob'; status='/dev/null']
    [java] ----------------------------------------
    [java] [[ Done sleeping ]]
    [java] ----------------------------------------
    [java]     POST SLEEP
    [java] ----------------------------------------
    [java] [TroubleTicket: submitter='bob'; status='/dev/null']
    [java] [TroubleTicket: submitter='dave'; status='/dev/null']
    [java] ----------------------------------------
BUILD SUCCESSFUL
Total time: 27 seconds
Finished at: Mon Mar 21 02:03:47 CET 2005

Understanding the Escallation Example

If the submitter of a new ticket is "bob" then set the ticket to notified straight away:

<rule name="Escalate Immediately If Bob">
  <parameter identifier="ticket">
    <class>org.drools.examples.escalation.TroubleTicket</class>
  </parameter>

  <java:condition>ticket.getSubmitter().equals( "bob" )</java:condition>
  <java:condition>ticket.getStatus() == TroubleTicket.NEW</java:condition>

  <java:consequence>
      import java.util.Date;
      import java.text.DateFormat;

      System.err.println( DateFormat.getTimeInstance( DateFormat.LONG ).format( new Date() ) );
      System.err.println( "** Escalating trouble ticket for bob, because he's special" );
      ticket.setStatus( TroubleTicket.NOTIFIED );
      drools.modifyObject( ticket );

    </java:consequence>
</rule>

Any new tickets should be given a duration of 2 seconds and if the rule is still true after that period then set the ticket to notified and print helpfull information.

<rule name="Escalate New Tickets">
  <parameter identifier="ticket">
    <class>org.drools.examples.escalation.TroubleTicket</class>
  </parameter>

  <java:condition>ticket.getStatus() == TroubleTicket.NEW</java:condition>

  <duration seconds="2" />

  <java:consequence>
      import java.util.Date;
      import java.text.DateFormat;

      System.err.println( "----------------------------------------" );
      System.err.println( DateFormat.getTimeInstance( DateFormat.LONG ).format( new Date() ) );
      System.err.println( "** ticket == " + ticket );
      ticket.setStatus( TroubleTicket.NOTIFIED );
      System.err.println( "** Escalating trouble ticket from NEW to NOTIFIED after 2 seconds" );
      System.err.println( "** ticket == " + ticket );
      System.err.println( "----------------------------------------" );

      drools.modifyObject( ticket );

  </java:consequence>
</rule>

This rule escalates tickets with the notified status after 5 seconds and prints helpfull information.

<rule name="Escalate Notified Tickets">
  <parameter identifier="ticket">
    <class>org.drools.examples.escalation.TroubleTicket </class>
  </parameter>

  <java:condition>ticket.getStatus() == TroubleTicket.NOTIFIED</java:condition>

  <duration seconds="5" />

  <java:consequence>
      import java.util.Date;
      import java.text.DateFormat;

      System.err.println( "----------------------------------------" );
      System.err.println( DateFormat.getTimeInstance( DateFormat.LONG ).format( new Date() ) );
      System.err.println( "** ticket == " + ticket );
      ticket.setStatus( TroubleTicket.NULL );
      System.err.println( "** Escalating trouble ticket from NOTIFIED to /dev/null after 5 seconds" );
      System.err.println( "** ticket == " + ticket );
      System.err.println( "----------------------------------------" );

      drools.modifyObject( ticket );

  </java:consequence>
</rule>