1 19 20 package com.hp.hpl.jena.reasoner.dig; 23 24 25 26 import java.io.*; 29 30 import org.apache.commons.logging.LogFactory; 31 32 import com.hp.hpl.jena.graph.*; 33 import com.hp.hpl.jena.ontology.*; 34 import com.hp.hpl.jena.ontology.OntModelSpec; 35 import com.hp.hpl.jena.rdf.model.*; 36 import com.hp.hpl.jena.reasoner.*; 37 import com.hp.hpl.jena.reasoner.rulesys.Util; 38 import com.hp.hpl.jena.util.FileUtils; 39 import com.hp.hpl.jena.vocabulary.ReasonerVocabulary; 40 41 42 51 public class DIGReasoner 52 implements Reasoner 53 { 54 55 58 61 62 protected Capabilities capabilities; 63 64 67 68 protected Graph m_tbox; 69 70 71 protected ReasonerFactory m_factory; 72 73 74 protected Resource m_configuration; 75 76 77 protected String m_extReasonerURL = DIGConnection.DEFAULT_REASONER_URL; 78 79 80 protected OntModelSpec m_ontLang = getModelSpec( ProfileRegistry.OWL_LANG ); 81 82 83 protected Model m_axioms = null; 84 85 86 89 99 public DIGReasoner( Graph tbox, ReasonerFactory factory, Resource configuration ) { 100 m_tbox = tbox; 101 m_factory = factory; 102 m_configuration = configuration; 103 104 configure( configuration ); 105 } 106 107 108 111 119 public Reasoner bindSchema( Graph tbox ) { 120 return new DIGReasoner( tbox, m_factory, m_configuration ); 121 } 122 123 124 132 public Reasoner bindSchema(Model tbox) { 133 return bindSchema( tbox.getGraph() ); 134 } 135 136 137 144 public InfGraph bind( Graph data ) { 145 return new DIGInfGraph( data, this ); 146 } 147 148 149 153 public void setDerivationLogging( boolean logOn ) { 154 throw new UnsupportedOperationException ( "DIG reasoner does not support derivation logging" ); 155 } 156 157 158 162 public Model getReasonerCapabilities() { 163 return (m_factory == null) ? null : m_factory.getCapabilities(); 164 } 165 166 167 173 public void addDescription( Model configSpec, Resource base ) { 174 if (m_configuration != null) { 175 StmtIterator i = m_configuration.listProperties(); 176 while (i.hasNext()) { 177 Statement st = i.nextStatement(); 178 configSpec.add(base, st.getPredicate(), st.getObject()); 179 } 180 } 181 } 182 183 190 public boolean supportsProperty(Property property) { 191 if (m_factory == null) return false; 192 Model caps = m_factory.getCapabilities(); 193 Resource root = caps.getResource(m_factory.getURI()); 194 return caps.contains(root, ReasonerVocabulary.supportsP, property); 195 } 196 197 198 211 public void setParameter(Property parameter, Object value) { 212 if (!doSetParameter(parameter, value)) { 213 throw new IllegalParameterException( "DIGReasoner does not recognize configuration parameter " + parameter ); 214 } 215 else { 216 if (m_configuration == null) { 218 Model configModel = ModelFactory.createDefaultModel(); 219 m_configuration = configModel.createResource(); 220 } 221 222 Util.updateParameter( m_configuration, parameter, value ); 223 } 224 } 225 226 227 232 public void configure( Resource configuration ) { 233 if (configuration != null) { 234 for (StmtIterator i = configuration.listProperties(); i.hasNext(); ) { 235 Statement s = i.nextStatement(); 236 if (!doSetParameter( s.getPredicate(), s.getObject() )) { 237 throw new IllegalParameterException( "DIGReasoner does not recognize configuration parameter " + s.getPredicate() ); 238 } 239 } 240 } 241 } 242 243 244 248 public String getReasonerURL() { 249 return m_extReasonerURL; 250 } 251 252 253 258 public OntModelSpec getOntLangModelSpec() { 259 return m_ontLang; 260 } 261 262 263 267 public Graph getSchema() { 268 return m_tbox; 269 } 270 271 272 277 public Model getAxioms() { 278 return m_axioms; 279 } 280 281 285 public Capabilities getGraphCapabilities() { 286 if (capabilities == null) { 287 capabilities = new BaseInfGraph.InfCapabilities(); 288 } 289 return capabilities; 290 } 291 292 295 309 protected boolean doSetParameter(Property parameter, Object value) { 310 if (parameter.equals(ReasonerVocabulary.EXT_REASONER_URL)) { 311 m_extReasonerURL = (value instanceof Resource) ? ((Resource) value).getURI() : value.toString(); 312 return true; 313 } 314 else if (parameter.equals(ReasonerVocabulary.EXT_REASONER_ONT_LANG)) { 315 String lang = (value instanceof Resource) ? ((Resource) value).getURI() : value.toString(); 316 m_ontLang = getModelSpec( lang ); 317 return true; 318 } 319 else if (parameter.equals(ReasonerVocabulary.EXT_REASONER_AXIOMS)) { 320 String axURL = (value instanceof Resource) ? ((Resource) value).getURI() : value.toString(); 321 m_axioms = ModelFactory.createDefaultModel(); 322 323 if (axURL.startsWith( "file:")) { 325 String fileName = axURL.substring( 5 ); 326 InputStream in = null; 327 try { 328 in = FileUtils.openResourceFileAsStream( fileName ); 329 m_axioms.read( in, axURL ); 330 } 331 catch (FileNotFoundException e) { 332 LogFactory.getLog( getClass() ).error( "Could not open DIG axioms " + axURL ); 333 } 334 finally { 335 if (in != null) { 336 try {in.close();} catch (IOException ignore) {} 337 } 338 } 339 } 340 else { 341 m_axioms.read( axURL ); 342 } 343 344 return true; 345 } 346 else { 347 return false; 348 } 349 } 350 351 352 357 protected OntModelSpec getModelSpec( String lang ) { 358 if (lang.equals( ProfileRegistry.OWL_LANG ) || 359 lang.equals( ProfileRegistry.OWL_DL_LANG ) || 360 lang.equals( ProfileRegistry.OWL_LITE_LANG )) { 361 return OntModelSpec.OWL_MEM; 362 } 363 else if (lang.equals( ProfileRegistry.DAML_LANG )) { 364 return OntModelSpec.DAML_MEM; 365 } 366 else if (lang.equals( ProfileRegistry.RDFS_LANG )) { 367 return OntModelSpec.RDFS_MEM; 368 } 369 else { 370 throw new IllegalParameterException( "DIG reasoner did not recognise ontology language " + lang ); 371 } 372 } 373 374 375 376 380 } 381 382 383 409 | Popular Tags |