stln
stln

Reputation: 33

RDF4j ShaclSailValidationException when using sh:targetNode with sh:minCount

RDF4J SailRepositoryConnection throws a ShaclSailValidationException in connection.commit() when adding SHACL rules with sh:targetNode and an sh:minCount>0. The issue disappears when sh:minCount is not used, or when sh:minCount 0. Does anyone know what could be the issue?

Exception:

org.eclipse.rdf4j.sail.shacl.ShaclSailValidationException: Failed SHACL validation
org.eclipse.rdf4j.repository.RepositoryException: org.eclipse.rdf4j.sail.shacl.ShaclSailValidationException: Failed SHACL validation
    at org.eclipse.rdf4j.repository.sail.SailRepositoryConnection.commit(SailRepositoryConnection.java:228)
    at ch.unisg.ics.interactions.hmas.interaction.validation.ShaclValidationTest.testContextValidationRDF4j(ShaclValidationTest.java:178)
    at java.base/java.lang.reflect.Method.invoke(Method.java:580)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
Caused by: org.eclipse.rdf4j.sail.shacl.ShaclSailValidationException: Failed SHACL validation
    at org.eclipse.rdf4j.sail.shacl.ShaclSailConnection.prepare(ShaclSailConnection.java:892)
    at org.eclipse.rdf4j.repository.sail.SailRepositoryConnection.commit(SailRepositoryConnection.java:225)

Code sample:

String shaclRules = "@prefix sh: <http://www.w3.org/ns/shacl#> .\n" +
        "@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .\n\n" +
        "<http://example.org/component/component-state-context>\n" +
        "                       a sh:PropertyShape;\n" +
        "                       sh:targetNode <http://example.org/component>;\n" +
        "                       sh:path <https://saref.etsi.org/core/hasState>;\n" +
        "                       sh:maxCount 1;\n" +
        "                       sh:minCount 1." ;

ShaclSail shaclSail = new ShaclSail(new MemoryStore());

SailRepository sailRepository = new SailRepository(shaclSail);
sailRepository.init();

try (SailRepositoryConnection connection = sailRepository.getConnection()) {
  connection.begin();
  connection.add(new StringReader(shaclRules), "", RDFFormat.TURTLE, RDF4J.SHACL_SHAPE_GRAPH);
  connection.commit();
}

Upvotes: 1

Views: 34

Answers (1)

stln
stln

Reputation: 33

The exception is raised because the RDF4J implementation is based on the premise that a target node doesn't actually have to exist in the graph for it to violate a shape. As a result, the absence of the node in the graph leads to violation.

Adding a conforming node to the graph, prior to the commitment of the SHACL rules does not raise an exception.

See more: https://github.com/eclipse-rdf4j/rdf4j/issues/5138

Upvotes: 0

Related Questions