Sparql 验证每个科目都有一种类型的类

Sparql 验证每个科目都有一种类型的类,sparql,rdf,turtle-rdf,shacl,Sparql,Rdf,Turtle Rdf,Shacl,我有以下数据和形状图 @prefix hr: <http://learningsparql.com/ns/humanResources#> . @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . @prefix xml: <http://www.w3.org/XML/1998

我有以下数据和形状图

@prefix hr: <http://learningsparql.com/ns/humanResources#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix xml: <http://www.w3.org/XML/1998/namespace> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix schema: <http://schema.org/> .
@prefix sh: <http://www.w3.org/ns/shacl#> .

hr:Employee a rdfs:Class .
hr:BadThree rdfs:comment "some comment about missing" .
hr:BadTwo a hr:BadOne .
hr:YetAnother a hr:Another .
hr:YetAnotherName a hr:AnotherName .
hr:Another a hr:Employee .
hr:AnotherName a hr:name .
hr:BadOne a hr:Dangling .
hr:name a rdf:Property .

schema:SchemaShape
    a sh:NodeShape ;
    sh:target [
        a sh:SPARQLTarget ;
        sh:prefixes hr: ;
        sh:select """
            SELECT ?this
            WHERE {
                ?this ?p ?o .
            }
            """ ;
    ] ; 

    sh:property [                
        sh:path rdf:type ;
        sh:nodeKind sh:IRI ;
        sh:hasValue rdfs:Class
    ] ; 
.
我认为应该发生的是,基于SPARQL的目标应该选择数据图中的每个主题,然后验证是否存在rdf:type的路径,该路径的值为rdfs:Class

我得到以下结果:

Validation Report
Conforms: True
预期的验证错误应仅包括以下主题:

| <http://learningsparql.com/ns/humanResources#BadOne>         |
| <http://learningsparql.com/ns/humanResources#BadTwo>         |
| <http://learningsparql.com/ns/humanResources#BadThree>       |
| <http://learningsparql.com/ns/humanResources#AnotherName>    |
| <http://learningsparql.com/ns/humanResources#name>           |
| <http://learningsparql.com/ns/humanResources#YetAnotherName> |
||
|          |
|        |
|     |
|            |
|  |

沙克能做到吗?如果是这样,形状文件应该是什么?

