Skip to content

Expression parsing

Antoine Tran edited this page Aug 11, 2019 · 7 revisions

Now that you know how the library is organized, you might find useful to be able to use it.
As you already know, Atosym has several features regarding expression parsing. Each of them (simplification, differentiation, etc) corresponds to a different MaskOperator.


A MaskOperator handles several things :

  • An input, being the initial expression the operator must base itself on
  • A container, being where the operator will store the result after computing it
  • Extra data, not related neither to the input nor to the container, but necessary to compute the result

Inputs and containers are defined by a class called Masks. A Mask is a simple mutable object wrapping a String, belonging to a context. For further information about contexts, see Defining a calculation environment.


It is not recommended to parse expressions using the operators directly. Instead, you should use a MaskOperatorHandler. MaskOperatorHandler is the manager class for the different operators, containing extra features :

  • A compute() method requiring an operator type. It will look through the different operators to retrieve the matching one.
    It also provides an optional Consumer<Mask> parameter than performs the defined action to the container, after reloading it.
  • A boolean field, defining whether after computing a result, it should be used as the default input for the next calculation. Note that regardless of the value of the boolean, the user will still be able to choose an input and a container. This parameter serves only when no input is given.
  • Direct conversions from the default (also called current) mask handled to primitive types (string, int, float)
  • Parsing chaining, to simplify and make the code clearer

Example

// to complete
package test;
import net.akami.mask.core.*;

public class OperatorTester {

    public static void main(String... args) {

        MaskOperatorHandler handler = MaskOperatorHandler.DEFAULT;
        Mask in = new Mask("(a+b)^2");
        Mask out = new Mask();

        String result = handler
            .compute(MaskSimplifier.class, in, out, null)
            .asExpression();

        System.out.println(result);
        System.out.println(out.getExpression());
    }
}

Output :

a^2 + b^2 + 2ab
a^2 + b^2 + 2ab