Search in sources :

Example 36 with SailRepositoryConnection

use of org.eclipse.rdf4j.repository.sail.SailRepositoryConnection in project FAIRDataPoint by FAIRDataTeam.

the class ShaclValidator method validate.

public void validate(Model shacl, Model data, String baseUri) {
    // 1. Prepare repository
    ShaclSail shaclSail = new ShaclSail(new MemoryStore());
    shaclSail.setRdfsSubClassReasoning(true);
    shaclSail.setUndefinedTargetValidatesAllSubjects(true);
    SailRepository sailRepository = new SailRepository(shaclSail);
    sailRepository.init();
    try (SailRepositoryConnection connection = sailRepository.getConnection()) {
        // 2. Save Shacl
        connection.begin();
        connection.add(shacl, RDF4J.SHACL_SHAPE_GRAPH);
        connection.commit();
        // 3. Validate data
        connection.begin();
        connection.add(new ArrayList<>(data), i(baseUri));
        connection.commit();
    } catch (RepositoryException exception) {
        Throwable cause = exception.getCause();
        if (cause instanceof ShaclSailValidationException) {
            Model validationReportModel = ((ShaclSailValidationException) cause).validationReportAsModel();
            throw new RdfValidationException(validationReportModel);
        }
        throw new ValidationException("Validation failed (unsupported exception");
    }
}
Also used : MemoryStore(org.eclipse.rdf4j.sail.memory.MemoryStore) ShaclSailValidationException(org.eclipse.rdf4j.sail.shacl.ShaclSailValidationException) ValidationException(nl.dtls.fairdatapoint.entity.exception.ValidationException) RdfValidationException(nl.dtls.fairdatapoint.entity.exception.RdfValidationException) RdfValidationException(nl.dtls.fairdatapoint.entity.exception.RdfValidationException) SailRepository(org.eclipse.rdf4j.repository.sail.SailRepository) Model(org.eclipse.rdf4j.model.Model) ShaclSail(org.eclipse.rdf4j.sail.shacl.ShaclSail) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException) SailRepositoryConnection(org.eclipse.rdf4j.repository.sail.SailRepositoryConnection) ShaclSailValidationException(org.eclipse.rdf4j.sail.shacl.ShaclSailValidationException)

Example 37 with SailRepositoryConnection

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

the class MongoEntityIndexIT method partialQuery_Test.

@Test
public void partialQuery_Test() throws Exception {
    final Sail sail = RyaSailFactory.getInstance(conf);
    final SailRepositoryConnection conn = new SailRepository(sail).getConnection();
    conn.begin();
    try (MongoEntityIndexer indexer = new MongoEntityIndexer()) {
        indexer.setConf(conf);
        indexer.init();
        setupTypes(indexer);
        addStatements(conn);
        conn.commit();
        final String query = "SELECT * WHERE { " + "<urn:george> <" + RDF.TYPE + "> <urn:person> ." + "<urn:george> <urn:name> ?name . " + "<urn:george> <urn:eye> ?eye . " + "}";
        final TupleQueryResult rez = conn.prepareTupleQuery(QueryLanguage.SPARQL, query).evaluate();
        final Set<BindingSet> results = new HashSet<>();
        while (rez.hasNext()) {
            final BindingSet bs = rez.next();
            System.out.println(bs);
            results.add(bs);
        }
        final MapBindingSet expected = new MapBindingSet();
        // expected.addBinding("name", VF.createIRI("http://www.w3.org/2001/SMLSchema#string", "George"));
        expected.addBinding("name", VF.createLiteral("George"));
        expected.addBinding("eye", VF.createLiteral("blue"));
        assertEquals(1, results.size());
        assertEquals(expected, results.iterator().next());
    } finally {
        conn.close();
    }
}
Also used : BindingSet(org.eclipse.rdf4j.query.BindingSet) MapBindingSet(org.eclipse.rdf4j.query.impl.MapBindingSet) MongoEntityIndexer(org.apache.rya.indexing.entity.update.mongo.MongoEntityIndexer) SailRepository(org.eclipse.rdf4j.repository.sail.SailRepository) Sail(org.eclipse.rdf4j.sail.Sail) 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 38 with SailRepositoryConnection

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

