Search in sources :

Example 21 with Sail

use of org.eclipse.rdf4j.sail.Sail in project incubator-rya by apache.

the class MongoPCJIndexIT method sparqlQuery_Test.

@Test
public void sparqlQuery_Test() throws Exception {
    // Setup a Rya Client.
    final MongoConnectionDetails connectionDetails = getConnectionDetails();
    final RyaClient ryaClient = MongoRyaClientFactory.build(connectionDetails, getMongoClient());
    final String pcjQuery = "SELECT ?name WHERE {" + " ?name <urn:likes> <urn:icecream> ." + " ?name <urn:hasEyeColor> <urn:blue> ." + " }";
    // Install an instance of Rya and load statements.
    ryaClient.getInstall().install(conf.getRyaInstanceName(), InstallConfiguration.builder().setEnablePcjIndex(true).build());
    ryaClient.getLoadStatements().loadStatements(conf.getRyaInstanceName(), getStatements());
    final String pcjId = ryaClient.getCreatePCJ().createPCJ(conf.getRyaInstanceName(), pcjQuery);
    ryaClient.getBatchUpdatePCJ().batchUpdate(conf.getRyaInstanceName(), pcjId);
    // purge contents of rya triples collection
    getMongoClient().getDatabase(conf.getRyaInstanceName()).getCollection(conf.getTriplesCollectionName()).drop();
    // run the query.  since the triples collection is gone, if the results match, they came from the PCJ index.
    conf.setBoolean(ConfigUtils.USE_PCJ, true);
    conf.setBoolean(ConfigUtils.USE_OPTIMAL_PCJ, true);
    conf.setBoolean(ConfigUtils.DISPLAY_QUERY_PLAN, true);
    final Sail sail = RyaSailFactory.getInstance(conf);
    final SailRepositoryConnection conn = new SailRepository(sail).getConnection();
    conn.begin();
    final TupleQuery tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, pcjQuery);
    tupleQuery.setBinding(RdfCloudTripleStoreConfiguration.CONF_QUERYPLAN_FLAG, RdfCloudTripleStoreConstants.VALUE_FACTORY.createLiteral(true));
    final TupleQueryResult rez = tupleQuery.evaluate();
    final Set<BindingSet> results = new HashSet<>();
    while (rez.hasNext()) {
        final BindingSet bs = rez.next();
        results.add(bs);
    }
    // Verify the correct results were loaded into the PCJ table.
    final Set<BindingSet> expectedResults = new HashSet<>();
    MapBindingSet bs = new MapBindingSet();
    bs.addBinding("name", VF.createIRI("urn:Alice"));
    expectedResults.add(bs);
    bs = new MapBindingSet();
    bs.addBinding("name", VF.createIRI("urn:Bob"));
    expectedResults.add(bs);
    bs = new MapBindingSet();
    bs.addBinding("name", VF.createIRI("urn:Charlie"));
    expectedResults.add(bs);
    bs = new MapBindingSet();
    bs.addBinding("name", VF.createIRI("urn:David"));
    expectedResults.add(bs);
    bs = new MapBindingSet();
    bs.addBinding("name", VF.createIRI("urn:Eve"));
    expectedResults.add(bs);
    bs = new MapBindingSet();
    bs.addBinding("name", VF.createIRI("urn:Frank"));
    expectedResults.add(bs);
    assertEquals(6, results.size());
    assertEquals(expectedResults, results);
}
Also used : MongoConnectionDetails(org.apache.rya.api.client.mongo.MongoConnectionDetails) BindingSet(org.eclipse.rdf4j.query.BindingSet) MapBindingSet(org.eclipse.rdf4j.query.impl.MapBindingSet) SailRepository(org.eclipse.rdf4j.repository.sail.SailRepository) Sail(org.eclipse.rdf4j.sail.Sail) TupleQuery(org.eclipse.rdf4j.query.TupleQuery) RyaClient(org.apache.rya.api.client.RyaClient) MapBindingSet(org.eclipse.rdf4j.query.impl.MapBindingSet) SailRepositoryConnection(org.eclipse.rdf4j.repository.sail.SailRepositoryConnection) TupleQueryResult(org.eclipse.rdf4j.query.TupleQueryResult) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 22 with Sail

use of org.eclipse.rdf4j.sail.Sail in project incubator-rya by apache.

the class MongoPcjIntegrationTest method testEvaluateThreeIndexValidate.

