Eval (query directive)
Line 3: | Line 3: | ||
Evaluates a script. The script can do simple modifications to the input value or be arbitrarily complex. | Evaluates a script. The script can do simple modifications to the input value or be arbitrarily complex. | ||
− | The script context is initialized with | + | The script context is initialized with some values. |
* ''input'' contains the entire input row. | * ''input'' contains the entire input row. | ||
* ''context'' contains the query context, you can get the active topic map from it. | * ''context'' contains the query context, you can get the active topic map from it. | ||
* ''val'' contains the active value of the input row. | * ''val'' contains the active value of the input row. | ||
+ | * ''dir'' contains the ''Eval'' directive itself. | ||
+ | |||
+ | The ''Eval'' directive contained in ''dir'' variable has some useful methods. | ||
+ | |||
+ | * ''set(String key,Object value)'' Sets an object in an internal hash map. You can use this to store anything you like. It is cleared between queries. Ff the same sub-query is executed several times in a query, it will retain its contents between each execution. | ||
+ | * ''get(String key)'' Gets an object from the internal hash map. | ||
+ | * ''empty()'' Returns an empty result iterator. | ||
The value on the last lime of the script is the return value of the script. it can be one of four things: | The value on the last lime of the script is the return value of the script. it can be one of four things: | ||
Line 15: | Line 22: | ||
* Any other object which will be added in the input row with the default column name. | * Any other object which will be added in the input row with the default column name. | ||
− | In most cases the last option is sufficient and is by far the easiest to implement. | + | In most cases the last option is sufficient and is by far the easiest to implement. Note that returning null falls under the last case and it will produce a result row with a null value. Easiest way to return nothing, that is zero result rows, is to use the ''empty'' method in the ''dir'' variable. Just doing ''dir.empty()'' creates a new empty ''ResultIterator''. |
== Constructor == | == Constructor == | ||
Line 29: | Line 36: | ||
.from(new BaseName()) | .from(new BaseName()) | ||
.from(new Instances()) | .from(new Instances()) | ||
+ | |||
+ | Following example returns instances that have the string "type" in their base name. (Note that you can do this easier with [[Regex (query directive) |Regex]] directive.) Note the use of ''input'' variable to return the input row as is and ''dir.empty()'' to return nothing. | ||
+ | |||
+ | importPackage(org.wandora.query2); | ||
+ | new Eval( | ||
+ | "if(val!=null && val.indexOf(\"type\")!=-1) "+ | ||
+ | " input;"+ | ||
+ | "else"+ | ||
+ | " dir.empty();" | ||
+ | ).from( | ||
+ | new BaseName().from( | ||
+ | new Instances().as("#instance") | ||
+ | ) | ||
+ | ) | ||
+ | |||
+ | Next example demonstrates the internal hash map. The eval part returns the previously stored object. | ||
+ | |||
+ | importPackage(org.wandora.query2); | ||
+ | new Eval( | ||
+ | "ret=dir.get(\"old\");"+ | ||
+ | "dir.set(\"old\",val);"+ | ||
+ | "ret;" | ||
+ | ).as("#eval").from(new Instances().as("#instance")) | ||
[[Category:Query directives]] | [[Category:Query directives]] |
Revision as of 10:44, 25 August 2009
Description
Evaluates a script. The script can do simple modifications to the input value or be arbitrarily complex.
The script context is initialized with some values.
- input contains the entire input row.
- context contains the query context, you can get the active topic map from it.
- val contains the active value of the input row.
- dir contains the Eval directive itself.
The Eval directive contained in dir variable has some useful methods.
- set(String key,Object value) Sets an object in an internal hash map. You can use this to store anything you like. It is cleared between queries. Ff the same sub-query is executed several times in a query, it will retain its contents between each execution.
- get(String key) Gets an object from the internal hash map.
- empty() Returns an empty result iterator.
The value on the last lime of the script is the return value of the script. it can be one of four things:
- A ResultIterator object which is returned as is as the result of the directive. It may be an empty iterator or iterate through any number of rows
- A ResultRow object which will be the single result row of the dirictive.
- An ArrayList containing ResultRow which will be converted to a ResultIterator iterating through the rows in the list.
- Any other object which will be added in the input row with the default column name.
In most cases the last option is sufficient and is by far the easiest to implement. Note that returning null falls under the last case and it will produce a result row with a null value. Easiest way to return nothing, that is zero result rows, is to use the empty method in the dir variable. Just doing dir.empty() creates a new empty ResultIterator.
Constructor
Eval()
Examples
Following example selects base names of all instances of the input topic and adds the prefix "BaseName: " before each of them.
importPackage(org.wandora.query2); new Expr("\"BaseName: \"+val") .from(new BaseName()) .from(new Instances())
Following example returns instances that have the string "type" in their base name. (Note that you can do this easier with Regex directive.) Note the use of input variable to return the input row as is and dir.empty() to return nothing.
importPackage(org.wandora.query2); new Eval( "if(val!=null && val.indexOf(\"type\")!=-1) "+ " input;"+ "else"+ " dir.empty();" ).from( new BaseName().from( new Instances().as("#instance") ) )
Next example demonstrates the internal hash map. The eval part returns the previously stored object.
importPackage(org.wandora.query2); new Eval( "ret=dir.get(\"old\");"+ "dir.set(\"old\",val);"+ "ret;" ).as("#eval").from(new Instances().as("#instance"))