OntologyDB.java
Go to the documentation of this file.
00001 /****************************************************************
00002  *
00003  * Copyright (c) 2011, 2012
00004  *
00005  * School of Engineering, Cardiff University, UK
00006  *
00007  * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00008  *
00009  * Project name: srs EU FP7 (www.srs-project.eu)
00010  * ROS stack name: srs
00011  * ROS package name: srs_knowledge
00012  * Description: 
00013  *                                                              
00014  * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00015  *
00016  * @author Ze Ji, email: jiz1@cf.ac.uk
00017  *
00018  * Date of creation: Oct 2011:
00019  * ToDo: 
00020  *
00021  * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00022  *
00023  * Redistribution and use in source and binary forms, with or without
00024  * modification, are permitted provided that the following conditions are met:
00025  *
00026  *       * Redistributions of source code must retain the above copyright
00027  *         notice, this list of conditions and the following disclaimer.
00028  *       * Redistributions in binary form must reproduce the above copyright
00029  *         notice, this list of conditions and the following disclaimer in the
00030  *         documentation and/or other materials provided with the distribution.
00031  *       * Neither the name of the school of Engineering, Cardiff University nor 
00032  *         the names of its contributors may be used to endorse or promote products 
00033  *         derived from this software without specific prior written permission.
00034  *
00035  * This program is free software: you can redistribute it and/or modify
00036  * it under the terms of the GNU Lesser General Public License LGPL as 
00037  * published by the Free Software Foundation, either version 3 of the 
00038  * License, or (at your option) any later version.
00039  * 
00040  * This program is distributed in the hope that it will be useful,
00041  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00042  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00043  * GNU Lesser General Public License LGPL for more details.
00044  * 
00045  * You should have received a copy of the GNU Lesser General Public 
00046  * License LGPL along with this program. 
00047  * If not, see <http://www.gnu.org/licenses/>.
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         // create an empty model
00085         this.model = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC);
00086         this.reasoning();
00087     }
00088 
00089     public OntologyDB(String filename)
00090     {
00091         try {
00092             //String modelFileName = filename;
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         // create an empty model
00104         this.model = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC);
00105 
00106         //this.model = ModelFactory.createDefaultModel();
00107         try {
00108             for(String filename : filenames) {
00109                 //String modelFileName = filename;
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         // use the FileManager to find the input file
00123         InputStream in = FileManager.get().open(filename);
00124         if (in == null) {
00125             throw new IllegalArgumentException("File: " + filename + " not found");
00126         }
00127         
00128         // read the RDF/XML file
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         //ResultSetFormatter.out(ostream, results, query);
00148         //ResultSetFormatter.out(System.out, results, query);
00149         String r = "";
00150         try{
00151             r = new String(ostream.toByteArray(), "UTF-8");
00152             //System.out.println(r);
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         //System.out.println(queryString);
00169         try {
00170         Query query = QueryFactory.create(queryString);
00171         
00172         QueryExecution qe = QueryExecutionFactory.create(query, model);
00173         ResultSet results = qe.execSelect();
00174         /*
00175         ByteArrayOutputStream ostream = new ByteArrayOutputStream();
00176         ResultSetFormatter.out(ostream, results, query);
00177         //ResultSetFormatter.out(System.out, results, query);
00178         String r = "";
00179         try{
00180             r = new String(ostream.toByteArray(), "UTF-8");
00181             System.out.println(r);
00182         }
00183         catch(Exception e){
00184             System.out.println(e.getMessage());
00185         }
00186         */
00187 
00188         /*
00189         ArrayList<QuerySolution> resList = new ArrayList<QuerySolution>();
00190         if(results.hasNext()) {
00191             
00192             QuerySolution qs = results.next();
00193             resList.add(qs);
00194             //double x = qs.getLiteral("x").getFloat();
00195             //Literal y = qs.getLiteral("y");
00196             //Literal theta = qs.getLiteral("theta");
00197         }
00198         */
00199         ArrayList<QuerySolution> resList = (ArrayList)ResultSetFormatter.toList(results);
00200         qe.close();
00201         return resList; //results;
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         // create an empty model
00212         //this.model = ModelFactory.createDefaultModel();
00213         this.model = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC);
00214 
00215         System.out.println("Load OWL File: " + file);
00216         // use the FileManager to find the input file
00217         InputStream in = FileManager.get().open(file);
00218         if (in == null) {
00219             throw new IllegalArgumentException("File: " + file + " not found");
00220         }
00221         
00222         // read the RDF/XML file
00223         model.read(in, null);
00224     }
00225     
00226     public void printModel()
00227     { 
00228         model.write(System.out);
00229 
00230         /*
00231         StmtIterator iter = model.listStatements();
00232         
00233         // print out the predicate, subject and object of each statement
00234         while (iter.hasNext()) {
00235             Statement stmt      = iter.nextStatement();         // get next statement
00236             Resource  subject   = stmt.getSubject();   // get the subject
00237             Property  predicate = stmt.getPredicate(); // get the predicate
00238             RDFNode   object    = stmt.getObject();    // get the object
00239             
00240             System.out.print(subject.toString());
00241             System.out.print(" " + predicate.toString() + " ");
00242             if (object instanceof Resource) {
00243                 System.out.print(object.toString());
00244             } else {
00245                 // object is a literal
00246                 System.out.print(" \"" + object.toString() + "\"");
00247             }
00248             System.out.println(" .");
00249         }
00250         */
00251     }
00252 
00253     public Iterator getInstancesOfClass(String className) 
00254     {
00255         // get the instances of a class
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         //http://www.srs-project.eu/ontologies/ipa-kitchen-map.owl#
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         //com.hp.hpl.jena.rdf.model.Statement stm = ind.getProperty(property);
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         //OntClass onto = model.getOntClass(classURI + className);
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         // TODO: error checking in future
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     //private String modelFileName;    
00392     //private Model model;
00393     public OntModel model;
00394     //    private LockMutex mutex;
00395 }


srs_knowledge
Author(s): Ze Ji
autogenerated on Sun Jan 5 2014 12:03:28