@Test
public void testEvaluateThreeIndexValidate() throws Exception {
    final Sail nonPcjSail = RyaSailFactory.getInstance(conf);
    final MongoDBRdfConfiguration pcjConf = conf.clone();
    pcjConf.setBoolean(ConfigUtils.USE_PCJ, true);
    final Sail pcjSail = RyaSailFactory.getInstance(pcjConf);
    final SailRepositoryConnection conn = new SailRepository(nonPcjSail).getConnection();
    final SailRepositoryConnection pcjConn = new SailRepository(pcjSail).getConnection();
    addPCJS(pcjConn);
    try {
        final IRI superclass = VF.createIRI("uri:superclass");
        final IRI superclass2 = VF.createIRI("uri:superclass2");
        final IRI howlsAt = VF.createIRI("uri:howlsAt");
        final IRI subType = VF.createIRI("uri:subType");
        final IRI superSuperclass = VF.createIRI("uri:super_superclass");
        conn.add(subclass, RDF.TYPE, superclass);
        conn.add(subclass2, RDF.TYPE, superclass2);
        conn.add(obj, RDFS.LABEL, VF.createLiteral("label"));
        conn.add(obj2, RDFS.LABEL, VF.createLiteral("label2"));
        conn.add(sub, howlsAt, superclass);
        conn.add(superclass, subType, superSuperclass);
        final String indexSparqlString = // 
        "" + // 
        "SELECT ?dog ?pig ?duck  " + // 
        "{" + // 
        "  ?pig a ?dog . " + // 
        "  ?pig <http://www.w3.org/2000/01/rdf-schema#label> ?duck " + // 
        "}";
        final String indexSparqlString2 = // 
        "" + // 
        "SELECT ?o ?f ?e ?c ?l  " + // 
        "{" + // 
        "  ?e <uri:talksTo> ?o . " + // 
        "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. " + // 
        "  ?c a ?f . " + // 
        "}";
        final String indexSparqlString3 = // 
        "" + // 
        "SELECT ?wolf ?sheep ?chicken  " + // 
        "{" + // 
        "  ?wolf <uri:howlsAt> ?sheep . " + // 
        "  ?sheep <uri:subType> ?chicken. " + // 
        "}";
        final String queryString = // 
        "" + // 
        "SELECT ?e ?c ?l ?f ?o " + // 
        "{" + // 
        "  ?e a ?c . " + // 
        "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l. " + // 
        "  ?e <uri:talksTo> ?o . " + // 
        "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. " + // 
        "  ?c a ?f . " + // 
        "  ?e <uri:howlsAt> ?f. " + // 
        "  ?f <uri:subType> ?o. " + // 
        "}";
        PcjIntegrationTestingUtil.createAndPopulatePcj(conn, getMongoClient(), conf.getMongoDBName() + 1, conf.getRyaInstanceName(), indexSparqlString);
        final MongoPcjQueryNode ais1 = new MongoPcjQueryNode(conf, conf.getMongoDBName() + 1);
        PcjIntegrationTestingUtil.createAndPopulatePcj(conn, getMongoClient(), conf.getMongoDBName() + 2, conf.getRyaInstanceName(), indexSparqlString2);
        final MongoPcjQueryNode ais2 = new MongoPcjQueryNode(conf, conf.getMongoDBName() + 2);
        PcjIntegrationTestingUtil.createAndPopulatePcj(conn, getMongoClient(), conf.getMongoDBName() + 3, conf.getRyaInstanceName(), indexSparqlString3);
        final MongoPcjQueryNode ais3 = new MongoPcjQueryNode(conf, conf.getMongoDBName() + 3);
        final List<ExternalTupleSet> index = new ArrayList<>();
        index.add(ais1);
        index.add(ais3);
        index.add(ais2);
        ParsedQuery pq = null;
        final SPARQLParser sp = new SPARQLParser();
        pq = sp.parseQuery(queryString, null);
        final List<TupleExpr> teList = Lists.newArrayList();
        final TupleExpr te = pq.getTupleExpr();
        final PCJOptimizer pcj = new PCJOptimizer(index, false, new MongoPcjIndexSetProvider(new StatefulMongoDBRdfConfiguration(conf, getMongoClient())));
        pcj.optimize(te, null, null);
        teList.add(te);
        final IndexPlanValidator ipv = new IndexPlanValidator(false);
        assertTrue(ipv.isValid(te));
    } finally {
        conn.close();
        pcjConn.close();
        nonPcjSail.shutDown();
        pcjSail.shutDown();
    }
}
Also used : IRI(org.eclipse.rdf4j.model.IRI) SPARQLParser(org.eclipse.rdf4j.query.parser.sparql.SPARQLParser) StatefulMongoDBRdfConfiguration(org.apache.rya.mongodb.StatefulMongoDBRdfConfiguration) MongoPcjQueryNode(org.apache.rya.indexing.mongodb.pcj.MongoPcjQueryNode) SailRepository(org.eclipse.rdf4j.repository.sail.SailRepository) ParsedQuery(org.eclipse.rdf4j.query.parser.ParsedQuery) ArrayList(java.util.ArrayList) IndexPlanValidator(org.apache.rya.indexing.IndexPlanValidator.IndexPlanValidator) SailRepositoryConnection(org.eclipse.rdf4j.repository.sail.SailRepositoryConnection) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr) ExternalTupleSet(org.apache.rya.indexing.external.tupleSet.ExternalTupleSet) PCJOptimizer(org.apache.rya.indexing.pcj.matching.PCJOptimizer) Sail(org.eclipse.rdf4j.sail.Sail) MongoPcjIndexSetProvider(org.apache.rya.indexing.mongodb.pcj.MongoPcjIndexSetProvider) MongoDBRdfConfiguration(org.apache.rya.mongodb.MongoDBRdfConfiguration) StatefulMongoDBRdfConfiguration(org.apache.rya.mongodb.StatefulMongoDBRdfConfiguration) Test(org.junit.Test)

