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");
}
}
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();
}
}
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);
}
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);
}
}
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);
}
}
Aggregations