我正在努力在 Jena 中执行 SPARQL 查询,导致我不理解的行为......
我正在尝试查询 Esco 本体(https://ec.europa.eu/esco/download),并且我正在使用 TDB 加载本体并创建模型(对不起,如果我使用的术语不准确,我不是很有经验)。
我的目标是在本体中找到与我之前提取的文本匹配的工作职位 uri:ex: 提取的术语:“ acuponcteur ” -> 本体中的标签:“Acuponcteur”@fr -> uri: < http:// ec.europa.eu/esco/occupation/14918 >
我所说的“奇怪的行为”与我在执行查询时得到(或没有)的结果有关,即:
执行以下查询时:
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX esco: <http://ec.europa.eu/esco/model#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT ?position
WHERE {
?s rdf:type esco:Occupation.
{ ?position skos:prefLabel ?label. }
UNION
{ ?position skos:altLabel ?label. }
FILTER (lcase(?label)= \"acuponcteur\"@fr )
}
LIMIT 10
我在 1 分钟后得到这些结果:
-----------------------------------------------
| position |
===============================================
| <http://ec.europa.eu/esco/occupation/14918> |
| <http://ec.europa.eu/esco/occupation/14918> |
| <http://ec.europa.eu/esco/occupation/14918> |
| <http://ec.europa.eu/esco/occupation/14918> |
| <http://ec.europa.eu/esco/occupation/14918> |
| <http://ec.europa.eu/esco/occupation/14918> |
| <http://ec.europa.eu/esco/occupation/14918> |
| <http://ec.europa.eu/esco/occupation/14918> |
| <http://ec.europa.eu/esco/occupation/14918> |
| <http://ec.europa.eu/esco/occupation/14918> |
-----------------------------------------------
但是,当我尝试添加 DISTINCT 关键字时,因此:
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX esco: <http://ec.europa.eu/esco/model#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT DISTINCT ?position
WHERE {
?s rdf:type esco:Occupation.
{ ?position skos:prefLabel ?label. }
UNION
{ ?position skos:altLabel ?label. }
FILTER (lcase(?label)= \"acuponcteur\"@fr )
}
LIMIT 10
似乎查询一直在运行(我在等待 20 分钟后停止了执行......)
在执行与第一个查询相同的查询时(因此没有 DISTINCT),我得到相同的行为,但要匹配另一个标签,我确定该标签不在本体中。虽然期待空结果,但它(看起来像它)继续运行,我必须在一段时间后杀死它(再一次,我最多等了 20 分钟):
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX esco: <http://ec.europa.eu/esco/model#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT ?position
WHERE {
?s rdf:type esco:Occupation.
{ ?position skos:prefLabel ?label. }
UNION
{ ?position skos:altLabel ?label. }
FILTER (lcase(?label)= \"assistante scolaire\"@fr )
}
LIMIT 10
这可能是我正在运行的代码中的问题吗?它是:
public static void main(String[] args) {
// Make a TDB-backed dataset
String directory = "data/testtdb" ;
Dataset dataset = TDBFactory.createDataset(directory) ;
// transaction (protects a TDB dataset against data corruption, unexpected process termination and system crashes)
dataset.begin( ReadWrite.WRITE );
// assume we want the default model, or we could get a named model here
Model model = dataset.getDefaultModel();
try {
// read the input file - only needs to be done once
String source = "data/esco.rdf";
FileManager.get().readModel(model, source, "RDF/XML-ABBREV");
// run a query
String queryString =
"PREFIX skos: <http://www.w3.org/2004/02/skos/core#> " +
"PREFIX esco: <http://ec.europa.eu/esco/model#> " +
"PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> " +
"SELECT ?position " +
"WHERE { " +
" ?s rdf:type esco:Occupation. " +
" { ?position skos:prefLabel ?label. } " +
" UNION " +
" { ?position skos:altLabel ?label. }" +
" FILTER (lcase(?label)= \"acuponcteur\"@fr ) " +
"}" +
"LIMIT 1 " ;
Query query = QueryFactory.create(queryString) ;
// execute the query
QueryExecution qexec = QueryExecutionFactory.create(query, model) ;
try {
ResultSet results = qexec.execSelect() ;
// taken from apache Jena tutorial
ResultSetFormatter.out(System.out, results, query) ;
} finally {
qexec.close() ;
}
} finally {
model.close() ;
dataset.end();
}
}
我在这里做错了什么?任何想法?
谢谢!