Eval (query directive)

From WandoraWiki
(Difference between revisions)
Jump to: navigation, search
(Examples)
(Constructor)
Line 26: Line 26:
 
== Constructor ==
 
== Constructor ==
  
Eval()
+
Eval(String script)
 +
 
 +
Eval(String script,Object param)
  
 
== Examples ==
 
== Examples ==

Revision as of 11:23, 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(String script)

Eval(String script,Object param)

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"))

Another way to store information is using the param object. Here we pass a java script table {sum:0} to Eval constructor and then access it in the script using param.

importPackage(org.wandora.query2);
new Last(
  new Eval(
    "param.sum+=parseInt(val);"+
    "param.sum;"
  ,{sum:0}).as("#sum")
  .from(
    new Count(
      new Instances()
    ).from(new Instances())
  )
)
Personal tools