|
QJCC homepage | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
Interface for logic expressions.
Logic expressions can serve for two purposes:
This logic expression definition can be used for both usage types.
Logic expressions are used to deliver a boolean value to say whether a certain expression is true or false in a given universe of Objects. Normally, this universe will be a Map containing key-value pairs and the expressions reasoning about these values.
Example: We have the following program:
Map m=new HashMap;m.put("X",new Integer(5)); Map n=new HashMap;n.put("X",new Integer(6)); LogicExpr le=new EqualsExpr("X",new Integer(6)); boolean bm=le.evaluate(m); boolean bn=le.evaluate(n);In this example,
bm
will be false
while bn
will be true
.
The whole concept becomes increasingly interesting with logical expressions linking other logical expressions according to boolean algebra:
LogicExpr german_chancellor_searcher= new ORExpr( new ANDExpr( new EqualsExpr("PRENAME","Helmut"), new ORExpr( new EqualsExpr("NAME","Schmidt"), new EqualsExpr("NAME","Kohl") ) ), new ANDExpr( new EqualsExpr("PRENAME","Gerhard"), new EqualsExpr("NAME","Schroeder") ) ); Map p1=new HashMap();p1.put("NAME","Kohl");p1.put("PRENAME","Helmut"); Map p2=new HashMap();p2.put("NAME","Beckenbauer");p2.put("PRENAME","Franz"); Map p3=new HashMap();p3.put("NAME","Schroeder");p3.put("PRENAME","Gerhard");
Boolean evaluations are also important in database searches and there especially in the "where" part of select clauses. LogicExpr allows therefore to create an SQL-equivalent of the defined expression. This can be used in the where-part of a select clause. The whole mechanism follows an implicit "column-to-map-entry"-paradigm. If you suppose that each column of an SQL query is stored in certain Map fields (and each row of the query result is put in its own Map), then you can either apply the logical expression to the Map being the result of the query or its SQL-equivalent already to the query itself.
Boolean evaluations as described above do already feature a "hidden" element selection, as the true/false decision picks a subset of the table as result set. Handling queries this way, however, restricts the database handling to those cases which are handleable by single SQL queries.
To overcome this restriction, we go one step further. We define the primary index of a certain database table as the above mentioned universe of constants. The logic expressions are describing database queries which have lists of such primary indexes as result. Having such lists, logical operations can be expressed as boolean algebra combining the lists after set theory.
AND and OR operation are simple intersection and unification operations of result sets. NOT, however, needs the complete universe to perform a rest operation. Archieving the complete universe can be a time-consuming task, therefore we define a sub-interface which delivers the universe.
As LogicExpr objects can be passed through GJSA connections, they can be used quite well as a general querying mechanism between clients and servers in the GJSA framework.
Nested Class Summary | |
static interface |
LogicExpr.Converter
Special interface for value conversion. |
static interface |
LogicExpr.UniverseGenerator
Special interface for universe generation. |
Method Summary | |
java.lang.Object |
clone()
Deep-copy the logic expression. |
void |
convertValues(LogicExpr.Converter c)
Conversion of values in the expressions (e.g. for replacing external representations of database entries with their internal key) |
boolean |
evaluate(java.lang.Object universe)
Is the expression valid in the given universe |
java.lang.Object[] |
getInternalVars()
Only for GJSA class parsers: get class variables to create object from a parsed string. |
int[] |
getResultSet(LogicExpr.UniverseGenerator generator)
Return all elements of the universe which match the expression |
boolean |
isReady()
Returns if the expression is actually evaluable, i.e. any of the evaluating methods can be called. |
boolean |
isTreeReady()
Returns whether this and all deeper expressions are evaluable. |
java.lang.String |
sqlString(java.util.Map identifiers)
Returns an SQL representation of the expression with the constant names replaced by the ones in the given Map. |
Method Detail |
public boolean isReady()
public boolean isTreeReady()
public boolean evaluate(java.lang.Object universe)
public int[] getResultSet(LogicExpr.UniverseGenerator generator)
public java.lang.String sqlString(java.util.Map identifiers)
public java.lang.Object[] getInternalVars()
public void convertValues(LogicExpr.Converter c)
public java.lang.Object clone() throws java.lang.CloneNotSupportedException
Note: This method must be defined in a way that makes it quite complicated to call it for simply cloning a logic expression tree: It returns an Object (which always happens to be a LogicExpr) and it might throw a CloneNotSupportedException - which will (a) never happen and is (b) no RuntimeException, so a caller would always have to catch it.
To make life simpler, there is a static method LogicExprUtilties.cloneLogicExpr() which will call clone() on the root object and take care of return value and exceptions.
java.lang.CloneNotSupportedException
|
QJCC homepage | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |