首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >获取直到根c#的类的所有SuperClasses时出现SPARQL错误

获取直到根c#的类的所有SuperClasses时出现SPARQL错误
EN

Stack Overflow用户
提问于 2020-03-03 22:50:55
回答 1查看 72关注 0票数 0

您好,我想获取subClass的所有superClasses直到根,我使用RDFDotNet,这是我的代码:

代码语言:javascript
复制
   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文件中尝试它,它可以工作,但当我使用另一个owl时,我得到错误:

错误消息为:

代码语言:javascript
复制
Unable to Cast object of type 'VDS.RDF.Query.Patterns.FixedBlankNodePattern' to type 'VDS.RDF.Query.Patterns.NodeMatchPattern'

这里是owl文件:OWL File我不确定,但是这个由Protege5.5制作的owl文件,因为它不是用Protege4打开的,如何解决这个问题?请帮帮我。感谢你的帮助

EN

回答 1

Stack Overflow用户

发布于 2020-03-04 17:44:12

这可能比使用SPARQL自己编写代码更好地使用API来实现。参见Using the Ontology API,我们可以在其中修改第一个示例,如下所示:

代码语言:javascript
复制
// 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
}

只需在TODOs中填入适合您的应用程序的值。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60509854

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档