Class Console

java.lang.Object
com.singularsys.jepexamples.consoles.Console
Direct Known Subclasses:
BigDecimalConsole, ChainedConsole, CPConsole, DJepConsole, FieldConsole, LambdaConsole, MatrixConsole, MrpEvalConsole, PostfixEvaluationConsole, PrefixDumperConsole, PrintConsole, RpEvalConsole, StructuredConsole, VectorConsole, XJepConsole

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
    • altered

      protected String altered
  • 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
      7. Calls finaliseCommand(String, Node, Object) to perform any post-processing actions
      Parameters:
      command - The line to be processed
      Returns:
      false if un-recoverable error or 'quit' or 'exit'
    • parseEvaluate

      public Console.NodeRes parseEvaluate(String command) throws JepException
      Throws:
      JepException
    • 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:
      One of
      • SPEC_ACTION.CONTINUE - continue parsing and evaluation this equation,
      • SPEC_ACTION.BREAK - stop processing this equation and add it to the history
      • SPEC_ACTION.EMPTY - stop processing this equation does not add it to the history
      • SPEC_ACTION.ALTERED - the input text has been altered, reprocess the new text
      • SPEC_ACTION.EXIT stop the program
      See Also:
    • 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
    • finaliseCommand

      public boolean finaliseCommand(String command, Node node, Object res)
      Perform any post-processing of a command after evaluation.
      Parameters:
      command - the string expression
      node - parsed expression
      res - result of evaluation
      Returns:
      true, false indicates the program should exit.
      Since:
      Jep 4.1
    • getCommand

      public 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.
    • getHistory

      public List<String> getHistory()
      Get the history of past commands
      Returns:
      a list of all previous command in order
      Since:
      Jep 4.1
    • 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.
    • printFunctionEntry

      public void printFunctionEntry(String name, PostfixMathCommandI pfmc)
      Print an individual
      Parameters:
      name - name of function
      pfmc - implementing function
      Since:
      Jep 4.1
    • 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.
    • clearVars

      public void clearVars()
      Clears all the variables, including constants, from Jep
      Since:
      Jep 4.1
    • setIndexShift

      public void setIndexShift(int i)
      Set the index shift for arrays.
      Parameters:
      i - the shift, either 0 or 1
      Since:
      Jep 4.1
    • setAlteredCommand

      public void setAlteredCommand(String alt)
      Set the command used if Console.SPEC_ACTION.ALTERED returned.
      Parameters:
      alt -
    • printHistory

      public void printHistory()
      Print the history
    • setFormat

      protected Console.SPEC_ACTION setFormat(String command, String[] args)
    • setFormat

      public void setFormat(String format)
      Sets the format for numbers
      Parameters:
      format -
    • setMonitor

      public void setMonitor(String[] args)
      Sets up monitoring of the call tree. Calls setEvaluator(Evaluator) with a MonitoringEvaluator, sub-classes may need to override this method disable this feature.
      Parameters:
      args - second argument can be one of "off", "calls" or "times"
      Since:
      Jep 4.1
    • setEvaluator

      public void setEvaluator(Evaluator ev)
      Sets the evaluator used. Default method simple calls Jep.setComponent(com.singularsys.jep.JepComponent) with the evaluator.
      Parameters:
      ev - the evaluator to use
      Since:
      Jep 4.1
    • showCalls

      public void showCalls()
      Shows a summary of function, operator, variable and constant calls.
      Since:
      Jep 4.1
    • showTree

      public void showTree()
      Shows the call tree with counts and times.
      Since:
      Jep 4.1
    • 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()
    • getJep

      public Jep getJep()