1 6 7 package com.hp.hpl.jena.db.test; 8 9 import com.hp.hpl.jena.graph.*; 10 import com.hp.hpl.jena.graph.test.*; 11 import com.hp.hpl.jena.graph.query.*; 12 import com.hp.hpl.jena.shared.*; 13 import com.hp.hpl.jena.util.iterator.*; 14 import com.hp.hpl.jena.graph.impl.*; 15 import com.hp.hpl.jena.vocabulary.RDF; 16 17 18 import java.util.*; 19 import junit.framework.*; 20 21 25 public abstract class AbstractTestQuery1 extends GraphTestBase 26 { 27 public AbstractTestQuery1(String name) 28 { super(name); } 29 30 public static TestSuite suite() 31 { return new TestSuite( AbstractTestQuery1.class ); } 32 33 public abstract Graph getGraph(); 34 public abstract Graph getGraph( ReificationStyle style ); 35 36 37 44 46 protected String [] statementList = { 47 48 50 "Pname Pcard O1", 51 "Psex Pcard O1", 52 "Pdept Pcardmax O5", 53 "Pmgr Pcardmax O1", 54 "Ptitle Pcardmax O5", 55 56 "Pname Prange Oliteral", 57 "Psex Prange Oliteral", 58 "Pdept Prange Oliteral", 59 "Pmgr Prange Oresource", 60 "Ptitle Prange Oliteral", 61 62 "S1 Pname Omm", 63 "S1 Psex Omale", 64 "S1 Pdept Ojena", 65 "S1 Pdept Oapp", 66 "S1 Pmgr US4", 67 "S1 Ptitle Oboss", 68 69 "S2 Pname Obb", 70 "S2 Psex Omale", 71 "S2 Pdept Ogenesis", 72 "S2 Pdept Oapp", 73 "S2 Pmgr US4", 74 "S2 Ptitle Oboss", 75 76 "S3 Pname Ojb", 77 "S3 Psex Ofemale", 78 "S3 Pdept Oapp", 79 "S3 Pmgr US1", 80 "S3 Ptitle Oboss", 81 82 "S4 Pname Odick", 83 "S4 Psex Omale", 84 "S4 Pmgr Oshane", 85 "S4 Ptitle Obigboss", 86 87 "S5 Pname Okers", 88 "S5 Psex Omale", 89 "S5 Pdept Ojena", 90 "S5 Pdept Ordql", 91 "S5 Pmgr US1", 92 "S5 Ptitle Ogrunt", 93 94 "S6 Pname Ojjc", 95 "S6 Psex Omale", 96 "S6 Pdept Ojena", 97 "S6 Pdept Ordf", 98 "S6 Pdept Oowl", 99 "S6 Pmgr US1", 100 "S6 Ptitle Ogrunt", 101 102 "S7 Pname Oder", 103 "S7 Psex Omale", 104 "S7 Pdept Ojena", 105 "S7 Pdept Ordf", 106 "S7 Pdept Oowl", 107 "S7 Pmgr US1", 108 "S7 Ptitle Ogrunt", 109 "S7 Ptitle Oboss", 110 111 "S8 Pname Obmk", 112 "S8 Psex Omale", 113 "S8 Pdept Ojena", 114 "S8 Pdept Ordf", 115 "S8 Pmgr US1", 116 "S8 Ptitle Ogrunt", 117 "S8 Ptitle Oboss", 118 119 "S9 Pname Oas", 120 "S9 Psex Omale", 121 "S9 Pdept Ojena", 122 "S9 Pdept Ordf", 123 "S9 Pdept Ordql", 124 "S9 Pmgr US1", 125 "S9 Ptitle Ogrunt", 126 127 "S10 Pname Oian", 128 "S10 Psex Omale", 129 "S10 Pdept Ojena", 130 "S10 Pdept Oowl", 131 "S10 Pmgr US1", 132 "S10 Ptitle Ogrunt", 133 134 "S11 Pname Osteveb", 135 "S11 Psex Omale", 136 "S11 Pdept Oapp", 137 "S11 Pmgr US3", 138 "S11 Ptitle Ogrunt", 139 140 "S12 Pname Ostevec", 141 "S12 Psex Omale", 142 "S12 Pdept Oapp", 143 "S12 Pmgr US3", 144 "S12 Ptitle Ogrunt", 145 146 "S13 Pname Ocs", 147 "S13 Psex Omale", 148 "S13 Pdept Ojena", 149 "S13 Pdept Ogenesis", 150 "S13 Pmgr US2", 151 "S13 Ptitle Ogrunt", 152 153 "S14 Pname Ohk", 154 "S14 Psex Ofemale", 155 "S14 Pdept Oapp", 156 "S14 Pdept Ogenesis", 157 "S14 Pmgr US2", 158 "S14 Ptitle Ogrunt", 159 160 "S15 Pname Oks", 161 "S15 Psex Omale", 162 "S15 Pdept Oapp", 163 "S15 Pdept Ogenesis", 164 "S15 Pmgr US2", 165 166 "S16 Pname Owkw", 167 "S16 Psex Omale", 168 "S16 Pdept Ojena", 169 "S16 Pdept Oapp", 170 "S16 Pmgr US2", 171 "S16 Ptitle Ogrunt", 172 173 175 "N1 S1 Pname Omm", 176 "N2 S1 Psex Omale", 177 "N3 S1 Pdept Ojena", 178 "N4 S1 Pdept Oapp", 179 "N5 S1 Pmgr US4", 180 "N6 S1 Ptitle Oboss", 181 182 "N7 S2 Pname Obb", 183 "N8 S2 Psex Omale", 184 "N9 S2 Pdept Ogenesis", 185 "N10 S2 Pdept Oapp", 186 "N11 S2 Pmgr US4", 187 "N12 S2 Ptitle Oboss", 188 189 "N13 S3 Pname Ojb", 190 "N14 S3 Psex Ofemale", 191 "N15 S3 Pdept Oapp", 192 "N16 S3 Pmgr US1", 193 "N17 S3 Ptitle Oboss", 194 195 "N18 S4 Pname Odick", 196 "N19 S4 Psex Omale", 197 "N20 S4 Pmgr Oshane", 198 "N21 S4 Ptitle Obigboss", 199 200 "N22 S5 Pname Okers", 201 "N23 S5 Psex Omale", 202 "N24 S5 Pdept Ojena", 203 "N25 S5 Pdept Ordql", 204 "N26 S5 Pmgr US1", 205 "N27 S5 Ptitle Ogrunt", 206 207 }; 208 209 public void setUp() throws Exception 210 { 211 super.setUp(); 212 } 213 214 protected Graph standard() 215 { return fetchGraph( ReificationStyle.Standard ); } 216 217 protected Graph convenient() 218 { return fetchGraph( ReificationStyle.Convenient ); } 219 220 protected Graph fetchGraph( ReificationStyle style ) 221 { Graph s = getGraph( style ); 222 loadGraph( s ); 223 return s; } 224 225 protected void tearDown() throws Exception { 226 super.tearDown(); 227 } 228 229 232 protected final String longPrefix = makeLongPrefix(); 233 234 237 private String makeLongPrefix() 238 { StringBuffer sb = new StringBuffer ( 256 ); 239 for (int i = 0; i < 256; i += 1) sb.append( '.' ); 240 return sb.toString(); } 241 242 protected Node makeResource ( String u ) 243 { return Node.createURI( expandLong( u ) ); } 244 245 protected Node makeObject ( String u ) { 246 boolean isRef = u.charAt(0) == 'U'; 247 return 248 isRef ? makeResource( u.substring(1) ) 249 : Node.createLiteral( new LiteralLabel( expandLong( u ) ) ); 250 } 251 252 protected String expandLong( String s ) 253 { return s.charAt(0) == 'L' ? longPrefix + s.substring(1) : s; } 254 255 static int stmtCnt = 0; 256 257 protected void loadGraph ( Graph g ) { 258 Reifier r = g.getReifier(); 259 for (int i = 0; i < statementList.length; i++) { 260 StringTokenizer st = new StringTokenizer( statementList[i] ); 261 String k = st.nextToken(); 262 if ( k.charAt(0) == 'N' ) 263 r.reifyAs( makeResource( k ), nextTriple( st.nextToken(), st ) ); 264 else 265 g.add( nextTriple( k, st ) ); 266 stmtCnt++; 267 } 268 } 269 270 protected Triple nextTriple( String k, StringTokenizer st ) 271 { Node s = makeResource( k ); 272 Node p = makeResource( st.nextToken() ); 273 Node o = makeObject( st.nextToken() ); 274 return Triple.create( s, p, o ); } 275 276 final Node V1 = node( "?v1" ); 277 final Node V2 = node( "?v2" ); 278 final Node V3 = node( "?v3" ); 279 final Node V4 = node( "?v4" ); 280 final Node V5 = node( "?v5" ); 281 282 final Node Ptitle = makeResource("Ptitle"); 283 final Node Psex = makeResource("Psex"); 284 final Node Pname = makeResource("Pname"); 285 final Node Pmgr = makeResource("Pmgr"); 286 final Node Pcard = makeResource("Pcard"); 287 final Node Pcardmax = makeResource("Pcardmax"); 288 final Node Prange = makeResource("Prange"); 289 final Node Pdept = makeResource("Pdept"); 290 final Node S1 = makeResource("S1"); 291 292 final Node Ogrunt = makeObject("Ogrunt"); 294 final Node Ofemale = makeObject("Ofemale"); 295 final Node Omale = makeObject("Omale"); 296 final Node Obigboss = makeObject("Obigboss"); 297 final Node Oboss = makeObject("Oboss"); 298 final Node Oshane = makeObject("Oshane"); 299 final Node Oliteral = makeObject("Oliteral"); 300 final Node Oresource = makeObject("Oresource"); 301 final Node Oapp = makeObject("Oapp"); 302 final Node Ogenesis = makeObject("Ogenesis"); 303 304 final Node O1 = makeObject("O1"); 305 306 public void test0() 307 { 308 Query query = new Query(); 309 query.addMatch( V1, Ptitle, Ogrunt ); 310 query.addMatch( V1, Psex, Ofemale ); 311 query.addMatch( V1, Pname, V3 ); 312 checkCount( 1, standard(), query, new Node[] {V1,V3} ); 313 } 314 315 318 public void test1() 319 { 320 Query query = new Query(); 321 query.addMatch( V1, Ptitle, Ogrunt ); 322 query.addMatch( V1, Psex, Ofemale ); 323 query.addMatch( V1, Pmgr, V2 ); 324 query.addMatch( V2, Pname, V3 ); 325 checkCount( 1, standard(), query, new Node[] {V1,V3} ); 326 } 327 328 331 public void test2() 332 { 333 Query query = new Query(); 334 query.addMatch( V1, Ptitle, Ogrunt ); 335 query.addMatch( V1, Psex, Ofemale ); 336 query.addMatch( V1, Pmgr, V2 ); 337 query.addMatch( V2, Psex, Ofemale ); 338 query.addMatch( V1, Pname, V3 ); 339 checkCount( 0, standard(), query, new Node[] {V1,V3} ); 340 } 341 342 345 public void test3a() 346 { 347 Query query = new Query(); 348 query.addMatch( V1, Ptitle, Obigboss ); 349 query.addMatch( V1, Pmgr, Oshane ); 350 query.addMatch( V1, V2, V3 ); 351 checkCount( 4, standard(), query, new Node[] {V1,V2,V3} ); 352 } 353 354 360 public void test3b() 361 { 362 Query query = new Query(); 363 query.addMatch( V1, Ptitle, Ogrunt ); 364 query.addMatch( V1, Psex, Ofemale ); 365 query.addMatch( V1, Pmgr, V2 ); 366 query.addMatch( V2, Psex, Omale ); 367 query.addMatch( V1, V3, V4 ); 368 checkCount( 6, standard(), query, new Node[] {V1,V3,V4} ); 369 } 370 371 375 public void test4() 376 { 377 Query query = new Query(); 378 query.addMatch( V1, Ptitle, Obigboss ); 379 query.addMatch( V1, Pmgr, Oshane ); 380 query.addMatch( V2, Pcard, O1 ); 381 query.addMatch( V2, Prange, Oliteral ); 382 query.addMatch( V1, V2, V3 ); 383 checkCount( 2, standard(), query, new Node[] {V2,V3} ); 384 } 385 386 391 public void test5() 392 { 393 Query query = new Query(); 394 query.addMatch( V1, Pcardmax, O1 ); query.addMatch( V1, Prange, Oresource ); 396 query.addMatch( S1, V1, V2 ); query.addMatch( V2, Pname, V3 ); 398 query.addMatch( V2, Psex, V4 ); 399 checkCount( 1, standard(), query, new Node[] {V2,V3,V4} ); 400 } 401 402 406 public void test6() 407 { 408 Query query = new Query(); 409 query.addMatch( V1, RDF.Nodes.subject, V2 ); 410 query.addMatch( V1, RDF.Nodes.predicate, V3 ); 411 query.addMatch( V1, RDF.Nodes.object, V4 ); 412 413 checkCount( 27, standard(), query, new Node[] {V2,V3,V4} ); 414 checkCount( 0, convenient(), query, new Node[] {V2,V3,V4} ); 415 } 416 417 421 public void test7() 422 { 423 Query query = new Query(); 424 query.addMatch( V1, RDF.Nodes.subject, V2 ); 425 query.addMatch( V1, RDF.Nodes.predicate, Ptitle ); 426 query.addMatch( V1, RDF.Nodes.object, Obigboss ); 427 query.addMatch( V3, RDF.Nodes.subject, V2 ); 428 query.addMatch( V3, RDF.Nodes.predicate, V4 ); 429 430 checkCount( 4, standard(), query, new Node[] {V2,V3} ); 431 } 432 433 437 public void test8() 438 { 439 Query query = new Query(); 440 query.addMatch( V1, RDF.Nodes.subject, V2 ); 441 query.addMatch( V1, RDF.Nodes.predicate, Ptitle ); 442 query.addMatch( V1, RDF.Nodes.object, Obigboss ); 443 query.addMatch( V3, RDF.Nodes.subject, V2 ); 444 query.addMatch( V3, V4, V5 ); checkCount( 16, standard(), query, new Node[] {V3} ); 446 } 447 448 457 private void checkCount( int expected, Graph g, Query q, Node [] results ) 458 { 459 BindingQueryPlan plan = g.queryHandler().prepareBindings( q, results ); 460 ExtendedIterator it = plan.executeBindings(); 461 assertEquals( "number of reified statements", expected, queryResultCount( it ) ); 462 it.close(); 463 } 464 465 472 protected int queryResultCount( ExtendedIterator it ) { 473 int n = 0; 474 while (it.hasNext()) { 475 n++; 476 ((List) it.next()).size(); } 478 return n; 479 } 480 481 } 482 483 484 | Popular Tags |