Example 23 with Sail

use of org.eclipse.rdf4j.sail.Sail in project incubator-rya by apache.

the class MongoPcjIntegrationTest method testEvaluateSingleIndex.

@Test
public void testEvaluateSingleIndex() throws Exception {
    final Sail nonPcjSail = RyaSailFactory.getInstance(conf);
    final MongoDBRdfConfiguration pcjConf = conf.clone();
    pcjConf.setBoolean(ConfigUtils.USE_PCJ, true);
    final Sail pcjSail = RyaSailFactory.getInstance(pcjConf);
    final SailRepositoryConnection conn = new SailRepository(nonPcjSail).getConnection();
    final SailRepositoryConnection pcjConn = new SailRepository(pcjSail).getConnection();
    addPCJS(pcjConn);
    try {
        final String indexSparqlString = // 
        "" + // 
        "SELECT ?e ?l ?c " + // 
        "{" + // 
        "  ?e a ?c . " + // 
        "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l " + // 
        "}";
        PcjIntegrationTestingUtil.createAndPopulatePcj(conn, getMongoClient(), conf.getMongoDBName() + 1, conf.getRyaInstanceName(), indexSparqlString);
        final String queryString = // 
        "" + // 
        "SELECT ?e ?c ?l ?o " + // 
        "{" + // 
        "  ?e a ?c . " + // 
        "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . " + // 
        "  ?e <uri:talksTo> ?o . " + // 
        "}";
        final CountingResultHandler crh1 = new CountingResultHandler();
        final CountingResultHandler crh2 = new CountingResultHandler();
        conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(crh1);
        pcjConn.prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(crh2);
        assertEquals(crh1.getCount(), crh2.getCount());
    } finally {
        conn.close();
        pcjConn.close();
        nonPcjSail.shutDown();
        pcjSail.shutDown();
    }
}
Also used : SailRepository(org.eclipse.rdf4j.repository.sail.SailRepository) Sail(org.eclipse.rdf4j.sail.Sail) MongoDBRdfConfiguration(org.apache.rya.mongodb.MongoDBRdfConfiguration) StatefulMongoDBRdfConfiguration(org.apache.rya.mongodb.StatefulMongoDBRdfConfiguration) SailRepositoryConnection(org.eclipse.rdf4j.repository.sail.SailRepositoryConnection) Test(org.junit.Test)

Example 24 with Sail

use of org.eclipse.rdf4j.sail.Sail in project incubator-rya by apache.

the class MongoPcjIntegrationTest method testEvaluateTwoIndexValidate.

