Search Mailing List Archives

Limit search to: Subject & Body Subject Author
Sort by: Reverse Sort
Limit to: All This Week Last Week This Month Last Month
Select Date Range     through    

[protege-owl] How to get SPARQL query results as an Excel or other format

Trish Whetzel whetzel at
Sun Apr 29 22:22:29 PDT 2007

Forwarding mail, with permission of the code author, from another list.


---------- Forwarded message ----------

In case anyone else is faced with the same problem we did, Josef Spidlen
in my group wrote some simple code to parse owl files into a format
suitable for the branches submission. The java code below uses Jena
library (tested with Jena 2.5.1; .jar files from Jena/lib need to be in
the class path). It runs a SPARQL query - in this version the query is
built in the Java code to make it "easier" to distribute. However, you
can just replace QueryFactory.create(sQueryString) with and create your SPARQL query externally.

The query has been tweaked to work with OWL files that we received our
collaborators. It expects the ontology classes being annotated in a
certain way, e.g., rumeta:def for definition, rumeta:editor for
definition editor, ... , rumeta:synonym or rumeta:acronym for
alternative terms. This may be easily modified in case you annotate
different way; it just needs to know "where to search for what".

The parent_term works fine for simple terms organized in a nice
hierarchy (i.e., 98% of what we had). A small problem is typically if a
class A is a subclass of B and also a subclass of C, while B and C are
not any hierarchical order. This can for example happen what you start
playing with owl constraints and defined classes. In this case, both B
and C are potential parents and two lines are outputted in the output
file. So far it was not worth to write something "smart" that could
handle this case somehow special (i.e., it was easier to fix two terms

Usage: ParseOwl OntologyFileName [OutputFileName]

- Processes the input OWL file and outputs a text based spreadsheet to
OutputFileName or to console (System.out) if OutputFileName isn't

The output is a text file with fixed field width; OpenOffice or MS Excel
text input can be used to create tab delimited files and to clean it a
bit - remove ^^xsd:string suffixes, OBI: prefixes, trim the text, remove
double-spaces, etc.


Code follows:

package org.flowcyt.parseowl;








import com.hp.hpl.jena.query.Query;

import com.hp.hpl.jena.query.QueryExecution;

import com.hp.hpl.jena.query.QueryExecutionFactory;

import com.hp.hpl.jena.query.QueryFactory;

import com.hp.hpl.jena.query.ResultSet;

import com.hp.hpl.jena.query.ResultSetFormatter;

import com.hp.hpl.jena.rdf.model.Model;

import com.hp.hpl.jena.rdf.model.ModelFactory;

public class ParseOwl {

                 private static void wrongUsage()


                                 System.out.println("Wrong usage, not all
arguments found.\n");

                                 System.out.println("Usage: ParseOwl
OntologyFileName OutputFileName");

                                 System.out.println(" - outputs to

                                 System.out.println("Usage: ParseOwl

                                 System.out.println(" - outputs to




                  * Parses an OWL ontology file and creates a simple
table out of it


                  * Usage: ParseOwl OntologyFileName OutputFileName

                  *  - outputs to file

                  * Usage: ParseOwl OntologyFileName

                  *  - outputs to System.out


                  * @author Josef Spidlen

                  * @param args one or two file names; the first one is
the input ontology, the second one the output file name



                 public static void main(String[] args) throws
FileNotFoundException, IOException {

                                 OutputStream out = null; InputStream in;

                                 if(null == args) {


                                 } else if(args.length == 1) {

                                                 out = System.out;

                                 } else if(args.length == 2) {

                                                 out = new
FileOutputStream(new File(args[1]));

                                 } else {



                                 Model model =
ModelFactory.createMemModelMaker().createModel("Model " + args[0]);

                                 in = new FileInputStream(new

                       , null);


                                 String sQuery =

                                                 "PREFIX dc:
<> " +

                                                 "PREFIX owl:
<> " +

                                                 "PREFIX protege:
<> " +

                                                 "PREFIX rameta:
<> " +

                                                 "PREFIX rdf:
<> " +

                                                 "PREFIX rdfs:
<> " +

                                                 "PREFIX rumeta:
<> " +

                                                 "PREFIX xsd:
<> " +

                                                 "PREFIX OBI:
<> " +

                                                 "" +

                                                 "SELECT DISTINCT
?preffered_term ?definition ?explanation ?definition_editor
?definition_source ?parent_term ?alternative_term " +

                                                 "WHERE  { " +

"?preffered_term a owl:Class . " +

"OPTIONAL { ?preffered_term rumeta:def ?definition . } " +

"OPTIONAL { ?preffered_term rumeta:editor ?definition_editor . } " +

"OPTIONAL { ?preffered_term rumeta:source ?definition_source . } " +

"OPTIONAL { ?preffered_term rdfs:subClassOf ?parent_term . } " +

"OPTIONAL { ?preffered_term rumeta:cls_expl ?explanation . } " +

"OPTIONAL { ?preffered_term ?p ?alternative_term . FILTER ( ?p =
rumeta:synonym || ?p = rumeta:acronym ) } " +


                                 Query query =

                                 QueryExecution qe =
QueryExecutionFactory.create(query, model);

                                 ResultSet results = qe.execSelect();

                                 ResultSetFormatter.out(out, results,

                                 out.close(); qe.close();


-------------- next part --------------
Take Surveys. Earn Cash. Influence the Future of IT
Join's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
-------------- next part --------------
Obi-devel mailing list
Obi-devel at

More information about the protege-owl mailing list