//========================================================== -*- Java -*- ===
// This file tests the the fix of watchdog bugs.
//
//   1. The time expression can be anything assignable to 'long'
//   2. The behavior of hard watchdogs when switching field values
//      is good (this behavior is good since JRules 3.1).
//===========================================================================

// For importing java.awt.Point and java.awt.Rectangle classes,
// easy for public field access.

import java.awt.*;

function void ilrmain(Object arg)
{
    var o = new Point(0,0);
    assert(o);

    for (var i = 0; i < 20; i++)
      {
	var t = time();
	if ((t % 6) == 0)
	  {
	    out.println("t=" + t + ",x:0");
	    modify o { x = 0; }
	  }
	if (((t + 3) % 6) == 0)
	  {
	    out.println("t=" + t + ",x:1");
	    modify o { x = 1; }
	  }
	fireAllRules();
	nextTime();
    }
}

/**
 * Tests the behavior of hard watchdogs when switching back and forth
 * between the same values, 0 and 1. Outputs should be:

t=0,x:0
t=3,x:1
It works (3)
t=6,x:0
t=9,x:1
It works (9)
t=12,x:0
t=15,x:1
It works (15)
t=18,x:0

 */
rule Test1
{
  priority = high;
  when
    {
      o : Point(x == 0);
      w1 : wait 10
	{
	  Point(x == 1);
	}
    }
  then
    {
      context.out.println("It works (" + ?w1.time() + ')');
      modify ?o { x = 0; }
      timeout ?w1
	{
	  context.out.println("timeout w1 !!");
	}
    }
};

/**
 * This rule no longer fires the following parsing error:
 *   "watchdog_compaq.ilr", line 56: error: time expression must of type int
 */
rule TestTimeExpression
{
  when
    {
      o : String();
      w1 : wait logical until ( ?context.time() +  10);
    }
  then
    {
      context.out.println("Done.");
    }
};

