1 18 19 package com.hp.hpl.jena.ontology.daml; 22 23 24 27 import java.util.*; 28 29 import com.hp.hpl.jena.rdf.model.*; 30 import com.hp.hpl.jena.util.iterator.*; 31 import com.hp.hpl.jena.vocabulary.DAML_OIL; 32 33 34 35 62 public class PropertyIterator 63 implements Iterator 64 { 65 68 69 72 73 76 77 protected LinkedList m_nodeQueue = new LinkedList(); 78 79 80 protected Property m_pred = null; 81 82 83 protected Property m_inverse = null; 84 85 86 protected HashSet m_predEquivs = new HashSet(); 87 88 89 protected HashSet m_inverseEquivs = new HashSet(); 90 91 92 protected boolean m_transitive = false; 93 94 95 protected WeakHashMap m_seen = new WeakHashMap(); 96 97 98 protected Resource m_root = null; 99 100 101 protected List m_roots = null; 102 103 104 protected Object m_defaultValue = null; 105 106 107 protected boolean m_defaultValueSeen = false; 108 109 110 protected boolean m_useEquivalence = true; 111 112 113 protected Model m_model = null; 114 115 116 117 120 135 public PropertyIterator( Resource root, Property pred, Property inverse, boolean isTransitive, boolean isReflexive ) { 136 this( root, pred, inverse, isTransitive, isReflexive, true ); 137 } 138 139 157 public PropertyIterator( Resource root, Property pred, Property inverse, boolean isTransitive, 158 boolean isReflexive, boolean useEquivalence ) { 159 m_root = root; 160 m_pred = pred; 161 m_inverse = inverse; 162 m_transitive = isTransitive; 163 m_useEquivalence = useEquivalence; 164 setModel(); 165 cachePropertyEquivs(); 166 167 if (isReflexive) { 169 enqueue( root ); 170 } 171 else { 172 expandQueue( root ); 174 } 175 } 176 177 178 194 public PropertyIterator( Iterator roots, Property pred, Property inverse, boolean isTransitive, boolean isReflexive ) { 195 this( roots, pred, inverse, isTransitive, isReflexive, true ); 196 } 197 198 199 218 public PropertyIterator( Iterator roots, Property pred, Property inverse, boolean isTransitive, 219 boolean isReflexive, boolean useEquivalence ) { 220 m_roots = new ArrayList(); 222 m_pred = pred; 223 m_inverse = inverse; 224 m_transitive = isTransitive; 225 m_useEquivalence = useEquivalence; 226 setModel(); 227 cachePropertyEquivs(); 228 229 if (isReflexive) { 231 while (roots.hasNext()) { 233 Resource next = (Resource) roots.next(); 234 235 if (m_model == null && next.getModel() != null) { 236 m_model = next.getModel(); 238 } 239 240 m_roots.add( next ); 241 enqueue( next ); 242 } 243 } 244 else { 245 while (roots.hasNext()) { 247 Resource next = (Resource) roots.next(); 248 249 if (m_model == null && next.getModel() != null) { 250 m_model = next.getModel(); 252 } 253 254 m_roots.add( next ); 255 expandQueue( next ); 256 } 257 } 258 } 259 260 261 264 270 public boolean hasNext() { 271 return !m_nodeQueue.isEmpty() || (hasDefaultValue() && !m_defaultValueSeen); 273 } 274 275 276 283 public Object next() { 284 if (!m_nodeQueue.isEmpty()) { 285 RDFNode next = (RDFNode) m_nodeQueue.removeFirst(); 287 288 if (hasDefaultValue() && m_defaultValue.equals( next )) { 290 m_defaultValueSeen = true; 291 } 292 293 if (next instanceof com.hp.hpl.jena.rdf.model.Resource) { 295 expandQueue( (Resource) next ); 296 } 297 298 return next; 300 } 301 else if (hasDefaultValue() && !m_defaultValueSeen) { 302 m_defaultValueSeen = true; 304 return m_defaultValue; 305 } 306 else { 307 throw new NoSuchElementException( "Tried to access next() element from empty property iterator" ); 309 } 310 } 311 312 313 318 public void remove() { 319 throw new UnsupportedOperationException ( "Cannot remove elements from a property iterator" ); 320 } 321 322 323 334 public void setDefaultValue( Object defaultValue ) { 335 m_defaultValue = defaultValue; 336 } 337 338 339 344 public boolean hasDefaultValue() { 345 return m_defaultValue != null; 346 } 347 348 349 350 353 354 360 private void enqueue( RDFNode node ) { 361 if (!m_seen.containsKey( node )) { 362 m_seen.put( node, Boolean.TRUE ); 364 365 m_nodeQueue.addLast( node ); 367 368 if (getUseEquivalence() && node instanceof DAMLCommon) { 370 for (Iterator i = ((DAMLCommon) node).getEquivalentValues(); i.hasNext(); ) { 371 enqueue( (RDFNode) i.next() ); 372 } 373 } 374 } 375 } 376 377 378 383 protected void expandQueue( Resource r ) { 384 if (m_pred != null && (m_transitive || isRoot( r ))) { 386 for (Iterator i = getStatementObjects( r ); 388 i.hasNext(); 389 enqueue( (RDFNode) i.next() )); 390 } 391 392 if (m_inverse != null && (m_transitive || isRoot( r ))) { 394 for (Iterator i = getStatementSubjects( r ); 396 i.hasNext(); 397 enqueue( (RDFNode) i.next() )); 398 } 399 } 400 401 402 408 protected boolean isRoot( Resource r ) { 409 if (m_roots != null) { 410 return m_roots.contains( r ); 412 } 413 else { 414 return r == m_root; 416 } 417 } 418 419 420 428 protected Iterator getStatementObjects( Resource res ) 429 { 430 Iterator i = null; 431 432 if (getUseEquivalence() && res instanceof DAMLCommon) { 433 for (Iterator j = m_predEquivs.iterator(); j.hasNext(); ) { 435 Iterator pIter = new PropertyIterator( res, (Property) j.next(), null, m_transitive, false, false ); 437 438 i = (i == null) ? pIter : new ConcatenatedIterator( pIter, i ); 440 } 441 } 442 else { 443 if (m_model != null) { 445 i = m_model.listObjectsOfProperty( res, m_pred ); 447 } 448 else { 449 i = new LinkedList().iterator(); 451 } 452 } 453 454 return i; 455 } 456 457 458 465 protected Iterator getStatementSubjects( Resource res ) 466 { 467 Iterator i = null; 468 469 if (getUseEquivalence() && res instanceof DAMLCommon) { 470 for (Iterator j = m_inverseEquivs.iterator(); j.hasNext(); ) { 472 Iterator pIter = new PropertyIterator( res, null, (Property) j.next(), m_transitive, false, false ); 474 475 i = (i == null) ? pIter : new ConcatenatedIterator( pIter, i ); 477 } 478 } 479 else { 480 if (m_model != null) { 482 i = m_model.listSubjectsWithProperty( m_inverse, res ); 484 } 485 else { 486 i = new LinkedList().iterator(); 488 } 489 } 490 491 return i; 492 } 493 494 495 499 protected void cachePropertyEquivs() { 500 if (getUseEquivalence()) { 501 if (m_pred != null) { 503 if (m_pred instanceof DAMLProperty) { 504 for (Iterator i = ((DAMLProperty) m_pred).getEquivalentValues(); i.hasNext(); ) { 506 cacheProperty( m_predEquivs, (Property) i.next() ); 507 } 508 } 509 else { 510 cacheProperty( m_predEquivs, m_pred ); 512 } 513 } 514 515 if (m_inverse != null) { 517 if (m_inverse instanceof DAMLProperty) { 518 for (Iterator i = ((DAMLProperty) m_inverse).getEquivalentValues(); i.hasNext(); ) { 520 cacheProperty( m_inverseEquivs, (Property) i.next() ); 521 } 522 } 523 else { 524 cacheProperty( m_inverseEquivs, m_inverse ); 526 } 527 } 528 } 529 } 530 531 532 538 protected void cacheProperty( HashSet s, Property p ) { 539 s.add( p ); 540 541 if (p instanceof DAMLProperty && !p.getLocalName().equals( DAML_OIL.subPropertyOf.getLocalName() )) { 545 for (Iterator i = ((DAMLProperty) p).getSubProperties(); i.hasNext(); ) { 546 s.add( i.next() ); 547 } 548 } 549 } 550 551 552 555 protected void setModel() { 556 if (m_root != null && m_root.getModel() != null) { 558 m_model = m_root.getModel(); 559 return; 560 } 561 562 if (m_pred != null && m_pred.getModel() != null) { 564 m_model = m_pred.getModel(); 565 return; 566 } 567 568 if (m_inverse != null && m_inverse.getModel() != null) { 570 m_model = m_inverse.getModel(); 571 return; 572 } 573 574 if (m_roots != null) { 576 for (Iterator i = m_roots.iterator(); i.hasNext(); ) { 577 RDFNode n = (RDFNode) i.next(); 578 if (n instanceof Resource && ((Resource) n).getModel() != null) { 579 m_model = ((Resource) n).getModel(); 580 return; 581 } 582 } 583 } 584 } 585 586 587 592 protected boolean getUseEquivalence() { 593 602 return false; 604 } 605 606 607 608 612 } 613 614 643 644 | Popular Tags |