From: Glen W. <gle...@bc...> - 2011-11-22 06:56:12
|
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 |
From: Rob V. <rv...@do...> - 2011-11-22 17:19:01
|
Hi Glen Re: Triple Store writers, yes some of them are designed to use multiple threads by default, some (but not all currently) allow you to switch them into single threaded mode via the IMultiThreadedWriter interface. I've raised an issue in our tracker (CORE-147) to have all triple store writers support single threaded writing for the next release [1] Wrt to your code example I would make the following comments: Iterating over the entire graph is not the best option especially as the graph gets larger. You should really generate nodes for the parts of the triples you are trying to match and then use one of the GetTriplesWithXY() methods. For most graph implementations these lookups are indexed and so will be significantly faster. If you know you are going to retract the selected triples you can even do something like g.Retract(g.GetTriplesWithXY().ToList()); Note the use of ToList() to avoid getting error messages about modifying a collection. You should compare nodes by value using the Equals() method not by comparing strings, the string forms of nodes are subject to collisions and won't properly account for the RDF rules on term equality e.g. INode uri = g.CreateUriNode(new Uri("http://example.org")); INode lit = g.CreateLiteralNode("http://example.org")); Console.WriteLine(uri.ToString().Equals(lit.ToString())); //Prints true which is incorrect Console.WriteLine(uri.Equals(lit)); //Prints false which is correct Also particularly when you get into URIs there are normalization and other concerns, e.g. http://example.org:80 would be considered equal to http://example.org via Equals() but not if you do string equality they will not be considered equal. As far as the RDF/XML output goes the nsXXX is the writer doing its job, if there is no suitable namespace defined in the graphs namespace map the writer must invent prefixes in order to serialize things, this has to follow XML namespace scoping rules so it may end up generating a ton of prefixes for the same namespace. This is in part due to the fact that the writer is designed to be a single pass writer (as far as possible), in order to determine that there is a frequently used undeclared namespace we'd have to do an extra pass over the graph which would be very costly for large graphs. You can avoid this behavior by declaring any required namespaces explicitly, if the graph is loaded from a file then any namespaces found there should be present in the resulting graph. Regards, Rob [1]: http://www.dotnetrdf.org/tracker/Issues/IssueDetail.aspx?id=147 ---------------------------------------- From: "Glen Worrall" <gle...@bc...> Sent: Tuesday, November 22, 2011 5:49 AM To: "dotNetRDF User Help and Support" <dot...@li...> Subject: Re: [dotNetRDF-Support] DELETE QUERY 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#> PREFIX tpl: <http://tpl.rdlfacade.org/data#> PREFIX 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 |
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 |