// (c) MIT 2003.  All rights reserved.

// -*-Mode: Java-*- 
// $Id: JTRETest.java,v 1.6 2005/02/17 21:40:54 moltmans Exp $
// JTRETest.java -- 
// Author: Michael Oltmans <moltmans@ai.mit.edu> 
// Created: <Mon May 01 02:04:16 2000> 
// Time-stamp: <2005-02-16 14:05:52 moltmans> 
package rec.tms;

import java.util.*;

public class JTRETest 
{
  static JTRE m_jtre = new JTRE( "Test JTRE" );
  
  public static void main( String[] args ) 
  {
    TestFact fact1 = new TestFact( 1, 2);
    TestFact fact2 = new TestFact( 1, 4);
    Rule r1 = new TestRule( 1 );

    m_jtre.insertRule( r1 );
    m_jtre.assume( fact2, "User" );
    m_jtre.runRules();
    System.out.println( m_jtre.factsToString() + "\n" );
    m_jtre.retract( fact2, "User" );
    System.out.println( m_jtre.factsToString() + "\n" );
    //    System.out.println( m_jtre.rulesToString() + "\n" );
  }
}

class TestRule implements Rule
{
  static int count = 1435;
  
  int m_x;

  public TestRule( int x ) 
  {
    m_x = x;
  }
  
  public LinkedList getTriggers() 
  {
    LinkedList triggers = new LinkedList();
    triggers.add( TestType.EVENT );
    triggers.add( TestType.EVENT );
    return triggers;
  }

  public boolean match( LinkedList params ) 
  {
    boolean result;
    
    if( params.size() < 2 )
      result = true;
    else {
      TestFact e1 = (TestFact)params.get(0);
      TestFact e2 = (TestFact)params.get(1);

      result = (e1.m_x == e2.m_x) && (e2.m_x == m_x);
    }
    
    return result;
  }

  public void run( JTRE jtre, LinkedList params )
  {
    TestFact e1 = (TestFact)params.get(0);
    TestFact e2 = (TestFact)params.get(1);

    TestFact e = new TestFact( count++, count++ );
    jtre.myassert( e, "same x's", params );
  }

  public String toString() 
  {
    return "<Rule: TestRule " + m_x  + ">";
  }
  
}

class TestFact implements Fact
{
  int m_x;
  int m_y;

  public TestFact( int x, int y ) 
  {
    m_x = x;
    m_y = y;
  }

  public String getFactType()
  {
    return TestType.EVENT;
  }
  public boolean sameFact( Fact fact )
  {
    return (fact instanceof TestFact &&
	    m_x == ((TestFact)fact).m_x &&
	    m_y == ((TestFact)fact).m_y );
  }

  public String toEnglish()
  {
    return "Event at: (" + m_x + ", " + m_y + ")";
  }
  
  
  public String toString() 
  {
    return "<Fact: (EVENT " + m_x + " " + m_y + ")>";
  }
  
}

class TestType 
{
  public static String EVENT = "EVENT";
}

