SPARQL xsd:dateTime相等

时间:2010-01-20 12:34:36

标签: datetime xsd semantic-web sparql

我有一个SPARQL查询:

PREFIX  rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX  person: <http://www.myOntDomain/person#>
PREFIX  likedEvent: <http://www.myOntDomain/likedEventRule#>
PREFIX  event: <http://www.myOntDomain/event#>
PREFIX  owl:  <http://www.w3.org/2002/07/owl#>
PREFIX  xsd:  <http://www.w3.org/2001/XMLSchema#>
PREFIX  rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX  weather: <http://www.myOntDomain/weather#>
PREFIX  eventHasSuitableWeather: <http://www.myOntDomain/eventHasSuitableWeather#>
PREFIX  freeAtEvent: <http://www.myOntDomain/freeAtEventRule#>

SELECT DISTINCT  ?Event ?Person ?Time
WHERE
  { ?Person   rdf:type              person:Person .
    ?Event    rdf:type              event:Event .
    ?WeatherEvent
              rdf:type              weather:WeatherEvent .
    ?WeatherType  rdf:type          weather:WeatherEventType .
    ?Person   likedEvent:likedEvents  ?Event ;
              freeAtEvent:freeAtEvent  ?Event .
    ?Event    eventHasSuitableWeather:eventHasSuitableWeather  true ;
              event:eventHasDate    ?Time .
    ?Person   person:hasName        ?PersonName ;
              person:hasAge         ?PersonAge .
    ?Event    event:hasEventType    ?EventType .
  }

返回特定本体的以下结果集:

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| Event                                                                   | Person                                            | Time                                                                |
=====================================================================================================================================================================================================
| <http://www.myOntDomain/event#SyntheticPitchFootballMatch> | <http://www.myOntDomain/person#Ali>  | "2010-01-19T16:00:00Z"^^<http://www.w3.org/2001/XMLSchema#dateTime> |
| <http://www.myOntDomain/event#SerlockHolmesMovie>          | <http://www.myOntDomain/person#Ali>  | "2010-01-19T16:00:00Z"^^<http://www.w3.org/2001/XMLSchema#dateTime> |
| <http://www.myOntDomain/event#SerlockHolmesMovie>          | <http://www.myOntDomain/person#Ayse> | "2010-01-19T16:00:00Z"^^<http://www.w3.org/2001/XMLSchema#dateTime> |
| <http://www.myOntDomain/event#SyntheticPitchFootballMatch> | <http://www.myOntDomain/person#Veli> | "2010-01-19T16:00:00Z"^^<http://www.w3.org/2001/XMLSchema#dateTime> |
| <http://www.myOntDomain/weather#AnkaraMuseumVisit>         | <http://www.myOntDomain/person#Ali>  | "2010-01-19T17:00:00Z"^^<http://www.w3.org/2001/XMLSchema#dateTime> |
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

我想添加一个时间约束来选择当时会发生的事件:

2010-01-19T16:00:00Z

所以我在我的查询中添加了一个FILTER元素:

PREFIX  rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX  person: <http://www.myOntDomain/person#>
PREFIX  likedEvent: <http://www.myOntDomain/likedEventRule#>
PREFIX  event: <http://www.myOntDomain/event#>
PREFIX  owl:  <http://www.w3.org/2002/07/owl#>
PREFIX  xsd:  <http://www.w3.org/2001/XMLSchema#>
PREFIX  rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX  weather: <http://www.myOntDomain/weather#>
PREFIX  eventHasSuitableWeather: <http://www.myOntDomain/eventHasSuitableWeather#>
PREFIX  freeAtEvent: <http://www.myOntDomain/freeAtEventRule#>

SELECT DISTINCT  ?Event ?Person ?Time
WHERE
  { ?Person   rdf:type              person:Person .
    ?Event    rdf:type              event:Event .
    ?WeatherEvent
              rdf:type              weather:WeatherEvent .
    ?WeatherType  rdf:type          weather:WeatherEventType .
    ?Person   likedEvent:likedEvents  ?Event ;
              freeAtEvent:freeAtEvent  ?Event .
    ?Event    eventHasSuitableWeather:eventHasSuitableWeather  true ;
              event:eventHasDate    ?Time .
    ?Person   person:hasName        ?PersonName ;
              person:hasAge         ?PersonAge .
    ?Event    event:hasEventType    ?EventType .
    FILTER ( ?Time = "2010-01-19T16:00:00Z"^^xsd:dateTime )
  }

但是这次查询在同一个本体上返回空结果列表:

-------------------------
| Event | Person | Time |
=========================
-------------------------

显然我遗漏了一些关于SPARQL或xsd:dateTime的比较,但我无法理解。 如果您有任何想法,请指导我。谢谢。

更新

我使用Jena Framework中使用的SPARQL的ARQ实现。我认为问题是关于实现而不是本体或查询

我检查了实现 com.hp.hpl.jena.datatypes.xsd.XSDDateTime和 com.hp.hpl.jena.datatypes.xsd.AbstractDateTime 可在此处找到:http://grepcode.com/file/repo1.maven.org/maven2/com.hp.hpl.jena/jena/2.6.0/com/hp/hpl/jena/datatypes/xsd

并看到dateTime对象由9个值表示:

protected final static int CY = 0, M = 1, D = 2, h = 3, m = 4, s = 5, ms = 6, utc=7, msscale=8

