Previous topic  Top  Next topic  Print this Topic
 

Queries

 

Querying through the API occurs in three steps:

1.Obtaining a Reasoner objects and Creating a Query.

Queries can be created from ObjectLogic code, or retrieved from the knowledge base. In the example below, we define a query directly using ObjectLogic.

Note the use of the outorder option, it lets you leave out any unnecessary helper variables from the result set and provides confidence concerning index range and element location when later accessing values in a result array.

Reasoner reasoner = ontology.createReasoner();

String queryText = "@{options[outorder(?D, ?T)]} " +

                  "?- ?M:Measurement[atDate->?D[_month->1], hasTemperature->?T].";

Query query = reasoner.createQuery(ns, queryText);

Namespaces

To keep the code readable, we avoid using fully qualified names for concepts and properties. This is made possible by a Namespaces object. A Namespaces object can be assigned one default and multiple prefixed namespaces allowing for more concise ObjectLogic code.

Namespaces ns = new Namespaces();

ns.setDefaultNamespace("http://www.ontoprise.de/ontology1#";);

 

2.Opening the Query and Processing the Results.

Upon calling the open method of a query, it is submitted to OntoBroker. As explained above, a result set is formed by a number of n-tuples, where n is the number of variables contained in the query. The number of tuples depends on the query and the knowledge base, it can always be a single tuple for an aggregation query, or a large number. However, you should always handle the case of an empty result set. (This may be surprising at first but, for example, the count aggregate function results in an empty result set rather than a single tuple holding a zero when no records are found).

Often, the number of tuples in a result set is not previously known and, in this case, the afterLast() method comes in handy. This method indicates if we have gone through all the tuples in the result set. If not, tupleBuffer() returns the current n-tuple as an array of Terms (See below for more information about query return types). The method next() moves a query's internal iterator to the next tuple.

query.open()

Term[] tuple = query.tupleBuffer();

while (!query.afterLast()) {

 System.out.println("Temp on " + tuple[0] + " was " + tuple[1]);

 query.next();

}

query.close();

 

3.Closing the Query.

To prevent a resource leak, remember to close your query. As exceptions can occur when querying, this is typically done in a final block.

query.close();

Working with Data Types

Query results are returned as an array of the generic type Term, the concrete type of the variables depends on what you queried for. For example, querying for simple attribute values yields a Constant. Having successfully cast a Term to Constant, the method Constant.getTypeURI() should provide sufficient guidance as to what Java datatype, the actual variable value returned by Constant.getValue() can be cast to. However, the preferred way to get a concrete value is using the TypeRegistry class:

// Extract a double typed temperature

if (TypeRegistry.isDouble(tuple[1])) {

   Double value = TypeRegistry.getDouble(tuple[1]);

}