zazuko / rdf-validate-shacl

Validate RDF data purely in JavaScript. An implementation of the W3C SHACL specification on top of the RDFJS stack.
MIT License
95 stars 12 forks source link

Checking for existence of instance of a class not working? #107

Open jakubklimek opened 1 year ago

jakubklimek commented 1 year ago

I want to check whether in the Data graph, there is at least one instance of dcat:Catalog. I followed this example from the SHACL wiki. tl;dr: It does not work in Zazuko, it works in SHACL Play!, but I am not sure which behavior is the correct one.

Data graph - note that there is one instance of dcat:Catalog:

@prefix dcat:    <http://www.w3.org/ns/dcat#> .
@prefix dct:     <http://purl.org/dc/terms/> .
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix vcard2006: <http://www.w3.org/2006/vcard/ns#> .

<https://data.gov.cz/lkod/mdcr/katalog> a dcat:Catalog ;
    dct:title "Katalog otevřených dat Ministerstva dopravy"@cs, "Open Data Catalog of the Ministry of Transport"@en ;
    dct:description "Otevřená data Ministerstva dopravy. Obsahuje datové sady o jízdních řádech a liniových vedeních veřejné dopravy."@cs;
    dct:description "Open data of the Ministry of Transport. It contains datasets regarding timetables of public transport."@en ;
    dct:publisher <https://rpp-opendata.egon.gov.cz/odrpp/zdroj/orgán-veřejné-moci/66003008> ;
    foaf:homepage <https://data.gov.cz/datové-sady?poskytovatel=Ministerstvo%20dopravy> ;
    dcat:contactPoint [
        a vcard2006:Organization ;
        vcard2006:fn "Ministerstvo dopravy, Odbor veřejné dopravy"@cs, "Ministry of Transport"@en ;
        vcard2006:hasEmail <mailto:sekretariat.190@mdcr.cz>
    ] ;
    dcat:dataset  <https://data.gov.cz/lkod/mdcr/datové-sady/seznam-zastavky> ,

Shapes graph:

@prefix dcat: <http://www.w3.org/ns/dcat#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix sh: <http://www.w3.org/ns/shacl#> .
@prefix ex: <https://example.org/> .

# Shapes graph
ex:CatalogExistence
    a sh:NodeShape ;
    sh:targetNode dcat:Catalog ;
    sh:property [
      sh:path [ sh:inversePath rdf:type ];
      sh:minCount 1;
    ] .

Like this, it validates in Zazuko. However, if I remove the dcat:Catalog, e.g., by emptying the data graph, it still validates, which is not what I want. According to the example, and also in SHACL Play! it does not validate, which is what I want. However, I am not sure that the example is correct, since the targetNode cannot target anything, when there is no dcat:Dataset in the data graph, and therefore there is nothing to validate, and therefore the data graph seems valid. 🤷🏻‍♂️

tpluscode commented 1 year ago

Indeed, that is a know issue. If nothing in the data graph matches any shape targets (ie. in your example there is no resource with type dcat:Catalog), then no validation takes place. I think this is according to the spec and the wiki is a little misleading.

SHACL Play! shows a different message, not equivalent to failed validation. That may be a UI-only feature

HolgerKnublauch commented 1 year ago

A shape with sh:targetNode will always be executed, regardless of whether the graph contains that node, or any node. It will simply set the target to the given targetNode even if it doesn't "exist". So - from my superficial look - the validation engine should report a violation if there is no instance of that class.

tpluscode commented 1 year ago

That does make sense but isn't it the opposite of what you said on Discord and also to how the engines behave?

I don't know which version of SHACL_TQ weso.es uses, but all engines report success when the shape has no target or target which is not present in data graph

https://rdfshape.weso.es/link/16789515368

By the way @jakubklimek, I noticed only now that you should change sh: targetNode to sh:targetClass

jakubklimek commented 1 year ago

@tpluscode Actually, if I understand the Wiki example correctly, having sh:targetNode pointing to the class instead of sh:targetClass and supplementing it with the inverse of rdf:type are parts of the whole trick here.

tpluscode commented 1 year ago

ah ok, I failed to really understand before.

Yes, without a logic as described by Holger this cannot work. Otherwise it does assume that no targets found => no validation

giacomociti commented 2 months ago

I propose we revert #81 because we misunderstood the spec (and the example to check there is at least one instance is useful)

tpluscode commented 2 months ago

Can you link to the spec where this is scenario mentioned?

giacomociti commented 2 months ago

https://www.w3.org/TR/shacl/#targets

RDF terms produced by targets are not required to exist as nodes in the data graph.