/** * Tests whether an algebra expression gives the same results when run both with and without a * given optimizer * * @param algStr Algebra * @param ds Dataset * @param opt Optimizer * @param expected Expected number of results */ public static void testAsAlgebra(String algStr, Dataset ds, Symbol opt, int expected) { Op op = SSE.parseOp(algStr); List<String> vars = new ArrayList<>(); for (Var v : OpVars.visibleVars(op)) { vars.add(v.getName()); } // Track current state boolean isEnabled = ARQ.isTrue(opt); boolean isDisabled = ARQ.isFalse(opt); try { // Run first without optimization ARQ.set(opt, false); QueryEngineMain engine = new QueryEngineMain(op, ds.asDatasetGraph(), BindingFactory.binding(), ARQ.getContext()); QueryIterator iter = engine.eval(op, ds.asDatasetGraph(), BindingFactory.binding(), ARQ.getContext()); ResultSetRewindable rs = ResultSetFactory.makeRewindable( new ResultSetStream(vars, ModelFactory.createDefaultModel(), iter)); if (expected != rs.size()) { System.err.println("Non-optimized results not as expected"); TextOutput output = new TextOutput((SerializationContext) null); output.format(System.out, rs); rs.reset(); } Assert.assertEquals(expected, rs.size()); iter.close(); // Run with optimization ARQ.set(opt, true); engine = new QueryEngineMain(op, ds.asDatasetGraph(), BindingFactory.binding(), ARQ.getContext()); QueryIterator iterOpt = engine.eval(op, ds.asDatasetGraph(), BindingFactory.binding(), ARQ.getContext()); ResultSetRewindable rsOpt = ResultSetFactory.makeRewindable( new ResultSetStream(vars, ModelFactory.createDefaultModel(), iterOpt)); if (expected != rsOpt.size()) { System.err.println("Optimized results not as expected"); TextOutput output = new TextOutput((SerializationContext) null); output.format(System.out, rsOpt); rsOpt.reset(); } Assert.assertEquals(expected, rsOpt.size()); iterOpt.close(); Assert.assertTrue(ResultSetCompare.isomorphic(rs, rsOpt)); } finally { // Restore previous state if (isEnabled) { ARQ.set(opt, true); } else if (isDisabled) { ARQ.set(opt, false); } else { ARQ.unset(opt); } } }
@Test public void directDataLinkToQuads() throws IOException { // first make a file of quads to load later final Model model = createDefaultModel(); final Path quads = createTempFile("quadExample", ".nq"); final Resource quadsURI = model.createResource(quads.toFile().toURI().toString()); final Resource simpleExample = model.createResource("test:simpleExample"); simpleExample.addProperty(type, DatasetAssemblerVocab.tDatasetTxnMem); simpleExample.addProperty(data, quadsURI); final DatasetGraph dsg = createTxnMem().asDatasetGraph(); model .listStatements() .mapWith(Statement::asTriple) .mapWith(t -> new Quad(quadsURI.asNode(), t)) .forEachRemaining(dsg::add); try (OutputStream out = new FileOutputStream(quads.toFile())) { write(out, dsg, NQUADS); } final Dataset dataset = assemble(simpleExample); final Model assembledDefaultModel = dataset.getDefaultModel(); final Model assembledNamedModel = dataset.getNamedModel(quadsURI.getURI()); assertTrue(assembledDefaultModel.isEmpty()); assertTrue( assembledNamedModel.contains( assembledNamedModel.createStatement(simpleExample, data, quadsURI))); }
@Test public void emptyDataset() { final Model model = createDefaultModel(); final Resource empty = model.createResource("test:empty"); empty.addProperty(type, DatasetAssemblerVocab.tDatasetTxnMem); Dataset dataset = assemble(empty); assertFalse(dataset.asDatasetGraph().find().hasNext()); }
public void setup() { DatasetGraph dsg = TDBFactory.createDatasetGraph(); dsg.getContext().set(TDB.symUnionDefaultGraph, true); Dataset myDataset = DatasetFactory.create(dsg); baseModel = myDataset.getNamedModel("http://example.com/baseModel"); baseModel = QueryEngineTest.populateModel(baseModel); dftModel = Factory.getInstance(eval, "http://example.com/securedModel", baseModel); dataset = DatasetFactory.createMem(); dataset.setDefaultModel(dftModel); }
@BeforeClass public static void setup() { // Dataset for implicit join tests implJoin = DatasetFactory.createTxnMem(); Node a = NodeFactory.createURI("http://a"); Node b = NodeFactory.createURI("http://b"); Node c = NodeFactory.createURI("http://c"); Node p1 = NodeFactory.createURI("http://p1"); Node p2 = NodeFactory.createURI("http://p2"); Node pSelf = NodeFactory.createURI("http://self"); Node o = NodeFactory.createLiteral("object"); DatasetGraph dsg = implJoin.asDatasetGraph(); dsg.add(Quad.defaultGraphNodeGenerated, a, p1, o); dsg.add(Quad.defaultGraphNodeGenerated, a, p2, o); dsg.add(Quad.defaultGraphNodeGenerated, b, p1, o); dsg.add(Quad.defaultGraphNodeGenerated, b, p2, o); dsg.add(Quad.defaultGraphNodeGenerated, c, p1, o); // dsg.add(Quad.defaultGraphNodeGenerated, a, pSelf, a); // Currently these optimizations are off by default Assert.assertFalse(ARQ.isFalse(ARQ.optFilterImplicitJoin)); Assert.assertFalse(ARQ.isFalse(ARQ.optImplicitLeftJoin)); }
@Test public void testOpenQueryType() { eval = new MockSecurityEvaluator(true, true, true, true, true, true); setup(); try { final String query = "prefix fn: <http://www.w3.org/2005/xpath-functions#> " + " SELECT ?foo ?bar WHERE " + " { ?foo a <http://example.com/class> ; " + "?bar [] ." + " } "; final QueryExecution qexec = QueryExecutionFactory.create(query, dataset); try { final ResultSet results = qexec.execSelect(); int count = 0; for (; results.hasNext(); ) { count++; results.nextSolution(); } Assert.assertEquals(8, count); } finally { qexec.close(); } } finally { dataset.close(); } }
@Test public void testSelectAllType() { eval = new MockSecurityEvaluator(true, true, true, true, true, true) { @Override public boolean evaluate( final Object principal, final Action action, final Node graphIRI, final Triple triple) { if (triple.getSubject().isURI() && triple.getSubject().getURI().equals("http://example.com/resource/1")) { return false; } return super.evaluate(principal, action, graphIRI, triple); } }; setup(); try { String query = "SELECT ?s ?p ?o WHERE " + " { ?s ?p ?o } "; QueryExecution qexec = QueryExecutionFactory.create(query, dataset); try { final ResultSet results = qexec.execSelect(); int count = 0; for (; results.hasNext(); ) { count++; results.nextSolution(); } // 2x 3 values + type triple Assert.assertEquals(8, count); } finally { qexec.close(); } query = "SELECT ?g ?s ?p ?o WHERE " + " { GRAPH ?g {?s ?p ?o } }"; qexec = QueryExecutionFactory.create(query, dataset); try { final ResultSet results = qexec.execSelect(); int count = 0; for (; results.hasNext(); ) { count++; results.nextSolution(); } // 2x 3 values + type triple // all are in the base graph so no named graphs Assert.assertEquals(0, count); } finally { qexec.close(); } } finally { dataset.close(); } }
@Test public void testUnionWorks() { Dataset ds = (Dataset) AssemblerUtils.build("union.ttl", SDBConnect.TYPE); ReconnectingDatasetGraph toQuery = (ReconnectingDatasetGraph) ds.asDatasetGraph(); toQuery.getDatasetGraph().getStore().getTableFormatter().format(); UpdateRequest ur = UpdateFactory.create( "insert data {" + "graph <http://example.com/a> { <http://example.com/1> <http://example.com/prop> 1 }" + "graph <http://example.com/b> { <http://example.com/2> <http://example.com/prop> 2 }" + "}"); UpdateProcessor u = UpdateExecutionFactory.create(ur, toQuery); u.execute(); QueryExecution qe = QueryExecutionFactory.create("SELECT * { ?s ?p ?o }", ds); ResultSetRewindable r = ResultSetFactory.makeRewindable(qe.execSelect()); assertEquals("We have a union!", 2, r.size()); }
public static List<String> listThingDescriptions(String query) { List<String> tds = new ArrayList<>(); Dataset dataset = Repository.get().dataset; dataset.begin(ReadWrite.READ); try { String q = "SELECT DISTINCT ?g WHERE { GRAPH ?g { " + query + " }}"; try (QueryExecution qexec = QueryExecutionFactory.create(q, dataset)) { ResultSet result = qexec.execSelect(); while (result.hasNext()) { tds.add(result.next().get("g").asResource().getURI()); } } } finally { dataset.end(); } return tds; }
@AfterClass public static void teardown() { if (implJoin != null) { implJoin.close(); implJoin = null; } // Currently these optimizations are off by default Assert.assertFalse(ARQ.isFalse(ARQ.optFilterImplicitJoin)); Assert.assertFalse(ARQ.isFalse(ARQ.optImplicitLeftJoin)); }
@Test public void directDataLinkForDefaultAndNamedGraphs() throws IOException { // first make a file of triples to load later final Model model = createDefaultModel(); final Path triples = createTempFile("simpleExample", ".nt"); final Resource triplesURI = model.createResource(triples.toFile().toURI().toString()); final Resource simpleExample = model.createResource("test:simpleExample"); simpleExample.addProperty(type, DatasetAssemblerVocab.tDatasetTxnMem); // add a default graph simpleExample.addProperty(data, triplesURI); // add a named graph final Resource namedGraphDef = model.createResource("test:namedGraphDef"); simpleExample.addProperty(pNamedGraph, namedGraphDef); final Resource namedGraphName = model.createResource("test:namedGraphExample"); namedGraphDef.addProperty(type, MemoryModel); namedGraphDef.addProperty(pGraphName, namedGraphName); namedGraphDef.addProperty(data, triplesURI); try (OutputStream out = new FileOutputStream(triples.toFile())) { write(out, model, NTRIPLES); } final Dataset dataset = assemble(simpleExample); final Model assembledDefaultModel = dataset.getDefaultModel(); final Model assembledNamedModel = dataset.getNamedModel(namedGraphName.getURI()); // we put the same triples in each model, so we check for the same triples in each model for (final Model m : new Model[] {assembledDefaultModel, assembledNamedModel}) { assertTrue(m.contains(simpleExample, pNamedGraph, namedGraphDef)); assertTrue(m.contains(namedGraphDef, pGraphName, namedGraphName)); assertTrue(m.contains(simpleExample, data, triplesURI)); } final Iterator<Node> graphNodes = dataset.asDatasetGraph().listGraphNodes(); assertTrue(graphNodes.hasNext()); assertEquals(namedGraphName.asNode(), graphNodes.next()); assertFalse(graphNodes.hasNext()); }
@Test public void dataset_05() { String graphName = "http://example/"; Dataset ds = createFixed(); ds.addNamedModel(graphName, model1); ds.replaceNamedModel(graphName, model2); assertTrue(ds.containsNamedModel(graphName)); List<String> x = Iter.toList(ds.listNames()); assertEquals(1, x.size()); assertEquals(graphName, x.get(0)); assertFalse(model1.isIsomorphicWith(ds.getNamedModel(graphName))); assertTrue(model2.isIsomorphicWith(ds.getNamedModel(graphName))); }
@Test public void testRestrictedQueryType() { eval = new MockSecurityEvaluator(true, true, true, true, true, true) { @Override public boolean evaluate( final Object principal, final Action action, final Node graphIRI, final Triple triple) { if (triple.getSubject().isURI() && triple.getSubject().getURI().equals("http://example.com/resource/1")) { return false; } return super.evaluate(principal, action, graphIRI, triple); } }; setup(); try { final String query = "prefix fn: <http://www.w3.org/2005/xpath-functions#> " + " SELECT ?foo ?bar WHERE " + " { ?foo a <http://example.com/class> ; " + "?bar [] ." + " } "; final QueryExecution qexec = QueryExecutionFactory.create(query, dataset); try { final ResultSet results = qexec.execSelect(); int count = 0; for (; results.hasNext(); ) { count++; results.nextSolution(); } Assert.assertEquals(4, count); } finally { qexec.close(); } } finally { dataset.close(); } }
@Test public void dataset_04() { String graphName = "http://example/"; Dataset ds = createFixed(); ds.addNamedModel(graphName, model1); assertTrue(ds.containsNamedModel(graphName)); List<String> x = Iter.toList(ds.listNames()); assertEquals(1, x.size()); assertEquals(graphName, x.get(0)); assertFalse(model1.isIsomorphicWith(ds.getDefaultModel())); Model m = ds.getNamedModel(graphName); assertNotNull(m); assertTrue(model1.isIsomorphicWith(m)); ds.removeNamedModel(graphName); // Not getNamedModel and test for null as some datasets are "auto graph creating" assertFalse(ds.containsNamedModel(graphName)); }
public static void main(String[] args) throws IOException { if (args.length != 2) { err.println("usage: difference <db1> <db2>"); } Dataset ds1 = dataset_(args[0]); Dataset ds2 = dataset_(args[1]); Model m1 = ds1.getDefaultModel(); Model m2 = ds2.getDefaultModel(); System.out.println(m1.size()); System.out.println(m2.size()); Model m1_minus_m2 = m1.difference(m2); RDFDataMgr.write( new FileOutputStream("missing-orthologousMatch-in-biological-concepts-db.ttl"), m1_minus_m2, Lang.TURTLE); ds1.close(); ds2.close(); }
@Test public void dataset_03() { Dataset ds = createFixed(); ds.setDefaultModel(model2); assertTrue(model2.isIsomorphicWith(ds.getDefaultModel())); }
@Test public void dataset_02() { Dataset ds = createFixed(); ds.getDefaultModel().add(s1, p1, o1); assertTrue(model1.isIsomorphicWith(ds.getDefaultModel())); }
@Test public void dataset_01() { Dataset ds = createFixed(); assertNotNull(ds.getDefaultModel()); assertNotNull(ds.asDatasetGraph()); }