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

// -*-Mode: Java-*- 
// $Id: Datum.java,v 1.5 2003/03/06 00:40:39 moltmans Exp $
// Datum.java -- 
// Author: Michael Oltmans <moltmans@ai.mit.edu> 
// Created: <Sun Apr 30 01:51:39 2000> 
// Time-stamp: <2000-05-16 22:28:06 moltmans> 
package rec.tms;

import java.util.*;

public class Datum
{
  private static int m_idCounter = 0;

  private JTRE m_jtre;
  private int m_id;
  /** the actual data represented by this datum **/
  private Fact m_data;
  /** Pointer to node in the tms. **/
  private Node m_node;
  /** The dbclass of this datum object. **/
  private DBClass m_class;
  /** When non-null indicates informant. **/
  private Object m_assumption;

  Datum( JTRE jtre, Fact fact ) 
  {
    m_jtre = jtre;
    m_id = m_idCounter++;
    m_data = fact;
    m_node = new Node( jtre.getJTMS(), this );
    m_class = m_jtre.getDBClass( fact.getFactType() );
    m_assumption = null;
  }

  int getId() 
  {
    return m_id;
  }

  public Fact getFact() 
  {
    return m_data;
  }

  public Node getNode() 
  {
    return m_node;
  }

  DBClass getDBClass() 
  {
    return m_class;
  }

  public boolean isAssumption() 
  {
    return m_assumption != null;
  }

  public Object getAssumption()
  {
    return m_assumption;
  }

  void setAssumption( Object reason ) 
  {
    m_assumption = reason;
  }

  String show() 
  {
    return m_data.toString();
  }
  
  public String toString() 
  {
    return "<Datum " + m_id + ": " + m_data + ">";
  }

  /**
   * Convert list of datums to a list of facts, and add Datum to the
   * end.
   **/
  public static LinkedList datumsToFacts( LinkedList datums )
  {
    return datumsToFacts( datums, null );
  }
  
  public static LinkedList datumsToFacts( LinkedList datums,
					  Datum datum ) 
  {
    LinkedList facts = new LinkedList();
    Iterator it = datums.iterator();
    while( it.hasNext() ) {
      Datum d = (Datum)it.next();
      facts.addLast( d.getFact() );
    }
    if( datum != null )
      facts.addLast( datum.getFact() );
    return facts;
  }

}
