Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ShaclSailValidationException in connection.commit() when adding SHACL rules with sh:targetNode #5138

Closed
danaivach opened this issue Sep 29, 2024 · 3 comments
Labels
specification issues related to compliance to standards and external specs wontfix issue won't be fixed (close reason)

Comments

@danaivach
Copy link

Current Behavior

RDF4J SailRepositoryConnection throws a ShaclSailValidationException in connection.commit() when adding SHACL rules with sh:targetNode combined with sh:minCount>0 or with sh:hasValue.

Expected Behavior

I'm flagging it as a bug because I would expect that sh:targetNode is supported based on #1362 and #1574.

Steps To Reproduce

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;\n" +
            "                       sh:hasValue \"ready\".";

    ShaclSail shaclSail = new ShaclSail(new MemoryStore());
    Repository repo = new SailRepository(shaclSail);

    try (RepositoryConnection connection = repo.getConnection()) {

      // add shapes
      connection.begin();
      connection.clear(RDF4J.SHACL_SHAPE_GRAPH);
      connection.add(new StringReader(shaclRules), "", RDFFormat.TURTLE, RDF4J.SHACL_SHAPE_GRAPH);
      connection.commit();

    } catch (IOException e) {
      throw new RuntimeException(e);
    }
 

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)

Version

5.0.2

Are you interested in contributing a solution yourself?

None

Anything else?

No response

@danaivach danaivach added the 🐞 bug issue is a bug label Sep 29, 2024
@hmottestad hmottestad added the specification issues related to compliance to standards and external specs label Sep 30, 2024
@hmottestad
Copy link
Contributor

Our implementation is based on the premise that a target node doesn't actually have to exist for it to violate a shape. In your case you are saying that <http://example.org/component> has to have at least one <https://saref.etsi.org/core/hasState> path when using sh:minCount 1.

I'll check the specification and test cases to see if this is the correct interpretation or not.

@hmottestad
Copy link
Contributor

The spec isn't exactly very clear: https://www.w3.org/TR/shacl/#targetNode

But there is a test case that is validated against the reference SHACL implementation:

There is also a test from the W3C SHACL test suite that tests for something similar: https://github.com/eclipse-rdf4j/rdf4j/blob/main/core/sail/shacl/src/test/resources/w3c/core/node/datatype-002.ttl

An issue with that test though is that it doesn't seem to differentiate between what is data and what are shapes, so the nodes are definitely present in the data graph.

@danaivach
Copy link
Author

Thank you. I had misinterpreted the role of the SHACL rules upon committing them, assuming that the rules will not be violated if the target node does not exist.

The ShaclSailValidationException is indeed not raised if the conforming node is added in the graph prior to the addition of the SHACL rules.

@hmottestad hmottestad reopened this Sep 30, 2024
@hmottestad hmottestad added wontfix issue won't be fixed (close reason) and removed 🐞 bug issue is a bug labels Sep 30, 2024
@eclipse-rdf4j eclipse-rdf4j locked and limited conversation to collaborators Sep 30, 2024
@hmottestad hmottestad converted this issue into discussion #5140 Sep 30, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
specification issues related to compliance to standards and external specs wontfix issue won't be fixed (close reason)
Projects
None yet
Development

No branches or pull requests

2 participants