0

如何为包含此类元组的 rdf 文件编写 SPARQL

<rdf:Description rdf:about="http://example.com/BEROVO">
            <j.0:destDetails rdf:resource="http://example.com/0c64a8f2-fd39-4e9d-a03b-527359af8661"/>
            <j.0:destDetails rdf:resource="http://example.com/31dd238d-0356-4d78-b6fb-26647902ecd3"/>
            <j.0:destDetails rdf:resource="http://example.com/76248058-5dd1-42a5-b988-affcf732ac6a"/>
            <j.0:destDetails rdf:resource="http://example.com/f8541d66-4107-464a-bc31-60df76a4f7a4"/>
            <j.0:destDetails rdf:resource="http://example.com/1298d38c-e69f-42ca-a329-f0cd8091a524"/>
            <j.0:destDetails rdf:resource="http://example.com/5122c7a1-ca0d-4302-84ad-fea4909e8551"/>
            <j.0:destDetails rdf:resource="http://example.com/cb25a063-0787-4079-8e4a-21e3440df8c3"/>
            <j.0:destDetails rdf:resource="http://example.com/f72eeb27-4852-4737-b248-338bc05206b4"/>
            <j.0:destDetails rdf:resource="http://example.com/0fc999e9-0a5a-4555-a7bf-6a4a41508ae8"/>
            <j.0:destDetails rdf:resource="http://example.com/3303225e-f2dd-4712-9cb3-d10a69bd4357"/>
            <j.0:destDetails rdf:resource="http://example.com/31d37728-a72d-445e-b554-d5cdc91830e5"/>
</rdf:Description>
<rdf:Description rdf:about="http://example.com/0c64a8f2-fd39-4e9d-a03b-527359af8661">
            <j.0:distrName>BEROVOTRANS-BEROVO</j.0:distrName>
            <j.0:moneyTwoDir>520 den.</j.0:moneyTwoDir>
            <j.0:moneyOneDir>420 den.</j.0:moneyOneDir>
            <j.0:hasTimeStop>17:15</j.0:hasTimeStop>
            <j.0:hasTimeStart>13:20</j.0:hasTimeStart>
</rdf:Description>

对于带有元组的列表中的每个项目,我喜欢获取属性的信息:

  • j.0:distrName,
  • j.0:moneyTwoDir,
  • j.0:moneyOneDir,
  • j.0:hasTimeStop, 和
  • j.0:hasTimeStart.
4

1 回答 1

6

虽然 Turtle 和 SPARQL 的语法并不完全相同,但许多 Turtle 表达式都适合作为 SPARQL 模式。如果我们对您的数据稍作修改(使其成为完整的 RDF 文档),我们会得到:

<rdf:RDF xmlns:j.0="http://example.com/"
         xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
  <rdf:Description rdf:about="http://example.com/BEROVO">
    <j.0:destDetails rdf:resource="http://example.com/0c64a8f2-fd39-4e9d-a03b-527359af8661"/>
    <j.0:destDetails rdf:resource="http://example.com/31dd238d-0356-4d78-b6fb-26647902ecd3"/>
    <j.0:destDetails rdf:resource="http://example.com/76248058-5dd1-42a5-b988-affcf732ac6a"/>
    <j.0:destDetails rdf:resource="http://example.com/f8541d66-4107-464a-bc31-60df76a4f7a4"/>
    <j.0:destDetails rdf:resource="http://example.com/1298d38c-e69f-42ca-a329-f0cd8091a524"/>
    <j.0:destDetails rdf:resource="http://example.com/5122c7a1-ca0d-4302-84ad-fea4909e8551"/>
    <j.0:destDetails rdf:resource="http://example.com/cb25a063-0787-4079-8e4a-21e3440df8c3"/>
    <j.0:destDetails rdf:resource="http://example.com/f72eeb27-4852-4737-b248-338bc05206b4"/>
    <j.0:destDetails rdf:resource="http://example.com/0fc999e9-0a5a-4555-a7bf-6a4a41508ae8"/>
    <j.0:destDetails rdf:resource="http://example.com/3303225e-f2dd-4712-9cb3-d10a69bd4357"/>
    <j.0:destDetails rdf:resource="http://example.com/31d37728-a72d-445e-b554-d5cdc91830e5"/>
  </rdf:Description>
  <rdf:Description rdf:about="http://example.com/0c64a8f2-fd39-4e9d-a03b-527359af8661">
    <j.0:distrName>BEROVOTRANS-BEROVO</j.0:distrName>
    <j.0:moneyTwoDir>520 den.</j.0:moneyTwoDir>
    <j.0:moneyOneDir>420 den.</j.0:moneyOneDir>
    <j.0:hasTimeStop>17:15</j.0:hasTimeStop>
    <j.0:hasTimeStart>13:20</j.0:hasTimeStart>
  </rdf:Description>
