1 /*
2 ORDI - Ontology Repository and Data Integration
3
4 Copyright (c) 2004-2007, OntoText Lab. / SIRMA
5
6 This library is free software; you can redistribute it and/or modify it under
7 the terms of the GNU Lesser General Public License as published by the Free
8 Software Foundation; either version 2.1 of the License, or (at your option)
9 any later version.
10 This library is distributed in the hope that it will be useful, but WITHOUT
11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13 details.
14 You should have received a copy of the GNU Lesser General Public License along
15 with this library; if not, write to the Free Software Foundation, Inc.,
16 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18 package examples;
19
20 import org.openrdf.query.MalformedQueryException;
21
22 import com.ontotext.ordi.wsmo4rdf.QueryResultListener;
23 import com.ontotext.ordi.wsmo4rdf.WsmoConnection;
24 import com.ontotext.ordi.wsmo4rdf.WsmoSource;
25
26 /**
27 * This class demonstrates how to evaluate a SPARQL query against default
28 * WsmoRepository implementation.
29 */
30 class EvaluateSPARQLQuery {
31
32 public static void main(String[] args) {
33 if (args.length == 0) {
34 System.out.println("Specify a SPARQL query to be evaluated!");
35 System.exit(1);
36 }
37
38 // Parse the SPARQL query specified by the user
39
40 // Create default WsmoRepository implementation
41 WsmoSource wsmoSource = com.ontotext.ordi.Factory.create(
42 WsmoSource.class, null);
43 WsmoConnection repository = wsmoSource.getConnection();
44
45 // Create helper class to dispatch the results
46 QueryResultListener resultDispatcher = new QueryResultListener() {
47 public void nextBindingSet() {
48 System.out.println();
49 }
50
51 public void nextBinding(String name, Object value) {
52 System.out.println(String.format("Binding: %s\t\tValue:%s",
53 name, value.toString()));
54 }
55 };
56
57 // Execute SPARQL query
58 try {
59 repository.evaluate(args[0], resultDispatcher);
60 } catch (MalformedQueryException e) {
61 throw new RuntimeException("Invalid SPARQL query!", e);
62 } finally {
63 wsmoSource.shutdown();
64 }
65 }
66 }