首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >可以检索RDF4J事务的update语句吗?

可以检索RDF4J事务的update语句吗?
EN

Stack Overflow用户
提问于 2021-07-09 02:27:45
回答 1查看 70关注 0票数 1

我正在尝试使用RDF4J支持SPARQL update查询的“预演”功能。我想通知用户将要插入/删除的语句,理想情况下是通过从当前事务中获取语句。我的想法是这样的:

代码语言:javascript
复制
conn.begin();
Update query = conn.prepareUpdate(queryString);
query.execute();
// Print statements in the transaction
System.out.println("Dry run completed.");
conn.rollback();
System.out.println("Dry run rolled back.");

有没有办法用RDF4J做到这一点?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-07-10 08:45:53

(从https://github.com/eclipse/rdf4j/discussions/3163复制)

您可以使用SailConnectionListener按照这些思路做一些事情。然而,访问它的方法有点笨拙。下面是一个例子:

代码语言:javascript
复制
Repository rep = new SailRepository(new MemoryStore());
try (SailRepositoryConnection conn = (SailRepositoryConnection) rep.getConnection()) {
    NotifyingSailConnection sailConn = (NotifyingSailConnection) conn.getSailConnection();
    sailConn.addConnectionListener(new SailConnectionListener() {

        @Override
        public void statementRemoved(Statement removed) {
            System.out.println("removed: " + removed);
        }

        @Override
        public void statementAdded(Statement added) {
            System.out.println("added: " + added);
        }
    });

    conn.begin();
    conn.add(FOAF.PERSON, RDF.TYPE, RDFS.CLASS);
    String update = "DELETE { ?p a rdfs:Class } INSERT { ?p rdfs:label \"Person\" } WHERE { ?p a rdfs:Class }";
    conn.prepareUpdate(update).execute();
    System.out.println("executed");
    conn.rollback();
    System.out.println("transaction aborted");
}

正如您所看到的,我们需要将RepositoryConnection转换为特定类型以检索底层SailConnection,然后我们还需要将该SailConnection转换为NotifyingSailConnection,以便能够在其上注册SailConnectionListener。此侦听器将接收单个语句的pre-commit、and和removed事件。运行上述代码将产生以下控制台输出:

代码语言:javascript
复制
added: (http://xmlns.com/foaf/0.1/Person, http://www.w3.org/1999/02/22-rdf-syntax-ns#type, http://www.w3.org/2000/01/rdf-schema#Class)
removed: (http://xmlns.com/foaf/0.1/Person, http://www.w3.org/1999/02/22-rdf-syntax-ns#type, http://www.w3.org/2000/01/rdf-schema#Class) [null]
added: (http://xmlns.com/foaf/0.1/Person, http://www.w3.org/2000/01/rdf-schema#label, "Person")
executed
transaction aborted
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68306956

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档