Basic and Expert Modes

Basic Mode

As the name suggests this is the easiest mode to use we simply use the Solver and Solver$ classes to evaluate an expression, for examle both of thses statements will display 13.0 in the console.

println(Solver.evaluate("sqrt(5^2 + 12^2)"));
println(Solver$.evaluate("sqrt(5^2 + 12^2)").get());

The first is evaluated in the main thread the second evaluated in a separate thread but in both cases any errors will be reported in the console window.

Although the second version is evaluated in a separate thread, the get() method will 'block' the main thread until the evaluation is complete. Using separate threads is only of value for scripts that require a long time to evaluate, and to prevent blocking of the main thread it is necessary to make sure the evaluation is finished before using the get method to fetch the result. See Solver$ class API reference.

The Result class

When an expression is evaluated it returns a object of type Result, for example

Result r1 = Solver.evaluate("sqrt(5^2 + 12^2)");
Result r2 = Solver$.evaluate("sqrt(5^2 + 12^2)").get();

are both valid statements and after execution both r1 and r2 hold the results of the evaluations. A result can be tested to see if it is valid or not like this.

if (r1.isValid())
  println("The answer is " + r1.answer);
else
  println(r1.errorMessage);
  

In most cases we will want to use the actual value of the answer and not just display it. Since QScript uses dynamic typing the Result-answer attribute is an instance of the Argument class.

The Argument class

So in the previous example r1.answer is of type Argument and this class provides a number of methods to convert this to a Java data trpe - so putting it althogether.

float hyp = 0;
Result r1 = Solver.evaluate("sqrt(5^2 + 12^2)");
if (r1.isValid())
  hyp = r1.answer.toFloat();
else
  println(r1.errorMessage);

The other convertion methods are

  • toInteger()
  • toDouble()
  • toBoolean()
  • toString()
  • toVector()
  • toComplex()

but If you attempt an ilegal conversion an error message will be displayed.

Read the API reference for full details on this class.

Expert Mode

The only significant difference between basic and advanced mode is who is responsible for handling errors. In basic mode QScript simply displays the error message and continues, but there will be situations where the user wants to take control when an error occurs.

These are the same as the expressions we discussed in basic mode, but now they are being run in expert mode. Notice the addition of an underscore (_) to the method names - this is how the library distiguishes between the modes.

println(Solver.evaluate_("sqrt(5^2 + 12^2)"));
println(Solver$.evaluate("sqrt(5^2 + 12^2)").get_());

The last 2 lines of code will not work because in this mode the methods throw exceptions which the user has to catch. They can be caught like this -

Result r1 = null;
try {
  r1 = Solver.evaluate_("sqrt(5^2 + 12^2)");
}
catch(SyntaxException e) {
  println(e.getMessage());
}
catch(EvaluationException e) {
  println(e.getMessage());
}

and if using a separate thread -

Result r2 = null;
try {
  r2 = Solver.evaluate("sqrt(5^2 + 12^2)").get_();
}
catch(SyntaxException e) {
  println(e.getMessage());
}
catch(EvaluationException e) {
  println(e.getMessage());
}

The SyntaxException and EvaluationException classes also have information about the error type, and the location in the source code where the error occured.

Everything else regarding the Result and Argument classes also applies in expert mode.