Getting violations while validating a simple shape with shacl Java library

82 views
Skip to first unread message

Roger Sacilotto

unread,
Sep 28, 2015, 11:57:06 AM9/28/15
to TopBraid Suite Users
Hello all,

I downloaded the shacl Java library from GitHub, but I'm having a problem with what should be a simple example.

I copied the SquareExample test, and tried run the validator with a very simple model.  After playing with it for some time, it seems that the validator fails if I'm defining a sh:Shape resource that has a sh:scopeClass property, but works with a sh:ShapeClass resource.

Here is the input:

@prefix avidtx: <http://meta.avid.com/class#> .
@prefix avids: <http://meta.avid.com/shapes#> .

# The sh:Graph ----------------------------------------------------------------

a sh:Graph ;
sh:shapesGraph <http://www.w3.org/ns/shacl> ;
rdfs:label "SHACL Test" ;
rdfs:comment "A simple example model." ;
.

# TestShape example does not validate

avids:TestShape
a sh:Shape ;
sh:scopeClass avidtx:Test ;
sh:property [
sh:predicate dc:title ;
sh:datatype sh:text ;
sh:minCount 1 ;
sh:maxCount 1 ;
]  .

<urn:avid:object:17324310-1787-4a7d-BB62-4179CABF0963> a avidtx:Test ;
        dc:title "Test Title 1" .

avidtx:Test a rdfs:Class .

# Test2 example validates (changed Shape to ShapeClass)

avids:Test2
a sh:ShapeClass ;
sh:property [
sh:predicate dc:title ;
sh:datatype sh:text ;
sh:minCount 1 ;
sh:maxCount 1 ;
]  .

<urn:avid:object:17324310-1787-4a7d-BB62-4179CABF0964> a avidtx:Test2 ;
        dc:title "Test Title 2" .

The output from the validator is as follows:

          "Values must be instances of sh:PropertyConstraint"^^<http://www.w3.org/2001/XMLSchema#string> ;
          []  ;
          []  ;
] .

I'm hoping that I have made a simple mistake, is there anything additional I need to declare to fix the violation?

Thanks,

Roger

Roger Sacilotto

unread,
Sep 28, 2015, 1:34:48 PM9/28/15
to TopBraid Suite Users


One small correction: there is a typo in the last resource, should read "a avids:Test2" instead of "a avidtx:Test2", doesn't affect results... 

Holger Knublauch

unread,
Sep 28, 2015, 7:20:32 PM9/28/15
to topbrai...@googlegroups.com
Hi Roger,

what you are experiencing is a side effect of which triples you are giving as input to the validation. The valueClass of sh:property is sh:PropertyConstraint, yet the file only has an untyped blank node. This is perfectly valid syntactic sugar in SHACL, yet it means that for validation purposes the additional triple(s) should be made visible. You can uncomment the following line and it will work:

        dataModel = SHACLUtil.withDefaultValueTypeInferences(shapesModel);

This is (temporarily) adding the rdf:type triple for the sh:PropertyConstraint.

The situation is different with sh:ShapeClass, which is declared to be a subclass of sh:Shape *in the SHACL metamodel graph*. However, the constraint on sh:property is defined for sh:Shape only, and in order to apply it to the data graph too, you need to make sure that the data graph has the SHACL metamodel (and its subclass of triple) as a sub-graph. You'd need to create a MultiUnion graph consisting of your own data plus the SHACL metamodel and pass this into the validation engine. Without that triple, the system will not check sh:ShapeClass at all, because it doesn't know that it's a subclass of sh:Shape in the data graph.

HTH
Holger
--
You received this message because you are subscribed to the Google Group "TopBraid Suite Users", the topics of which include Enterprise Vocabulary Network (EVN), Reference Data Manager (RDM), TopBraid Composer, TopBraid Live, TopBraid Insight, SPARQLMotion, SPARQL Web Pages and SPIN.
To post to this group, send email to topbrai...@googlegroups.com
---
You received this message because you are subscribed to the Google Groups "TopBraid Suite Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to topbraid-user...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Roger Sacilotto

unread,
Sep 29, 2015, 9:11:59 AM9/29/15
to TopBraid Suite Users
Hi Holger,