</rdf:RDF>

使用 Jena 的rdfcat工具,我们可以在 Turtle 中得到这个:

$ rdfcat -out Turtle data.rdf
@prefix rdf:     <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .

<http://example.com/BEROVO>
      <http://example.com/destDetails>
              <http://example.com/31dd238d-0356-4d78-b6fb-26647902ecd3> , <http://example.com/5122c7a1-ca0d-4302-84ad-fea4909e8551> , <http://example.com/cb25a063-0787-4079-8e4a-21e3440df8c3> , <http://example.com/3303225e-f2dd-4712-9cb3-d10a69bd4357> , <http://example.com/0c64a8f2-fd39-4e9d-a03b-527359af8661> , <http://example.com/1298d38c-e69f-42ca-a329-f0cd8091a524> , <http://example.com/31d37728-a72d-445e-b554-d5cdc91830e5> , <http://example.com/0fc999e9-0a5a-4555-a7bf-6a4a41508ae8> , <http://example.com/76248058-5dd1-42a5-b988-affcf732ac6a> , <http://example.com/f72eeb27-4852-4737-b248-338bc05206b4> , <http://example.com/f8541d66-4107-464a-bc31-60df76a4f7a4> .

<http://example.com/0c64a8f2-fd39-4e9d-a03b-527359af8661>
      <http://example.com/distrName>
              "BEROVOTRANS-BEROVO" ;
      <http://example.com/hasTimeStart>
              "13:20" ;
      <http://example.com/hasTimeStop>
              "17:15" ;
      <http://example.com/moneyOneDir>
              "420 den." ;
      <http://example.com/moneyTwoDir>
              "520 den." .

果然,这几乎就是相应的 SPARQL 查询的样子:

prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> 

select ?details ?name ?moneyTwoDir ?moneyOneDir ?timeStop ?timeStart where { 

  <http://example.com/BEROVO>
    <http://example.com/destDetails>
      ?details .

  ?details
    <http://example.com/distrName>
      ?name ;
    <http://example.com/hasTimeStart>
      ?timeStart ;
    <http://example.com/hasTimeStop>
      ?timeStop ;
    <http://example.com/moneyOneDir>
      ?moneyOneDir ;
    <http://example.com/moneyTwoDir>
      ?moneyTwoDir .
}

如果需要,可以使查询更简洁:

prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> 
prefix ex: <http://example.com/>

select ?details ?name ?moneyTwoDir ?moneyOneDir ?timeStop ?timeStart where { 

  ex:BEROVO ex:destDetails ?details .

  ?details ex:distrName ?name ;
           ex:hasTimeStart ?timeStart ;
           ex:hasTimeStop ?timeStop ;
           ex:moneyOneDir ?moneyOneDir ;
           ex:moneyTwoDir ?moneyTwoDir .
}

结果如您所料:

$ arq --data data.rdf --query query.sparql
---------------------------------------------------------------------------------------------------------------------
| details                                 | name                 | moneyTwoDir | moneyOneDir | timeStop | timeStart |
=====================================================================================================================
| ex:0c64a8f2-fd39-4e9d-a03b-527359af8661 | "BEROVOTRANS-BEROVO" | "520 den."  | "420 den."  | "17:15"  | "13:20"   |
---------------------------------------------------------------------------------------------------------------------
于 2013-08-06T23:33:59.553 回答