Package hussam.math.operations.dataBase

Used to store default Operators, operands and functions.

See:
          Description

Interface Summary
OperatorSourceThis resembles a Data source of Special Arguments for any expression such as Operands, Functions and Variables, etc..
 

Class Summary
OperatorFactoryDBThis is the default DataSource.
OperatorFactoryDB.CommaOperandThis class was used in previous Versions of the ExpressionReader implementations.
OperatorSourceImplThis is the default implemetation of the OperatorSource interface.
OperatorSourceWrapperThis DataSource wrapps a Datasource and gives the user means to access the both sources without changing the content of the base source.
 

Package hussam.math.operations.dataBase Description

Used to store default Operators, operands and functions. This package is used to map all operands, functions, variables and Constants used in any expression. Also you can have ready made OperatorFactorys. For example: useing the default data store, which is OperatorFactoryDB sole instance, you can have all default functions and operands. if you needed a Divide Operand, for example you can call:

Operand divide = OperatorFactoryDB.getInstance().getOperand("/");
divide.setNext(new ConstantNumber(12));
divide.setPrevious(new ConstantNumber(3));
System.out.println(divide.result());

Output:
4.0
There is also the OperatorSourceWrapper which can be used to wrap OperatorSources so that you can add factories in it without effecting the underlying OperatorSource.

To Add New Functions

You can easily add new functions. Let us suppose you want to add (isEven) Function that should return true if the first argument is even and false otherwise. This is how you can do it:
//Construc a new Function with the isEven implementation.
class IsEven extends Function{
        IsEven(){
                super("IsEven");
        }
        //comute the result
        public double result()throws MathException{
                //check if there is at least one argument in the function
                //other wise throw an Exception
                checkReady(1);
                //compute the value by taking the result of the first argument.
                if(getOperation(0).result()%2==0)return OperatorFactoryDB.TRUE;//or 1;
                return OperatorFactoryDB.FALSE;//or 0;
        }
}
After constructing this class, Add it to the OperatorFactoryDB so that is can create instances as needed.
public static void main(String[]args){
    //get the dataBase of all factories.
    OperatorSourceImpl source=OperatorFactoryDB.getInstance();
    //add a new factory with the name and discription and the default number of arguments.
    source.addFunction(new OperatorFactoryImpl(
                "IsEven","Returns true if the number enterred is true!",1){
        //Return a new Function
        public Function getNewOperator(){
                return new IsEven();    
        }
    });
}
That is it, now you can write this expression and have it evaluated:
OperationReader reader = ExpressionReader.getInstance();
Operation o1=reader.readOperation("if (isEven(4) , 100, 1000)");
System.out.println(o1.result());
//output: 100

Package Specification

Needs operations package

Related Documentation

For overviews, tutorials, examples, guides, and tool documentation, please see: