Pyshacl inference

121 views
Skip to first unread message

richarddi...@gmail.com

unread,
May 26, 2021, 1:24:30 PM5/26/21
to rdflib-dev
A new conversation because I can not add a new to the previous.

@Boris, Yes, sometimes even inference within TB Composer is complex.

So I hoped pyshacl is an alternative. But a minimal running example does not give inferred triples; sorry to be a hard learner. Here I try to infer keyWords for schema:Avatar.

Python code
---------------------------------------------------------------------
from rdflib import GraphURIRef
from pyshacl import validate

shapes = Graph()
shapes.parse('testshapes.ttl'format='ttl')
data_graph = Graph()
data_graph.parse('testdata.ttl'format='ttl')

data_graph_orig = Graph()
for t in data_graph:
    data_graph_orig.add(t)

r = validate(data_graph,
      shacl_graph=shapes,
      #ont_graph=og,
      inference='rdfs',
      abort_on_error=False,
      meta_shacl=True,
      advanced=True,
      js=False,
      debug=True)

conformsresults_graphresults_text = r

added_triples = data_graph - data_graph_orig
print('Added triples: 'len(added_triples))
print(added_triples.all_nodes())


Data
-----------------------------------------------------
@prefix schema: <http://schema.org/> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix : <http://example.org#>.

:movies a owl:Ontology ;
    owl:imports schema: .

:Avatar a schema:Movie ;
    schema:actor :Sam .



Shapes:
------------------------------------------------------
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix schema: <http://schema.org/> .
@prefix : <http://example.org#>.

:movieOntology
    a owl:Ontology ;
    owl:imports schema: ;
    owl:imports sh: ;
    owl:imports xsd: ;
    sh:declare
    [ sh:prefix "sh" ;
      sh:namespace "http://www.w3.org/ns/shacl#"^^xsd:anyURI ; ] ,
    [ sh:prefix "schema" ;
      sh:namespace "http://schema.org/"^^xsd:anyURI ;] ,
    [ sh:prefix "owl" ;
      sh:namespace "http://www.w3.org/2002/07/owl#"^^xsd:anyURI ; ]
    .

:keywordsRule
    a sh:NodeShape ;
    sh:targetClass schema:Movie ;
    sh:rule [
        a sh:SPARQLRule ;
        sh:prefixes schema: ;
        sh:construct """
            CONSTRUCT {
                $this schema:keyWords ?actor  .
            }
            WHERE {
                $this schema:actor ?actor .
            }
            """ ;
    ] ;
.


Regards,
Richard

Boris Pelakh

unread,
May 26, 2021, 8:54:55 PM5/26/21
to rdfli...@googlegroups.com
When I run your code, among the several triples added I see

(rdflib.term.URIRef('http://example.org#Avatar'), rdflib.term.URIRef('http://schema.org/keyWords'), rdflib.term.URIRef('http://example.org#Sam'))

If you turn off inference, it is the only triple added. So perhaps some sort of an environment issue.

--
http://github.com/RDFLib
---
You received this message because you are subscribed to the Google Groups "rdflib-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rdflib-dev+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rdflib-dev/15aef451-28f3-4501-872a-9ae2ca5193d2n%40googlegroups.com.

richarddi...@gmail.com

unread,
May 27, 2021, 2:07:30 AM5/27/21
to rdflib-dev
Thank you Boris. I'll have a look.

richarddi...@gmail.com

unread,
May 27, 2021, 5:46:22 AM5/27/21
to rdflib-dev
Without inference during validation the triple is inferred in my environment too. Thank you.
Reply all
Reply to author
Forward
0 new messages