1 6 7 package com.hp.hpl.jena.graph.query; 8 9 import com.hp.hpl.jena.graph.*; 10 import com.hp.hpl.jena.util.CollectionFactory; 11 import com.hp.hpl.jena.util.iterator.*; 12 13 import java.util.*; 14 15 21 22 public class SimpleQueryHandler implements QueryHandler 23 { 24 25 protected Graph graph; 26 27 28 public SimpleQueryHandler( Graph graph ) 29 { this.graph = graph; } 30 31 public Stage patternStage( Mapping map, ExpressionSet constraints, Triple [] t ) 32 { return new PatternStage( graph, map, constraints, t ); } 33 34 public BindingQueryPlan prepareBindings( Query q, Node [] variables ) 35 { return new SimpleQueryPlan( graph, q, variables ); } 36 37 public TreeQueryPlan prepareTree( Graph pattern ) 38 { return new SimpleTreeQueryPlan( graph, pattern ); } 39 40 public ExtendedIterator objectsFor( Node s, Node p ) 41 { return objectsFor( graph, s, p ); } 42 43 public ExtendedIterator subjectsFor( Node p, Node o ) 44 { return subjectsFor( graph, p, o ); } 45 46 public ExtendedIterator predicatesFor( Node s, Node o ) 47 { return predicatesFor( graph, s, o ); } 48 49 public static ExtendedIterator objectsFor( Graph g, Node s, Node p ) 50 { 51 Set objects = CollectionFactory.createHashedSet(); 52 ClosableIterator it = g.find( s, p, Node.ANY ); 53 while (it.hasNext()) objects.add( ((Triple) it.next()).getObject() ); 54 return WrappedIterator.createNoRemove( objects.iterator() ); 55 } 56 57 public static ExtendedIterator subjectsFor( Graph g, Node p, Node o ) 58 { 59 Set objects = CollectionFactory.createHashedSet(); 60 ClosableIterator it = g.find( Node.ANY, p, o ); 61 while (it.hasNext()) objects.add( ((Triple) it.next()).getSubject() ); 62 return WrappedIterator.createNoRemove( objects.iterator() ); 63 } 64 65 public static ExtendedIterator predicatesFor( Graph g, Node s, Node o ) 66 { 67 Set predicates = CollectionFactory.createHashedSet(); 68 ClosableIterator it = g.find( s, Node.ANY, o ); 69 while (it.hasNext()) predicates.add( ((Triple) it.next()).getPredicate() ); 70 return WrappedIterator.createNoRemove( predicates.iterator() ); 71 } 72 73 78 public boolean containsNode( Node n ) 79 { 80 return 81 graph.contains( n, Node.ANY, Node.ANY ) 82 || graph.contains( Node.ANY, n, Node.ANY ) 83 || graph.contains( Node.ANY, Node.ANY, n ) 84 ; 85 } 86 } 87 88 117 | Popular Tags |