Class ShaclSail
java.lang.Object
org.eclipse.rdf4j.sail.helpers.SailWrapper
org.eclipse.rdf4j.sail.helpers.NotifyingSailWrapper
org.eclipse.rdf4j.sail.shacl.ShaclSailBaseConfiguration
org.eclipse.rdf4j.sail.shacl.ShaclSail
- All Implemented Interfaces:
FederatedServiceResolverClient, NotifyingSail, Sail, StackableSail
A
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.
Working example
import java.io.IOException;
import java.io.StringReader;
import org.eclipse.rdf4j.common.exception.ValidationException;
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.rio.WriterConfig;
import org.eclipse.rdf4j.rio.helpers.BasicWriterSettings;
import org.eclipse.rdf4j.sail.memory.MemoryStore;
import org.eclipse.rdf4j.sail.shacl.ShaclSail;
public class ShaclSampleCode {
public static void main(String[] args) throws IOException {
ShaclSail shaclSail = new ShaclSail(new MemoryStore());
SailRepository sailRepository = new SailRepository(shaclSail);
sailRepository.init();
try (SailRepositoryConnection connection = sailRepository.getConnection()) {
connection.begin();
StringReader shaclRules = new StringReader(String.join("\n", "",
"@prefix ex: <http://example.com/ns#> .",
"@prefix sh: <http://www.w3.org/ns/shacl#> .",
"@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .",
"@prefix foaf: <http://xmlns.com/foaf/0.1/>.",
"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: <http://example.com/ns#> .",
"@prefix foaf: <http://xmlns.com/foaf/0.1/>.",
"@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .",
"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 ValidationException) {
// use the validationReportModel to understand validation violations
Model validationReportModel = ((ValidationException) cause).validationReportAsModel();
// Pretty print the validation report
WriterConfig writerConfig = new WriterConfig()
.set(BasicWriterSettings.PRETTY_PRINT, true)
.set(BasicWriterSettings.INLINE_BLANK_NODES, true);
Rio.write(validationReportModel, System.out, RDFFormat.TURTLE, writerConfig);
System.out.println();
}
throw exception;
}
}
}
}
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) static classprotected static classstatic class -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final intprivate StampedLockManager.Cache<List<ContextWithShape>> private static final ConcurrentCleanerprivate final ShaclSail.RevivableExecutorServiceprivate final AtomicBooleanprivate static final org.slf4j.Logger(package private) final ReadPrefReadWriteLockManagerprivate SailRepositoryan initializedRepositoryfor storing/retrieving Shapes dataprivate final AtomicLong(package private) final Objectprivate booleanFields inherited from class ShaclSailBaseConfiguration
sparqlValidation -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription(package private) void(package private) static SailException(package private) StampedLockManager.Cache<List<ContextWithShape>>.WritableStateOpens a connection on the Sail which can be used to query and update data.protected ShaclSail.RevivableExecutorServicegetShapes(RepositoryConnection shapesRepoConnection, IRI[] shapesGraphs) getShapes(RepositoryConnection shapesRepoConnection, SailConnection sailConnection, IRI[] shapesGraphs) Lists the predicates that have been implemented in the ShaclSail.voidinit()Initializes the Sail.booleanOn transactions using SNAPSHOT isolation the ShaclSail can run the validation serializably.voidsetBaseSail(Sail baseSail) Sets the base Sail that this Sail will work on top of.voidsetShapesGraphs(Set<IRI> shapesGraphs) voidshutDown()Shuts down the Sail, giving it the opportunity to synchronize any stale data.private booleanshutdownExecutorService(boolean forced) (package private) <T> Future<T> submitToExecutorService(Callable<T> runnable) (package private) booleanMethods inherited from class ShaclSailBaseConfiguration
disableValidation, enableValidation, getDefaultIsolationLevel, getEffectiveValidationResultsLimitPerConstraint, getShapesGraphs, getTransactionalValidationLimit, getValidationResultsLimitPerConstraint, getValidationResultsLimitTotal, isCacheSelectNodes, isDashDataShapes, isEclipseRdf4jShaclExtensions, isGlobalLogValidationExecution, isLogValidationPlans, isLogValidationViolations, isParallelValidation, isPerformanceLogging, isRdfsSubClassReasoning, isValidationEnabled, setCacheSelectNodes, setDashDataShapes, setEclipseRdf4jShaclExtensions, setGlobalLogValidationExecution, setLogValidationPlans, setLogValidationViolations, setParallelValidation, setPerformanceLogging, setRdfsSubClassReasoning, setSerializableValidation, setTransactionalValidationLimit, setValidationResultsLimitPerConstraint, setValidationResultsLimitTotalMethods inherited from class NotifyingSailWrapper
addSailChangedListener, getBaseSail, removeSailChangedListenerMethods inherited from class SailWrapper
getCollectionFactory, getDataDir, getFederatedServiceResolver, getSupportedIsolationLevels, getValueFactory, isWritable, setDataDir, setFederatedServiceResolver, verifyBaseSailSetMethods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface Sail
getCollectionFactory, getDataDir, getSupportedIsolationLevels, getValueFactory, isWritable, setDataDir
-
Field Details
-
AVAILABLE_PROCESSORS
private static final int AVAILABLE_PROCESSORS -
logger
private static final org.slf4j.Logger logger -
cleaner
-
shapesRepo
an initializedRepositoryfor storing/retrieving Shapes data -
serializableValidationLock
-
cachedShapes
-
supportsSnapshotIsolation
private boolean supportsSnapshotIsolation -
singleConnectionCounter
-
singleConnectionMonitor
-
initialized
-
executorService
-
-
Constructor Details
-
ShaclSail
-
ShaclSail
public ShaclSail()
-
-
Method Details
-
getCachedShapesForWriting
@InternalUseOnly StampedLockManager.Cache<List<ContextWithShape>>.WritableState getCachedShapesForWriting() throws InterruptedException- Throws:
InterruptedException
-
getCachedShapes
@InternalUseOnly public StampedLockManager.Cache<List<ContextWithShape>>.ReadableState getCachedShapes() throws InterruptedException- Throws:
InterruptedException
-
setBaseSail
Description copied from interface:StackableSailSets the base Sail that this Sail will work on top of. This method will be called before the initialize() method is called.- Specified by:
setBaseSailin interfaceStackableSail- Overrides:
setBaseSailin classNotifyingSailWrapper
-
getExecutorService
- Returns:
-
closeConnection
void closeConnection() -
usesSingleConnection
boolean usesSingleConnection() -
getSupportedShaclPredicates
Lists the predicates that have been implemented in the ShaclSail. All of these, and all combinations, should work, please report any bugs. For sh:path, only single predicate paths, or single predicate inverse paths are supported. DASH and RSX features may need to be enabled.- Returns:
- List of IRIs (SHACL predicates)
-
init
Description copied from interface:SailInitializes the Sail. Care should be taken that required initialization parameters have been set before this method is called. Please consult the specific Sail implementation for information about the relevant parameters.- Specified by:
initin interfaceSail- Overrides:
initin classSailWrapper- Throws:
SailException- If the Sail could not be initialized.
-
getShapes
@InternalUseOnly public List<ContextWithShape> getShapes(RepositoryConnection shapesRepoConnection, SailConnection sailConnection, IRI[] shapesGraphs) throws SailException - Throws:
SailException
-
getShapes
@InternalUseOnly public List<ContextWithShape> getShapes(RepositoryConnection shapesRepoConnection, IRI[] shapesGraphs) throws SailException - Throws:
SailException
-
shutDown
Description copied from interface:SailShuts down the Sail, giving it the opportunity to synchronize any stale data. Care should be taken that all initialized Sails are being shut down before an application exits to avoid potential loss of data. Once shut down, a Sail can no longer be used until it is re-initialized.- Specified by:
shutDownin interfaceSail- Overrides:
shutDownin classSailWrapper- Throws:
SailException- If the Sail object encountered an error or unexpected situation internally.
-
shutdownExecutorService
private boolean shutdownExecutorService(boolean forced) -
submitToExecutorService
-
getConnection
Description copied from interface:SailOpens a connection on the Sail which can be used to query and update data. Depending on how the implementation handles concurrent access, a call to this method might block when there is another open connection on this Sail.- Specified by:
getConnectionin interfaceNotifyingSail- Specified by:
getConnectionin interfaceSail- Overrides:
getConnectionin classNotifyingSailWrapper- Throws:
SailException- If no transaction could be started, for example because the Sail is not writable.
-
getShapes
@InternalUseOnly public List<ContextWithShape> getShapes(IRI[] shapesGraphs, boolean onlyRdf4jShaclShapeGraph) -
setShapesGraphs
- Overrides:
setShapesGraphsin classShaclSailBaseConfiguration
-
isSerializableValidation
public boolean isSerializableValidation()Description copied from class:ShaclSailBaseConfigurationOn transactions using SNAPSHOT isolation the ShaclSail can run the validation serializably. This stops the sail from becoming inconsistent due to race conditions between two transactions. Serializable validation limits TPS (transactions per second), it is however considerably faster than actually using SERIALIZABLE isolation.- Overrides:
isSerializableValidationin classShaclSailBaseConfiguration- Returns:
trueif serializable validation is enabled,falseotherwise.
-
convertToSailException
-