SPARQL选择查询条件?

时间:2016-06-21 10:40:43

标签: sparql rdf semantic-web ontology virtuoso

我正在尝试形成SPARQL查询,该查询将根据RDF下面的给定Java类提供域名和方法名称。例如

选择DomainNames,MethodName,其中JavaClass ='MyJavaClass'。 这只是一个伪查询。我需要帮助在SPARQL中形成类似的查询。谢谢。

<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:DOL="http://www.MyOnt.com/something/v1#"
    xmlns:DC="http://purl.org/dc/dcmitype/"
    xmlns:foaf="http://xmlns.com/foaf/0.1/">
  <rdf:Description rdf:about="http://www.MyOnt.com/something/data/MyJavaClass">
    <DOL:belongsTo>
      <rdf:Description rdf:about="http://www.MyOnt.com/something/data/MyDomain">
        <DOL:domainName>MyDomainValue2</DOL:domainName>
        <DOL:domainName>MyDomainValue</DOL:domainName>
      </rdf:Description>
    </DOL:belongsTo>
    <DOL:hasMethod>
      <rdf:Description rdf:about="http://www.MyOnt.com/something/data/MyMethod">
        <DOL:returnType>MethodReturnType</DOL:returnType>
      </rdf:Description>
    </DOL:hasMethod>
    <foaf:name>MyJavaClass</foaf:name>
  </rdf:Description>
</rdf:RDF>

1 个答案:

答案 0 :(得分:2)

如果您首先将数据放入Turtle(通常具有与SPARQL非常类似的语法),通常更容易理解SPARQL查询应该是什么样子。以下是您在Turtle中的数据:

@prefix DOL:   <http://www.MyOnt.com/something/v1#> .
@prefix rdf:   <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix foaf:  <http://xmlns.com/foaf/0.1/> .
@prefix DC:    <http://purl.org/dc/dcmitype/> .

<http://www.MyOnt.com/something/data/MyDomain>
        DOL:domainName  "MyDomainValue2" , "MyDomainValue" .

<http://www.MyOnt.com/something/data/MyJavaClass>
        DOL:belongsTo  <http://www.MyOnt.com/something/data/MyDomain> ;
        DOL:hasMethod  <http://www.MyOnt.com/something/data/MyMethod> ;
        foaf:name      "MyJavaClass" .

<http://www.MyOnt.com/something/data/MyMethod>
        DOL:returnType  "MethodReturnType" .

完成后,查询看起来几乎与数据完全一样,除了包含变量。这里唯一的问题是,由于您正在寻找域和方法,因此需要使用 union (假设您希望将域和方法绑定为不同的变量)。

prefix DOL:   <http://www.MyOnt.com/something/v1#>
prefix foaf:  <http://xmlns.com/foaf/0.1/>

select ?domain ?method {
  ?class foaf:name "MyJavaClass" .
  { ?class DOL:belongsTo ?domain }
  union
  { ?class DOL:hasMethod ?method }
}
---------------------------------------------------------------------------------------------------
| domain                                         | method                                         |
===================================================================================================
| <http://www.MyOnt.com/something/data/MyDomain> |                                                |
|                                                | <http://www.MyOnt.com/something/data/MyMethod> |
---------------------------------------------------------------------------------------------------

如果您愿意将域和方法绑定到同一个变量,可以使用替换属性路径选择 域或方法:

prefix DOL:   <http://www.MyOnt.com/something/v1#>
prefix foaf:  <http://xmlns.com/foaf/0.1/>

select ?domainOrMethod {
  ?class foaf:name "MyJavaClass" ;
         DOL:belongsTo|DOL:hasMethod ?domainOrMethod 
}
--------------------------------------------------
| domainOrMethod                                 |
==================================================
| <http://www.MyOnt.com/something/data/MyDomain> |
| <http://www.MyOnt.com/something/data/MyMethod> |
--------------------------------------------------

作为另一种选择,您可以使用块来指定要关注的属性( hasMethod belongsTo ),在哪种情况下,您也可以选择它以了解您拥有哪种类型的值:

prefix DOL:   <http://www.MyOnt.com/something/v1#>
prefix foaf:  <http://xmlns.com/foaf/0.1/>

select ?property ?value {
  values ?property { DOL:belongsTo DOL:hasMethod }
  ?class foaf:name "MyJavaClass" ;
         ?property ?value  
}
------------------------------------------------------------------
| property      | value                                          |
==================================================================
| DOL:belongsTo | <http://www.MyOnt.com/something/data/MyDomain> |
| DOL:hasMethod | <http://www.MyOnt.com/something/data/MyMethod> |
------------------------------------------------------------------