Package util
Class MathExpressionManager
- java.lang.Object
-
- util.MathExpressionManager
-
public class MathExpressionManager extends java.lang.ObjectObjects of this class attempt to optimize and then evaluate a math expression. If the expression's MathExpression object is present in the functions database (actually an ArrayList of MathExpression objects) then it just retrieves the scanned and validated code and solves it. Else,it must go through all the steps and then solve it. This happens when: 1. The expression is a new one. 2. The expression has been computed before but to limit memory usage, it has been deleted from memory.- Author:
- GBEMIRO
-
-
Constructor Summary
Constructors Constructor Description MathExpressionManager()
-
Method Summary
Modifier and Type Method Description static java.lang.StringanalyzeSolution(MathExpression expr)booleancanOptimizeFunction(java.lang.String expression)checks if the MathExpression object passed to it as an argument can be optimized or notbooleancontains(MathExpression func)MathExpressioncreateFunction(java.lang.String expr)intgetDrgStatus()MathExpressiongetFunction(MathExpression function)MathExpressiongetFunctionAt(int index)MathExpressiongetFunctionByExpression(java.lang.String expression)MathExpressiongetFunctionByScanner(java.util.ArrayList<java.lang.String> scanner)java.util.ArrayList<MathExpression>getFunctions()intgetMaxSize()VariableManagergetVariableManager()A workspace uses a MathExpressionManager to manage and optimize functions used in calculations.booleanisEmpty()MathExpressionoptimizeFunction(java.lang.String name)voidremoveFunctionAt(int index)removes the MathExpression object at the specified locationvoidremoveFunctionByName(java.lang.String funcName)Removes the MathExpression object in the store that goes by that namevoidremoveFunctionByScanner(java.util.ArrayList<java.lang.String> scanner)removes the MathExpression object that has that scannervoidsetDrgStatus(int drgStatus)voidsetFunctions(java.util.ArrayList<MathExpression> functions)voidsetMaxSize(int maxSize)java.lang.Stringsolve(java.lang.String expr)Takes a math expression, optimizes it if possible, and then solves it.voidstoreFunction(MathExpression function)stores a MathExpression in objects of this class.
-
-
-
Method Detail
-
getFunctions
public java.util.ArrayList<MathExpression> getFunctions()
- Returns:
- all function objects stored by this MathExpressionManager object
-
setFunctions
public void setFunctions(java.util.ArrayList<MathExpression> functions)
- Parameters:
functions- the store of MathExpression objects that this MathExpressionManager object will store
-
getMaxSize
public int getMaxSize()
-
setMaxSize
public void setMaxSize(int maxSize)
-
setDrgStatus
public void setDrgStatus(int drgStatus)
-
getDrgStatus
public int getDrgStatus()
-
getVariableManager
public VariableManager getVariableManager()
A workspace uses a MathExpressionManager to manage and optimize functions used in calculations. Each of these functions have different VariableManager objects but all these objects are actually accessing, storing creating and modifying their variables and constants from one single Variable database( actually an ArrayList of Variable objects ) in the program. This method hence allows an object of this class to return a reference to any of the VariableManager objects associated with the MathExpression objects it manages,since it knows that the destination database(ArrayList) is the same,i.e all the MathExpression objects are only accessing the same set and copy of variables and constants.- Returns:
- the VariableManager object associated with the first MathExpression object stored by objects of this class.
-
canOptimizeFunction
public boolean canOptimizeFunction(java.lang.String expression)
checks if the MathExpression object passed to it as an argument can be optimized or not- Parameters:
expression- : The expression to evaluate- Returns:
- true if the MathExpression is optimizable
-
storeFunction
public void storeFunction(MathExpression function)
stores a MathExpression in objects of this class. The storage is done in such a way that the object is inserted from the front.- Parameters:
function- the new MathExpression to store
-
contains
public boolean contains(MathExpression func)
- Parameters:
func- the MathExpression object to search for- Returns:
- true if the MathExpression object is found
-
getFunction
public MathExpression getFunction(MathExpression function)
-
getFunctionAt
public MathExpression getFunctionAt(int index)
- Parameters:
index- the location from which we wish to retrieve the MathExpression object- Returns:
- the MathExpression object stored at the specified index.
-
getFunctionByExpression
public MathExpression getFunctionByExpression(java.lang.String expression) throws java.lang.NullPointerException
- Parameters:
expression- the String representation of the MathExpression object- Returns:
- the MathExpression object in the store that goes by that name or null if none is found.
- Throws:
java.lang.NullPointerException
-
getFunctionByScanner
public MathExpression getFunctionByScanner(java.util.ArrayList<java.lang.String> scanner)
- Parameters:
scanner- the scanner object to search for in the store- Returns:
- the MathExpression object in the store that possesses that scanned form
-
removeFunctionAt
public void removeFunctionAt(int index)
removes the MathExpression object at the specified location- Parameters:
index- the location from which the MathExpression is to be removed
-
removeFunctionByName
public void removeFunctionByName(java.lang.String funcName)
Removes the MathExpression object in the store that goes by that name- Parameters:
funcName- the String representation of the MathExpression object
-
removeFunctionByScanner
public void removeFunctionByScanner(java.util.ArrayList<java.lang.String> scanner)
removes the MathExpression object that has that scanner- Parameters:
scanner- the scanner object to search for in the store
-
optimizeFunction
public MathExpression optimizeFunction(java.lang.String name)
- Parameters:
name- the name or String format of the MathExpression object to be optimized- Returns:
- the optimized MathExpression object such that it is given its attributes already processed and ready for use
-
isEmpty
public boolean isEmpty()
- Returns:
- true if this object has no MathExpression objects to manage.
-
createFunction
public MathExpression createFunction(java.lang.String expr)
- Parameters:
expr- The expression to be evaluated by the MathExpression to be created.
-
solve
public java.lang.String solve(java.lang.String expr) throws java.lang.NullPointerExceptionTakes a math expression, optimizes it if possible, and then solves it. Else if the expression cannot be optimized, it has to interprete and then evaluate it. It then stores the expression.- Parameters:
expr- The expression to evaluate.- Returns:
- the result.
- Throws:
java.lang.NullPointerException
-
analyzeSolution
public static java.lang.String analyzeSolution(MathExpression expr)
-
-