From: Glen W. <gle...@bc...> - 2011-11-22 13:48:56
|
Hi Rob, FYI .... the following code is updating the graph as I expect and storing only a single value in the resultant RDF/XML file, i.e. working as I need ...(although I need to now test downstream use of the file ... ) However .... is it efficient ? Is it the best way ? Note I also have to revisit this as it is NOT working for strings, but that may be something special for the strings that are being displayed as other strings have not been classified correctly. I read that the nodes are immutable so I need to retract and assert rather than If aTriple.Object.NodeType = NodeType.Literal Then aTriple.Object.Value = szLiteral Public Sub UpdateRDF(ByVal szKey As String, ByVal szProperty As String, ByVal szLiteral As String) ' v2 : update the graph ' OIMGraph has the graph ' lets try getting the triples with the szKey and szProperty Dim tmpGraph As New Graph Dim tsResults As Triple() Dim aTriple As Triple, tplKey As Triple, tplProperty As Triple Dim szSubject As String, szPredicate As String Dim ndSubject As VDS.RDF.UriNode Dim ndPredicate As VDS.RDF.UriNode Dim szNS As String Dim iPos As Integer szSubject = "" szPredicate = "" For Each aTriple In OIMGraph.Triples If aTriple.Object.ToString = szKey Then tplKey = aTriple szSubject = tplKey.Subject.ToString szPredicate = "" End If If aTriple.Object.ToString = szProperty Then If szSubject = aTriple.Subject.ToString Then tplProperty = aTriple szPredicate = tplProperty.Subject.ToString Else szSubject = "" szPredicate = "" End If End If If szSubject <> "" And szPredicate <> "" Then If aTriple.Object.NodeType = NodeType.Literal Then OIMGraph.Retract(aTriple) Dim nodNew As LiteralNode nodNew = OIMGraph.CreateLiteralNode(szLiteral) Dim tplNew As New Triple(aTriple.Subject, aTriple.Predicate, nodNew) OIMGraph.Assert(tplNew) ' note we have altered the collection so we have to exit !! Exit For End If Debug.Print("<SUBJ:" & aTriple.Subject.ToString & "><PRED:" & aTriple.Predicate.ToString & "><OBJC:" & aTriple.Object.ToString & ">") End If Next End Sub Feedback welcome ... Glen From: Glen Worrall [mailto:gle...@bc...] Sent: 22 November 2011 06:56 To: dot...@li... Subject: Re: [dotNetRDF-Support] DELETE QUERY Thanks Rob, FYI the TripleStore Writers do not work with VB.NET WinForm applications http://www.vbforums.com/showthread.php?t=552398 Therefore ... apologies ... for what is about to follow ... This is meant to delete the original triple ... szSPARQLDelete = szPrefix & "DELETE DATA {" & _ szSubject & " " & szPredicate & " " & szObject & "}" aTripleStore.ExecuteUpdate(szSPARQLDelete) i = InStr(szObject, "^^") szObject = szLiteral '& Mid$(szObject, i) ' .substring seems to return 1 char short 'nObject = OIMGraph.CreateLiteralNode(szObject) 'and replace it with the new value szSPARQLInsert = szPrefix & "INSERT DATA {" & _ szSubject & " " & szPredicate & " " & szObject & "}" 'OIMGraph.Assert(New Triple(nSubject, nPredicate, nObject)) aTripleStore.ExecuteUpdate(szSPARQLInsert) ' TripleStoreWriters all cause an issue with WinForms ' Dim rdfWriter As New TriGWriter ' aTripleStore.SaveToFile("d:\data\testing.trig", rdfWriter) ' Dim rdfWriterX As New TriXWriter ' aTripleStore.SaveToFile("d:\data\testing.trix", rdfWriter) ' Dim rdfWriterN As New NQuadsWriter ' aTripleStore.SaveToFile("d:\data\testing.trin", rdfWriterN) ' so we need to create a graph from the triplestore !! SaveRDF("d:\data\testing.owl", aTripleStore) End If End Sub Public Sub SaveRDF(ByVal szFilename As String, ByVal aTripleStore As TripleStore) ' aTripleStore.SaveToFile(szFilename) ha if only Dim rdfWriter As New RdfXmlWriter Dim sparqlparser As New SparqlQueryParser Dim query As SparqlQuery Dim results As Object query = sparqlparser.ParseFromString("CONSTRUCT { ?s ?p ?o } WHERE {?s ?p ?o}") results = aTripleStore.ExecuteQuery(query) If (results.GetType.ToString = "VDS.RDF.Graph") Then rdfWriter.Save(results, szFilename) End If End Sub All runs .... but ... note the edited triple <ns0:Thing rdf:about="http://example.org/data#GATE_VALVE.DGNEC__1543070000__ECXA__2.DRY _WEIGHT"> <ns63:R10528152386 xmlns:ns63="http://tpl.rdlfacade.org/data#" rdf:resource="http://rdl.rdlfacade.org/data#R39123699723" /> <ns64:R11450580697 xmlns:ns64="http://tpl.rdlfacade.org/data#" rdf:resource="http://example.org/data#GATE_VALVE.DGNEC__1543070000__ECXA__2" /> <ns65:R56806886394 xmlns:ns65="http://tpl.rdlfacade.org/data#" rdf:resource="http://rdl.rdlfacade.org/data#R66709707623" /> <ns66:R65789235245 xmlns:ns66="http://tpl.rdlfacade.org/data#" rdf:datatype="&xsd;float">0</ns66:R65789235245> <rdf:type rdf:resource="http://tpl.rdlfacade.org/data#R33612674560" /> <xsd:R65789235245 rdf:datatype="&xsd;integer">70</xsd:R65789235245> </ns0:Thing> compared to an unedited triple <ns0:Thing rdf:about="http://example.org/data#HORIZONTAL_CENTRIFUGAL_PUMP.DGNEC__151607 0000__ECXA__2.DRY_WEIGHT"> <ns136:R10528152386 xmlns:ns136="http://tpl.rdlfacade.org/data#" rdf:resource="http://rdl.rdlfacade.org/data#R39123699723" /> <ns137:R11450580697 xmlns:ns137="http://tpl.rdlfacade.org/data#" rdf:resource="http://example.org/data#HORIZONTAL_CENTRIFUGAL_PUMP.DGNEC__151 6070000__ECXA__2" /> <ns138:R56806886394 xmlns:ns138="http://tpl.rdlfacade.org/data#" rdf:resource="http://rdl.rdlfacade.org/data#R66709707623" /> <ns139:R65789235245 xmlns:ns139="http://tpl.rdlfacade.org/data#" rdf:datatype="&xsd;float">0</ns139:R65789235245> <rdf:type rdf:resource="http://tpl.rdlfacade.org/data#R33612674560" /> </ns0:Thing> the original value and new value are stored ... Note also the nsxxx peculiarity ... Glen From: Rob Vesse [mailto:rv...@do...] Sent: 21 November 2011 17:40 To: Glen Worrall Subject: RE: DELETE QUERY Hi Glen SPARQL Query and Update are two separate standards and so the library handles them separately. If you want to run updates then you need to use the ExecuteUpdate() method instead. See [1] for alternative ways to apply updates. If you want to create typed literals then you need to use the two argument version of CreateLiteralNode() which takes a URI as the second argument for the datatype. Or if you have .Net primitive types you can use the ToLiteral() extension methods to turn these directly into literals with appropriate types. SparqlGUI was designed as a query only tool, I may add update to it in the future but currently it does not support this. If you have further questions please let me know, Rob Vesse [1]: http://www.dotnetrdf.org/content.asp?pageID=Updating%20with%20SPARQL _____ From: "Glen Worrall" <gle...@bc...> Sent: Monday, November 21, 2011 9:13 AM To: rv...@do... Subject: RE: DELETE QUERY Hi Rob, Just to let you know I have been trying this with other options ... If szSubject <> "" Then szPrefix = "" ' seems that the subject. predicate, object MUST have a qualifier ... !!! iPos = InStr(szSubject, "#") szQualifier = Mid$(szSubject, 1, iPos) szURI = New Uri(szQualifier) szNS = OIMGraph.NamespaceMap.GetPrefix(szURI) If szNS = "" Then szNS = "ns" & gNSCounter.ToString OIMGraph.NamespaceMap.AddNamespace(szNS, szURI) gNSCounter = gNSCounter + 1 End If 'szPrefix = "PREFIX q1: <" & szQualifier & "> " szSubject = szNS & ":" & Mid$(szSubject, iPos + 1) iPos = InStr(szPredicate, "#") szQualifier = Mid$(szPredicate, 1, iPos) szURI = New Uri(szQualifier) szNS = OIMGraph.NamespaceMap.GetPrefix(szURI) If szNS = "" Then szNS = "ns" & gNSCounter.ToString OIMGraph.NamespaceMap.AddNamespace(szNS, szURI) gNSCounter = gNSCounter + 1 End If 'szPrefix = szPrefix & "PREFIX q2: <" & szQualifier & "> " szPredicate = szNS & ":" & Mid$(szPredicate, iPos + 1) i = InStr(szObject, "^^") iPos = InStr(szObject, "#") ' note we cannot get here unless Object is a Literal ... !!! szQualifier = Mid$(szObject, i + 2, iPos - i - 1) ' szPrefix = szPrefix & "PREFIX q3: <" & szQualifier & "> " szURI = New Uri(szQualifier) szNS = OIMGraph.NamespaceMap.GetPrefix(szURI) If szNS = "" Then szNS = "ns" & gNSCounter.ToString OIMGraph.NamespaceMap.AddNamespace(szNS, szURI) gNSCounter = gNSCounter + 1 End If szObject = Mid$(szObject, 1, i - 1) & "^^" & szNS & ":" & Mid$(szObject, (iPos + 1)) nSubject = OIMGraph.CreateUriNode(szSubject) nPredicate = OIMGraph.CreateUriNode(szPredicate) nObject = OIMGraph.CreateLiteralNode(szObject) OIMGraph.Retract(New Triple(nSubject, nPredicate, nObject)) ' the following fails ... seems execute query cannot work with DELETE & INSERT ' szSPARQLDelete = szPrefix & "DELETE DATA WHERE {" & _ ' szSubject & " " & szPredicate & " " & szObject & "}" ' aTripleStore.ExecuteQuery(szSPARQLDelete) i = InStr(szObject, "^^") szObject = szLiteral & Mid$(szObject, i) ' .substring seems to return 1 char short nObject = OIMGraph.CreateLiteralNode(szObject) 'szSPARQLInsert = szPrefix & "INSERT DATA {" & _ ' szSubject & " " & szPredicate & " " & szObject & "}" OIMGraph.Assert(New Triple(nSubject, nPredicate, nObject)) Gets me much closer .... but the RDF file now has double values .... Note an edited value ... <owl:Thing rdf:about="&ex;GATE_VALVE.DGNEC__1543070000__ECXA__2.INSIDE_DIAMETER"> <tpl:R10528152386 rdf:resource="&part4;R34755432270" /> <tpl:R11450580697 rdf:resource="&ex;GATE_VALVE.DGNEC__1543070000__ECXA__2" /> <tpl:R56806886394 rdf:resource="&part4;R37466989245" /> <tpl:R65789235245>100^^xsd:float</tpl:R65789235245> <tpl:R65789235245 rdf:datatype="&xsd;float">0</tpl:R65789235245> <type rdf:resource="&tpl;R33612674560" /> </owl:Thing> Unedited <owl:Thing rdf:about="&ex;GATE_VALVE.DGNEC__1543070000__ECXA__2.INSULATION_THICKNESS"> <tpl:R10528152386 rdf:resource="&part4;R34755432270" /> <tpl:R11450580697 rdf:resource="&ex;GATE_VALVE.DGNEC__1543070000__ECXA__2" /> <tpl:R56806886394 rdf:resource="&part4;R93517282348" /> <tpl:R65789235245 rdf:datatype="&xsd;float">0</tpl:R65789235245> <type rdf:resource="&tpl;R33612674560" /> </owl:Thing> If I remove the "additional" incorrect triple from the property graph ... then <owl:Thing rdf:about="&ex;GATE_VALVE.DGNEC__1543070000__ECXA__2.ELEVATION"> <tpl:R10528152386 rdf:resource="&part4;R82749412674" /> <tpl:R11450580697 rdf:resource="&ex;GATE_VALVE.DGNEC__1543070000__ECXA__2" /> <tpl:R56806886394 rdf:resource="&part4;R60277357355" /> <tpl:R65789235245>1000^^xsd:float</tpl:R65789235245> <type rdf:resource="&tpl;R33612674560" /> </owl:Thing> This prevents my app from working (ie it no longer recognises literal values) Again I am just looking for a "best" practice rather than debugging my examples Thanks Glen _____________________________________________ From: Glen Worrall [mailto:gle...@bc...] Sent: 21 November 2011 09:50 To: 'rv...@do...' Subject: DELETE QUERY Hi Rob, sorry to bother you but wanted to check if the DELETE option in a SPARQL query is working in dotNETRDF ... I have tried various modifications etc to my file and seem to now get the same error in my code as in the SPARQL GUI PREFIX ex: < <http://example.org/data> http://example.org/data#> PREFIX tpl: < <http://tpl.rdlfacade.org/data> http://tpl.rdlfacade.org/data#> PREFIX xmlschema: < <http://www.w3.org/2001/XMLSchema> http://www.w3.org/2001/XMLSchema#> DELETE {ex:GATE_VALVE.DGNEC__1543070000__ECXA__2.DRY_WEIGHT tpl:R65789235245 0^^xmlschema:float} Note the prefixes have been created otherwise I received example.org is not a valid QName exception. Really all I want to do is update the literal in the triple store and then write out the file ... ie open OWL file display literals to user edit literals as required save OWL file to .OWL file Is DELETE supported in TripleStore.ExecuteSQL(query as String) ? I can of course send you the .OWL file I am using for testing Thanks Glen |