// FILE. . . . . d:/hak/hlt/src/hlt/fot/fuz/syntax/sources/fff-ancillary-classes.grm
// EDIT BY . . . Hassan Ait-Kaci
// ON MACHINE. . Hak-Laptop
// STARTED ON. . Tue Aug 21 05:20:57 2018

// Last modified on Wed Nov 28 15:10:47 2018 by hak

%%

/**
 * This is the class for terms being partially parsed as syntactic
 * objects and eventually translated into a <tt>FirstOrderTerm</tt>.
 * <b>N.B.</b>: numbers are not parsed as we consider only
 * <i>symbolic</i> functors.
 */
class SyntacticTerm
{
  String symbol = null;
  Vector body = null;

  Functor functor = null;
  FirstOrderTerm term = null;
  FirstOrderTerm[] arguments = null;

  boolean isVariable = false;

  SyntacticTerm markAsVariable ()
  {
    isVariable = true;
    return this;
  }
  
  SyntacticTerm (String symbol)
    {
      this.symbol = symbol;
    }

  SyntacticTerm (String symbol, Vector body)
    {
      this.symbol = symbol;
      this.body = body;
    }

   final boolean hasBody ()
    {
      return (body != null && !body.isEmpty());
    }

   public String toString ()
    {
      if (!hasBody())
        return symbol;
      
      StringBuffer s = new StringBuffer(symbol);

      s.append("(");
      for (int i=0; i<body.size(); i++)
        {
          s.append(body.get(i));
          if (i<body.size()-1) s.append(",");
        }
      s.append(")");

      return s.toString();
    }

  /* ************************************************************************ */

  /**
   * Converts this parsed <tt>SyntacticTerm</tt> into an actual
   * <tt>FirstOrderTerm</tt> where functors are registered in the
   * provided <tt>Signature</tt> and in the provided
   * <tt>name -> functor</tt> table <tt>functorTable</tt>.
   */
  FirstOrderTerm canonical (Signature signature, HashMap functorTable)
  {
    if (isVariable)
      return Variable.getVariable(symbol);

    if (!hasBody())
      {
	FuzzyFOTLatticeMain.parser.registerFunctor
	  (symbol,functor = signature.functor(symbol,0));
	return new FirstOrderTermStructure(functor);
      }

    FuzzyFOTLatticeMain.parser.registerFunctor
      (symbol,functor = signature.functor(symbol,body.size()));
    arguments = new FirstOrderTerm[body.size()];
    term = new FirstOrderTermStructure(functor,arguments);

    for (int i = 0; i < arguments.length; i++)
      arguments[i] = ((SyntacticTerm)body.get(i)).canonical(signature,functorTable);
    
    return term;
  }
}

/* ************************************************************************ */