Thanks, your response did help.  I'm still a little confused on the validation of the ShapeClass - I already did as you suggested by merging the datamodel with the SHACL metamodel, the Java class is a copy of the ValidateSquareExampleTest class with a different input file name, so ShapeClass should have been processed as a subclass of Shape.  But - this may be a good time to ask a related question - what is the right method for processing shape triples vs. data triples when they are in different files?  I tried the following:


        // Load the main data model
        Model dataModel = JenaUtil.createMemoryModel();
        dataModel.read(getClass().getResourceAsStream("/one-spike-clip-1.ttl"), "urn:dummy", FileUtils.langTurtle);

        // Load the shapes for the data
        Model schemaModel = JenaUtil.createMemoryModel();
        schemaModel.read(getClass().getResourceAsStream("/one-spike-shacl.ttl"), "urn:dummy", FileUtils.langTurtle);

        // Load the shapes Model (here, includes the dataModel because that has templates in it)
        Model shaclModel = JenaUtil.createDefaultModel();
        InputStream is = getClass().getResourceAsStream("/etc/shacl.shacl.ttl");
        shaclModel.read(is, SH.BASE_URI, FileUtils.langTurtle);
        MultiUnion unionGraph = new MultiUnion(new Graph[] {
                shaclModel.getGraph(),
                schemaModel.getGraph()
        });
        Model shapesModel = ModelFactory.createModelForGraph(unionGraph);

        // Note that we don't perform validation of the shape definitions themselves.
        // To do that, activate the following line to make sure that all required triples are present:
        // dataModel = SHACLUtil.withDefaultValueTypeInferences(shapesModel);

        // Make sure all sh:Functions are registered
        SHACLFunctions.registerFunctions(shapesModel);

        // Create Dataset that contains both the main query model and the shapes model
        // (here, using a temporary URI for the shapes graph)
        URI shapesGraphURI = URI.create("urn:x-shacl-shapes-graph:" + UUID.randomUUID().toString());
        Dataset dataset = ARQFactory.get().getDataset(dataModel);
        dataset.addNamedModel(shapesGraphURI.toString(), shapesModel);

        // Run the validator and print results
        Model results = ModelConstraintValidator.get().validateModel(dataset, shapesGraphURI, null, false, null);

I thought that merging my SHACL triples with the metamodel would be sufficient, but the validator failed to detect a violation that I put into the data file. I did get the validator to detect the violation if I combined the two files into the dataModel and then follow the previous code. What is the recommended method for this pattern?

Thanks,

Roger 

Holger Knublauch

unread,
Sep 29, 2015, 11:16:52 PM9/29/15
to topbrai...@googlegroups.com
On 9/29/2015 23:11, Roger Sacilotto wrote:
Hi Holger,

Thanks, your response did help.  I'm still a little confused on the validation of the ShapeClass - I already did as you suggested by merging the datamodel with the SHACL metamodel, the Java class is a copy of the ValidateSquareExampleTest class with a different input file name, so ShapeClass should have been processed as a subclass of Shape. 

Yes, the shapes graph would know that sh:ShapeClass is a subclass of sh:Shape, yet the data graph does not know that unless you also merge the SHACL system vocabulary into the data graph. The SHACL engine uses the shapes graph to figure out which constraints to run, but it relies on the data graph to contain the relevant rdfs:subClassOf triples to find all instances of the classes affected by the constraints.

All this requires better explanations and tool support - apologies if this is very early in the process.
The ValidateSquareExample was already doing the right thing AFAIK - it was just using the square file as data graph. So maybe there is something else going on that I don't see. Have you tried to run your file through TBC 5.0.1 validation? Otherwise, feel free to send me the files.

Holger

Roger Sacilotto

unread,
Sep 30, 2015, 4:25:01 PM9/30/15
to TopBraid Suite Users
Hi Holger,

Understood, I think I see how it works.

On a different issue (sorry if I'm overloading this topic, I can raise a new one if it's better), I am getting unexpected results trying a sh:filterShape.

@prefix avidtx: <http://meta.avid.com/class#> .
@prefix avids: <http://meta.avid.com/shapes#> .

# The sh:Graph ----------------------------------------------------------------

a sh:Graph ;
sh:shapesGraph <http://www.w3.org/ns/shacl> ;
rdfs:label "SHACL Test" ;
rdfs:comment "A simple example model." ;
.