接下来的内容会导致预期的验证错误,但是,有几件事我不明白

  • sh:前缀hr:是不需要的。它将为SPARQL目标SELECT语句本身提供前缀,仅此而已

  • 需要禁用。它是插入三元组并尝试验证它们。在这个用例中,这不是我们想要的。应该验证的是模式中的内容,而不是其他内容

  • 我也在想,根据显然是误解的东西,把所有的东西都放在一张图表中不会是一个问题

  • graph_data=”“”
    @前缀hr:。
    @前缀rdf:。
    @前缀rdfs:。
    @前缀xml:。
    @前缀xsd:。
    @前缀架构:。
    @前缀sh:。
    hr:Employee a rdfs:Class。
    hr:BAD三个RDF:注释“关于缺失的一些注释”。
    人力资源部:差两个小时:差一个小时。
    另一个。
    hr:YetAnotherName a hr:AnotherName。
    人力资源:另一个a人力资源:员工。
    hr:AnotherName a hr:name。
    hr:BadOne a hr:Dangling。
    hr:name一个rdf:Property。
    """
    形状_数据=“”
    @前缀hr:。
    @前缀rdf:。
    @前缀rdfs:。
    @前缀xml:。
    @前缀xsd:。
    @前缀架构:。
    @前缀sh:。
    模式:SchemaShape
    阿什:NodeShape;
    sh:目标[
    sh:SPARQLTarget;
    sh:前缀hr:;
    sh:选择“”
    选择这个
    在哪里{
    这是邮政局。
    }
    """ ;
    ] ; 
    sh:物业[
    sh:path(rdf:type[sh:zeroOrMorePath-rdf:type]);
    sh:nodeKind sh:IRI;
    sh:hasValue-rdfs:Class
    ] ; 
    .
    '''
    data=rdflib.Graph().parse(data=Graph\u data,格式='turtle')
    shape=rdflib.Graph().parse(数据=shape\u数据,格式='turtle')
    报告=验证(数据,shacl\u图形=形状,中止\u错误=假,元\u shacl=假,调试=假,高级=真)
    
    使用基于SPARQL的约束的替代方案如下所示:

    graph_data = """
    @prefix hr: <http://learningsparql.com/ns/humanResources#> .
    @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
    @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
    @prefix xml: <http://www.w3.org/XML/1998/namespace> .
    @prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
    @prefix schema: <http://schema.org/> .
    @prefix sh: <http://www.w3.org/ns/shacl#> .
    
    hr:Employee a rdfs:Class .
    hr:BadThree rdfs:comment "some comment about missing" .
    hr:BadTwo a hr:BadOne .
    hr:YetAnother a hr:Another .
    hr:YetAnotherName a hr:AnotherName .
    hr:Another a hr:Employee .
    hr:AnotherName a hr:name .
    hr:BadOne a hr:Dangling .
    hr:name a rdf:Property .
    """
    
    shape_data = '''
    @prefix hr: <http://learningsparql.com/ns/humanResources#> .
    @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
    @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
    @prefix xml: <http://www.w3.org/XML/1998/namespace> .
    @prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
    @prefix schema: <http://schema.org/> .
    @prefix sh: <http://www.w3.org/ns/shacl#> .
    
    schema:SchemaShape
        a sh:NodeShape ;
        sh:target [
            a sh:SPARQLTarget ;
            sh:select """
                SELECT ?this
                WHERE {
                    ?this ?p ?o .
                }
                """ ;
        ] ; 
    
        sh:sparql [ 
            a sh:SPARQLConstraint ; 
            sh:message "Node does not have type rdfs:Class." ; 
            sh:prefixes hr: ; 
            sh:select """ 
                SELECT $this 
                WHERE { 
                    $this rdf:type ?o . 
    
                    FILTER NOT EXISTS {
                        ?o rdf:type* rdfs:Class
                    }
                    FILTER ( strstarts( str( $this ), str( hr: ) ) ) 
                }
                """ ;
        ]
    .
    '''
    
    
    data  = rdflib.Graph().parse( data = graph_data, format = 'turtle' )
    shape = rdflib.Graph().parse( data = shape_data, format = 'turtle' )
    
    report = validate( data, shacl_graph=shape, abort_on_error = False, meta_shacl = False, debug = False, advanced = True )
    
    graph_data=”“”
    @前缀hr:。
    @前缀rdf:。
    @前缀rdfs:。
    @前缀xml:。
    @前缀xsd:。
    @前缀架构:。
    @前缀sh:。
    hr:Employee a rdfs:Class。
    hr:BAD三个RDF:注释“关于缺失的一些注释”。
    人力资源部:差两个小时:差一个小时。
    另一个。
    hr:YetAnotherName a hr:AnotherName。
    人力资源:另一个a人力资源:员工。
    hr:AnotherName a hr:name。
    hr:BadOne a hr:Dangling。
    hr:name一个rdf:Property。
    """
    形状_数据=“”
    @前缀hr:。
    @前缀rdf:。
    @前缀rdfs:。
    @前缀xml:。
    @前缀xsd:。
    @前缀架构:。
    @前缀sh:。
    模式:SchemaShape
    阿什:NodeShape;
    sh:目标[
    sh:SPARQLTarget;
    sh:选择“”
    选择这个
    在哪里{
    这是邮政局。
    }
    """ ;
    ] ; 
    sh:sparql[
    sh:sparql约束;
    sh:消息“节点没有类型rdfs:Class。”;
    sh:前缀hr:;
    sh:选择“”
    选择$this
    何处{
    $thisrdf:type?o。
    筛选器不存在{
    ?o rdf:类型*rdfs:类别
    }
    过滤器(strstarts(str($this),str(hr:))
    }
    """ ;
    ]
    .
    '''
    data=rdflib.Graph().parse(data=Graph\u data,格式='turtle')
    shape=rdflib.Graph().parse(数据=shape\u数据,格式='turtle')
    报告=验证(数据,shacl\u图形=形状,中止\u错误=假,元\u shacl=假,调试=假,高级=真)
    
    接下来的操作会导致预期的验证错误,但是,有几件事我还不明白

  • sh:前缀hr:是不需要的。它将为SPARQL目标SELECT语句本身提供前缀,仅此而已

  • 需要禁用。它是插入三元组并尝试验证它们。在这个用例中,这不是我们想要的。应该验证的是模式中的内容,而不是其他内容

  • 我也在想,根据显然是误解的东西,把所有的东西都放在一张图表中不会是一个问题

  • graph_data=”“”
    @前缀hr:。
    @前缀rdf:。
    @前缀rdfs:。
    @前缀xml:。
    @前缀xsd:。
    @前缀架构:。
    @前缀sh:。
    hr:Employee a rdfs:Class。
    hr:BAD三个RDF:注释“关于缺失的一些注释”。
    人力资源部:差两个小时:差一个小时。
    另一个。
    hr:YetAnotherName a hr:AnotherName。
    人力资源:另一个a人力资源:员工。
    hr:AnotherName a hr:name。
    hr:BadOne a hr:Dangling。
    hr:name一个rdf:Property。
    """
    形状_数据=“”
    @前缀hr:。
    @前缀rdf:。
    @前缀rdfs:。
    @前缀xml:。
    @前缀xsd:。
    @前缀架构:。
    @前缀sh:。
    模式:SchemaShape
    阿什:NodeShape;
    sh:目标[
    sh:SPARQLTarget;
    sh:前缀hr:;
    sh:选择“”
    选择这个
    在哪里{
    这是邮政局。
    }
    """ ;
    ] ; 
    sh:物业[
    sh:path(rdf:type[sh:zeroOrMorePath-rdf:type]);
    sh:nodeKind sh:IRI;
    sh:hasValue-rdfs:Class
    ] ; 
    .
    '''
    data=rdflib.Graph().parse(data=Graph\u data,格式='turtle')
    shape=rdflib.Graph().parse(数据=shape\u数据,格式='turtle')
    报告=验证(数据,shacl\u图形=形状,中止\u错误=假,元\u shacl=假,调试=假,高级=真)
    
    使用基于SPARQL的约束的替代方案如下所示:

    graph_data = """
    @prefix hr: <http://learningsparql.com/ns/humanResources#> .
    @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
    @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
    @prefix xml: <http://www.w3.org/XML/1998/namespace> .
    @prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
    @prefix schema: <http://schema.org/> .
    @prefix sh: <http://www.w3.org/ns/shacl#> .
    
    hr:Employee a rdfs:Class .
    hr:BadThree rdfs:comment "some comment about missing" .
    hr:BadTwo a hr:BadOne .
    hr:YetAnother a hr:Another .
    hr:YetAnotherName a hr:AnotherName .
    hr:Another a hr:Employee .
    hr:AnotherName a hr:name .
    hr:BadOne a hr:Dangling .
    hr:name a rdf:Property .
    """
    
    shape_data = '''
    @prefix hr: <http://learningsparql.com/ns/humanResources#> .
    @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
    @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
    @prefix xml: <http://www.w3.org/XML/1998/namespace> .
    @prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
    @prefix schema: <http://schema.org/> .
    @prefix sh: <http://www.w3.org/ns/shacl#> .
    
    schema:SchemaShape
        a sh:NodeShape ;
        sh:target [
            a sh:SPARQLTarget ;
            sh:select """
                SELECT ?this
                WHERE {
                    ?this ?p ?o .
                }
                """ ;
        ] ; 
    
        sh:sparql [ 
            a sh:SPARQLConstraint ; 
            sh:message "Node does not have type rdfs:Class." ; 
            sh:prefixes hr: ; 
            sh:select """ 
                SELECT $this 
                WHERE { 
                    $this rdf:type ?o . 
    
                    FILTER NOT EXISTS {
                        ?o rdf:type* rdfs:Class
                    }
                    FILTER ( strstarts( str( $this ), str( hr: ) ) ) 
                }
                """ ;
        ]
    .
    '''
    
    
    data  = rdflib.Graph().parse( data = graph_data, format = 'turtle' )
    shape = rdflib.Graph().parse( data = shape_data, format = 'turtle' )
    
    report = validate( data, shacl_graph=shape, abort_on_error = False, meta_shacl = False, debug = False, advanced = True )
    
    graph_data=”“”
    @前缀hr:。
    @前缀
    
    graph_data = """
    @prefix hr: <http://learningsparql.com/ns/humanResources#> .
    @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
    @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
    @prefix xml: <http://www.w3.org/XML/1998/namespace> .
    @prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
    @prefix schema: <http://schema.org/> .
    @prefix sh: <http://www.w3.org/ns/shacl#> .
    
    hr:Employee a rdfs:Class .
    hr:BadThree rdfs:comment "some comment about missing" .
    hr:BadTwo a hr:BadOne .
    hr:YetAnother a hr:Another .
    hr:YetAnotherName a hr:AnotherName .
    hr:Another a hr:Employee .
    hr:AnotherName a hr:name .
    hr:BadOne a hr:Dangling .
    hr:name a rdf:Property .
    """
    
    shape_data = '''
    @prefix hr: <http://learningsparql.com/ns/humanResources#> .
    @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
    @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
    @prefix xml: <http://www.w3.org/XML/1998/namespace> .
    @prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
    @prefix schema: <http://schema.org/> .
    @prefix sh: <http://www.w3.org/ns/shacl#> .
    
    schema:SchemaShape
        a sh:NodeShape ;
        sh:target [
            a sh:SPARQLTarget ;
            sh:select """
                SELECT ?this
                WHERE {
                    ?this ?p ?o .
                }
                """ ;
        ] ; 
    
        sh:sparql [ 
            a sh:SPARQLConstraint ; 
            sh:message "Node does not have type rdfs:Class." ; 
            sh:prefixes hr: ; 
            sh:select """ 
                SELECT $this 
                WHERE { 
                    $this rdf:type ?o . 
    
                    FILTER NOT EXISTS {
                        ?o rdf:type* rdfs:Class
                    }
                    FILTER ( strstarts( str( $this ), str( hr: ) ) ) 
                }
                """ ;
        ]
    .
    '''
    
    
    data  = rdflib.Graph().parse( data = graph_data, format = 'turtle' )
    shape = rdflib.Graph().parse( data = shape_data, format = 'turtle' )
    
    report = validate( data, shacl_graph=shape, abort_on_error = False, meta_shacl = False, debug = False, advanced = True )