当从java.util.Calendar对象创建XSDDateTime对象并且从本体解析的XSDDateTime对象始终为零时,值msscale始终为3。 此外,compare函数检查所有这9个值是否相等,因此它们永远不会相等。 例如,当我通过编辑查询文本添加FILTER时,我会在相等检查后得到所需的结果。 但是当我以编程方式添加FILTER时,两个FILTER的序列化是相同的,但结果却不一样。 这是一个示例,两个查询及其结果会立即显示在它们之后。

PREFIX  rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX  person: <http://www.myOntDomain/person#>
PREFIX  likedEvent: <http://www.myOntDomain/likedEventRule#>
PREFIX  event: <http://www.myOntDomain/event#>
PREFIX  owl:  <http://www.w3.org/2002/07/owl#>
PREFIX  xsd:  <http://www.w3.org/2001/XMLSchema#>
PREFIX  rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX  weather: <http://www.myOntDomain/weather#>
PREFIX  eventHasSuitableWeather: <http://www.myOntDomain/eventHasSuitableWeather#>
PREFIX  freeAtEvent: <http://www.myOntDomain/freeAtEventRule#>

SELECT DISTINCT  ?Event ?Person ?Time
WHERE
  { ?Person   rdf:type              person:Person .
    ?Event    rdf:type              event:Event .
    ?WeatherEvent
              rdf:type              weather:WeatherEvent .
    ?WeatherType  rdf:type          weather:WeatherEventType .
    ?Person   likedEvent:likedEvents  ?Event ;
              freeAtEvent:freeAtEvent  ?Event .
    ?Event    eventHasSuitableWeather:eventHasSuitableWeather  true ;
              event:eventHasDate    ?Time .
    ?Person   person:hasName        ?PersonName ;
              person:hasAge         ?PersonAge .
    ?Event    event:hasEventType    ?EventType .
    FILTER ( ?Time = "2010-01-19T16:00:00Z"^^xsd:dateTime )
  }

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| Event                                                                   | Person                                            | Time                                                                |
=====================================================================================================================================================================================================
| <http://www.myOntDomain/event#SerlockHolmesMovie>          | <http://www.myOntDomain/person#Ayse> | "2010-01-19T16:00:00Z"^^<http://www.w3.org/2001/XMLSchema#dateTime> |
| <http://www.myOntDomain/event#SerlockHolmesMovie>          | <http://www.myOntDomain/person#Ali>  | "2010-01-19T16:00:00Z"^^<http://www.w3.org/2001/XMLSchema#dateTime> |
| <http://www.myOntDomain/event#SyntheticPitchFootballMatch> | <http://www.myOntDomain/person#Veli> | "2010-01-19T16:00:00Z"^^<http://www.w3.org/2001/XMLSchema#dateTime> |
| <http://www.myOntDomain/event#SyntheticPitchFootballMatch> | <http://www.myOntDomain/person#Ali>  | "2010-01-19T16:00:00Z"^^<http://www.w3.org/2001/XMLSchema#dateTime> |
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

PREFIX  rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX  person: <http://www.myOntDomain/person#>
PREFIX  likedEvent: <http://www.myOntDomain/likedEventRule#>
PREFIX  event: <http://www.myOntDomain/event#>
PREFIX  owl:  <http://www.w3.org/2002/07/owl#>
PREFIX  xsd:  <http://www.w3.org/2001/XMLSchema#>
PREFIX  rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX  weather: <http://www.myOntDomain/weather#>
PREFIX  eventHasSuitableWeather: <http://www.myOntDomain/eventHasSuitableWeather#>
PREFIX  freeAtEvent: <http://www.myOntDomain/freeAtEventRule#>

SELECT DISTINCT  ?Event ?Person ?Time
WHERE
  { ?Person   rdf:type              person:Person .
    ?Event    rdf:type              event:Event .
    ?WeatherEvent
              rdf:type              weather:WeatherEvent .
    ?WeatherType  rdf:type          weather:WeatherEventType .
    ?Person   likedEvent:likedEvents  ?Event ;
              freeAtEvent:freeAtEvent  ?Event .
    ?Event    eventHasSuitableWeather:eventHasSuitableWeather  true ;
              event:eventHasDate    ?Time .
    ?Person   person:hasName        ?PersonName ;
              person:hasAge         ?PersonAge .
    ?Event    event:hasEventType    ?EventType .
    FILTER ( ?Time = "2010-01-19T16:00:00Z"^^xsd:dateTime )
    FILTER ( ?Time = "2010-01-19T16:00:00Z"^^xsd:dateTime )
  }

-------------------------
| Event | Person | Time |
=========================
-------------------------

更新

我在jena-dev中问了同样的问题,并了解到查询或代码没有任何问题。这是Jena 2.6.0中的问题,但它已在Jena 2.6.2中修复。

1 个答案:

答案 0 :(得分:1)

您的查询没有明显错误,这可能是您使用的SPARQL引擎如何实现=运算符的问题。您使用的是哪个SPARQL引擎?

=运算符应该执行值相等,这样当您根据本体评估查询时,您的过滤器应该仍然可以返回大多数相同的结果。

即使=运算符只在您使用的SPARQL引擎中执行RDF术语相等,您仍然应该得到结果,因为文字仍将完全匹配。

如果您发布了正在使用的SPARQL引擎以及实际本体的副本,我可以为您提供比“它应该工作”更好的答案

<强>更新

我怀疑但不能肯定地说这可能是xsd:dateTime序列化/反序列化中的一些奇怪问题。我会向Jena开发人员邮件列表发送一封电子邮件,详细说明您的问题,看看开发人员和社区是否可以为您提供答案:

http://tech.groups.yahoo.com/group/jena-dev/