Package org.eclipse.rdf4j.sail.shacl
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 class
protected static class
static class
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final int
private StampedLockManager.Cache
<List<ContextWithShape>> private static final ConcurrentCleaner
private final ShaclSail.RevivableExecutorService
private final AtomicBoolean
private static final org.slf4j.Logger
(package private) final ReadPrefReadWriteLockManager
private SailRepository
an initializedRepository
for storing/retrieving Shapes dataprivate final AtomicLong
(package private) final Object
private boolean
Fields inherited from class org.eclipse.rdf4j.sail.shacl.ShaclSailBaseConfiguration
sparqlValidation
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription(package private) void
(package private) static SailException
(package private) StampedLockManager.Cache<List<ContextWithShape>>.WritableState
Opens a connection on the Sail which can be used to query and update data.protected ShaclSail.RevivableExecutorService
getShapes
(RepositoryConnection shapesRepoConnection, IRI[] shapesGraphs) getShapes
(RepositoryConnection shapesRepoConnection, SailConnection sailConnection, IRI[] shapesGraphs) Lists the predicates that have been implemented in the ShaclSail.void
init()
Initializes the Sail.boolean
On transactions using SNAPSHOT isolation the ShaclSail can run the validation serializably.void
setBaseSail
(Sail baseSail) Sets the base Sail that this Sail will work on top of.void
setShapesGraphs
(Set<IRI> shapesGraphs) void
shutDown()
Shuts down the Sail, giving it the opportunity to synchronize any stale data.private boolean
shutdownExecutorService
(boolean forced) (package private) <T> Future
<T> submitToExecutorService
(Callable<T> runnable) (package private) boolean
Methods inherited from class org.eclipse.rdf4j.sail.shacl.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, setValidationResultsLimitTotal
Methods inherited from class org.eclipse.rdf4j.sail.helpers.NotifyingSailWrapper
addSailChangedListener, getBaseSail, removeSailChangedListener
Methods inherited from class org.eclipse.rdf4j.sail.helpers.SailWrapper
getCollectionFactory, getDataDir, getFederatedServiceResolver, getSupportedIsolationLevels, getValueFactory, isWritable, setDataDir, setFederatedServiceResolver, verifyBaseSailSet
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.eclipse.rdf4j.sail.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 initializedRepository
for 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:StackableSail
Sets the base Sail that this Sail will work on top of. This method will be called before the initialize() method is called.- Specified by:
setBaseSail
in interfaceStackableSail
- Overrides:
setBaseSail
in 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:Sail
Initializes 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:
init
in interfaceSail
- Overrides:
init
in 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:Sail
Shuts 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:
shutDown
in interfaceSail
- Overrides:
shutDown
in 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:Sail
Opens 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:
getConnection
in interfaceNotifyingSail
- Specified by:
getConnection
in interfaceSail
- Overrides:
getConnection
in 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:
setShapesGraphs
in classShaclSailBaseConfiguration
-
isSerializableValidation
public boolean isSerializableValidation()Description copied from class:ShaclSailBaseConfiguration
On 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:
isSerializableValidation
in classShaclSailBaseConfiguration
- Returns:
true
if serializable validation is enabled,false
otherwise.
-
convertToSailException
-