使用dotNetRDF删除RDF元组

时间:2013-08-15 17:13:46

标签: rdf sparql dotnetrdf

我想使用dotNetRDF删除RDF元组。这是我的RDF文件:

<rdf:RDF xml:base="http://www.example.org/destDetails#" 
         xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" 
         xmlns:xsd="http://www.w3.org/2001/XMLSchema#" 
         xmlns:ns0="http://www.example.org/destDetails#" 
         xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
  <rdf:Description rdf:about="&ns0;0165a659-54ea-4e80-bee7-9d3951d47ae3">
    <ns0:ID>0165a659-54ea-4e80-bee7-9d3951d47ae3</ns0:ID>
    <ns0:destination rdf:resource="&ns0;VELES" />
    <ns0:distrName>Test Test</ns0:distrName>
    <ns0:hasTimeStart>17:00</ns0:hasTimeStart>
    <ns0:hasTimeStop>17:55</ns0:hasTimeStop>
    <ns0:moneyOneDir>130 den.</ns0:moneyOneDir>
    <ns0:moneyTwoDir>---</ns0:moneyTwoDir>
  </rdf:Description>
</rdf:RDF>

以下是我正在使用的代码:

        TripleStore magacinTorki = new TripleStore();
        //kreiranje na graf
        Graph rdf = new Graph();
        // Create a dataset and use the named graph as the default graph
        FileLoader.Load(rdf, rdfDatoteka, new RdfXmlParser());
        rdf.BaseUri = new Uri("http://www.example.org/destDetails"); // Remove the name from the graph
        // If the graph has no name it is added as the default graph
        magacinTorki.Add(rdf);
        SparqlUpdateParser parser = new SparqlUpdateParser();
        SparqlParameterizedString cmdString = new SparqlParameterizedString();

        cmdString.CommandText = @"PREFIX  ns0: <http://www.example.org/destDetails#>"
            + " PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>"
            + " DELETE "
            + " WHERE  {"
            + " GRAPH @graph {  ?dest ?p ?o"
            + " ?dest ns0:nodeID @destID} }";

        cmdString.SetUri("graph",rdf.BaseUri);
        cmdString.SetLiteral("destID",destID);
        SparqlUpdateCommandSet cmds = parser.ParseFromString(cmdString);
        LeviathanUpdateProcessor processor = new LeviathanUpdateProcessor(magacinTorki);
        processor.ProcessCommandSet(cmds);
        rdf.SaveToFile(rdfDatoteka);

然而,RDF文件没有发生任何事情。


此代码对我来说很好,因为我没有被要求使用SPARQL删除三元组

        Graph rdf = new Graph();
        // Create a dataset and use the named graph as the default graph
        FileLoader.Load(rdf, rdfDatoteka, new RdfXmlParser());
        rdf.BaseUri = new Uri("http://www.example.org/destDetails");
        INode n = rdf.GetUriNode(new Uri("http://www.example.org/destDetails#" + destID));
        if (n != null)
        {
            rdf.Retract(rdf.GetTriplesWithSubject(n));
        }
        rdf.SaveToFile(rdfDatoteka);

其中destID是所有三元组的主题。

1 个答案:

答案 0 :(得分:2)

您的查询实际上与您的数据不符,这就是DELETE无效的原因。

在您的数据中,您有ns0:ID,但在DELETE中,您尝试与ns0:nodeID匹配 - 因此不会匹配任何数据,也不会删除任何内容。