@Test
public void testEvaluateTwoIndexValidate() throws Exception {
    final Sail nonPcjSail = RyaSailFactory.getInstance(conf);
    final MongoDBRdfConfiguration pcjConf = conf.clone();
    pcjConf.setBoolean(ConfigUtils.USE_PCJ, true);
    final Sail pcjSail = RyaSailFactory.getInstance(pcjConf);
    final SailRepositoryConnection conn = new SailRepository(nonPcjSail).getConnection();
    final SailRepositoryConnection pcjConn = new SailRepository(pcjSail).getConnection();
    addPCJS(pcjConn);
    try {
        final IRI superclass = VF.createIRI("uri:superclass");
        final IRI superclass2 = VF.createIRI("uri:superclass2");
        conn.add(subclass, RDF.TYPE, superclass);
        conn.add(subclass2, RDF.TYPE, superclass2);
        conn.add(obj, RDFS.LABEL, VF.createLiteral("label"));
        conn.add(obj2, RDFS.LABEL, VF.createLiteral("label2"));
        final String indexSparqlString = // 
        "" + // 
        "SELECT ?dog ?pig ?duck  " + // 
        "{" + // 
        "  ?pig a ?dog . " + // 
        "  ?pig <http://www.w3.org/2000/01/rdf-schema#label> ?duck " + // 
        "}";
        final String indexSparqlString2 = // 
        "" + // 
        "SELECT ?o ?f ?e ?c ?l  " + // 
        "{" + // 
        "  ?e <uri:talksTo> ?o . " + // 
        "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. " + // 
        "  ?c a ?f . " + // 
        "}";
        final String queryString = // 
        "" + // 
        "SELECT ?e ?c ?l ?f ?o " + // 
        "{" + // 
        "  ?e a ?c . " + // 
        "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l. " + // 
        "  ?e <uri:talksTo> ?o . " + // 
        "  ?o <http://www.w3.org/2000/01/rdf-schema#label> ?l. " + // 
        "  ?c a ?f . " + // 
        "}";
        PcjIntegrationTestingUtil.createAndPopulatePcj(conn, getMongoClient(), conf.getMongoDBName() + 1, conf.getRyaInstanceName(), indexSparqlString);
        final MongoPcjQueryNode ais1 = new MongoPcjQueryNode(conf, conf.getMongoDBName() + 1);
        PcjIntegrationTestingUtil.createAndPopulatePcj(conn, getMongoClient(), conf.getMongoDBName() + 2, conf.getRyaInstanceName(), indexSparqlString2);
        final MongoPcjQueryNode ais2 = new MongoPcjQueryNode(conf, conf.getMongoDBName() + 2);
        final List<ExternalTupleSet> index = new ArrayList<>();
        index.add(ais1);
        index.add(ais2);
        ParsedQuery pq = null;
        final SPARQLParser sp = new SPARQLParser();
        pq = sp.parseQuery(queryString, null);
        final List<TupleExpr> teList = Lists.newArrayList();
        final TupleExpr te = pq.getTupleExpr();
        final PCJOptimizer pcj = new PCJOptimizer(index, false, new MongoPcjIndexSetProvider(new StatefulMongoDBRdfConfiguration(conf, getMongoClient())));
        pcj.optimize(te, null, null);
        teList.add(te);
        final IndexPlanValidator ipv = new IndexPlanValidator(false);
        assertTrue(ipv.isValid(te));
    } finally {
        conn.close();
        pcjConn.close();
        nonPcjSail.shutDown();
        pcjSail.shutDown();
    }
}
Also used : IRI(org.eclipse.rdf4j.model.IRI) SPARQLParser(org.eclipse.rdf4j.query.parser.sparql.SPARQLParser) StatefulMongoDBRdfConfiguration(org.apache.rya.mongodb.StatefulMongoDBRdfConfiguration) MongoPcjQueryNode(org.apache.rya.indexing.mongodb.pcj.MongoPcjQueryNode) SailRepository(org.eclipse.rdf4j.repository.sail.SailRepository) ParsedQuery(org.eclipse.rdf4j.query.parser.ParsedQuery) ArrayList(java.util.ArrayList) IndexPlanValidator(org.apache.rya.indexing.IndexPlanValidator.IndexPlanValidator) SailRepositoryConnection(org.eclipse.rdf4j.repository.sail.SailRepositoryConnection) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr) ExternalTupleSet(org.apache.rya.indexing.external.tupleSet.ExternalTupleSet) PCJOptimizer(org.apache.rya.indexing.pcj.matching.PCJOptimizer) Sail(org.eclipse.rdf4j.sail.Sail) MongoPcjIndexSetProvider(org.apache.rya.indexing.mongodb.pcj.MongoPcjIndexSetProvider) MongoDBRdfConfiguration(org.apache.rya.mongodb.MongoDBRdfConfiguration) StatefulMongoDBRdfConfiguration(org.apache.rya.mongodb.StatefulMongoDBRdfConfiguration) Test(org.junit.Test)

Example 25 with Sail

use of org.eclipse.rdf4j.sail.Sail in project incubator-rya by apache.

the class MongoStatementMetadataIT method simpleQueryWithBindingSet.