# TestShape example does not validate

avidtx:Test a rdfs:Class .

avids:TestShape
a sh:Shape ;
sh:scopeClass avidtx:Test ;
sh:property [
   a sh:PropertyConstraint ;
sh:predicate dc:title ;
sh:datatype sh:text ;
sh:minCount 1 ;
sh:maxCount 1 ;
sh:filterShape [
   a sh:Shape ;
            sh:predicate avidc:isComplete ;
            sh:hasValue "true"^^xsd:boolean ;
] ;
]  .

#succeed
<urn:avid:object:17324310-1787-4a7d-BB62-4179CABF0963> a avidtx:Test ;
        dc:title "Test Title 1" ;
        avidc:isComplete "true"^^xsd:boolean .

#succeed, should not fail dc:title constraint because avidc:isComplete is not true
<urn:avid:object:17324310-1787-4a7d-BB62-4179CABF0973> a avidtx:Test ;
        avidc:isComplete "false"^^xsd:boolean .

#fail, dc:title constraint is violated
<urn:avid:object:17324310-1787-4a7d-BB62-4179CABF0983> a avidtx:Test ;
        avidc:isComplete "true"^^xsd:boolean .

The third resource fails validation as expected, but the second one fails, too, and I can't figure out why.  The filter shape should prevent the dc:title property constraint from being checked, correct?  I don't know what the error is here, do you have any advice?

Thanks,

Roger


On Tuesday, September 29, 2015 at 11:16:52 PM UTC-4, Holger Knublauch wrote:
Yes, the shapes graph would know that sh:ShapeClass is a subclass of sh:Shape, yet the data graph does not know that unless you also merge the SHACL system vocabulary into the data graph. The SHACL engine uses the shapes graph to figure out which constraints to run, but it relies on the data graph to contain the relevant rdfs:subClassOf triples to find all instances of the classes affected by the constraints.

All this requires better explanations and tool support - apologies if this is very early in the process.

Holger Knublauch

unread,
Sep 30, 2015, 4:44:03 PM9/30/15
to topbrai...@googlegroups.com
The sh:filterShape looks syntactically incorrect - missing sh:property. Could you try

sh:filterShape [
    sh:property [
        sh:predicate ...
        sh:hasValue ...
    ]
]

HTH
Holger




#succeed
<urn:avid:object:17324310-1787-4a7d-BB62-4179CABF0963> a avidtx:Test ;
        dc:title "Test Title 1" ;
        avidc:isComplete "true"^^xsd:boolean .

#succeed, should not fail dc:title constraint because avidc:isComplete is not true
<urn:avid:object:17324310-1787-4a7d-BB62-4179CABF0973> a avidtx:Test ;
        avidc:isComplete "false"^^xsd:boolean .

#fail, dc:title constraint is violated
<urn:avid:object:17324310-1787-4a7d-BB62-4179CABF0983> a avidtx:Test ;
        avidc:isComplete "true"^^xsd:boolean .

The third resource fails validation as expected, but the second one fails, too, and I can't figure out why.  The filter shape should prevent the dc:title property constraint from being checked, correct?  I don't know what the error is here, do you have any advice?

Thanks,

Roger


On Tuesday, September 29, 2015 at 11:16:52 PM UTC-4, Holger Knublauch wrote:
Yes, the shapes graph would know that sh:ShapeClass is a subclass of sh:Shape, yet the data graph does not know that unless you also merge the SHACL system vocabulary into the data graph. The SHACL engine uses the shapes graph to figure out which constraints to run, but it relies on the data graph to contain the relevant rdfs:subClassOf triples to find all instances of the classes affected by the constraints.

All this requires better explanations and tool support - apologies if this is very early in the process.

The ValidateSquareExample was already doing the right thing AFAIK - it was just using the square file as data graph. So maybe there is something else going on that I don't see. Have you tried to run your file through TBC 5.0.1 validation? Otherwise, feel free to send me the files.

Holger

Roger Sacilotto

unread,
Sep 30, 2015, 8:40:16 PM9/30/15
to TopBraid Suite Users
Ugh, my apologies for the stupid mistake.  Thanks for the help, and the patience!


Roger 
Reply all
Reply to author
Forward
0 new messages