Package com.singularsys.extensions.field

A type system for Jep allowing use of new datatype like matrices or rational numbers. See the Field Documentation.

New datatypes can be added to the Jep system by implementing the FieldI interface. This defines methods such as add(Object l, Object r) which the field must implement. Different fields can be mixed together using to produce a complex type system.

In general each method takes one or two Objects as input and returns an Object. The methods should return null if do not know how to handle a particular input. In rare circumstances they can also throw an EvaluationException. A typical implementation for add would be

   @Override
   public Object add(Object l, Object r) throws EvaluationException {
        if(l instanceof Double && r instanceof Double) {
            double ld = ((Double) l).doubleValue();
            double rd = ((Double) r).doubleValue();
               return Double.valueOf(ld+rd);        
        }
        return null;
    }

Configuration

The FieldOperatorTable is a OperatorTable which allow one or more fields to be used by Jep for evaluation. The FieldCollection can be given multiple field implementations and use each in turn to calculate the results. If the first field returns non-null then that value will be used, otherwise the next in the list will be tried.

A typical implementation would be

    FieldColection fc = new FieldColection();
    fc.addField(new DoubleField());
    fc.addField(new StringField());
    fc.addField(new BooleanField());
    FieldOperatorTable fieldOperatorTable = new FieldOperatorTable(fc);
    Jep jep = new Jep(fieldOperatorTable);
This allows double number, strings and boolean logical operations to be used together.

Example Field

An implementation for String's would be

// just need to implement additive and comparative operations
public class StringField  implements FieldI {

    // If both arguments are string then concatenate the results
    // otherwise return null
    @Override
    public Object add(Object l, Object r) throws EvaluationException {
        if(l instanceof String && r instanceof String)
            return ((String) l)+((String)r);
        return null;
    }
    
    // Test equality of two strings, 
    // returns true is equal, false if not equal or null if not strings
    @Override
    public Boolean eq(Object l, Object r) throws EvaluationException {
        if(l instanceof String && r instanceof String) {
            String ls = (String) l;
            String rs = (String) r;
            boolean res = ls.equals(rs);
            return res;
        }
        return null;
    }
    // Other comparison operations need to be implemented
    public Boolean ne(Object l, Object r)  { .... }
    public Boolean gt(Object l, Object r)  { .... }
    public Boolean ge(Object l, Object r)  { .... }
    public Boolean lt(Object l, Object r)  { .... }
    public Boolean le(Object l, Object r)  { .... }
    
    // Null results for other operators
    public Object sub(Object l, Object r)  { return null; }
    public Object neg(Object l)            { return null; }
    public Object mul(Object l, Object r)  { return null; }
    public Object div(Object l, Object r)  { return null; }
    public Object mod(Object l, Object r)  { return null; }
    public Object pow(Object l, Object r)  { return null; }
    public Boolean and(Object l, Object r) { return null; }
    public Boolean or(Object l, Object r)  { return null; }
    public Boolean not(Object l)           { return null; }
}

Sub packages

com.singularsys.extensions.field.implementations
Standard implementation, and utility class for constructing fields.
com.singularsys.extensions.field.functions
Some functions which can be used with most fields.
Since:
Jep 3.5 / Extensions 2.0
See Also:
Field documentation