如何匹配CIM / RDF中的JAXB元素? [英] How match JAXB elements in CIM/RDF?

查看:96
本文介绍了如何匹配CIM / RDF中的JAXB元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试根据IEC 61970(通用信息模型,电力系统模型)从CIM / XML文件加载模型,我发现了一个问题;
根据JAXB,元素之间的图形由@XmlREF @XmlID提供,这两者应该等于匹配。但是在CIM / RDF中,通过ID引用资源,即rdf:resource =#_ 37C0E103000D40CD812C47572C31C0AD包含#字符,因此当在rdf中JAXB无法匹配GeographicalRegion与SubGeographicalRegion.Region :资源属性存在#字符。

Trying to load a model from a CIM/XML file acording to IEC 61970 (Common Information Model, for power systems models), I found a problem; According JAXB´s graphs between elements are provided by @XmlREF @XmlID and these both should be equals to match. But in CIM/RDF the references to a resource through an ID, i.e. rdf:resource="#_37C0E103000D40CD812C47572C31C0AD" contain the "#" character, consequently JAXB is unable to match "GeographicalRegion" vs. "SubGeographicalRegion.Region" when in the rdf:resource atribute the "#" character is present.

这里有一个例子:

<cim:GeographicalRegion rdf:ID="_37C0E103000D40CD812C47572C31C0AD">
<cim:IdentifiedObject.name>GeoRegion</cim:IdentifiedObject.name>
<cim:IdentifiedObject.localName>OpenCIM3bus</cim:IdentifiedObject.localName>
</cim:GeographicalRegion>
<cim:SubGeographicalRegion rdf:ID="_ID_SubGeographicalRegion">
<cim:IdentifiedObject.name>SubRegion</cim:IdentifiedObject.name>
<cim:IdentifiedObject.localName>SubRegion</cim:IdentifiedObject.localName>
<cim:SubGeographicalRegion.Region rdf:resource="#_37C0E103000D40CD812C47572C31C0AD"/>
</cim:SubGeographicalRegion>


推荐答案

我意识到你要求使用JAXB解决方案,但我建议你考虑一个基于RDF的解决方案,因为它更灵活,更健壮。你基本上是在尝试重新构建已经内置的RDF解析器.RDF / XML是一种难以解析的格式,试图破解你自己的解析没有多大意义 - 特别是因为具有非常不同的XML结构的文件可以表达完全相同的信息:只有在查看RDF的级别时才会显而易见。您可能会发现您的JAXB解析器解决方法适用于一个CIM / RDF文件,但在另一个CIM / RDF文件上完全失败。

I realize you're asking for a solution using JAXB, but I would urge you to consider an RDF-based solution as it is more flexible and robust. You're basically trying to reinvent what RDF parsers already have built in. RDF/XML is a difficult format to parse, it doesn't make much sense to try and hack your own parsing together - especially since files that have very different XML structures can express exactly the same information: this only becomes apparent when looking at the level of the RDF. You may find that your JAXB parser workaround works on one CIM/RDF file but completely fails on another.

所以,这是一个如何使用Sesame RDF API处理文件的示例。不涉及推理,这只是解析文件并将其放入内存中的RDF模型,然后您可以从任何角度进行操作和查询。

So, here's an example of how to process your file using the Sesame RDF API. No inferencing is involved, this just parses the file and puts it in an in-memory RDF model, which you can then manipulate and query from any angle.

假设您的CIM文件的根元素如下所示:

Assuming the root element of your CIM file looks something like this:

<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" 
         xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" 
         xmlns:cim="http://example.org/cim/">

(当然只是猜测,但我需要前缀作为正确的例子)

(only a guess of course, but I need prefixes for a proper example)

然后你可以使用Sesame的Rio RDF / XML解析器执行以下操作:

Then you can do the following, using Sesame's Rio RDF/XML parser:

 String baseURI = "http://example.org/my/file";
 FileInputStream in = new FileInputStream("/path/to/my/cim.rdf"); 
 Model model = Rio.parse(in, baseURI, RDFFormat.RDFXML);

这会为您的文档创建内存中的RDF模型。然后,您可以简单地对其进行过滤查询。例如,要打印出 _37C0E103000D40CD812C47572C31C0AD 所有资源的属性作为 SubGeographicalRegion.Region

This creates an in-memory RDF model of your document. You can then simply filter-query over that. For example, to print out the properties of all resources that have _37C0E103000D40CD812C47572C31C0AD as their SubGeographicalRegion.Region:

 String CIM_NS = "http://example.org/cim/";
 ValueFactory vf = ValueFactoryImpl.getInstance();
 URI subRegion = vf.createURI(CIM_NS, "SubGeographicalRegion.Region");
 URI res = vf.createURI("http://example.org/my/file#_37C0E103000D40CD812C47572C31C0AD");
 Set<Resource> subs = model.filter(null, subRegion, res).subjects();

 for (Resource sub: subs) {
     System.out.println("resource: " + sub + " has the following properties: ");
     for (URI prop: model.filter(sub, null, null).predicates()) {
          System.out.println(prop + ": " + model.filter(sub, prop, null).objectValue());
     }
 } 

当然此时你也可以选择转换将模型转换为其他语法格式,以便您的应用程序进一步处理 - 您认为合适。关键是RDF / XML解析器已经为您解决了带有前导的标识符之间的区别。

Of course at this point you can also choose to convert the model to some other syntax format for further handling by your application - as you see fit. The point is that the difference between the identifiers with the leading # and without has been resolved for you by the RDF/XML parser.

这当然只是个人意见,因为我不知道你的用例的细节,但我想你会发现这很快和灵活。我还应该指出,虽然上述解决方案将整个模型保留在内存中,但如果您发现文件太大,您可以轻松地将其调整为更流式(因此内存密集程度更低)的方法。

This is of course personal opinion only, since I don't know the details of your use case, but I think you'll find that this is quite quick and flexible. I should also point out that although the above solution keeps the entire model in memory, you can easily adapt this to a more streaming (and therefore less memory-intensive) approach if you find your files are too big.

这篇关于如何匹配CIM / RDF中的JAXB元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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