用于将字符串消息转换为Jena模型的Java代码 [英] Java code for Converting string message into Jena model

查看:97
本文介绍了用于将字符串消息转换为Jena模型的Java代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个来自AMQP消息代理的字符串消息,该消息由RDF语句组成.我想使用Java将其转换为Jena模型,然后将转换后的模型与另一个模型合并为单个模型.我该怎么办?

I have a string message from an AMQP message broker that consists of RDF statements. I want to covert it into a Jena model using Java and then merge the converted model with another one into single model. How can I do this?

推荐答案

这可以分为三个逻辑步骤.其中一些,您可能已经完成:

This can be divided into three logical steps. Some of these, you may have already done:

  1. 从非rdf文本中隔离RDF语句
  2. 确定用于RDF的语法(可以是#1的一部分)
  3. 将结果字符串解析为Apache Jena Model.

由于前两个是特定于域的,除非您可以提供一些示例输入,否则您在这里可能不会找到太多帮助.此外,这可能会被视为一个单独的问题(例如:如何基于RDF语法的存在来对字符串进行分区?")

As the first two are domain-specific, you will not likely find much help here unless you can provide some example input. Additionally, that would likely be considered a separate question (eg: 'how can I partition a string based on presence of RDF syntax?')

对于第三个而言,它非常快速且容易实现.让我们假设您有一个N-Triples格式的文档,该文档是从其余文本中提取的.以下JUnit测试演示了解析该内容并与其内容进行交互的能力.

For the third, it's extremely quick and easy to do. Let us assume that you have a document in the N-Triples Format that you have extracted from the rest of your text. The following JUnit test demonstrates the ability to parse that and interact with its content.

final String nTriplesDoc = "<urn:ex:s> <urn:ex:p> <urn:ex:o> . ";

final Model model = ModelFactory.createDefaultModel();
try( final InputStream in = new ByteArrayInputStream(nTriplesDoc.getBytes("UTF-8")) ) {
    /* Naturally, you'd substitute the syntax of your actual
     * content here rather than use N-TRIPLE.
     */
    model.read(in, null, "N-TRIPLE");
}

final Resource s = ResourceFactory.createResource("urn:ex:s");
final Property p = ResourceFactory.createProperty("urn:ex:p");
final Resource o = ResourceFactory.createResource("urn:ex:o");
assertTrue( model.contains(s,p,o) );

编辑/第二部分

在看到您的评论后,我觉得最好添加有关模型合并的另一条注释.在Jena中,您可以通过将所有三元组加在一起来合并模型.这样会创建一些剩余模型,最终被垃圾回收,这不一定是您系统的最佳方案.

After seeing your comment, I felt it prudent to add another note regarding the merging of models. In Jena, you can merge models by adding all of their triples together. This can create some leftover models that end up being garbage collected, which may not necessarily be the best scenario for your system.

final Model model0 = ModelFactory.create();
// Add things to model 0...
final Model model1 = ModelFactory.create();
// Add things to model 1...

final Model merger = ModelFactory.create();
merger.add(model0);
merger.add(model1);

在此示例中,现在需要收集两个源模型.使用现有模型之一作为所有三元组的目的地,或者创建两个基础图的并集模型(会导致两个图都被重用)是明智的:

In this example, the two source models now need collecting. It would be wise to either use one of the existing models as the destination for all the triples, or to create a model of the union of the two underlying graphs (resulting in both graphs being re-used):

final Model union = ModelFactory.createModelForGraph( new Union(model0.getGraph(), model1.getGraph()) );

我还保留了一个假设,即您能够从AMQP服务器获取String对象,因此,答案前半部分的#3仍然与您的情况相关.

I also leave an assumption in place that you are able to get String objects from your AMQP server, so that #3 from the first half of this answer is still relevant to your scenario.

这篇关于用于将字符串消息转换为Jena模型的Java代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