可以遵循类型链来测试它的结束位置吗?

Can a type chain be followed to test where it ends?

假设我有以下内容:

@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#> .

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 .

我 运行 以下 SPARQL 查询:

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX sch:  <http://schema.org/>

SELECT DISTINCT ?s
WHERE {
    {
        ?s ?p ?o .
        FILTER NOT EXISTS {
            ?s a ?c .
            FILTER(?c IN (rdfs:Class, rdf:Property))
        }
    }
}

返回的结果将是:

----------------------------------------------------------------
| s                                                            |
================================================================
| <http://learningsparql.com/ns/humanResources#Another>        |
| <http://learningsparql.com/ns/humanResources#BadOne>         |
| <http://learningsparql.com/ns/humanResources#BadTwo>         |
| <http://learningsparql.com/ns/humanResources#YetAnother>     |
| <http://learningsparql.com/ns/humanResources#BadThree>       |
| <http://learningsparql.com/ns/humanResources#AnotherName>    |
| <http://learningsparql.com/ns/humanResources#YetAnotherName> |
----------------------------------------------------------------

我想要返回的唯一两个结果是:

----------------------------------------------------------------
| s                                                            |
================================================================
| <http://learningsparql.com/ns/humanResources#BadOne>         |
| <http://learningsparql.com/ns/humanResources#BadTwo>         |
| <http://learningsparql.com/ns/humanResources#BadThree>       | 
----------------------------------------------------------------

是什么让他们变坏了?如果我查看 rdf:type 信息,类型链不会以 rdfs:Class 或 rdf:Property.

类型终止

如果我查看 hr:YetAnother,它的 rdf:type 为 hr:Another。 hr:Another 的 rdf:type 为 hr:Employee。因此,来自 hr:YetAnother 和 hr:Another 的类型链以 rdfs:Class 终止,它们不应由查询返回。

在我的示例中,类型链很小,但链中可能有更多链接,使它们更长。

是否可以用 SPARQL 编写这样的查询?如果是这样,该查询是什么?

解决此问题所需的 SPARQL 功能称为 Property Paths

以下查询:

SELECT DISTINCT ?s
WHERE {
    {
        ?s ?p ?o .    
        FILTER NOT EXISTS {
            ?s rdf:type* ?c .
             FILTER(?c IN (rdfs:Class, rdf:Property) && ?s NOT IN (rdfs:Class, rdf:Property) )
        }
    }
}

将return预期结果:

----------------------------------------------------------
| s                                                      |
==========================================================
| <http://learningsparql.com/ns/humanResources#BadOne>   |
| <http://learningsparql.com/ns/humanResources#BadTwo>   |
| <http://learningsparql.com/ns/humanResources#BadThree> |
----------------------------------------------------------

中断查询确实可以更好地理解正在发生的事情,请考虑,

(A)

SELECT DISTINCT *
WHERE {
    {
        ?s ?p ?o .       
    }
}

这将 return 以下结果:

-------------------------------------------------------------------------------------------------------------------------------------------
| s                                                            | p            | o                                                         |
===========================================================================================================================================
| <http://learningsparql.com/ns/humanResources#Another>        | rdf:type     | <http://learningsparql.com/ns/humanResources#Employee>    |
| <http://learningsparql.com/ns/humanResources#BadOne>         | rdf:type     | <http://learningsparql.com/ns/humanResources#Dangling>    |
| <http://learningsparql.com/ns/humanResources#BadTwo>         | rdf:type     | <http://learningsparql.com/ns/humanResources#BadOne>      |
| <http://learningsparql.com/ns/humanResources#Employee>       | rdf:type     | rdfs:Class                                                |
| <http://learningsparql.com/ns/humanResources#YetAnother>     | rdf:type     | <http://learningsparql.com/ns/humanResources#Another>     |
| <http://learningsparql.com/ns/humanResources#BadThree>       | rdfs:comment | "some comment about missing"                              |
| <http://learningsparql.com/ns/humanResources#AnotherName>    | rdf:type     | <http://learningsparql.com/ns/humanResources#name>        |
| <http://learningsparql.com/ns/humanResources#name>           | rdf:type     | rdf:Property                                              |
| <http://learningsparql.com/ns/humanResources#YetAnotherName> | rdf:type     | <http://learningsparql.com/ns/humanResources#AnotherName> |
-------------------------------------------------------------------------------------------------------------------------------------------

然后,考虑以下查询:

(B)

SELECT DISTINCT ?s
WHERE {
    {
        ?s rdf:type* ?c .
        FILTER(?c IN (rdfs:Class, rdf:Property) && ?s NOT IN (rdfs:Class, rdf:Property) )

    }
}

其中 return 结果:

----------------------------------------------------------------
| s                                                            |
================================================================
| <http://learningsparql.com/ns/humanResources#Employee>       |
| <http://learningsparql.com/ns/humanResources#Another>        |
| <http://learningsparql.com/ns/humanResources#YetAnother>     |
| <http://learningsparql.com/ns/humanResources#name>           |
| <http://learningsparql.com/ns/humanResources#AnotherName>    |
| <http://learningsparql.com/ns/humanResources#YetAnotherName> |
----------------------------------------------------------------

通过将 (B) 放入 FILTER NOT EXISTS 中,在 (A) 中找到的主题将被删除,只留下所需的结果。