the class MongoPCJIndexIT method sparqlQuery_Test_complex.

@Test
public void sparqlQuery_Test_complex() 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> ." + " }";
    final String testQuery = "SELECT ?name WHERE {" + " ?name <urn:hasHairColor> <urn:brown> ." + " ?name <urn:likes> <urn:icecream> ." + " ?name <urn:hasEyeColor> <urn:blue> ." + " }";
    // Install an instance of Rya and load statements.
    conf.setBoolean(ConfigUtils.USE_PCJ, true);
    conf.setBoolean(ConfigUtils.USE_OPTIMAL_PCJ, true);
    conf.setBoolean(ConfigUtils.DISPLAY_QUERY_PLAN, true);
    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);
    System.out.println("Triples: " + getMongoClient().getDatabase(conf.getRyaInstanceName()).getCollection(conf.getTriplesCollectionName()).countDocuments());
    System.out.println("PCJS: " + getMongoClient().getDatabase(conf.getRyaInstanceName()).getCollection("pcjs").countDocuments());
    // run the query.  since the triples collection is gone, if the results match, they came from the PCJ index.
    final Sail sail = RyaSailFactory.getInstance(conf);
    final SailRepositoryConnection conn = new SailRepository(sail).getConnection();
    conn.begin();
    final TupleQuery tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, testQuery);
    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 = 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(3, 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 39 with SailRepositoryConnection

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

the class AccumuloRyaSailFactoryLoadFilesIT method addTriples.

private static void addTriples(final SailRepository repo, final InputStream triplesStream, final RDFFormat rdfFormat) throws RDFParseException, RepositoryException, IOException {
    SailRepositoryConnection conn = null;
    try {
        conn = repo.getConnection();
        conn.begin();
        conn.add(triplesStream, "", rdfFormat);
        conn.commit();
    } finally {
        closeQuietly(conn);
    }
}
Also used : SailRepositoryConnection(org.eclipse.rdf4j.repository.sail.SailRepositoryConnection)

Example 40 with SailRepositoryConnection

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

the class MongoDbRyaSailFactoryLoadFilesIT method addTriples.

private static void addTriples(final SailRepository repo, final InputStream triplesStream, final RDFFormat rdfFormat) throws RDFParseException, RepositoryException, IOException {
    SailRepositoryConnection conn = null;
    try {
        conn = repo.getConnection();
        conn.begin();
        conn.add(triplesStream, "", rdfFormat);
        conn.commit();
    } finally {
        closeQuietly(conn);
    }
}
Also used : SailRepositoryConnection(org.eclipse.rdf4j.repository.sail.SailRepositoryConnection)

Aggregations

SailRepositoryConnection (org.eclipse.rdf4j.repository.sail.SailRepositoryConnection)65 SailRepository (org.eclipse.rdf4j.repository.sail.SailRepository)44 Sail (org.eclipse.rdf4j.sail.Sail)37 Test (org.junit.Test)35 BindingSet (org.eclipse.rdf4j.query.BindingSet)24 HashSet (java.util.HashSet)17 MapBindingSet (org.eclipse.rdf4j.query.impl.MapBindingSet)15 TupleQueryResult (org.eclipse.rdf4j.query.TupleQueryResult)13 Statement (org.eclipse.rdf4j.model.Statement)11 ValueFactory (org.eclipse.rdf4j.model.ValueFactory)9 ArrayList (java.util.ArrayList)8 Connector (org.apache.accumulo.core.client.Connector)8 Configuration (org.apache.hadoop.conf.Configuration)8 PrecomputedJoinStorage (org.apache.rya.indexing.pcj.storage.PrecomputedJoinStorage)8 RepositoryException (org.eclipse.rdf4j.repository.RepositoryException)8 RyaStatement (org.apache.rya.api.domain.RyaStatement)7 AccumuloPcjStorage (org.apache.rya.indexing.pcj.storage.accumulo.AccumuloPcjStorage)7 StatefulMongoDBRdfConfiguration (org.apache.rya.mongodb.StatefulMongoDBRdfConfiguration)7 SimpleValueFactory (org.eclipse.rdf4j.model.impl.SimpleValueFactory)7 RyaIRI (org.apache.rya.api.domain.RyaIRI)6