Sparql Delete query does not give results using dotnetrdf

212 views Asked by At

I am using dotnetrdf to delete an individual based on its data property taken from text box in windows application form. I read previous questions in this form about delete query and have made those changes to my code but it give neither an error nor changes the ontology.

here is the code that im using

public void deleteActuator(int actuatorCode)
        {
            TripleStore store = new TripleStore();
            Graph mygraph = new Graph();
            FileLoader.Load(mygraph, "D:/Masters 
            Studies/Ontology/Providers/Actuator/ActuatorProvider.owl", new RdfXmlParser());
            mygraph.BaseUri = null;
            store.Add(mygraph);
            SparqlUpdateParser myparser = new SparqlUpdateParser();
            SparqlParameterizedString querystring = new SparqlParameterizedString();
            querystring.CommandText = "PREFIX AP0:
            <http://www.semanticweb.org/faiza/ontologies/2014/10/ActuatorProvider#> " +
                    "PREFIX rdf: <>" +
                    "PREFIX owl: <>" +
                    "PREFIX xsd: <>" +
                    "PREFIX rdfs: <>" +
                    "DELETE " +
                    "WHERE { ?code AP0:ActuatorCode " + actuatorCode + "}";      

        SparqlUpdateCommandSet cmds = myparser.ParseFromString(querystring);
        LeviathanUpdateProcessor processor = new LeviathanUpdateProcessor(store);
        processor.ProcessCommandSet(cmds);
        mygraph.SaveToFile("D:/Masters       
        Studies/Ontology/Providers/Actuator/ActuatorProvider.owl");


    }

can anyone kindly point out my error. here is my owl file

<owl:NamedIndividual rdf:about="&ActuatorProvider;ActuatingDevice1">
    <ace_lexicon:PN_sg>ActuatingDevice1</ace_lexicon:PN_sg>
    <ActuatorProvider:ActuatorExplain rdf:datatype="&xsd;string">"TemperatureControl"</ActuatorProvider:ActuatorExplain>
    <ActuatorProvider:ActuatorID rdf:datatype="&xsd;string">"SD01"</ActuatorProvider:ActuatorID>
    <ActuatorProvider:ActuatorName rdf:datatype="&xsd;string">"Fan"</ActuatorProvider:ActuatorName>
    <ActuatorProvider:ConnectsTo rdf:resource="&ActuatorProvider;ActuatorMiddleware1" />
    <ActuatorProvider:PowerConsumption rdf:datatype="&xsd;integer">400</ActuatorProvider:PowerConsumption>
    <ActuatorProvider:hasState rdf:resource="&ActuatorProvider;Offline" />
    <rdf:type rdf:resource="&ActuatorProvider;Actuating_Device" />   </owl:NamedIndividual>
1

There are 1 answers

1
RobV On

There is no ActuatorCode property in the fragment of your ontology that you've shown so this query cannot ever delete anything. However you haven't shown a complete ontology so we can't tell if this is actually the problem. Note that the DELETE WHERE only deletes the specific things that it matches so it won't delete anything other than the triple stating the ActuatorCode property.

If this did exist originally then it has been deleted, however it sounds like maybe you wanted to delete everything relevant to an individual in which case you want a query more like the following:

PREFIX AP0: <http://www.semanticweb.org/faiza/ontologies/2014/10/ActuatorProvider#> 
DELETE
WHERE 
{ 
  ?code AP0:ActuatorCode 1234 .
  ?code ?p ?o . 
}

Notes - Code Improvements

I note a few other oddities with your code, firstly your PREFIX definitions are empty for most prefixes which is usually a bad sign. If you don't need those prefixes don't bother including them in your query.

Secondly you are appending a constant into the query by simple string concatenation which is very fragile. It is best to use a SparqlParamterizedString as shown in the documentation if you need to inject values into a query template. You are using this class but don't take advantage of this capability.

Finally you can use verbatim string literals in C# to make your query easier to write and read in your code e.g.

public void deleteActuator(int actuatorCode)
{
  TripleStore store = new TripleStore();
  Graph mygraph = new Graph();
  FileLoader.Load(mygraph, "D:/Masters Studies/Ontology/Providers/Actuator/ActuatorProvider.owl", new RdfXmlParser());
  mygraph.BaseUri = null;
  store.Add(my graph);

  SparqlUpdateParser myparser = new SparqlUpdateParser();
  SparqlParameterizedString querystring = new SparqlParameterizedString();
  querystring.CommandText = @"PREFIX AP0: <http://www.semanticweb.org/faiza/ontologies/2014/10/ActuatorProvider#> 
DELETE
WHERE { ?code AP0:ActuatorCode @ActuatorCode }";

  // Inject parameter safely
  querystring.SetLiteral("ActuatorCode", actuatorCode);

  SparqlUpdateCommandSet cmds = myparser.ParseFromString(querystring);
  LeviathanUpdateProcessor processor = new LeviathanUpdateProcessor(store);
  processor.ProcessCommandSet(cmds);
  mygraph.SaveToFile("D:/Masters       
  Studies/Ontology/Providers/Actuator/ActuatorProvider.owl");

}