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
public class ShaclSail extends ShaclSailBaseConfiguration
ASailimplementation 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:
- SHACL W3C Recommendation
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static classShaclSail.CleanableStateprotected static classShaclSail.RevivableExecutorServicestatic classShaclSail.TransactionSettings
-
Field Summary
Fields Modifier and Type Field Description private static intAVAILABLE_PROCESSORSprivate StampedLockManager.Cache<java.util.List<ContextWithShape>>cachedShapesprivate static ConcurrentCleanercleanerprivate ShaclSail.RevivableExecutorServiceexecutorServiceprivate java.util.concurrent.atomic.AtomicBooleaninitializedprivate static org.slf4j.Loggerlogger(package private) ReadPrefReadWriteLockManagerserializableValidationLockprivate SailRepositoryshapesRepoan initializedRepositoryfor storing/retrieving Shapes dataprivate java.util.concurrent.atomic.AtomicLongsingleConnectionCounter(package private) java.lang.ObjectsingleConnectionMonitorprivate booleansupportsSnapshotIsolation-
Fields inherited from class org.eclipse.rdf4j.sail.shacl.ShaclSailBaseConfiguration
sparqlValidation
-
-
Constructor Summary
Constructors Constructor Description ShaclSail()ShaclSail(NotifyingSail baseSail)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description (package private) voidcloseConnection()(package private) static SailExceptionconvertToSailException(java.lang.InterruptedException e)StampedLockManager.Cache.ReadableStategetCachedShapes()(package private) StampedLockManager.Cache.WritableStategetCachedShapesForWriting()NotifyingSailConnectiongetConnection()Opens a connection on the Sail which can be used to query and update data.protected ShaclSail.RevivableExecutorServicegetExecutorService()java.util.List<ContextWithShape>getShapes(IRI[] shapesGraphs, boolean onlyRdf4jShaclShapeGraph)java.util.List<ContextWithShape>getShapes(RepositoryConnection shapesRepoConnection, IRI[] shapesGraphs)java.util.List<ContextWithShape>getShapes(RepositoryConnection shapesRepoConnection, SailConnection sailConnection, IRI[] shapesGraphs)static java.util.List<IRI>getSupportedShaclPredicates()Lists the predicates that have been implemented in the ShaclSail.voidinit()Initializes the Sail.booleanisSerializableValidation()On 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(java.util.Set<IRI> shapesGraphs)voidshutDown()Shuts down the Sail, giving it the opportunity to synchronize any stale data.private booleanshutdownExecutorService(boolean forced)(package private) <T> java.util.concurrent.Future<T>submitToExecutorService(java.util.concurrent.Callable<T> runnable)(package private) booleanusesSingleConnection()-
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 Detail
-
AVAILABLE_PROCESSORS
private static final int AVAILABLE_PROCESSORS
-
logger
private static final org.slf4j.Logger logger
-
cleaner
private static final ConcurrentCleaner cleaner
-
shapesRepo
private SailRepository shapesRepo
an initializedRepositoryfor storing/retrieving Shapes data
-
serializableValidationLock
final ReadPrefReadWriteLockManager serializableValidationLock
-
cachedShapes
private StampedLockManager.Cache<java.util.List<ContextWithShape>> cachedShapes
-
supportsSnapshotIsolation
private boolean supportsSnapshotIsolation
-
singleConnectionCounter
private final transient java.util.concurrent.atomic.AtomicLong singleConnectionCounter
-
singleConnectionMonitor
final java.lang.Object singleConnectionMonitor
-
initialized
private final java.util.concurrent.atomic.AtomicBoolean initialized
-
executorService
private final ShaclSail.RevivableExecutorService executorService
-
-
Constructor Detail
-
ShaclSail
public ShaclSail(NotifyingSail baseSail)
-
ShaclSail
public ShaclSail()
-
-
Method Detail
-
getCachedShapesForWriting
@InternalUseOnly StampedLockManager.Cache.WritableState getCachedShapesForWriting() throws java.lang.InterruptedException
- Throws:
java.lang.InterruptedException
-
getCachedShapes
@InternalUseOnly public StampedLockManager.Cache.ReadableState getCachedShapes() throws java.lang.InterruptedException
- Throws:
java.lang.InterruptedException
-
setBaseSail
public void setBaseSail(Sail baseSail)
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
@Experimental protected ShaclSail.RevivableExecutorService getExecutorService()
- Returns:
-
closeConnection
void closeConnection()
-
usesSingleConnection
boolean usesSingleConnection()
-
getSupportedShaclPredicates
public static java.util.List<IRI> 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
public void init() throws SailExceptionDescription 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 java.util.List<ContextWithShape> getShapes(RepositoryConnection shapesRepoConnection, SailConnection sailConnection, IRI[] shapesGraphs) throws SailException
- Throws:
SailException
-
getShapes
@InternalUseOnly public java.util.List<ContextWithShape> getShapes(RepositoryConnection shapesRepoConnection, IRI[] shapesGraphs) throws SailException
- Throws:
SailException
-
shutDown
public void shutDown() throws SailExceptionDescription 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
<T> java.util.concurrent.Future<T> submitToExecutorService(java.util.concurrent.Callable<T> runnable)
-
getConnection
public NotifyingSailConnection getConnection() throws SailException
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 java.util.List<ContextWithShape> getShapes(IRI[] shapesGraphs, boolean onlyRdf4jShaclShapeGraph)
-
setShapesGraphs
public void setShapesGraphs(java.util.Set<IRI> shapesGraphs)
- 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
static SailException convertToSailException(java.lang.InterruptedException e)
-
-