我想要填写一些SPARQL查询模板。因此,我有替换的映射。现在我想从替换中创建所有可能的组合。我想出了以下“算法”(下面是实际代码的链接!):
for(replacement in replacements){
//replacement == "<NNP1>" -> "<http://dbpedia.org/resource/Nile>"
for(template in templates){
//template == SELECT DISTINCT ?uri WHERE { <^NNP4^> <^NN3^> ?x . <^NN2^> <^NN1^> ?uri }
for(argument in template){
//an argument is here everything inside the SPARL triple
// "<^NNP4^> <^NN3^> ?x" has 3 arguments: <^NNP4^>, <^NN3^> and ?x
//Note: There is definitely something wrong with the following if conditions!
if(argument==replacement AND position isnt replaced before){
//replacement.key == <^NNP1^>
//replacement.value == <http://dbpedia.org/resource/Nile>
template.replaceOnce(replacement.key, replacement.value)
//In the triple "<^NNP4^> <^NN3^> ?x"
//<^NNP4^> would be position 0, <^NN3^> position 1 and ?x position 2
save_position()
}
}
}
}
创造了这个:
SELECT DISTINCT ?uri WHERE { <http://dbpedia.org/resource/Nile> <^NN3^> ?x . <http://dbpedia.org/resource/Nile> <http://dbpedia.org/ontology/start> ?uri }
SELECT DISTINCT ?uri WHERE { <http://dbpedia.org/resource/Country> <^NN3^> ?x . <http://dbpedia.org/resource/Country> <http://dbpedia.org/ontology/cityType> ?uri }
SELECT DISTINCT ?uri WHERE { <^NNP4^> <^NN3^> ?x . <^NN2^> <http://dbpedia.org/ontology/start> ?uri }
SELECT DISTINCT ?uri WHERE { <^NNP4^> <^NN3^> ?x . <^NN2^> <http://dbpedia.org/ontology/cityType> ?uri }
但它应该是这样的:
SELECT DISTINCT ?uri WHERE { <http://dbpedia.org/resource/Nile> <http://dbpedia.org/ontology/start> ?x . <http://dbpedia.org/resource/Country> <http://dbpedia.org/ontology/cityType> ?uri }
SELECT DISTINCT ?uri WHERE { <http://dbpedia.org/resource/Country> <http://dbpedia.org/ontology/cityType> ?x . <http://dbpedia.org/resource/Nile> <http://dbpedia.org/ontology/start> ?uri }
SELECT DISTINCT ?uri WHERE { <http://dbpedia.org/resource/Nile> <http://dbpedia.org/ontology/cityType> ?x . <http://dbpedia.org/resource/Country> <http://dbpedia.org/ontology/start> ?uri }
SELECT DISTINCT ?uri WHERE { <http://dbpedia.org/resource/Country> <http://dbpedia.org/ontology/start> ?x . <http://dbpedia.org/resource/Nile> <http://dbpedia.org/ontology/cityType> ?uri }
我为您创建了一个测试用例,您可以自行尝试:https://www.jdoodle.com/a/arx
我知道if(argument==replacement AND position isnt replaced before)
的部分不正确,因为正如您在预期结果中看到的那样,一些替换可以在同一个位置多次完成。例如:
SELECT DISTINCT ?uri WHERE { <http://dbpedia.org/resource/Nile> <http://dbpedia.org/ontology/start> ?x . <http://dbpedia.org/resource/Country> <http://dbpedia.org/ontology/cityType> ?uri }
SELECT DISTINCT ?uri WHERE { <http://dbpedia.org/resource/Nile> <http://dbpedia.org/ontology/cityType> ?x . <http://dbpedia.org/resource/Country> <http://dbpedia.org/ontology/start> ?uri }
两者在同一位置包含相同的替换(<http://dbpedia.org/resource/Nile>
)。
有人能给我一个暗示正确的解决方案可能是什么样子,或者可能为我提供更好的方法吗?
注意:生成的SPARQL查询只是示例,它们没有意义!