8.9 Validating RDF Data with SHACL Constraints
This section explains how to validate RDF graphs with SHACL (Shapes Constraint Language) constraints using Oracle RDF Graph Adapter for Eclipse RDF4J.
SHACL is a W3C standard for specifying constraints for RDF graphs. For
example, SHACL allows you to specify that all instances of the
ex:Person class must have a value for the ex:name
property. SHACL defines an RDF vocabulary that allows you to specify constraints. An RDF
graph that contains SHACL constraints is referred to as a shapes graph, and the RDF
graph to be validated is referred to as a data graph.
RDF4J supports SHACL through its ShaclSail class. The SHACL
engine in RDF4J validates a graph when changes to the graph are committed through
RDF4J’s transaction mechanism. See the RDF4J Documentation for general information
about RDF4J’s SHACL support.
When using ShaclSail with Oracle Adapter for Eclipse RDF4J
through the OracleShaclSail class, a full SHACL validation runs when a
transaction is committed. Incremental validation targeted at a subset of data is not
supported. You should therefore avoid committing many small changes through an
OracleShaclSail object, and use OracleShaclSail
for large, bulk validation operations. The RDF4J SHACL engine sends a series of SPARQL
queries to Oracle AI Database to check for constraint
violations. A typical workflow for bulk validation is shown below:
- Create an
OracleSailStoreobject for an RDF data graph stored in Oracle AI Database. - Create an
OracleShaclSailobject that wraps theOracleSailStore. - Create a
SailRepositoryfrom theOracleShaclSailobject. - Begin a transaction on the
SailRepository. - Add a shapes graph to the
RDF4J.SHACL_SHAPE_GRAPHcontext as a part of the transaction (this shapes graph can be loaded from a variety of sources). - Commit the transaction.
- A
RepositoryExceptionwill be raised if a constraint is violated. - Check the validation report if an exception was raised.
These steps are illustrated in the following code fragment:
// Get an OracleSailStore instance for the stored data graph to validate
OraclePool op = new OraclePool(jdbcURL, user, password);
NotifyingSail store = new OracleSailStore(op, "DATA_GRAPH", "SCOTT", "NET1");
// Create an OracleShaclSail on top of the underlying OracleSailStore
ShaclSail shaclSail = new OracleShaclSail(store);
SailRepository sailRepository = new SailRepository(shaclSail);
sailRepository.init();
// Get a connection from the repository, start a transaction,
// load a shapes graph and commit the transaction to validate the data graph
try (SailRepositoryConnection conn = sailRepository.getConnection()) {
conn.begin();
// clear any existing shapes graph
conn.clear(RDF4J.SHACL_SHAPE_GRAPH);
// Add current shapes graph
// Every person must have a name property
StringReader shaclRules = new StringReader(
String.join(
"\n", "",
"@prefix ex: <http://oracle.example.com/ns#> .",
"@prefix sh: <http://www.w3.org/ns/shacl#> .",
"@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .",
"@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .",
"@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .",
"ex:MinCountShape",
" a sh:NodeShape ;",
" sh:targetClass ex:Person ;",
" sh:property [",
" sh:path ex:name ;",
" sh:minCount 1 ;",
" ] ."
)
);
try {
conn.add(shaclRules, null, RDFFormat.TURTLE, RDF4J.SHACL_SHAPE_GRAPH);
}
catch (IOException e) {
e.printStackTrace();
}
// Commit transaction to validate the data graph with the current shapes graph
try {
conn.commit();
}
catch (RepositoryException e) {
Throwable cause = e.getCause();
if (cause instanceof ValidationException) {
Model validationReportModel = ((ValidationException) cause).validationReportAsModel();
WriterConfig writerConfig = new WriterConfig()
.set(BasicWriterSettings.INLINE_BLANK_NODES, true)
.set(BasicWriterSettings.XSD_STRING_TO_PLAIN_LITERAL, true)
.set(BasicWriterSettings.PRETTY_PRINT, true);
Rio.write(validationReportModel, System.out, RDFFormat.TURTLE, writerConfig);
}
else {
e.printStackTrace();
}
}
}- SHACL Features Supported by Oracle Adapter for Eclipse RDF4J
This section lists the SHACL core constraints supported by Oracle Adapter for Eclipse RDF4J. - Restrictions on the use of RDF4J SHACL Features
This section describes the restrictions on the use of RDF4J SHACL features.
Parent topic: RDF Graph Support for Eclipse RDF4J
8.9.1 SHACL Features Supported by Oracle Adapter for Eclipse RDF4J
This section lists the SHACL core constraints supported by Oracle Adapter for Eclipse RDF4J.
http://www.w3.org/ns/shacl#alternativePathhttp://www.w3.org/ns/shacl#classhttp://www.w3.org/ns/shacl#datatypehttp://www.w3.org/ns/shacl#deactivatedhttp://www.w3.org/ns/shacl#flagshttp://www.w3.org/ns/shacl#hasValuehttp://www.w3.org/ns/shacl#inhttp://www.w3.org/ns/shacl#inversePathhttp://www.w3.org/ns/shacl#languageInhttp://www.w3.org/ns/shacl#maxCounthttp://www.w3.org/ns/shacl#maxExclusivehttp://www.w3.org/ns/shacl#maxInclusivehttp://www.w3.org/ns/shacl#maxLengthhttp://www.w3.org/ns/shacl#minCounthttp://www.w3.org/ns/shacl#minExclusivehttp://www.w3.org/ns/shacl#minInclusivehttp://www.w3.org/ns/shacl#minLengthhttp://www.w3.org/ns/shacl#nodehttp://www.w3.org/ns/shacl#nodeKindhttp://www.w3.org/ns/shacl#pathhttp://www.w3.org/ns/shacl#patternhttp://www.w3.org/ns/shacl#propertyhttp://www.w3.org/ns/shacl#severityhttp://www.w3.org/ns/shacl#targethttp://www.w3.org/ns/shacl#targetClasshttp://www.w3.org/ns/shacl#targetNodehttp://www.w3.org/ns/shacl#targetObjectsOfhttp://www.w3.org/ns/shacl#targetSubjectsOfhttp://www.w3.org/ns/shacl#uniqueLang
oracle.rdf4j.adapter.restrictShaclFeatures to the value
F. These features should only be used with smaller RDF datasets because the RDF4J SHACL
engine’s implementation reads the set of target nodes into the client program’s memory
when evaluating constraints with these features.
http://www.w3.org/ns/shacl#andhttp://www.w3.org/ns/shacl#nothttp://www.w3.org/ns/shacl#orhttp://www.w3.org/ns/shacl#qualifiedMaxCounthttp://www.w3.org/ns/shacl#qualifiedMinCounthttp://www.w3.org/ns/shacl#qualifiedValueShapehttp://www.w3.org/ns/shacl#sparql
Parent topic: Validating RDF Data with SHACL Constraints
8.9.2 Restrictions on the use of RDF4J SHACL Features
This section describes the restrictions on the use of RDF4J SHACL features.
Oracle Adapter for Eclipse RDF4J only supports SHACL Shapes stored in the
reserved named graph (context)
http://rdf4j.org/schema/rdf4j#SHACLShapeGraph. In general,
ShaclSail allows you to load shapes graphs from arbitrary named
graphs identified with the setShapesGraph method. However,
setShapesGraph is not supported by Oracle Adapter for Eclipse
RDF4J.
Parent topic: Validating RDF Data with SHACL Constraints