// -*-mode: rules; c-basic-offset: 2; -*-

import lhs_wait_context.*;

ruleset waitcontext
{
  property context.class = lhs_wait_context.DerivedContext;
};

function void ilrmain(Object o)
{
  bind ?i=0;
  assert("started");
  assert("error");
  assert("error2");
  assert("error3");

  for (?i=0;?i<20;?i++)
    {
      nextTime();
      if (time()==3) modify ?context.a {changeNumber(1);}
      if (time()==4)
	{
	  ?context.a.changeNumber(2);
	  ?context.updateContext();
	}
      if (time()==5)
	{	
	  ?context.updateContext(true);
	}
      fireAllRules();
    }
};

rule waitforme
{
  when
    {
      ?s:String(equals("started"));
      lhs_wait_context.A(number==0);
      ?w:wait 5
	{
	  lhs_wait_context.A(number==1) from ?context.a;
	}
    }
  then
    {
      retract ?s;
      timeout ?w
	{
	  System.out.println("** Error in lhs_wait_context.ilr, context modified not seen by wait ");
	}
    }
};

rule error
{
  when
    {
      String(equals("error"));
      wait logical 15; 
      ?s:String(equals("started"));
    }
  then
    {
      System.out.println("** Error in lhs_wait_context.ilr, rule waitforme not fired");
    }
};

rule checkforit
{
  when
    {
      ?s:String(equals("error2"));
      lhs_wait_context.A(number == 2) from ?context.a;
    }
  then
    {
      if (time()==4)
	retract ?s;
    }
};

rule error2
{
  when
    {
      String(equals("error"));
      wait logical 15; 
      ?s:String(equals("error2"));
    }
  then
    {
      System.out.println("** Error in lhs_wait_context.ilr, rule checkforit not fired");
    }
}

rule refreshit
{
  when
    {
      ?s:String(equals("error3"));
      lhs_wait_context.A(number ==2) from ?context.a;
    }
  then
    {
      if (time()==5) retract ?s;
    }
};

rule error3
{
  when
    {
      String(equals("error"));
      wait logical 15; 
      ?s:String(equals("error3"));
    }
  then
    {
      System.out.println("** Error in lhs_wait_context.ilr, rule refreshit not fired");
    }
};
