001: /*
002: * Copyright Aduna (http://www.aduna-software.com/) (c) 1997-2006.
003: *
004: * Licensed under the Aduna BSD-style license.
005: */
006: package org.openrdf.sail.lubm;
007:
008: import java.io.File;
009: import java.io.FilenameFilter;
010: import java.net.URL;
011:
012: import edu.lehigh.swat.bench.ubt.api.Query;
013: import edu.lehigh.swat.bench.ubt.api.QueryResult;
014:
015: import org.openrdf.query.MalformedQueryException;
016: import org.openrdf.query.QueryEvaluationException;
017: import org.openrdf.query.QueryLanguage;
018: import org.openrdf.query.TupleQueryResult;
019: import org.openrdf.query.UnsupportedQueryLanguageException;
020: import org.openrdf.repository.Repository;
021: import org.openrdf.repository.RepositoryConnection;
022: import org.openrdf.repository.RepositoryException;
023: import org.openrdf.repository.sail.SailRepository;
024: import org.openrdf.rio.RDFFormat;
025: import org.openrdf.rio.RDFParseException;
026: import org.openrdf.rio.UnsupportedRDFormatException;
027: import org.openrdf.sail.Sail;
028:
029: public abstract class LUBMRepository implements
030: edu.lehigh.swat.bench.ubt.api.Repository {
031:
032: private Repository repository;
033:
034: private String ontologyURL;
035:
036: public LUBMRepository() {
037: }
038:
039: // implements edu.lehigh.swat.bench.ubt.api.Repository.open(...)
040: public void open(String database) {
041: try {
042: repository = new SailRepository(createSail(database));
043: repository.initialize();
044: } catch (RepositoryException e) {
045: throw new RuntimeException(e);
046: }
047: }
048:
049: /**
050: * Creates the (stack of) Sails that is subject to testing.
051: *
052: * @param database
053: * A identifier for the database that is to be tested, for example a
054: * JDBC-URL or a data directory.
055: * @return An uninitialized Sail object for the specified 'database'.
056: */
057: public abstract Sail createSail(String database);
058:
059: // implements edu.lehigh.swat.bench.ubt.api.Repository.close()
060: public void close() {
061: try {
062: repository.shutDown();
063: } catch (RepositoryException e) {
064: throw new RuntimeException(e);
065: }
066: }
067:
068: // implements edu.lehigh.swat.bench.ubt.api.Repository.setOntology(...)
069: public void setOntology(String ontology) {
070: ontologyURL = ontology;
071: }
072:
073: // implements edu.lehigh.swat.bench.ubt.api.Repository.load(...)
074: public boolean load(String dataDir) {
075: try {
076: RepositoryConnection con = repository.getConnection();
077:
078: // load ontology first
079: System.out.println("Loading ontology");
080: con
081: .add(new URL(ontologyURL), ontologyURL,
082: RDFFormat.RDFXML);
083:
084: File dir = new File(dataDir);
085: File[] fileList = dir.listFiles(new FilenameFilter() {
086:
087: public boolean accept(File dir, String name) {
088: return name.endsWith(".owl");
089: }
090: });
091:
092: if (fileList == null) {
093: System.err
094: .println("Invalid data directory: " + dataDir);
095: return false;
096: }
097: for (int i = 0; i < fileList.length; i++) {
098: System.out.println("Loading " + fileList[i]);
099: con.add(fileList[i], fileList[i].getPath(),
100: RDFFormat.RDFXML);
101: }
102:
103: // System.out.println("Commiting transaction");
104: // txn.commit();
105:
106: System.out.println("Done loading");
107: con.close();
108:
109: return true;
110: } catch (RDFParseException e) {
111: e.printStackTrace();
112: } catch (RepositoryException e) {
113: e.printStackTrace();
114: } catch (java.io.IOException e) {
115: e.printStackTrace();
116: } catch (UnsupportedRDFormatException e) {
117: e.printStackTrace();
118: }
119:
120: return false;
121: }
122:
123: // implements edu.lehigh.swat.bench.ubt.api.Repository.issueQuery(...)
124: public QueryResult issueQuery(Query query) {
125:
126: try {
127: RepositoryConnection con = repository.getConnection();
128: TupleQueryResult queryResult = con.prepareTupleQuery(
129: QueryLanguage.SERQL, query.getString()).evaluate();
130: con.close();
131:
132: return new LUBMQueryResult(queryResult);
133: } catch (MalformedQueryException e) {
134: e.printStackTrace();
135: } catch (RepositoryException e) {
136: e.printStackTrace();
137: } catch (UnsupportedQueryLanguageException e) {
138: e.printStackTrace();
139: } catch (QueryEvaluationException e) {
140: e.printStackTrace();
141: }
142:
143: return null;
144: }
145:
146: // implements edu.lehigh.swat.bench.ubt.api.Repository.clear()
147: public void clear() {
148: try {
149: RepositoryConnection con = repository.getConnection();
150: con.clear();
151: con.close();
152: } catch (RepositoryException e) {
153: throw new RuntimeException(e);
154: }
155: }
156:
157: private static class LUBMQueryResult implements QueryResult {
158:
159: private int num;
160:
161: private TupleQueryResult queryResult;
162:
163: public LUBMQueryResult(TupleQueryResult queryResult) {
164: this .queryResult = queryResult;
165: }
166:
167: public long getNum() {
168: return num;
169: }
170:
171: public boolean next() {
172: try {
173: if (queryResult.hasNext()) {
174: queryResult.next();
175: num++;
176: return true;
177: } else {
178: queryResult.close();
179: return false;
180: }
181: } catch (QueryEvaluationException e) {
182: e.printStackTrace();
183: return false;
184: }
185: }
186:
187: }
188: }
|