关于 SPARQL 查询的本体和数据分离的最佳实践?

问题描述 投票:0回答:1

我正在研究本体和使用它的链接开放数据集。我在 Mac 上使用 Protégé 5.5.0。我已经尝试在 Protégé 中运行 SPARQL 查询,并且我也在 docker 上使用了 stain/jena-fuseki。

我创建了类 Item 和 Scale。 Item 实例可以有一个 Scale,1_One、2_Two 和 3_Three 之一。每个 Scale 都有一个值和一个标签。我有兴趣进行如下所示的关联项目和价值的查询。

有没有更好的分离本体和数据的方法?

查询:

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX tst: <http://www2.cs.uregina.ca/~hepting/vocab.owl#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX : <http://www2.cs.uregina.ca/~hepting/data.ttl#>

SELECT * WHERE
{
  ?item tst:hasScale ?scale .
  ?scale tst:hasScaleValue ?value .
}
ORDER BY ?s

原始查询结果:

{
  "head": {
    "vars": [ "item" , "scale" , "value" ]
  } ,
  "results": {
    "bindings": [
      {
        "item": { "type": "uri" , "value": "http://www2.cs.uregina.ca/~hepting/data.ttl#Item1" } ,
        "scale": { "type": "uri" , "value": "http://www2.cs.uregina.ca/~hepting/vocab.owl#1_One" } ,
        "value": { "type": "literal" , "datatype": "http://www.w3.org/2001/XMLSchema#positiveInteger" , "value": "1" }
      } ,
      {
        "item": { "type": "uri" , "value": "http://www2.cs.uregina.ca/~hepting/data.ttl#Item2" } ,
        "scale": { "type": "uri" , "value": "http://www2.cs.uregina.ca/~hepting/vocab.owl#2_Two" } ,
        "value": { "type": "literal" , "datatype": "http://www.w3.org/2001/XMLSchema#positiveInteger" , "value": "2" }
      } ,
      {
        "item": { "type": "uri" , "value": "http://www2.cs.uregina.ca/~hepting/data.ttl#Item3" } ,
        "scale": { "type": "uri" , "value": "http://www2.cs.uregina.ca/~hepting/vocab.owl#3_Three" } ,
        "value": { "type": "literal" , "datatype": "http://www.w3.org/2001/XMLSchema#positiveInteger" , "value": "3" }
      } ,
      {
        "item": { "type": "uri" , "value": "http://www2.cs.uregina.ca/~hepting/data.ttl#Item4" } ,
        "scale": { "type": "uri" , "value": "http://www2.cs.uregina.ca/~hepting/vocab.owl#3_Three" } ,
        "value": { "type": "literal" , "datatype": "http://www.w3.org/2001/XMLSchema#positiveInteger" , "value": "3" }
      } ,
      {
        "item": { "type": "uri" , "value": "http://www2.cs.uregina.ca/~hepting/data.ttl#Item5" } ,
        "scale": { "type": "uri" , "value": "http://www2.cs.uregina.ca/~hepting/vocab.owl#3_Three" } ,
        "value": { "type": "literal" , "datatype": "http://www.w3.org/2001/XMLSchema#positiveInteger" , "value": "3" }
      } ,
      {
        "item": { "type": "uri" , "value": "http://www2.cs.uregina.ca/~hepting/data.ttl#Item6" } ,
        "scale": { "type": "uri" , "value": "http://www2.cs.uregina.ca/~hepting/vocab.owl#3_Three" } ,
        "value": { "type": "literal" , "datatype": "http://www.w3.org/2001/XMLSchema#positiveInteger" , "value": "3" }
      } ,
      {
        "item": { "type": "uri" , "value": "http://www2.cs.uregina.ca/~hepting/data.ttl#Item7" } ,
        "scale": { "type": "uri" , "value": "http://www2.cs.uregina.ca/~hepting/vocab.owl#3_Three" } ,
        "value": { "type": "literal" , "datatype": "http://www.w3.org/2001/XMLSchema#positiveInteger" , "value": "3" }
      }
    ]
  }
}

