Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051 package org.srs.srs_knowledge.knowledge_engine;
00052
00053 import com.hp.hpl.jena.rdf.model.*;
00054 import com.hp.hpl.jena.vocabulary.*;
00055 import com.hp.hpl.jena.util.FileManager;
00056
00057 import com.hp.hpl.jena.query.Query;
00058 import com.hp.hpl.jena.query.QueryFactory;
00059 import com.hp.hpl.jena.query.ResultSetFormatter;
00060 import com.hp.hpl.jena.query.QueryExecutionFactory;
00061 import com.hp.hpl.jena.query.ResultSet;
00062 import com.hp.hpl.jena.query.QuerySolution;
00063 import com.hp.hpl.jena.query.QueryExecution;
00064 import com.hp.hpl.jena.sparql.engine.ResultSetStream;
00065 import com.hp.hpl.jena.rdf.model.Property;
00066 import org.mindswap.pellet.jena.PelletReasonerFactory;
00067 import com.hp.hpl.jena.ontology.OntClass;
00068 import com.hp.hpl.jena.ontology.OntModel;
00069 import com.hp.hpl.jena.rdf.model.Statement;
00070 import com.hp.hpl.jena.ontology.Individual;
00071 import com.hp.hpl.jena.shared.Lock;
00072 import com.hp.hpl.jena.ontology.OntResource;
00073 import com.hp.hpl.jena.ontology.OntProperty;
00074 import java.io.*;
00075 import java.util.ArrayList;
00076 import java.util.Iterator;
00077
00078 import org.srs.srs_knowledge.knowledge_engine.*;
00079
00080 public class OntologyDB
00081 {
00082 public OntologyDB()
00083 {
00084
00085 this.model = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC);
00086 this.reasoning();
00087 }
00088
00089 public OntologyDB(String filename)
00090 {
00091 try {
00092
00093 this.reloadOWLFile(filename);
00094 }
00095 catch(IllegalArgumentException e) {
00096 System.out.println("Caught Exception : " + e.getMessage());
00097 }
00098 this.reasoning();
00099 }
00100
00101 public OntologyDB(ArrayList<String> filenames)
00102 {
00103
00104 this.model = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC);
00105
00106
00107 try {
00108 for(String filename : filenames) {
00109
00110 this.importOntology(filename);
00111 }
00112 }
00113 catch(IllegalArgumentException e) {
00114 System.out.println("Caught Exception : " + e.getMessage());
00115 }
00116 this.reasoning();
00117 }
00118
00119 public void importOntology(String filename)
00120 {
00121 System.out.println("Load OWL File: " + filename);
00122
00123 InputStream in = FileManager.get().open(filename);
00124 if (in == null) {
00125 throw new IllegalArgumentException("File: " + filename + " not found");
00126 }
00127
00128
00129 model.read(in, null);
00130 }
00131
00132 public void reasoning()
00133 {
00134 }
00135
00136 public String executeQuery(String queryString)
00137 {
00139 try {
00140 Query query = QueryFactory.create(queryString);
00141
00142 QueryExecution qe = QueryExecutionFactory.create(query, model);
00143 ResultSet results = qe.execSelect();
00144
00145 ByteArrayOutputStream ostream = new ByteArrayOutputStream();
00146 ResultSetFormatter.outputAsJSON(ostream, results);
00147
00148
00149 String r = "";
00150 try{
00151 r = new String(ostream.toByteArray(), "UTF-8");
00152
00153 }
00154 catch(Exception e){
00155 System.out.println(e.getMessage());
00156 }
00157 qe.close();
00158 return r;
00159 }
00160 catch(Exception e) {
00161 System.out.println(e.toString());
00162 return "";
00163 }
00164 }
00165
00166 public ArrayList<QuerySolution> executeQueryRaw(String queryString)
00167 {
00168
00169 try {
00170 Query query = QueryFactory.create(queryString);
00171
00172 QueryExecution qe = QueryExecutionFactory.create(query, model);
00173 ResultSet results = qe.execSelect();
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199 ArrayList<QuerySolution> resList = (ArrayList)ResultSetFormatter.toList(results);
00200 qe.close();
00201 return resList;
00202 }
00203 catch(Exception e) {
00204 System.out.println(e.toString());
00205 return new ArrayList<QuerySolution>();
00206 }
00207 }
00208
00209 public void reloadOWLFile(String file)
00210 {
00211
00212
00213 this.model = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC);
00214
00215 System.out.println("Load OWL File: " + file);
00216
00217 InputStream in = FileManager.get().open(file);
00218 if (in == null) {
00219 throw new IllegalArgumentException("File: " + file + " not found");
00220 }
00221
00222
00223 model.read(in, null);
00224 }
00225
00226 public void printModel()
00227 {
00228 model.write(System.out);
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251 }
00252
00253 public Iterator getInstancesOfClass(String className)
00254 {
00255
00256 OntClass onto = model.getOntClass( className );
00257
00258 if(onto == null) {
00259 System.out.println("ONT CLASS IS NULL");
00260 return (new ArrayList()).iterator();
00261 }
00262
00263 Iterator instances = onto.listInstances();
00264 return instances;
00265 }
00266
00267 public String getNamespaceByPrefix(String namespacePrefix)
00268 {
00269 model.enterCriticalSection(Lock.READ);
00270
00271 String pre = model.getNsPrefixURI(namespacePrefix);
00272 model.leaveCriticalSection();
00273 return pre;
00274 }
00275
00282 public com.hp.hpl.jena.rdf.model.NodeIterator listPropertiesOf(String proNameSpace, String proLocalName, Individual ind )
00283 {
00284 model.enterCriticalSection(Lock.READ);
00285 com.hp.hpl.jena.rdf.model.Property property = model.getProperty(proNameSpace, proLocalName);
00286 com.hp.hpl.jena.rdf.model.NodeIterator nodeIt = ind.listPropertyValues(property);
00287
00288 model.leaveCriticalSection();
00289 return nodeIt;
00290 }
00291
00298 public com.hp.hpl.jena.rdf.model.Statement getPropertyOf(String proNameSpace, String proLocalName, Individual ind )
00299 {
00300 model.enterCriticalSection(Lock.READ);
00301 com.hp.hpl.jena.rdf.model.Property property = model.getProperty(proNameSpace, proLocalName);
00302 com.hp.hpl.jena.rdf.model.Statement stm = ind.getProperty(property);
00303 model.leaveCriticalSection();
00304 return stm;
00305 }
00306
00307
00308 public OntModel getModel() {
00309 return model;
00310 }
00311
00312 public void insertInstance(String classURI, String className, String instanceURI, String instanceName) throws DuplicatedEntryException, UnknownClassException
00313 {
00314 model.enterCriticalSection(Lock.WRITE);
00315 Resource rs = model.getResource(classURI + className);
00316 if(rs == null) {
00317 model.leaveCriticalSection();
00318 throw new UnknownClassException(className);
00319 }
00320
00321
00322
00323 Individual ind = model.getIndividual(instanceURI + instanceName);
00324 if(ind != null) {
00325 model.leaveCriticalSection();
00326 throw new DuplicatedEntryException(instanceName);
00327 }
00328 ind = model.createIndividual(instanceURI + instanceName, rs);
00329 ind.setOntClass(rs);
00330 model.leaveCriticalSection();
00331 }
00332
00333 public void deleteInstance(String instanceURI, String instanceName) throws NonExistenceEntryException, UnknownException
00334 {
00335 model.enterCriticalSection(Lock.WRITE);
00336 Individual ind = model.getIndividual(instanceURI + instanceName);
00337 if(ind == null) {
00338 model.leaveCriticalSection();
00339 throw new NonExistenceEntryException(instanceName);
00340 }
00341 ind.remove();
00342
00343 model.leaveCriticalSection();
00344 }
00345
00346 public boolean removeStatement(Statement stm)
00347 {
00348
00349 model.enterCriticalSection(Lock.WRITE);
00350 model.remove(stm);
00351 model.leaveCriticalSection();
00352 return true;
00353 }
00354
00355 public Individual getIndividual(String uri) throws NonExistenceEntryException
00356 {
00357 model.enterCriticalSection(Lock.READ);
00358 Individual ind = model.getIndividual(uri);
00359 if(ind == null) {
00360 model.leaveCriticalSection();
00361 throw new NonExistenceEntryException(uri);
00362 }
00363 model.leaveCriticalSection();
00364 return ind;
00365 }
00366
00367 public Property getProperty(String uri) throws NonExistenceEntryException
00368 {
00369 model.enterCriticalSection(Lock.READ);
00370 Property pro = model.getProperty(uri);
00371 if (pro == null) {
00372 model.leaveCriticalSection();
00373 throw new NonExistenceEntryException(uri);
00374 }
00375 model.leaveCriticalSection();
00376 return pro;
00377 }
00378
00379 public OntProperty getOntProperty(String uri) throws NonExistenceEntryException
00380 {
00381 model.enterCriticalSection(Lock.READ);
00382 OntProperty pro = model.getOntProperty(uri);
00383 if (pro == null) {
00384 model.leaveCriticalSection();
00385 throw new NonExistenceEntryException(uri);
00386 }
00387 model.leaveCriticalSection();
00388 return pro;
00389 }
00390
00391
00392
00393 public OntModel model;
00394
00395 }