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.iterator.*; 11 import com.hp.hpl.jena.shared.*; 12 13 import java.util.*; 14 15 21 22 public class Query 23 { 24 27 public static final Node ANY = Node.ANY; 28 29 32 public static final Node S = Node.createVariable( "S" ); 33 36 public static final Node P = Node.createVariable( "P" ); 37 40 public static final Node O = Node.createVariable( "O" ); 41 44 public static final Node X = Node.createVariable( "X" ); 45 48 public static final Node Y = Node.createVariable( "Y" ); 49 52 public static final Node Z = Node.createVariable( "Z" ); 53 54 57 public Query() 58 { } 59 60 65 public Query( Graph pattern ) 66 { addMatches( pattern ); } 67 68 71 public static class UnboundVariableException extends JenaException 72 { public UnboundVariableException( Node n ) { super( n.toString() ); } } 73 74 82 public Query addMatch( Node s, Node p, Node o ) 83 { return addNamedMatch( NamedTripleBunches.anon, s, p, o ); } 84 85 91 public Query addMatch( Triple t ) 92 { triples.add( NamedTripleBunches.anon, t ); 93 return this; } 94 95 104 public Query addMatch( String name, Node s, Node p, Node o ) 105 { return addNamedMatch( name, s, p, o ); } 106 107 private ExpressionSet constraint = new ExpressionSet(); 108 109 public ExpressionSet getConstraints() 110 { return constraint; } 111 112 public Query addConstraint( Expression e ) 113 { 114 if (e.isApply() && e.getFun().equals( ExpressionFunctionURIs.AND )) 115 for (int i = 0; i < e.argCount(); i += 1) addConstraint( e.getArg( i ) ); 116 else if (e.isApply() && e.argCount() == 2 && e.getFun().equals( ExpressionFunctionURIs.Q_StringMatch)) 117 constraint.add( Rewrite.rewriteStringMatch( e ) ); 118 else 119 constraint.add( e ); 120 return this; 121 } 122 123 126 private void addMatches( Graph p ) 127 { 128 ClosableIterator it = GraphUtil.findAll( p ); 129 while (it.hasNext()) addMatch( (Triple) it.next() ); 130 } 131 132 public ExtendedIterator executeBindings( Graph g, Node [] results ) 133 { return executeBindings( args().put( NamedTripleBunches.anon, g ), results ); } 134 135 public ExtendedIterator executeBindings( Graph g, List stages, Node [] results ) 136 { return executeBindings( stages, args().put( NamedTripleBunches.anon, g ), results ); } 137 138 public ExtendedIterator executeBindings( NamedGraphMap args, Node [] nodes ) 139 { return executeBindings( new ArrayList(), args, nodes ); } 140 141 144 public ExtendedIterator executeBindings( List outStages, NamedGraphMap args, Node [] nodes ) 145 { 146 SimpleQueryEngine e = new SimpleQueryEngine( triples, sortMethod, constraint ); 147 ExtendedIterator result = e.executeBindings( outStages, args, nodes ); 148 lastQueryEngine = e; 149 return result; 150 } 151 152 private SimpleQueryEngine lastQueryEngine = null; 153 154 155 156 private NamedTripleBunches triples = new NamedTripleBunches(); 157 158 public NamedTripleBunches getTriples() 159 { return triples; } 160 161 162 private NamedGraphMap argMap = new NamedGraphMap(); 163 164 public NamedGraphMap args() 165 { return argMap; } 166 167 private Query addNamedMatch( String name, Node s, Node p, Node o ) 168 { triples.add( name, Triple.create( s, p, o ) ); 169 return this; } 170 171 public TripleSorter getSorter() 172 { return sortMethod; } 173 174 public void setTripleSorter( TripleSorter ts ) 175 { sortMethod = ts == null ? TripleSorter.dontSort : ts; } 176 177 180 public static final TripleSorter dontSort = TripleSorter.dontSort; 181 182 private TripleSorter sortMethod = TripleSorter.dontSort; 183 184 public int getVariableCount() 185 { return lastQueryEngine.getVariableCount(); } 186 } 187 188 217 | Popular Tags |