下面是简单的本体(vocab.owl)和数据(data.ttl文件

vocab.owl:

@prefix tst: <http://www2.cs.uregina.ca/~hepting/vocab.owl#> .
@prefix : <http://www2.cs.uregina.ca/~hepting/data.ttl#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix xml: <http://www.w3.org/XML/1998/namespace> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@base <http://www2.cs.uregina.ca/~hepting/vocab.owl> .

<http://www2.cs.uregina.ca/~hepting/vocab> rdf:type owl:Ontology .

#################################################################
#    Object Properties
#################################################################

tst:hasScale rdf:type owl:ObjectProperty .


#################################################################
#    Data properties
#################################################################

tst:hasScale rdf:type owl:DatatypeProperty .

tst:hasScaleLabel rdf:type owl:DatatypeProperty ;
               rdfs:subPropertyOf tst:hasScale .

tst:hasScaleValue rdf:type owl:DatatypeProperty ;
               rdfs:subPropertyOf tst:hasScale .

#################################################################
#    Classes
#################################################################

tst:Item rdf:type owl:Class ;
      rdfs:subClassOf [ rdf:type owl:Restriction ;
                        owl:onProperty tst:hasScale ;
                        owl:someValuesFrom tst:Scale
                      ] .

tst:Scale rdf:type owl:Class ;
       owl:equivalentClass [ rdf:type owl:Class ;
                             owl:oneOf ( tst:1_One
                                         tst:2_Two
                                         tst:3_Three
                                       )
                           ] .

数据.ttl:

@prefix tst: <http://www2.cs.uregina.ca/~hepting/vocab.owl#> .
@prefix : <http://www2.cs.uregina.ca/~hepting/data.ttl#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix xml: <http://www.w3.org/XML/1998/namespace> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .

tst:1_One a owl:NamedIndividual , tst:Scale ;
      tst:hasScaleValue "1"^^xsd:positiveInteger ;
      tst:hasScaleLabel "One"^^xsd:string .

tst:2_Two a owl:NamedIndividual , tst:Scale ;
      tst:hasScaleValue "2"^^xsd:positiveInteger ;
      tst:hasScaleLabel "Two"^^xsd:string .

tst:3_Three a owl:NamedIndividual , tst:Scale ;
      tst:hasScaleValue "3"^^xsd:positiveInteger ;
      tst:hasScaleLabel "Three"^^xsd:string .

:Item1 a owl:NamedIndividual , tst:Item ;
    tst:hasScale tst:1_One .

:Item2 a owl:NamedIndividual , tst:Item ;
    tst:hasScale tst:2_Two .

:Item3 a owl:NamedIndividual , tst:Item ;
    tst:hasScale tst:3_Three .

:Item4 a owl:NamedIndividual , tst:Item ;
    tst:hasScale tst:3_Three .

:Item5 a owl:NamedIndividual , tst:Item ;
    tst:hasScale tst:3_Three .

:Item6 a owl:NamedIndividual , tst:Item ;
    tst:hasScale tst:3_Three .

:Item7 a owl:NamedIndividual , tst:Item ;
    tst:hasScale tst:3_Three .

目前的安排行之有效。我最初将这些单独的实例(现在在 data.ttl 中)放在 vocab.owl 中:

tst:1_One a owl:NamedIndividual , tst:Scale ;
      tst:hasScaleValue "1"^^xsd:positiveInteger ;
      tst:hasScaleLabel "One"^^xsd:string .

tst:2_Two a owl:NamedIndividual , tst:Scale ;
      tst:hasScaleValue "2"^^xsd:positiveInteger ;
      tst:hasScaleLabel "Two"^^xsd:string .

tst:3_Three a owl:NamedIndividual , tst:Scale ;
      tst:hasScaleValue "3"^^xsd:positiveInteger ;
      tst:hasScaleLabel "Three"^^xsd:string .

它没有用(我错过了什么吗?)。

将以下部分从 vocab.owl 移动到 data.ttl 是否更好?

tst:Scale rdf:type owl:Class ;
       owl:equivalentClass [ rdf:type owl:Class ;
                             owl:oneOf ( tst:1_One
                                         tst:2_Two
                                         tst:3_Three
                                       )
                           ] .

还有其他问题吗?

sparql owl
1个回答
0
投票

使用 SPARQL 查询时,通常认为最好的做法是将本体(模式)与数据分开。这种分离有助于使您的查询更加模块化、可维护和灵活。

以下是在 SPARQL 查询中分离本体和数据的一些最佳实践:

  1. 与数据分开定义本体(模式):本体应该在与数据分开的文件或部分中定义。这将使在不更改数据的情况下更新模式以及跨不同查询重用本体变得更加容易。
  2. 使用前缀来引用本体元素:在查询中引用本体元素时,使用前缀来避免对完整的 URI 进行硬编码。这将使您的查询更具可读性和可维护性。
  3. 使用变量来引用数据元素:在查询中使用变量来引用数据元素,而不是硬编码值。这将使您的查询更加灵活和可重用。
  4. 使用一致的命名约定:对本体元素和数据元素使用一致的命名约定。这将使编写和维护查询变得更加容易,并确保查询适用于不同的数据集。
  5. 使用评论来记录您的查询:使用评论来记录您的查询,包括关于本体和数据源的信息、查询的目的以及任何假设或约束。

通过遵循这些最佳实践,您可以创建更易于维护和更灵活的 SPARQL 查询,这些查询适用于广泛的本体和数据源。

© www.soinside.com 2019 - 2024. All rights reserved.