public class ShaclSail extends NotifyingSailWrapper
Sail implementation that adds support for the Shapes Constraint Language (SHACL).
The ShaclSail looks for SHACL shape data in a special named graph RDF4J.SHACL_SHAPE_GRAPH.
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import org.eclipse.rdf4j.model.Model;
import org.eclipse.rdf4j.model.vocabulary.RDF4J;
import org.eclipse.rdf4j.repository.RepositoryException;
import org.eclipse.rdf4j.repository.sail.SailRepository;
import org.eclipse.rdf4j.repository.sail.SailRepositoryConnection;
import org.eclipse.rdf4j.rio.RDFFormat;
import org.eclipse.rdf4j.rio.Rio;
import org.eclipse.rdf4j.sail.memory.MemoryStore;
import org.eclipse.rdf4j.sail.shacl.ShaclSail;
import org.eclipse.rdf4j.sail.shacl.ShaclSailValidationException;
import org.eclipse.rdf4j.sail.shacl.results.ValidationReport;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.StringReader;
public class ShaclSampleCode {
public static void main(String[] args) throws IOException {
ShaclSail shaclSail = new ShaclSail(new MemoryStore());
// Logger root = (Logger) LoggerFactory.getLogger(ShaclSail.class.getName());
// root.setLevel(Level.INFO);
// shaclSail.setLogValidationPlans(true);
// shaclSail.setGlobalLogValidationExecution(true);
// shaclSail.setLogValidationViolations(true);
SailRepository sailRepository = new SailRepository(shaclSail);
sailRepository.init();
try (SailRepositoryConnection connection = sailRepository.getConnection()) {
connection.begin();
StringReader shaclRules = new StringReader(String.join("\n", "",
"@prefix ex: .",
"@prefix sh: .",
"@prefix xsd: .",
"@prefix foaf: .",
"ex:PersonShape",
" a sh:NodeShape ;",
" sh:targetClass foaf:Person ;",
" sh:property ex:PersonShapeProperty .",
"ex:PersonShapeProperty ",
" sh:path foaf:age ;",
" sh:datatype xsd:int ;",
" sh:maxCount 1 ;",
" sh:minCount 1 ."));
connection.add(shaclRules, "", RDFFormat.TURTLE, RDF4J.SHACL_SHAPE_GRAPH);
connection.commit();
connection.begin();
StringReader invalidSampleData = new StringReader(String.join("\n", "",
"@prefix ex: .",
"@prefix foaf: .",
"@prefix xsd: .",
"ex:peter a foaf:Person ;",
" foaf:age 20, \"30\"^^xsd:int ."
));
connection.add(invalidSampleData, "", RDFFormat.TURTLE);
try {
connection.commit();
} catch (RepositoryException exception) {
Throwable cause = exception.getCause();
if (cause instanceof ShaclSailValidationException) {
ValidationReport validationReport = ((ShaclSailValidationException) cause).getValidationReport();
Model validationReportModel = ((ShaclSailValidationException) cause).validationReportAsModel();
// use validationReport or validationReportModel to understand validation violations
Rio.write(validationReportModel, System.out, RDFFormat.TURTLE);
}
throw exception;
}
}
}
}
| Modifier and Type | Class and Description |
|---|---|
static class |
ShaclSail.TransactionSettings |
| Constructor and Description |
|---|
ShaclSail() |
ShaclSail(NotifyingSail baseSail) |
| Modifier and Type | Method and Description |
|---|---|
void |
disableValidation()
Disable the SHACL validation on commit()
|
void |
enableValidation()
Enabled the SHACL validation on commit()
|
NotifyingSailConnection |
getConnection()
Opens a connection on the Sail which can be used to query and update data.
|
List<org.eclipse.rdf4j.sail.shacl.ast.Shape> |
getCurrentShapes() |
IsolationLevel |
getDefaultIsolationLevel()
Retrieves the default
IsolationLevel level on which transactions in this Sail operate. |
long |
getEffectiveValidationResultsLimitPerConstraint() |
List<org.eclipse.rdf4j.sail.shacl.ast.Shape> |
getShapes(RepositoryConnection shapesRepoConnection) |
static List<IRI> |
getSupportedShaclPredicates()
Lists the predicates that have been implemented in the ShaclSail.
|
long |
getValidationResultsLimitPerConstraint()
ValidationReports contain validation results.
|
long |
getValidationResultsLimitTotal()
ValidationReports contain validation results.
|
void |
initialize()
Initializes the Sail.
|
boolean |
isCacheSelectNodes()
Check if selected nodes caches is enabled.
|
boolean |
isDashDataShapes()
Support for DASH Data Shapes Vocabulary Unofficial Draft (http://datashapes.org/dash).
|
boolean |
isEclipseRdf4jShaclExtensions()
Support for Eclipse RDF4J SHACL Extensions (http://rdf4j.org/shacl-extensions#).
|
boolean |
isGlobalLogValidationExecution()
Check if logging of every execution steps is enabled.
|
boolean |
isIgnoreNoShapesLoadedException()
Deprecated.
|
boolean |
isLogValidationPlans()
Check if logging of validation plans is enabled.
|
boolean |
isLogValidationViolations()
Check if logging a list of violations and the triples that caused the violations is enabled.
|
boolean |
isParallelValidation()
Check if SHACL validation is run in parellel.
|
boolean |
isPerformanceLogging() |
boolean |
isRdfsSubClassReasoning() |
boolean |
isSerializableValidation()
On transactions using SNAPSHOT isolation the ShaclSail can run the validation serializably.
|
boolean |
isUndefinedTargetValidatesAllSubjects()
Deprecated.
|
boolean |
isValidationEnabled()
Check if SHACL validation on commit() is enabled.
|
void |
setCacheSelectNodes(boolean cacheSelectNodes)
The ShaclSail retries a lot of its relevant data through running SPARQL Select queries against the underlying
sail and against the changes in the transaction.
|
void |
setDashDataShapes(boolean dashDataShapes)
Support for DASH Data Shapes Vocabulary Unofficial Draft (http://datashapes.org/dash).
|
void |
setEclipseRdf4jShaclExtensions(boolean eclipseRdf4jShaclExtensions)
Support for Eclipse RDF4J SHACL Extensions (http://rdf4j.org/shacl-extensions#).
|
void |
setGlobalLogValidationExecution(boolean loggingEnabled)
Log (INFO) every execution step of the SHACL validation.
|
void |
setIgnoreNoShapesLoadedException(boolean ignoreNoShapesLoadedException)
Deprecated.
|
void |
setLogValidationPlans(boolean logValidationPlans)
Log (INFO) the executed validation plans as GraphViz DOT Recommended to disable parallel validation with
setParallelValidation(false)
|
void |
setLogValidationViolations(boolean logValidationViolations)
Log (INFO) a list of violations and the triples that caused the violations (BETA).
|
void |
setParallelValidation(boolean parallelValidation)
EXPERIMENTAL! Run SHACL validation in parallel.
|
void |
setPerformanceLogging(boolean performanceLogging)
Log (INFO) the execution time per shape.
|
void |
setRdfsSubClassReasoning(boolean rdfsSubClassReasoning) |
void |
setSerializableValidation(boolean serializableValidation)
Enable or disable serializable validation.On transactions using SNAPSHOT isolation the ShaclSail can run the
validation serializably.
|
void |
setUndefinedTargetValidatesAllSubjects(boolean undefinedTargetValidatesAllSubjects)
Deprecated.
|
void |
setValidationResultsLimitPerConstraint(long validationResultsLimitPerConstraint)
ValidationReports contain validation results.
|
void |
setValidationResultsLimitTotal(long validationResultsLimitTotal)
ValidationReports contain validation results.
|
void |
shutDown()
Shuts down the Sail, giving it the opportunity to synchronize any stale data.
|
addSailChangedListener, getBaseSail, removeSailChangedListener, setBaseSailgetDataDir, getSupportedIsolationLevels, getValueFactory, isWritable, setDataDir, setFederatedServiceResolver, verifyBaseSailSetclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitgetDataDir, getSupportedIsolationLevels, getValueFactory, init, isWritable, setDataDirpublic ShaclSail(NotifyingSail baseSail)
public ShaclSail()
public static List<IRI> getSupportedShaclPredicates()
public void initialize()
throws SailException
Sailinitialize in interface Sailinitialize in class SailWrapperSailException - If the Sail could not be initialized.@InternalUseOnly public List<org.eclipse.rdf4j.sail.shacl.ast.Shape> getShapes(RepositoryConnection shapesRepoConnection) throws SailException
SailExceptionpublic void shutDown()
throws SailException
SailshutDown in interface SailshutDown in class SailWrapperSailException - If the Sail object encountered an error or unexpected situation internally.public NotifyingSailConnection getConnection() throws SailException
SailgetConnection in interface NotifyingSailgetConnection in interface SailgetConnection in class NotifyingSailWrapperSailException - If no transaction could be started, for example because the Sail is not writable.public void setGlobalLogValidationExecution(boolean loggingEnabled)
loggingEnabled - public boolean isGlobalLogValidationExecution()
true if enabled, false otherwise.setGlobalLogValidationExecution(boolean)public boolean isLogValidationViolations()
setParallelValidation(boolean)setLogValidationViolations(boolean)public void setLogValidationViolations(boolean logValidationViolations)
logValidationViolations - @Deprecated public void setUndefinedTargetValidatesAllSubjects(boolean undefinedTargetValidatesAllSubjects)
undefinedTargetValidatesAllSubjects - default false@Deprecated public boolean isUndefinedTargetValidatesAllSubjects()
true if enabled, false otherwisesetUndefinedTargetValidatesAllSubjects(boolean)public boolean isParallelValidation()
true if enabled, false otherwise.public void setParallelValidation(boolean parallelValidation)
May cause deadlock, especially when using NativeStore.
parallelValidation - default truepublic boolean isCacheSelectNodes()
true if enabled, false otherwise.setCacheSelectNodes(boolean)public void setCacheSelectNodes(boolean cacheSelectNodes)
cacheSelectNodes - default truepublic boolean isRdfsSubClassReasoning()
public void setRdfsSubClassReasoning(boolean rdfsSubClassReasoning)
public void disableValidation()
public void enableValidation()
public boolean isValidationEnabled()
true if validation is enabled, false otherwise.public boolean isLogValidationPlans()
true if validation plan logging is enabled, false otherwise.@Deprecated public boolean isIgnoreNoShapesLoadedException()
@Deprecated public void setIgnoreNoShapesLoadedException(boolean ignoreNoShapesLoadedException)
ignoreNoShapesLoadedException - public void setLogValidationPlans(boolean logValidationPlans)
logValidationPlans - public boolean isPerformanceLogging()
public void setPerformanceLogging(boolean performanceLogging)
performanceLogging - default falsepublic boolean isSerializableValidation()
true if serializable validation is enabled, false otherwise.public void setSerializableValidation(boolean serializableValidation)
To increase TPS, serializable validation can be disabled. Validation will then be limited to the semantics of the SNAPSHOT isolation level (or whichever is specified). If you use any other isolation level than SNAPSHOT, disabling serializable validation will make no difference on performance.
serializableValidation - default true@InternalUseOnly public List<org.eclipse.rdf4j.sail.shacl.ast.Shape> getCurrentShapes()
@Experimental public void setEclipseRdf4jShaclExtensions(boolean eclipseRdf4jShaclExtensions)
eclipseRdf4jShaclExtensions - true to enable (default: false)@Experimental public boolean isEclipseRdf4jShaclExtensions()
@Experimental public void setDashDataShapes(boolean dashDataShapes)
dashDataShapes - true to enable (default: false)@Experimental public boolean isDashDataShapes()
public long getValidationResultsLimitPerConstraint()
public long getEffectiveValidationResultsLimitPerConstraint()
public void setValidationResultsLimitPerConstraint(long validationResultsLimitPerConstraint)
validationResultsLimitPerConstraint - the limit for the number of validation results per report per
constraint, -1 for no limitpublic long getValidationResultsLimitTotal()
public void setValidationResultsLimitTotal(long validationResultsLimitTotal)
validationResultsLimitTotal - the limit for the number of validation results per report in total, -1 for no
limitpublic IsolationLevel getDefaultIsolationLevel()
SailIsolationLevel level on which transactions in this Sail operate.getDefaultIsolationLevel in interface SailgetDefaultIsolationLevel in class SailWrapperIsolationLevel that will be used with SailConnection.begin(), for SAIL connections
returned by Sail.getConnection().Copyright © 2015-2021 Eclipse Foundation. All Rights Reserved.