0

我正在尝试使用 dotNetRDF 修改 Rdf 节点,然后将其保存在一个新文件中,但我得到的是相同的文件!

我想将 Identification/12 更改为 Identification/18。

模板文件:

@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>.
@prefix owl:  <http://www.w3.org/2002/07/owl#>.
@prefix xsd:  <http://www.w3.org/2001/XMLSchema#>.
@prefix qudt: <http://qudt.org/schema/qudt#>.
@prefix qudt-unit: <http://qudt.org/vocab/unit#>.
@prefix knr: <http://kurl.org/NET/knr#>.

@prefix keak: <http://kurl.org/NET/keak#>.
@prefix keak-time: <http://kurl.org/NET/keak/time#>.
@prefix keak-eval: <http://kurl.org/NET/keak/eval#>.
@prefix keak-quantity: <http://kurl.org/NET/keak/quantity#>.
@prefix keak-ev: <http://kurl.org/NET/keak/ev#>.

@base <http://data.info/keak/knr/>.

<Identification/12> a keak-ev:Identification.

<Quantity/45> a qudt:Quantity ;
  qudt:quantityType keak-quantity:ElectricConsumption .

VB.NET 代码:

Dim gKnr As IGraph = New Graph()
Dim ttlParser As TurtleParser = New TurtleParser()

'Load the file template
ttlParser.Load(gKnr, PATH_TEMPLATE)
gKnr.BaseUri = New Uri(keak_BASE_URI_Knr)

Dim oNode As INode = gKnr.CreateUriNode(New Uri("http://kurl.org/NET/keak/ev#Identification"))

'retrieve the item
Dim listRes As List(Of Triple) = gKnr.GetTriplesWithObject(oNode)
'?s = http://data.info/keak/Knr/Identification/12 , 
'?p = http://www.w3.org/1999/02/22-rdf-syntax-ns#type , 
'?o = http://kurl.org/NET/keak/ev#Identification

'modify the item
Dim tIdentification As Triple
If listRes.Count = 1 Then
    tIdentification = listRes(0)
    tIdentification.Subject.GraphUri = New Uri("http://data.info/kseak/knr/Identification/18")

End If

gKnr.Assert(tIdentification)

' Serialisation and Save
Dim ttlWriter As New CompressingTurtleWriter()
ttlWriter.DefaultNamespaces = gKnr.NamespaceMap
ttlWriter.Save(gKnr, PATH_NEW_FILE)
4

2 回答 2

1

这是行不通的,GraphUri它是一个属性INode,指示节点来自哪个图,与节点的实际 URI 无关

无论如何INode都是不可变的,并且您无法像尝试那样更改节点的 URI。

如果您希望更改 RDF 图中的 URI,那么您需要使用该 URI 的Retract()所有三元组,并使用新的 URI 和Assert()它们创建新的三元组。

下面的例子可能在语法上是不正确的 VB,但希望它能给你一个大致的想法:

Dim listRes As List(Of Triple) = gKnr.GetTriplesWithObject(oNode).ToList()

For Each origTriple in listRes
  gKnr.Retract(origTriple)
  Dim newTriple as Triple
  newTriple = new Triple(New Uri("http://data.info/kseak/knr/Identification/18"), origTriple.Predicate, origTriple.Object)
  gKnr.Assert(newTriple)
Next

当然,如果您要更改的 URI 不仅仅出现在主题位置,那么您将需要适当地更改逻辑

于 2015-10-22T09:47:40.503 回答
0

谢谢 robV,它帮了我很多,我从视觉上得到了一个异常,但我设法纠正了它,这是最终的代码:

    For Each origTriple In listRes
        gKnr.Retract(origTriple)
        Dim sNode As INode = gKnr.CreateUriNode(UriFactory.Create("http://data.info/keask/Knr/Identification/18"))
        Dim newTriple As Triple
        newTriple = New Triple(sNode, origTriple.Predicate, origTriple.Object)
        gKnr.Assert(newTriple)
    Next
于 2015-10-22T12:38:55.607 回答