@Test
public void simpleQueryWithBindingSet() throws Exception {
    final Sail sail = RyaSailFactory.getInstance(conf);
    final MongoDBRyaDAO dao = new MongoDBRyaDAO();
    try {
        dao.setConf(conf);
        dao.init();
        final StatementMetadata metadata = new StatementMetadata();
        metadata.addMetadata(new RyaIRI("http://createdBy"), new RyaType("Joe"));
        metadata.addMetadata(new RyaIRI("http://createdOn"), new RyaType(XMLSchema.DATE, "2017-01-04"));
        final RyaStatement statement1 = new RyaStatement(new RyaIRI("http://Joe"), new RyaIRI("http://worksAt"), new RyaType("CoffeeShop"), new RyaIRI("http://context"), "", metadata);
        final RyaStatement statement2 = new RyaStatement(new RyaIRI("http://Joe"), new RyaIRI("http://worksAt"), new RyaType("HardwareStore"), new RyaIRI("http://context"), "", metadata);
        dao.add(statement1);
        dao.add(statement2);
        final SailRepositoryConnection conn = new SailRepository(sail).getConnection();
        final TupleQueryResult result = conn.prepareTupleQuery(QueryLanguage.SPARQL, query1).evaluate();
        final Set<BindingSet> expected = new HashSet<>();
        final QueryBindingSet expected1 = new QueryBindingSet();
        expected1.addBinding("x", VF.createLiteral("CoffeeShop"));
        expected1.addBinding("y", VF.createLiteral("Joe"));
        final QueryBindingSet expected2 = new QueryBindingSet();
        expected2.addBinding("x", VF.createLiteral("HardwareStore"));
        expected2.addBinding("y", VF.createLiteral("Joe"));
        expected.add(expected1);
        expected.add(expected2);
        final Set<BindingSet> bsSet = new HashSet<>();
        while (result.hasNext()) {
            bsSet.add(result.next());
        }
        assertEquals(expected, bsSet);
        dao.delete(statement1, conf);
        dao.delete(statement2, conf);
    } finally {
        dao.destroy();
        sail.shutDown();
    }
}
Also used : QueryBindingSet(org.eclipse.rdf4j.query.algebra.evaluation.QueryBindingSet) BindingSet(org.eclipse.rdf4j.query.BindingSet) RyaIRI(org.apache.rya.api.domain.RyaIRI) SailRepository(org.eclipse.rdf4j.repository.sail.SailRepository) StatementMetadata(org.apache.rya.api.domain.StatementMetadata) RyaStatement(org.apache.rya.api.domain.RyaStatement) RyaType(org.apache.rya.api.domain.RyaType) SailRepositoryConnection(org.eclipse.rdf4j.repository.sail.SailRepositoryConnection) QueryBindingSet(org.eclipse.rdf4j.query.algebra.evaluation.QueryBindingSet) MongoDBRyaDAO(org.apache.rya.mongodb.MongoDBRyaDAO) Sail(org.eclipse.rdf4j.sail.Sail) TupleQueryResult(org.eclipse.rdf4j.query.TupleQueryResult) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

Sail (org.eclipse.rdf4j.sail.Sail)68 SailRepository (org.eclipse.rdf4j.repository.sail.SailRepository)49 SailRepositoryConnection (org.eclipse.rdf4j.repository.sail.SailRepositoryConnection)37 Test (org.junit.Test)33 AccumuloRdfConfiguration (org.apache.rya.accumulo.AccumuloRdfConfiguration)15 BindingSet (org.eclipse.rdf4j.query.BindingSet)13 TupleQueryResult (org.eclipse.rdf4j.query.TupleQueryResult)13 SailException (org.eclipse.rdf4j.sail.SailException)13 HashSet (java.util.HashSet)11 ValueFactory (org.eclipse.rdf4j.model.ValueFactory)11 SailConnection (org.eclipse.rdf4j.sail.SailConnection)11 RyaClient (org.apache.rya.api.client.RyaClient)9 ArrayList (java.util.ArrayList)8 AccumuloSecurityException (org.apache.accumulo.core.client.AccumuloSecurityException)8 Configuration (org.apache.hadoop.conf.Configuration)8 RyaClientException (org.apache.rya.api.client.RyaClientException)8 MongoDBRdfConfiguration (org.apache.rya.mongodb.MongoDBRdfConfiguration)7 RyaSailRepository (org.apache.rya.rdftriplestore.RyaSailRepository)7 MapBindingSet (org.eclipse.rdf4j.query.impl.MapBindingSet)7 RepositoryException (org.eclipse.rdf4j.repository.RepositoryException)7