2015-10-20 153 views
0

我想修改一個使用dotNetRDF的Rdf節點,然後將它保存在一個新文件中,但我得到的是同一個文件!替換節點DotnetRDF

我想將標識/ 12更改爲標識/ 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) 

回答

1

這是行不通的,GraphUriINode一個屬性,指示其繪製一個節點來自並且與節點的實際URI無關

無論如何INode是不可變的,你不能像你試圖做的那樣改變節點的URI。

如果您希望更改RDF圖中的URI,那麼您需要使用該URI的所有三元組,並使用新的URI和Assert()創建新的三元組。

下面的例子可能是synactically不正確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不僅僅是主體地位更加那麼你就需要發生改變邏輯適當

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