简体   繁体   English

Sparql Delete查询不使用dotnetrdf给出结果

[英]Sparql Delete query does not give results using dotnetrdf

I am using dotnetrdf to delete an individual based on its data property taken from text box in windows application form. 我正在使用dotnetrdf根据从Windows应用程序窗体的文本框中获取的数据属性删除个人。 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>

There is no ActuatorCode property in the fragment of your ontology that you've shown so this query cannot ever delete anything. 所显示的本体片段中没有ActuatorCode属性,因此该查询永远无法删除任何内容。 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. 请注意, DELETE WHERE仅删除它匹配的特定内容,因此除了三元组表示ActuatorCode属性外,它不会删除其他任何内容。

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. 我在代码中注意到其他一些奇怪之处,首先,对于大多数前缀,您的PREFIX定义为空,通常是一个不好的信号。 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. 如果需要将值注入查询模板,则最好使用SparqlParamterizedString中所示的SparqlParamterizedString 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 eg 最后,您可以在C#中使用逐字字符串文字 ,以使查询更易于编写和读取代码,例如

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");

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM