Package com.singularsys.jep
Class EmptyOperatorTable
- java.lang.Object
-
- com.singularsys.jep.EmptyOperatorTable
-
- All Implemented Interfaces:
JepComponent
,OperatorTableI
,java.io.Serializable
- Direct Known Subclasses:
OperatorTable2
public class EmptyOperatorTable extends java.lang.Object implements OperatorTableI
A base class for operator tables, this class has no pre-defined operators. For most cases its subclassOperatorTable2
should be used which has the standard set of operators.This class has a sub-interface
OperatorKey
which is used to index operators, enums using this are defined inOperatorTable2
.- Since:
- 3.4
- Author:
- Richard Morris
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
EmptyOperatorTable.OperatorKey
All operators enums must implement this interface.
-
Constructor Summary
Constructors Modifier Constructor Description EmptyOperatorTable()
Constructor with an empty map.protected
EmptyOperatorTable(java.util.Map<EmptyOperatorTable.OperatorKey,Operator> oldMap)
Constructor with a specified map.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Operator
addOperator(EmptyOperatorTable.OperatorKey key, Operator op)
Add an operator with a given key.Operator
addOperator(EmptyOperatorTable.OperatorKey key, Operator op, Operator existingOp)
Add operator with the same precedence level as an existing operator.Operator
addOperator(Operator op, Operator existingOp)
Add operator with the same precedence level as an existing operator.Operator
appendOperator(EmptyOperatorTable.OperatorKey key, Operator op, Operator existingOp)
Add an operator with a new precedence level which is lower than the existing op.Operator
appendOperator(Operator op, Operator existingOp)
Add an operator with a new precedence level which is lower than the existing op.java.util.Set<java.util.Map.Entry<EmptyOperatorTable.OperatorKey,Operator>>
entrySet()
Operator
getAdd()
Returns the operator used for addition.Operator
getAnd()
Operator
getAssign()
Operator
getCross()
Operator
getDivide()
Operator
getDot()
Operator
getEle()
Operator
getEQ()
Operator
getGE()
Operator
getGT()
EmptyOperatorTable.OperatorKey
getKey(Operator op)
Gets the key associated with a specific operatorOperator
getLE()
JepComponent
getLightWeightInstance()
Gets a light-weight instance suitable for using in multiple threads.Operator
getList()
Operator
getLT()
Operator
getMod()
Operator
getMultiply()
Operator
getNE()
Operator
getNop()
Operator
getNot()
int
getNumOps()
Operator
getOperator(EmptyOperatorTable.OperatorKey key)
Get operator by its key code.protected java.util.Map<EmptyOperatorTable.OperatorKey,Operator>
getOperatorMap()
java.util.Collection<Operator>
getOperators()
Return the full list of operatorsOperator
getOperatorsByName(java.lang.String name)
Get the operator with a given namejava.util.List<Operator>
getOperatorsBySymbol(java.lang.String symbol)
Gets all the operators with a given symbolOperator
getOr()
Operator
getPower()
Operator
getRange()
Operator
getSubtract()
Operator
getUDivide()
unary division i.e. 1/x or x^(-1) *Operator
getUMinus()
Operator
getUPlus()
void
init(Jep jep)
Initialize the component.Operator
insertOperator(EmptyOperatorTable.OperatorKey key, Operator op, Operator existingOp)
Add an operator with a new precedence level which is equal to the existing op.Operator
insertOperator(Operator op, Operator existingOp)
Add an operator with a new precedence level which is equal to the existing op.java.util.Set<EmptyOperatorTable.OperatorKey>
keySet()
void
removeOperator(EmptyOperatorTable.OperatorKey key)
Remove a give operator from the tablevoid
removeOperator(Operator op)
Remove a give operator from the tablevoid
replaceOperator(EmptyOperatorTable.OperatorKey key, Operator op)
Replace a give operator with a new one.void
replaceOperator(Operator oldOp, Operator op)
Replace a give operator with a new one.protected boolean
setBinaryInverseOp(EmptyOperatorTable.OperatorKey key1, EmptyOperatorTable.OperatorKey key2)
protected boolean
setDistributiveOver(EmptyOperatorTable.OperatorKey key1, EmptyOperatorTable.OperatorKey key2)
protected void
setGroupOperatorRelations(EmptyOperatorTable.OperatorKey root, EmptyOperatorTable.OperatorKey inv, EmptyOperatorTable.OperatorKey composite)
Sets relations of three operators.protected void
setGroupOperatorRelations(Operator root, Operator inv, Operator composite)
Sets relations of three operators.protected boolean
setInverseOp(EmptyOperatorTable.OperatorKey key1, EmptyOperatorTable.OperatorKey key2)
boolean
setPrecedenceTable(EmptyOperatorTable.OperatorKey[][] precArray)
Sets the precedence of all operators at one time.protected boolean
setRootOp(EmptyOperatorTable.OperatorKey key1, EmptyOperatorTable.OperatorKey key2)
protected void
setStandardOperatorRelations()
Sets up the standard relations between +,-,*,/OperatorTableI
shallowCopy()
Create a new version of this OperatorTable.protected java.util.Map<EmptyOperatorTable.OperatorKey,Operator>
threadSafeMapCopy()
Creates a copy of the internal map used for operators.java.lang.String
toString()
String rep operators ordered by precedence, one per line.
-
-
-
Constructor Detail
-
EmptyOperatorTable
public EmptyOperatorTable()
Constructor with an empty map.
-
EmptyOperatorTable
protected EmptyOperatorTable(java.util.Map<EmptyOperatorTable.OperatorKey,Operator> oldMap)
Constructor with a specified map. Subclasses should define similar constructors to allow theshallowCopy()
to work.- Parameters:
oldMap
- map with existing set of functions- Since:
- 4.0
-
-
Method Detail
-
getAdd
public Operator getAdd()
Returns the operator used for addition.- Specified by:
getAdd
in interfaceOperatorTableI
-
getAnd
public Operator getAnd()
- Specified by:
getAnd
in interfaceOperatorTableI
-
getAssign
public Operator getAssign()
- Specified by:
getAssign
in interfaceOperatorTableI
-
getCross
public Operator getCross()
- Specified by:
getCross
in interfaceOperatorTableI
-
getDivide
public Operator getDivide()
- Specified by:
getDivide
in interfaceOperatorTableI
-
getDot
public Operator getDot()
- Specified by:
getDot
in interfaceOperatorTableI
-
getEQ
public Operator getEQ()
- Specified by:
getEQ
in interfaceOperatorTableI
-
getEle
public Operator getEle()
- Specified by:
getEle
in interfaceOperatorTableI
-
getGE
public Operator getGE()
- Specified by:
getGE
in interfaceOperatorTableI
-
getGT
public Operator getGT()
- Specified by:
getGT
in interfaceOperatorTableI
-
getLE
public Operator getLE()
- Specified by:
getLE
in interfaceOperatorTableI
-
getLT
public Operator getLT()
- Specified by:
getLT
in interfaceOperatorTableI
-
getList
public Operator getList()
- Specified by:
getList
in interfaceOperatorTableI
-
getMod
public Operator getMod()
- Specified by:
getMod
in interfaceOperatorTableI
-
getMultiply
public Operator getMultiply()
- Specified by:
getMultiply
in interfaceOperatorTableI
-
getNE
public Operator getNE()
- Specified by:
getNE
in interfaceOperatorTableI
-
getNop
public Operator getNop()
- Specified by:
getNop
in interfaceOperatorTableI
-
getNot
public Operator getNot()
- Specified by:
getNot
in interfaceOperatorTableI
-
getOr
public Operator getOr()
- Specified by:
getOr
in interfaceOperatorTableI
-
getPower
public Operator getPower()
- Specified by:
getPower
in interfaceOperatorTableI
-
getRange
public Operator getRange()
- Specified by:
getRange
in interfaceOperatorTableI
-
getSubtract
public Operator getSubtract()
- Specified by:
getSubtract
in interfaceOperatorTableI
-
getUDivide
public Operator getUDivide()
Description copied from interface:OperatorTableI
unary division i.e. 1/x or x^(-1) *- Specified by:
getUDivide
in interfaceOperatorTableI
-
getUMinus
public Operator getUMinus()
- Specified by:
getUMinus
in interfaceOperatorTableI
-
getUPlus
public Operator getUPlus()
- Specified by:
getUPlus
in interfaceOperatorTableI
-
init
public void init(Jep jep)
Description copied from interface:JepComponent
Initialize the component. This methods is called whenever a component is added to Jep. Hence it allows components to keep track of the other components they may rely on.- Specified by:
init
in interfaceJepComponent
- Parameters:
jep
- the current Jep instance
-
getNumOps
public int getNumOps()
-
addOperator
public Operator addOperator(EmptyOperatorTable.OperatorKey key, Operator op)
Add an operator with a given key. The precedence will need to be set bysetPrecedenceTable(OperatorKey[][])
.- Parameters:
key
- key value used to identify the operatorop
- operator- Returns:
- the operator op
-
addOperator
public Operator addOperator(EmptyOperatorTable.OperatorKey key, Operator op, Operator existingOp)
Add operator with the same precedence level as an existing operator.- Parameters:
key
- unique key used to identify the operator.op
- the new operatorexistingOp
- and existing operator, the precedence of this operator will be the same as the existing operator.- Returns:
- the operator
-
addOperator
public Operator addOperator(Operator op, Operator existingOp)
Add operator with the same precedence level as an existing operator. The key will be automatically generated and it can be recovered usinggetKey(Operator)
.- Parameters:
op
- the new operatorexistingOp
- and existing operator, the precedence of this operator will be the same as the existing operator.- Returns:
- the operator
-
insertOperator
public Operator insertOperator(EmptyOperatorTable.OperatorKey key, Operator op, Operator existingOp)
Add an operator with a new precedence level which is equal to the existing op. Increments the precedence of the existing operator all operators with a greater or equal precedence by one.- Parameters:
key
- a key used to identify the operatorop
-existingOp
-- Returns:
- the operator
-
insertOperator
public Operator insertOperator(Operator op, Operator existingOp)
Add an operator with a new precedence level which is equal to the existing op. Increments the precedence of the existing operator all operators with a greater or equal precedence by one. The key will be automatically generated and it can be recovered usinggetKey(Operator)
.- Parameters:
op
-existingOp
-- Returns:
- the operator
-
appendOperator
public Operator appendOperator(EmptyOperatorTable.OperatorKey key, Operator op, Operator existingOp)
Add an operator with a new precedence level which is lower than the existing op. Increments the precedence of all operators with a greater precedence by one.- Parameters:
key
-op
-existingOp
-- Returns:
- the operator
-
appendOperator
public Operator appendOperator(Operator op, Operator existingOp)
Add an operator with a new precedence level which is lower than the existing op. Increments the precedence of all operators with a greater precedence by one. The key will be automatically generated and it can be recovered usinggetKey(Operator)
.- Parameters:
op
- Operator to addexistingOp
- operator to copy precedence from- Returns:
- the operator
-
getKey
public EmptyOperatorTable.OperatorKey getKey(Operator op)
Gets the key associated with a specific operator- Parameters:
op
-- Returns:
- the key, null if op is not in the table
-
removeOperator
public void removeOperator(Operator op)
Remove a give operator from the table- Parameters:
op
-
-
removeOperator
public void removeOperator(EmptyOperatorTable.OperatorKey key)
Remove a give operator from the table- Parameters:
key
-
-
replaceOperator
public void replaceOperator(Operator oldOp, Operator op)
Replace a give operator with a new one. The new operator will have the same precedence as the old one- Parameters:
oldOp
- the operator to be replacedop
- the new operator
-
replaceOperator
public void replaceOperator(EmptyOperatorTable.OperatorKey key, Operator op)
Replace a give operator with a new one. The new operator will have the same precedence as the old one- Parameters:
key
- the key for the operatorop
- the new operator
-
getOperator
public Operator getOperator(EmptyOperatorTable.OperatorKey key)
Get operator by its key code. Most efficient way to get an operator.- Parameters:
key
- code used in the table- Returns:
- the operator or null if not found
-
getOperatorsBySymbol
public java.util.List<Operator> getOperatorsBySymbol(java.lang.String symbol)
Gets all the operators with a given symbol- Specified by:
getOperatorsBySymbol
in interfaceOperatorTableI
- Parameters:
symbol
- symbol used for operator- Returns:
- list of operators with a given symbol, empty list if none are found.
-
getOperatorsByName
public Operator getOperatorsByName(java.lang.String name)
Get the operator with a given name- Specified by:
getOperatorsByName
in interfaceOperatorTableI
- Parameters:
name
- unique name of operator- Returns:
- the operator or null if not found
-
getOperators
public java.util.Collection<Operator> getOperators()
Return the full list of operators- Specified by:
getOperators
in interfaceOperatorTableI
-
entrySet
public java.util.Set<java.util.Map.Entry<EmptyOperatorTable.OperatorKey,Operator>> entrySet()
-
getOperatorMap
protected java.util.Map<EmptyOperatorTable.OperatorKey,Operator> getOperatorMap()
-
keySet
public java.util.Set<EmptyOperatorTable.OperatorKey> keySet()
-
setPrecedenceTable
public final boolean setPrecedenceTable(EmptyOperatorTable.OperatorKey[][] precArray)
Sets the precedence of all operators at one time. An array of arrays of key codes of operators is used to set the table operators with the same precedence are grouped together and listed from tighter binding to loose binding. For examplesetPrecedenceTable(new OperatorKey[][] { { POWER }, { NEG, UPLUS, NOT }, { MUL, DIV, MOD, DOT, CROSS }, { ADD, SU }, { LT, LE, GT, GE }, { EQ, NE }, { AND }, { OR }, { ASSIGN }, });
- Parameters:
precArray
-- Returns:
- flag
-
setInverseOp
protected boolean setInverseOp(EmptyOperatorTable.OperatorKey key1, EmptyOperatorTable.OperatorKey key2)
-
setBinaryInverseOp
protected boolean setBinaryInverseOp(EmptyOperatorTable.OperatorKey key1, EmptyOperatorTable.OperatorKey key2)
-
setRootOp
protected boolean setRootOp(EmptyOperatorTable.OperatorKey key1, EmptyOperatorTable.OperatorKey key2)
-
setDistributiveOver
protected boolean setDistributiveOver(EmptyOperatorTable.OperatorKey key1, EmptyOperatorTable.OperatorKey key2)
-
setGroupOperatorRelations
protected void setGroupOperatorRelations(Operator root, Operator inv, Operator composite)
Sets relations of three operators. A mathematical group has three related operators the root operatorx+y
, the inverseuminus z
, and compositex-y = x+(uminus y)
- Parameters:
root
- the root operator i.e. ADDinv
- the inverse operator i.e. UMINUScomposite
- the composite operator i.e. SUBTRACT
-
setGroupOperatorRelations
protected void setGroupOperatorRelations(EmptyOperatorTable.OperatorKey root, EmptyOperatorTable.OperatorKey inv, EmptyOperatorTable.OperatorKey composite)
Sets relations of three operators. A mathematical group has three related operators the root operatorx+y
, the inverseuminus z
, and compositex-y = x+(uminus y)
- Parameters:
root
- the root operator i.e. ADDinv
- the inverse operator i.e. UMINUScomposite
- the composite operator i.e. SUBTRACT
-
setStandardOperatorRelations
protected void setStandardOperatorRelations()
Sets up the standard relations between +,-,*,/
-
toString
public java.lang.String toString()
String rep operators ordered by precedence, one per line.- Overrides:
toString
in classjava.lang.Object
-
shallowCopy
public OperatorTableI shallowCopy()
Create a new version of this OperatorTable. Operators are copied into new table. However Operators whose pfmc implement JepComponent are duplicated. This means the table could be used in a separate thread without problem. Subclasses should override with method to return a table of the correct type, a typical implementation would be@Override public OperatorTableI shallowCopy() { Map<OperatorKey, Operator> map = this.threadSafeMapCopy(); return new MyOperatorTable(map); }
using thethreadSafeMapCopy()
method to return a copy of the map of operators and a constructor taking this map which uses theEmptyOperatorTable(Map)
constructor.- Specified by:
shallowCopy
in interfaceOperatorTableI
- Returns:
- a new Table
-
getLightWeightInstance
public JepComponent getLightWeightInstance()
Description copied from interface:JepComponent
Gets a light-weight instance suitable for using in multiple threads.- Specified by:
getLightWeightInstance
in interfaceJepComponent
- Returns:
- this
-
threadSafeMapCopy
protected java.util.Map<EmptyOperatorTable.OperatorKey,Operator> threadSafeMapCopy()
Creates a copy of the internal map used for operators. Most operators are simply copied to the new table. However Operators whose pfmc's implement JepComponent are duplicated, this means the table could be used in multiple threads without problem.- Returns:
- a new map with one entry for each each operator in the current table.
-
-