0

在 rdf4j 中是否可以通过它们来过滤模型rdf:id?我已经尝试了以下方法:

model.filter(res, null, null)

rdf:resource但是有了这个,我也得到了and的所有出现rdf:about。目前,我首先过滤所有出现的所需类型(返回一个模型)的整个模型。然后我为资源过滤此模型,并使用此资源过滤整个模型以获得所需的模型部分:

Model typeModel = model.filter(null, RDF.TYPE, iri);
// the following obj contains only the id (found in an rdf:about or rdf:resource)
// normally I also do some checks before .iterator().next()
Resource res = typeModel.filter((Resource) obj, null, null).subjects().iterator().next();
Model resModel = model.filter(res, null, null);

我认为我的解决方案会产生太多开销,因为我还需要typeModel为每个类型创建一个。是否有另一种过滤模型的方法rdf:id

更新:

这是一个简短的示例:我需要ACLineSegment借助rdf:resource.Terminal.ConductingEquipment

<cim:Terminal rdf:ID="_8fd6a918-5a8d-42f2-ae19-3ee77bc76911">
  <cim:ACDCTerminal.sequenceNumber>2</cim:ACDCTerminal.sequenceNumber>
  <cim:IdentifiedObject.name>XXXX</cim:IdentifiedObject.name>
  <cim:Terminal.ConductingEquipment rdf:resource="#_50c99578-6e17-45e1-a113-a4a28d643b40" />
  <cim:Terminal.ConnectivityNode rdf:resource="#_eefd8021-6f56-4154-9b2b-9e275c0f43d0" />
  <cim:Terminal.phases rdf:resource="http://iec.ch/TC57/2013/CIM-schema-cim16#PhaseCode.ABC" />
</cim:Terminal>
<cim:ACLineSegment rdf:ID="_50c99578-6e17-45e1-a113-a4a28d643b40">
  <cim:ACLineSegment.b0ch>5.44828e-5</cim:ACLineSegment.b0ch>
  <cim:ACLineSegment.bch>5.44828e-5</cim:ACLineSegment.bch>
  ....
</cim:ACLineSegment>
4

1 回答 1

2

您永远不应该使用 RDF/XML 语法阅读您的 RDF 文档——正如您已经认识到的那样,这并不是真正的人类可读的并且被设计为机器的交换格式。RDF 数据集包含一组三元组,查看这些三元组的好格式是 N-Triples 或 Turtle。

我将您的数据转换为 N-Triples(我假设http://example.org/是前缀的命名空间cimhttp://example.org/data作为基本 URI):

<http://example.org/data#_8fd6a918-5a8d-42f2-ae19-3ee77bc76911> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.org/Terminal> .
<http://example.org/data#_8fd6a918-5a8d-42f2-ae19-3ee77bc76911> <http://example.org/ACDCTerminal.sequenceNumber> "2" .
<http://example.org/data#_8fd6a918-5a8d-42f2-ae19-3ee77bc76911> <http://example.org/IdentifiedObject.name> "XXXX" .
<http://example.org/data#_8fd6a918-5a8d-42f2-ae19-3ee77bc76911> <http://example.org/Terminal.ConductingEquipment> <http://example.org/data#_50c99578-6e17-45e1-a113-a4a28d643b40> .
<http://example.org/data#_8fd6a918-5a8d-42f2-ae19-3ee77bc76911> <http://example.org/Terminal.ConnectivityNode> <http://example.org/data#_eefd8021-6f56-4154-9b2b-9e275c0f43d0> .
<http://example.org/data#_8fd6a918-5a8d-42f2-ae19-3ee77bc76911> <http://example.org/Terminal.phases> <http://iec.ch/TC57/2013/CIM-schema-cim16#PhaseCode.ABC> .
<http://example.org/data#_50c99578-6e17-45e1-a113-a4a28d643b40> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.org/ACLineSegment> .
<http://example.org/data#_50c99578-6e17-45e1-a113-a4a28d643b40> <http://example.org/ACLineSegment.b0ch> "5.44828e-5" .
<http://example.org/data#_50c99578-6e17-45e1-a113-a4a28d643b40> <http://example.org/ACLineSegment.bch> "5.44828e-5" .

可以看到,实际上有 9 个 RDF 三元组。

你的任务是

ACLineSegment在的帮助rdf:resource下找到Terminal.ConductingEquipment

这个公式听起来超级人为,不是人类在查询数据时会说的。我试图翻译它(我不习惯数据的域):

给定Terminal, 给我它的Terminal.ConductingEquipment元素

那么到目前为止我们有什么?

  1. 我们有终端,即我们有http://example.org/data#_8fd6a918-5a8d-42f2-ae19-3ee77bc76911.
  2. 我们也知道你想要获取对象的谓词,它是cim:ACLineSegment

这是什么意思?我们有一个 RDF 三元组的主语和谓语,并且想要得到它的宾语。现在,您应该已经知道解决方案了,只需按主题和谓词过滤模型,即

ValueFactory vf = SimpleValueFactory.getInstance();

// the subject which is the IRI of the terminal
IRI s = vf.createIRI("**http://example.org/data#_8fd6a918-5a8d-42f2-ae19-3ee77bc76911**");

// the predicate which is the IRI of the property cim:Terminal.ConductingEquipment
IRI p = vf.createIRI("http://example.org/Terminal.ConductingEquipment");

// filter by subject and predicate
Model filteredModel = model.filter(s, p, null);

// get the object, if one exists
Resource acLineSegment = Models.objectResource(filteredModel).orElse(null);

不要忘记,我假设http://example.org/data它是 RDF/XML 文档的基本 URI 和http://example.org/前缀的命名空间cim——您应该在上面的代码中用正确的值替换它。

于 2017-09-01T12:54:07.673 回答