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

// -*-Mode: Java-*- 
// $Id: Justification.java,v 1.4 2003/03/06 00:40:39 moltmans Exp $
// Justification.java -- 
// Author: Michael Oltmans <moltmans@ai.mit.edu> 
// Created: <Wed Apr 26 18:27:18 2000> 
// Time-stamp: <2000-05-13 17:11:34 moltmans> 
package rec.tms;

import java.util.*;

public class Justification
{
  private static int m_indexCounter = 0;
  public static final Justification ENABLED_ASSUMPTION
    = new Justification( "PREMISE", null, null );

  /** unique id **/
  private int m_index;

  /** A description of the justification **/
  private Object m_informant;

  /**
   * Collection of nodes that must be in for this justification to be
   * satisied
   **/
  private Collection m_antecedents;
  /** The node supported by this justification **/
  private Node m_consequence;

  Justification( Object informant,
		 Node consequence, Collection antecedents ) 
  {
    m_index = m_indexCounter++;
    m_informant = informant;
    m_consequence = consequence;
    m_antecedents = new LinkedList( );
    if( antecedents != null )
      m_antecedents.addAll( antecedents );
  }

  /** @return Node that is the consequence of this justification. **/
  public Node getConsequence() 
  {
    return m_consequence;
  }

  /** @return the nodes that this justification requires. **/
  public Collection getAntecedents() 
  {
    return m_antecedents;
  }

  public Object getInformant() 
  {
    return m_informant;
  }
  
  /** @return true if we are satisfied and the consequence is out. **/
  public boolean checkJustification()
  {
    return m_consequence.isOut() && isSatisfied();
  }

  /** @return true if all antecedents are in. **/
  public boolean isSatisfied()
  {
    Iterator it = m_antecedents.iterator();

    while( it.hasNext() ) {
      Node node = (Node)it.next();
      if( node.isOut() ) {
	return false;
      }
    }
    return true;
  }

  public boolean isEnabledAssumption()
  {
    return equals( ENABLED_ASSUMPTION );
  }

  public String toString() 
  {
    if( isEnabledAssumption() )
      return "PREMISE";
    return "Justification of " + m_consequence + " by " + m_informant +
      " using " + m_antecedents;
  }
  
}
