Query language
(→Introduction) |
(→Introduction) |
||
Line 29: | Line 29: | ||
! #DEFAULT | ! #DEFAULT | ||
|- | |- | ||
+ | | align="center" | 9 | ||
+ | |} | ||
+ | |||
+ | Now lets modify the query slightly. | ||
+ | |||
+ | 1 importPackage(org.wandora.query2); | ||
+ | 2 new Count( | ||
+ | 3 new Instances() | ||
+ | 4 ).from( | ||
+ | 5 new Instances() | ||
+ | 6 ) | ||
+ | |||
+ | The ''from'' method on line 4 causes the input for the ''Count'' directive to come from some other directive. In this case we use again an ''Instances'' directive. The execution of this query goes as follows. The initial input, the currently open topic, first goes to the directive inside the ''from'' part, that is ''Instances'' directive on line 5. This returns the instances of the currently open topic. Then each of these result rows are fed one at a time to the ''Count'' directive and all the results of ''Count'' are combined. The ''Count'' directive itself works exactly as in previous example, it only gets a different input this time. Now it counts the instances of all instances of the currently open topic. We still only get one column in the final result because at each step the default column gets overwritten. The final result might look something like this. | ||
+ | |||
+ | {| border="1" | ||
+ | ! #DEFAULT | ||
+ | |- | ||
+ | | align="center" | 7 | ||
+ | |- | ||
+ | | align="center" | 2 | ||
+ | |- | ||
+ | | align="center" | 5 | ||
+ | |- | ||
+ | | align="center" | 0 | ||
+ | |- | ||
+ | | align="center" | 4 | ||
+ | |- | ||
+ | | align="center" | 1 | ||
+ | |- | ||
+ | | align="center" | 5 | ||
+ | |- | ||
+ | | align="center" | 1 | ||
+ | |- | ||
+ | | align="center" | 9 | ||
+ | |} | ||
+ | |||
+ | In the above table each number tells the number of instances of some topic which is an instance of the currently open topic. But this isn't very useful since we can't know which number corresponds to which topic. So let's modify the query a bit again. | ||
+ | |||
+ | 1 importPackage(org.wandora.query2); | ||
+ | 2 new Count( | ||
+ | 3 new Instances() | ||
+ | 4 ).as("#count").from( | ||
+ | 5 new Instances().as("#instance") | ||
+ | 6 ) | ||
+ | |||
+ | The ''as'' methods on line 4 and 5 reset the column name to something other than the default. On line 5 we change the column containing the instance topics to "#instance" and on line 4 the column for the count number to "#count". Now our final result has these two columns and we can actually see which topic each of the instance counts belongs to. | ||
+ | |||
+ | {| border="1" | ||
+ | ! #instance | ||
+ | ! #count | ||
+ | |- | ||
+ | | align="center" | Role | ||
+ | | align="center" | 7 | ||
+ | |- | ||
+ | | align="center" | Wandora variant name version | ||
+ | | align="center" | 2 | ||
+ | |- | ||
+ | | align="center" | Association type | ||
+ | | align="center" | 5 | ||
+ | |- | ||
+ | | align="center" | Wandora class | ||
+ | | align="center" | 0 | ||
+ | |- | ||
+ | | align="center" | Wandora language | ||
+ | | align="center" | 4 | ||
+ | |- | ||
+ | | align="center" | Role class | ||
+ | | align="center" | 1 | ||
+ | |- | ||
+ | | align="center" | Content type | ||
+ | | align="center" | 5 | ||
+ | |- | ||
+ | | align="center" | Occurrence type | ||
+ | | align="center" | 1 | ||
+ | |- | ||
+ | | align="center" | Schema type | ||
| align="center" | 9 | | align="center" | 9 | ||
|} | |} | ||
== Directives == | == Directives == |
Revision as of 14:17, 11 August 2009
NOTE: This page describes a feature not yet available in the public version of Wandora. It should be available soon in a future release.
Wandora uses a custom query language to select topics in a topic map. Currently the query language is used only in the search tool. In the future it will also be added to Custom topic panel and Query topic map which currently use the old version of the query language.
Introduction
Wandora does not use any standard query language. Instead queries are done by invoking a method of a Java class implementing a certain interface. The class may then perform anything whatsoever as long as in the end it returns query results in the format specified by the Java interface. Wandora does however include a number of classes designed in a way that makes it possible to build complex queries by combining these simple predefined query directive classes. This somewhat resembles a traditional query language.
The queries are defined using a generic scripting language. Wandora uses Java scripting API so it should be possible to use a number of different languages. Examples in this article use Mozilla Rhino 1.6 language that should be found in most installations. This scripting language uses a syntax that is identical to regular Java syntax in nearly every way. Because of this you should be at least somewhat familiar with Java syntax to understand the examples on this page.
Following example demonstrates a query that selects the number of instances in a topic.
1 importPackage(org.wandora.query2); 2 new Count( 3 new Instances() 4 );
First line imports the query package. This is one few things where the scripting language syntax is different than normal Java syntax. Lines 2 to 4 contain the actual query. The Count directive counts the number of rows in the result of the directive inside it. The Instances directive inside Count on line 3 selects all instances of the input.
All directives may return any number of rows and get as input a single row. Each row may contain any number of values indexed. Values are indexed with column names which can be any text strings but are generally formed like URIs. One of the columns in a row is marked as active. This is usually the column that was last added or modified by a directive and its value is the primary input for other directives. Most directives use the default column name "#DEFAULT".
Picking the above example further apart, the Count directive on line 2 is the topmost directive and is called first. It gets as input a single row. The initial input row depends on how and where the query is started but let's say that in this case it is the currently open topic in Wandora. To be more specific, it contains a single column with the default name and the value is the currently open topic in Wandora and this single column is the active column of the row.
The Count directive passes its input to the inner Instances directive as is. The Instances directive uses the active column of its input, in this case the currently open topic in Wandora, and gets all instances of that. Generally directives add their results to the input row as new columns with the default name and set the new column as active. In this case the only column in input uses the default name and gets overwritten. Thus the result of the Instances directive is some number of rows, each containing a single column with the default name and the value is some topic which is an instance of the input.
The result of the Instances directive goes back to the Count directive which counts the rows in it. It adds this number in the input column, not the results of Instances directive. Again the single column gets overwritten because it had the default name. The final result is a single row which contains a single column with the default name and a number indicating the number of instances in the currently open topic.
#DEFAULT |
---|
9 |
Now lets modify the query slightly.
1 importPackage(org.wandora.query2); 2 new Count( 3 new Instances() 4 ).from( 5 new Instances() 6 )
The from method on line 4 causes the input for the Count directive to come from some other directive. In this case we use again an Instances directive. The execution of this query goes as follows. The initial input, the currently open topic, first goes to the directive inside the from part, that is Instances directive on line 5. This returns the instances of the currently open topic. Then each of these result rows are fed one at a time to the Count directive and all the results of Count are combined. The Count directive itself works exactly as in previous example, it only gets a different input this time. Now it counts the instances of all instances of the currently open topic. We still only get one column in the final result because at each step the default column gets overwritten. The final result might look something like this.
#DEFAULT |
---|
7 |
2 |
5 |
0 |
4 |
1 |
5 |
1 |
9 |
In the above table each number tells the number of instances of some topic which is an instance of the currently open topic. But this isn't very useful since we can't know which number corresponds to which topic. So let's modify the query a bit again.
1 importPackage(org.wandora.query2); 2 new Count( 3 new Instances() 4 ).as("#count").from( 5 new Instances().as("#instance") 6 )
The as methods on line 4 and 5 reset the column name to something other than the default. On line 5 we change the column containing the instance topics to "#instance" and on line 4 the column for the count number to "#count". Now our final result has these two columns and we can actually see which topic each of the instance counts belongs to.
#instance | #count |
---|---|
Role | 7 |
Wandora variant name version | 2 |
Association type | 5 |
Wandora class | 0 |
Wandora language | 4 |
Role class | 1 |
Content type | 5 |
Occurrence type | 1 |
Schema type | 9 |