使用Jena以及自定义函数的SPARQL查询到GraphDB的存储库:

huangapple go评论87阅读模式
英文:

Query to GraphDB's repository with custom functions SPARQL using Jena

问题

我已经使用Jena ARQ实现了一些自定义函数,按照Property Function文档的说明进行了操作。这些函数在使用模型处理本地数据集时有效:

Model model = ModelFactory.createDefaultModel();
model.read(new FileInputStream("data/data.ttl"), null, "TTL");

Query query = QueryFactory.create(queryString); // queryString包含使用Jena定义的自定义属性函数

try (QueryExecution qexec = QueryExecutionFactory.create(query, model)) {
    // ...
}

然而,我需要将这些属性函数应用于我的Graphdb Repository上的查询,因此我尝试使用Jena连接到GraphDB。

我尝试了按照使用GraphDB与Jena的方式,但似乎已经过时,无法实施,因为SailRepository.initialize();已被弃用,而SesameDataset不再存在以初始化我的模型。

import org.eclipse.rdf4j.repository.sail.SailRepository;
import org.eclipse.rdf4j.repository.RepositoryConnection;
import com.ontotext.jena.SesameDataset;

// ...
RepositoryConnection connection = repository.getConnection();

// 最后,创建DatasetGraph实例
SesameDataset dataset = new SesameDataset(connection);

我还尝试了Jena的RDFConnection,但它不适用于我的自定义函数,因为没有模型后端来应用查询。

是否有人可以告诉我在哪里找到SesameDataset进行导入,或者是否有其他方法可以使用自定义函数查询GraphDB的存储库?

英文:

I've implemented some custom functions SPARQL with Jena ARQ following the Property Function docs. Those functions work with a local dataset using a model:

Model model = ModelFactory.createDefaultModel();

model.read(new FileInputStream("data/data.ttl"), null, "TTL");

Query query = QueryFactory.create(queryString) ; // the queryString contains a custom property function defined with Jena

try (QueryExecution qexec = QueryExecutionFactory.create(query, model)) {
		   ...
		  }

However, I need to apply these property functions for queries on a dataset on my Graphdb Repository, so I tried to connect program to GraphDB using Jena.

I've tried following Using GraphDB with Jena but it seems out of date and cannot be implemented because SailRepository.initialize(); was deprecated and SesameDataset doesn't longer exist to initialize my model.

import org.eclipse.rdf4j.repository.sail.SailRepository;
import org.eclipse.rdf4j.repository.RepositoryConnection;
import com.ontotext.jena.SesameDataset;

...
RepositoryConnection connection = repository.getConnection();

// finally, create the DatasetGraph instance
SesameDataset dataset = new SesameDataset(connection);

I've also tried RDFConnection of Jena but it doesn't work with my custom functions since there's no model backend to apply queries.

Can anyone tell me where can I find the SesameDataset to import or whether there is other way to query a GraphDB's repository with custom functions?

答案1

得分: 1

你可以考虑使用RDF4J与GraphDB进行通信,而不是Jena,这会让你的生活变得更加轻松 - 这是GraphDB开发人员推荐的用于此目的的Java API。你可以按照这个教程在RDF4J/GraphDB中实现自定义的SPARQL函数 - 总的来说,就是实现org.eclipse.rdf4j.query.algebra.evaluation.function.Function接口,并确保你的实现位于GraphDB的类路径上并且作为SPI实现进行了注册。

英文:

You would probably make your life a lot easier by using RDF4J to communicate with GraphDB, instead of Jena - it is the recommended Java API for this purpose by the GraphDB developers.

You can implement custom SPARQL functions in RDF4J/GraphDB by following this tutorial - in summary it's a matter of implementing the org.eclipse.rdf4j.query.algebra.evaluation.function.Function interface and making sure your implementation is on the classpath of GraphDB and registered as an SPI implementation.

答案2

得分: 0

Jeen(rdf4j的创建者)指出了您如何创建自定义函数。

然而,正如AndyS(Jena的创建者)所指出的,您似乎需要魔术谓词(自定义属性函数)。为此,我认为您需要创建一个插件并根据此处描述实现模式解释:http://graphdb.ontotext.com/documentation/standard/plug-in-api.html#pattern-interpretation。

大多数插件都是开源的,位于https://github.com/Ontotext-AD,因此您可以看到许多示例。我可以确定的是https://github.com/Ontotext-AD/graphdb-geosparql-plugin 使用了魔术谓词,例如:

  • ?x geo:sfWithin ?y 是一个检查(或返回)所有满足 ?x?y 内的要素的谓词
  • geof:sfWithin(?x,?y)(注意不同的命名空间)是一个函数,用于检查相同的条件,但必须使用绑定的参数进行调用:它无法找到满足条件的要素。

请查看此搜索,然后查看IRI GEO_SF_WITHIN,使用GEO_SF_WITHIN,您会找到com/ontotext/trree/geosparql/GeoSparqlFunction.java 和 com/useekm/geosparql/algebra/PropertyToFunctionOptimizer.java。

英文:

Jeen (the creator of rdf4j) pointed how you can create custom functions.

However, as AndyS (the creator of Jena) points out, you seem to need magic predicates (custom property functions). For that I think that you need to create a plugin and implement pattern interpretation as described here: http://graphdb.ontotext.com/documentation/standard/plug-in-api.html#pattern-interpretation.

Most plugins are open source at https://github.com/Ontotext-AD, so you can see many examples. I know for sure that https://github.com/Ontotext-AD/graphdb-geosparql-plugin uses magic predicates, eg

  • ?x geo:sfWithin ?y is a predicate that checks (or returns) all features where ?x is within ?y
  • geof:sfWithin(?x,?y) (note the different namespace) is a function that checks the same condition, but must be called with bound params: it cannot find features satisfying the condition.

See this search and then see IRI GEO_SF_WITHIN, search with GEO_SF_WITHIN, and you find com/ontotext/trree/geosparql/GeoSparqlFunction.java and com/useekm/geosparql/algebra/PropertyToFunctionOptimizer.java

huangapple
  • 本文由 发表于 2020年8月25日 00:30:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/63565056.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定