1 19 20 package com.hp.hpl.jena.reasoner.dig; 23 24 25 import java.util.*; 28 import java.util.Iterator ; 29 import java.util.List ; 30 31 import org.apache.commons.logging.LogFactory; 32 import org.w3c.dom.Document ; 33 import org.w3c.dom.Element ; 34 35 import com.hp.hpl.jena.graph.*; 36 import com.hp.hpl.jena.rdf.model.Model; 37 import com.hp.hpl.jena.reasoner.TriplePattern; 38 import com.hp.hpl.jena.reasoner.rulesys.Node_RuleVariable; 39 import com.hp.hpl.jena.util.iterator.*; 40 import com.hp.hpl.jena.util.xml.SimpleXMLPath; 41 import com.hp.hpl.jena.util.xml.SimpleXMLPathElement; 42 43 44 52 public abstract class DIGQueryTranslator { 53 56 public static final String ALL = "*"; 57 58 59 62 63 66 67 private Node m_subject; 68 69 70 private Node m_object; 71 72 73 private Node m_pred; 74 75 76 79 83 public DIGQueryTranslator( String subject, String predicate, String object ) { 84 m_subject = mapNode( subject ); 85 m_pred = mapNode( predicate ); 86 m_object = mapNode( object ); 87 } 88 89 90 93 101 public ExtendedIterator find( TriplePattern pattern, DIGAdapter da ) { 102 DIGConnection dc = da.getConnection(); 103 104 Document query = translatePattern( pattern, da ); 106 if (query == null) { 107 LogFactory.getLog( getClass() ).warn( "Could not find pattern translator for nested DIG query " + pattern ); 108 } 109 Document response = da.getConnection().sendDigVerb( query, da.getProfile() ); 110 111 boolean warn = dc.warningCheck( response ); 112 if (warn) { 113 for (Iterator i = dc.getWarnings(); i.hasNext(); ) { 114 LogFactory.getLog( getClass() ).warn( i.next() ); 115 } 116 } 117 118 return translateResponse( response, pattern, da ); 120 } 121 122 123 133 public ExtendedIterator find( TriplePattern pattern, DIGAdapter da, Model premises ) { 134 DIGConnection dc = da.getConnection(); 135 136 Document query = translatePattern( pattern, da, premises ); 138 if (query == null) { 139 LogFactory.getLog( getClass() ).warn( "Could not find pattern translator for nested DIG query " + pattern ); 140 return NullIterator.instance; 141 } 142 else { 143 Document response = da.getConnection().sendDigVerb( query, da.getProfile() ); 144 145 boolean warn = dc.warningCheck( response ); 146 if (warn) { 147 for (Iterator i = dc.getWarnings(); i.hasNext(); ) { 148 LogFactory.getLog( getClass() ).warn( i.next() ); 149 } 150 } 151 152 return translateResponse( response, pattern, da ); 154 } 155 } 156 157 158 166 public boolean trigger( TriplePattern pattern, DIGAdapter da, Model premises ) { 167 return trigger( m_subject, pattern.getSubject(), premises ) && 168 trigger( m_object, pattern.getObject(), premises ) && 169 trigger( m_pred, pattern.getPredicate(), premises ) && 170 checkTriple( pattern, da, premises ); 171 } 172 173 174 183 public boolean checkTriple( TriplePattern pattern, DIGAdapter da, Model premises ) { 184 return checkSubject( pattern.getSubject(), da, premises ) && 185 checkObject( pattern.getObject(), da, premises ) && 186 checkPredicate( pattern.getPredicate(), da, premises ); 187 188 } 189 190 191 201 public boolean checkSubject( Node subject, DIGAdapter da, Model premises ) { 202 return true; 203 } 204 205 206 216 public boolean checkObject( Node object, DIGAdapter da, Model premises ) { 217 return true; 218 } 219 220 221 231 public boolean checkPredicate( Node pred, DIGAdapter da, Model premises ) { 232 return true; 233 } 234 235 236 239 public abstract Document translatePattern( TriplePattern query, DIGAdapter da ); 240 241 246 public abstract Document translatePattern( TriplePattern pattern, DIGAdapter da, Model premises ); 247 248 252 public final ExtendedIterator translateResponse( Document response, TriplePattern query, DIGAdapter da ) { 253 ExtendedIterator i = translateResponseHook( response, query, da ); 254 Filter f = getResultsTripleFilter( query ); 255 return (f == null) ? i : i.filterKeep( f ); 256 } 257 258 259 262 266 protected abstract ExtendedIterator translateResponseHook( Document response, TriplePattern query, DIGAdapter da ); 267 268 269 274 protected Node mapNode( String uri ) { 275 if (uri == null) { 276 return null; 277 } 278 else { 279 return (uri.equals( ALL )) ? Node_RuleVariable.WILD : Node.createURI( uri ); 280 } 281 } 282 283 284 294 protected boolean trigger( Node lhs, Node rhs, Model premises ) { 295 return (lhs == null) || lhs.equals( rhs ); 296 } 297 298 299 304 protected boolean isTrue( Document response ) { 305 return new SimpleXMLPath( true ) 306 .appendElementPath( DIGProfile.TRUE ) 307 .getAll( response ) 308 .hasNext(); 309 } 310 311 312 317 protected boolean isFalse( Document response ) { 318 return new SimpleXMLPath( true ) 319 .appendElementPath( DIGProfile.FALSE ) 320 .getAll( response ) 321 .hasNext(); 322 } 323 324 325 334 protected ExtendedIterator translateConceptSetResponse( Document response, TriplePattern query, boolean object, DIGAdapter da ) { 335 return translateNameSetResponse( response, query, object, 336 new String [] {DIGProfile.CONCEPT_SET, DIGProfile.SYNONYMS, DIGProfile.CATOM} ) 337 .andThen( translateSpecialConcepts( response, da, 338 object ? query.getSubject() : query.getObject(), 339 query.getPredicate(), object )); 340 } 341 342 343 352 protected ExtendedIterator translateRoleSetResponse( Document response, TriplePattern query, boolean object ) { 353 return translateNameSetResponse( response, query, object, 354 new String [] {DIGProfile.ROLE_SET, DIGProfile.SYNONYMS, DIGProfile.RATOM} ); 355 } 356 357 358 367 protected ExtendedIterator translateIndividualSetResponse( Document response, TriplePattern query, boolean object ) { 368 return translateNameSetResponse( response, query, object, 369 new String [] {DIGProfile.INDIVIDUAL_SET, DIGProfile.INDIVIDUAL} ); 370 } 371 372 373 383 protected ExtendedIterator translateNameSetResponse( Document response, TriplePattern query, boolean object, String [] path ) { 384 SimpleXMLPath p = new SimpleXMLPath( true ); 386 387 for (int i = 0; i < path.length; i++) { 389 p.appendElementPath( path[i] ); 390 } 391 p.appendAttrPath( DIGProfile.NAME ); 392 393 ExtendedIterator iNodes = p.getAll( response ).mapWith( new DIGValueToNodeMapper() ); 395 396 if (object) { 398 return iNodes.mapWith( new TripleObjectFiller( query.getSubject(), query.getPredicate() ) ); 399 } 400 else { 401 return iNodes.mapWith( new TripleSubjectFiller( query.getPredicate(), query.getObject() ) ); 402 } 403 } 404 405 416 protected ExtendedIterator conceptSetNameCheck( Document response, DIGAdapter da, Node node, Triple result ) { 417 ExtendedIterator catoms = new SimpleXMLPath( true ) 419 .appendElementPath( DIGProfile.CONCEPT_SET ) 420 .appendElementPath( DIGProfile.SYNONYMS ) 421 .appendElementPath( SimpleXMLPathElement.ALL_CHILDREN ) 422 .getAll( response ); 423 424 String oName = da.getNodeID( node ); 426 427 boolean seekingTop = oName.equals( da.getOntLanguage().THING().getURI() ); 428 boolean seekingBottom = oName.equals( da.getOntLanguage().NOTHING().getURI() ); 429 430 boolean found = false; 431 while (!found && catoms.hasNext()) { 432 Element name = (Element ) catoms.next(); 433 434 found = (seekingTop && name.getNodeName().equals( DIGProfile.TOP )) || 435 (seekingBottom && name.getNodeName().equals( DIGProfile.BOTTOM )) || 436 name.getAttribute( DIGProfile.NAME ).equals( oName ); 437 } 438 439 return found ? (ExtendedIterator) new SingletonIterator( result ) 441 : NullIterator.instance; 442 } 443 444 445 457 protected ExtendedIterator translateSpecialConcepts( Document response, DIGAdapter da, Node ref, Node pred, boolean refSubject ) { 458 SimpleXMLPath topPath = new SimpleXMLPath( true ) 459 .appendElementPath( DIGProfile.CONCEPT_SET ) 460 .appendElementPath( DIGProfile.SYNONYMS ) 461 .appendElementPath( DIGProfile.TOP ); 462 SimpleXMLPath bottomPath = new SimpleXMLPath( true ) 463 .appendElementPath( DIGProfile.CONCEPT_SET ) 464 .appendElementPath( DIGProfile.SYNONYMS ) 465 .appendElementPath( DIGProfile.BOTTOM ); 466 467 List specials = new ArrayList(); 468 469 if (topPath.getAll( response ).hasNext()) { 470 Node n = da.getOntLanguage().THING().asNode(); 472 specials.add( refSubject ? new Triple( ref, pred, n ) 473 : new Triple( n, pred, ref ) ); 474 475 } 476 if (bottomPath.getAll( response ).hasNext()) { 477 Node n = da.getOntLanguage().NOTHING().asNode(); 479 specials.add( refSubject ? new Triple( ref, pred, n ) 480 : new Triple( n, pred, ref ) ); 481 482 } 483 484 return WrappedIterator.create( specials.iterator() ); 485 } 486 487 488 495 protected Filter getResultsTripleFilter( TriplePattern query ) { 496 return null; 497 } 498 499 500 504 } 505 506 507 533 | Popular Tags |