C# 获取类的所有超类直到根c时发生SPARQL错误#

C# 获取类的所有超类直到根c时发生SPARQL错误#,c#,sparql,rdf,owl,protege,C#,Sparql,Rdf,Owl,Protege,嗨,我想得到子类的所有超类,直到根,我使用RDFDotNet,下面是我的代码: string GetSuperClassesUntilRoot = @" PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX owl: <http://www.w3.org/2002/07/owl#> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-

嗨,我想得到子类的所有超类,直到根,我使用RDFDotNet,下面是我的代码:

   string GetSuperClassesUntilRoot = @"
  PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> 
  PREFIX owl: <http://www.w3.org/2002/07/owl#> 
  PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> 
PREFIX : <" + OntologyUrl + @">
  select ?superclass where {
  <" + Class+ @"> (rdfs:subClassOf|(owl:intersectionOf/rdf:rest*/rdf:first))* ?superclass .
  }
";
                string GetSuperClassesUntilRoot2 = @"
  PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> 
  PREFIX owl: <http://www.w3.org/2002/07/owl#> 
  PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> 
PREFIX : <" + OntologyUrl + @">
  SELECT ?superClass WHERE
{ <" + Class + @"> rdfs:subClassOf* ?superClass .
}
";
 // FILTER (!isBlank(rdfs:subClassOf))
 //FILTER(!isBlank(?superClass))

                Object results = g.ExecuteQuery(GetSuperClassesUntilRoot2);

                if (results is SparqlResultSet)
                {
                    //SELECT/ASK queries give a SparqlResultSet
                    SparqlResultSet rset = (SparqlResultSet)results;

                    foreach (SparqlResult r in rset)
                    {

                        Classes.Add(r["superClass"].ToString());

                        //Do whatever you want with each Result
                    }

                }
                else if (results is IGraph)
                {
                    //CONSTRUCT/DESCRIBE queries give a IGraph
                    IGraph resGraph = (IGraph)results;
                    foreach (Triple t in resGraph.Triples)
                    {

                        //Do whatever you want with each Triple
                    }
                }
                else
                {
                    //If you don't get a SparqlResutlSet or IGraph something went wrong 
                    //but didn't throw an exception so you should handle it here
                    MessageBox.Show("No Data Found.");
                }

以下是owl文件: 我不确定,但是我可以修改这个由Protege 5.5制作的owl文件,因为它不是用Protege 4打开的 ,如何解决这个问题?请帮帮我。
感谢您的帮助

仅使用API可能比使用SPARQL自己编写代码更好。从中我们可以对第一个示例进行如下修改:

// First create an OntologyGraph and load in some data
OntologyGraph g = new OntologyGraph();
// TODO: Load your data into the graph using whatever method is appropriate

// Get the class of interest
// TODO: Substitute the correct URI for your class of interest
OntologyClass someClass = g.CreateOntologyClass(new Uri("http://example.org/someClass"));

// Find Super Classes
foreach (OntologyClass c in someClass.SuperClasses)
{
  // TODO: Process the class as appropriate
}

只需在TODO中填入适用于您的应用程序的适当值。

有人知道如何修复此错误吗?好的,如何获取类的所有超类直到根?看起来您的一个类是匿名的,即它没有URI,但是一个空节点。当您尝试将其替换回后续查询时,它会中断,因为查询引擎不希望遇到空白节点。一般来说,尝试将空白节点替换回查询很少(如果有的话)会得到预期的结果,应该避免。感谢您的回答,但我发现我发送给获取SuperlClass的类中存在的问题,这是不正确的,它必须是Url#标签,但它不是此形式,因此这会出错
// First create an OntologyGraph and load in some data
OntologyGraph g = new OntologyGraph();
// TODO: Load your data into the graph using whatever method is appropriate

// Get the class of interest
// TODO: Substitute the correct URI for your class of interest
OntologyClass someClass = g.CreateOntologyClass(new Uri("http://example.org/someClass"));

// Find Super Classes
foreach (OntologyClass c in someClass.SuperClasses)
{
  // TODO: Process the class as appropriate
}