Importing SHACL for graph validation (namespaces..?)

Hello,

I want to use neosemantics to validate a graph in neo4j, but I'm having trouble to get the model constraints loaded on a minimal example:

Suppose I have the following graph:

CREATE (n:Person {name: 'Andy'})

Then, I initialize neosemantics as follows:

CALL n10s.graphconfig.init();

Now I should be good to go and validate the graph. Based on what I found here (Validating Neo4j graphs against SHACL - Neosemantics), I thought that the following schema should work as a first approach:

call n10s.validation.shacl.import.inline('

@prefix neo4j: <http://neo4j.com/myvoc#> .
@prefix sh: <http://www.w3.org/ns/shacl#> .

neo4j:PersonShape a sh:NodeShape ;
  sh:targetClass neo4j:Person ;
  sh:property [
    sh:path neo4j:name ;
    sh:datatype xsd:string ;
  ];
.','Turtle')

However, that gives me:

Failed to invoke procedure `n10s.validation.shacl.import.inline`: Caused by: n10s.utils.UriUtils$UriNamespaceHasNoAssociatedPrefix: Prefix Undefined: No prefix defined for namespace <http://neo4j.com/myvoc#Person>. Use n10s.nsprefixes.add(...) procedure.

It felt weird, but to match that error message, I tried adding the line

@prefix neo4jPerson: <http://neo4j.com/myvoc#Person> .

but that didn't help.

I then tried the following:

CALL n10s.graphconfig.init({handleVocabUris:"IGNORE"});

Then, the validation rule is accepted, but it doesn't really look connected to anything:

target  propertyOrRelationshipPath  param   value
"__NONE__"  "__NONE__"  "datatype"  "string"

I thought that it would be helpful to remove that namespace stuff all along. So I also tried

call n10s.validation.shacl.import.inline('

@prefix neo4j: <http://neo4j.com/myvoc#> .
@prefix sh: <http://www.w3.org/ns/shacl#> .

PersonShape a sh:NodeShape ;
  sh:targetClass Person ;
  sh:property [
    sh:path name ;
    sh:datatype xsd:string ;
  ];
.','Turtle')

But that didn't work either as the parser insists on getting that namespace stuff to work:

Failed to invoke procedure `n10s.validation.shacl.import.inline`: Caused by: org.eclipse.rdf4j.rio.RDFParseException: Expected ':', found ' ' [line 6]

I'm a bit at a loss here. The code I've given is more or less taken from the neosemantics user guide (Neosemantics(n10s) User Guide - Neosemantics) and only adapted slightly.

I should add that I can import the original schema just fine with handleVocabUris: KEEP.

But still something isn't linked correctly. I added a shape that definitely violates the specification (name is missing):

CREATE (n:Person {violation: 5})

Then I ran the validation with

call n10s.validation.shacl.validate() yield focusNode, nodeType,propertyShape,offendingValue,resultPath,severity

but the result remained empty.
What am I missing?

I solved it: cypher - neo4j neosemantics: loading model constraints - Stack Overflow

I had a similar problem. Does this seems familiar to you?
https://community.neo4j.com/t/failed-to-import-shacl-with-neosemantics/64181

Troubleshooting neosemantics in Neo4j, ensure the namespace prefix is defined using n10s.nsprefixes.add(...). Add this line before your schema definition to resolve the "Prefix Undefined" error. Adjust your schema definition to use the correct prefix, and initialize neosemantics with handleVocabUris: "IGNORE". Confirm that the prefix is consistent throughout. Then, import and validate the SHACL schema. If issues persist, double-check the prefix and rerun the validation. This step-by-step approach ensures proper namespace handling and enhances schema validation in Neo4j using neosemantics.
https://stackoverflow.com/questions/69689710/neo4j-neosemantics-loading-model-constraints/ccsp training

1 Like