我正在研究本体和使用它的链接开放数据集。我在 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 查询时,通常认为最好的做法是将本体(模式)与数据分开。这种分离有助于使您的查询更加模块化、可维护和灵活。
以下是在 SPARQL 查询中分离本体和数据的一些最佳实践:
通过遵循这些最佳实践,您可以创建更易于维护和更灵活的 SPARQL 查询,这些查询适用于广泛的本体和数据源。