Class Console

java.lang.Object
com.singularsys.jepexamples.consoles.Console
Direct Known Subclasses:
BigDecimalConsole, CPConsole, PostfixEvaluationConsole, PrefixDumperConsole, PrintConsole, VectorConsole

public class Console extends Object
This class implements a simple command line utility for evaluating mathematical expressions.
   Usage: java com.singularsys.jepexamples.consoles.Console [expression]
 
If an argument is passed, it is interpreted as an expression and evaluated. Otherwise, a prompt is printed, and the user can enter expressions to be evaluated.

This class has been designed to be subclassed to allow different console applications. The methods

 public void initialise()
 public Object processEquation(Node node) throws Exception
 public boolean testSpecialCommands(String command)
 public void printPrompt()
 public void printIntroText()
 public void printHelp()
 
can all be overwritten.

Furthermore, main should be overwritten. For example

        public static void main(String args[]) {
                Console c = new DJepConsole();
                c.run(args);
    }
 

The main input loop is approximately

 initialise();
 printIntroText();
 print(getPrompt());
 String command;
 while((command = getCommand()) != null)
 {
        if(command.equals("quit") || command.equals("exit"))
                break;
        if(!testSpecialCommands(command)) continue;
   try {
          Node n = j.parse(command);
          processEquation(n);
   } catch(Exception e) {}
        print(getPrompt());
 }
 
  • Field Details

    • jep

      protected Jep jep
      Main Jep object
    • doubleFormat

      protected String doubleFormat
      Format for double output
    • history

      protected final List<String> history
      History
    • showHistory

      protected boolean showHistory
  • Constructor Details

    • Console

      public Console()
      Constructor
  • Method Details

    • initialise

      public void initialise()
      sets up all the needed objects.
    • run

      public void run(String[] args)
      The main entry point with command line arguments
    • inputLoop

      public void inputLoop()
      The main input loop for interactive operation. Repeatedly calls getCommand() and processCommand().
    • processCommand

      public boolean processCommand(String command)
      Process a single command.
      1. Calls testSpecialCommands(String)
      2. Tests for exit, break, and altered results.
      3. Adds the command to the history.
      4. Parses the command.
      5. Calls processEquation(Node)
      6. Checks for errors, calling handleError(Exception) in necessary
      Parameters:
      command - The line to be processed
      Returns:
      false if un-recoverable error or 'quit' or 'exit'
    • processEquation

      public Object processEquation(Node node) throws JepException
      Performs the required operation on a node. Typically, evaluates the node and prints the value.
      Parameters:
      node - Node representing expression
      Returns:
      The result of the calculation
      Throws:
      JepException - if a Parse or evaluation error
    • getCommand

      protected String getCommand()
      Get a command from the input.
      Returns:
      null if an IO error or EOF occurs.
    • getPrompt

      public String getPrompt()
      Prints the prompt string.
    • printStdHelp

      public final void printStdHelp()
      Prints a standard help message. Type 'quit' or 'exit' to quit, 'help' for help.
    • printHelp

      public void printHelp()
      Print help message.
    • printIntroText

      public void printIntroText()
      Prints introductory text.
    • printFuns

      public void printFuns()
      Prints a list of defined functions.
    • printOps

      public void printOps()
      Prints a list of defined operators.
    • printVars

      public void printVars()
      Prints a list of variable.
    • testSpecialCommands

      public Console.SPEC_ACTION testSpecialCommands(String command)
      Checks for special commands. For example a subclass may have a verbose mode switched on of off using the command
       verbose on
       

      This method can be used detected this input, perform required actions and skip normal processing by returning true.

      In general subclasses should call the superclass methods to test for special commands that class implements

      Parameters:
      command -
      Returns:
      SPEC_ACTION.CONTINUE - continue processing this equation, SPEC_ACTION.BREAK - stop processing this equation and get the next line of input, SPEC_ACTION.ALTERED - the input text has been altered, SPEC_ACTION.EXIT stop the program
      See Also:
    • setAlteredCommand

      public void setAlteredCommand(String alt)
      Set the command used if @link{SPEC_ACTION.ALTERED} returned.
      Parameters:
      alt -
    • setFormat

      public void setFormat(String format)
    • handleError

      public boolean handleError(Exception e)
      Handle an error in the parse and evaluate routines. Default is to print the error message for JepExceptions and a stack trace for other exceptions
      Parameters:
      e -
      Returns:
      false if the error cannot be recovered and the program should exit
    • split

      public String[] split(String s)
      Splits a string on spaces.
      Parameters:
      s - the input string
      Returns:
      an array of the tokens in the string
    • print

      public void print(Object o)
      Prints a line of text no newline. Subclasses should call this method rather than System.out.print to allow for output to different places.
    • toString

      public String toString(Object o)
      Return string representation of object. Used the doubleFormat if specified.
      Parameters:
      o -
      Returns:
    • println

      public void println(Object o)
      Prints a line of text followed by a newline. Subclasses should call this method rather than System.out.print to allow for output to different places.
    • main

      public static void main(String[] args)
      Creates a new Console object and calls run()