//  FILE. . . . . d:/hak/hlt/src/hlt/osf/hoot/sources/HootMain.java
//  EDIT BY . . . Hassan Ait-Kaci
//  ON MACHINE. . Hak-Laptop
//  STARTED ON. . Sat May  4 04:49:34 2019

/**
 * @version     Last modified on Sun Aug 25 16:43:53 2019 by hak
 * @author      <a href="mailto:hak@acm.org">Hassan A&iuml;t-Kaci</a>
 * @copyright   &copy; <a href="http://www.hassan-ait-kaci.net/">by the author</a>
 */

package hlt.osf.hoot;

import java.io.File;
import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalTime;

import hlt.osf.util.BitCode;
import hlt.language.util.Error;

/**
 * This is the main entry file for running any version of the OSF
 * constraint language <a href="../ctr16.pdf">HOOT</a>
 * (<b>H</b>ierarchical <b>O</b>ntologies of <b>O</b>bjects and
 * <b>T</b>ypes). It is based on the internal representation of the OSF
 * data structure described in the <a href="../ctr16.pdf">HOOT
 * specification</a>. It also relies on operations to build and
 * manipulate such internal structures, all of which are defined in the
 * other <a href="index.html">Jacc grammar files</a>. It also provides
 * the basic lattice operations for unification and generalization,
 * together with the machinary for inference remembering tag bindings
 * for undoing purposes such as bactracking. This class creates a
 * tokenizer and a parser for HOOT and starts an interactive session
 * with HOOT.
 */
public class HootMain
{
  final static String version = "0.00";

  final static String now ()
  {
    return LocalDate.now()+" - "+LocalTime.now().toString().substring(0,8);
  }

  final static void p ()
  {
    System.out.println();
  }

  final static void p (String line)
  {
    System.out.println(line);
  }

  final static void welcome ()
    {
      p();
      p("****************************************************");
      p("***                                              ***");
      p("*** This is HOOT - a language to build and query ***");
      p("***                                              ***");
      p("*** Hierarchical Ontologies of Objects and Types ***");
      p("***                                              ***");
      p("****************************************************");
      p();
      p("*** HOOT Version "+version+" - run of "+now());
      p("*** Interactive mode: type '%help.' for where to go.");
      p();
    }

  final static void exit ()
    {
      p();
      p();
      p("******************************************************");
      p("*** Exiting HOOT Version "+version+" on "+now());
      p("******************************************************");
      p();
      System.exit(0);
    }

  final static void report (HootParser parser, Throwable issue, String reason)
  {
    parser
      .errorManager()
      .reportError(new Error()
		   .setLabel(reason+": ")
		   .setMsg(issue.getMessage()+" reporting")
		   .setSee("..."));
  }

  final public static void main (String args[])
    {
      // create a HOOT tokenizer:
      HootTokenizer tokenizer = new HootTokenizer();
      // use it to create a HOOT parser:
      HootParser parser = new HootParser(tokenizer);
      // say hello:
      welcome();
      // set the bit-vector notation using +/1 (instead of default 1/0)
      BitCode.setOnChar('+');
      BitCode.setOffChar('-');

      try
        {
	  // load all the files in the command line:
          for (int i=0; i<args.length; i++)
            {
              parser.displayLine("*** Loading file: "+args[i]);
              parser.parse(args[i]);
            }
	  // enter an interactive session:
          tokenizer.setInteractive();
	  // XML representation of the parse tree:
	  parser.setTreeType("XML");
	  // initiate parsing the interactive input:
          parser.parse();
        }
      catch (IOException issue)
        {
          report(parser,issue,"IO Error");
        }
      catch (Throwable issue)
        {
          report(parser,issue,"Fatal Error");
          issue.printStackTrace();
        }
      finally
        {
          exit();
        }
    }
}
