您好,我想获取subClass的所有superClasses直到根,我使用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文件中尝试它,它可以工作,但当我使用另一个owl时,我得到错误:

错误消息为:
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打开的,如何解决这个问题?请帮帮我。感谢你的帮助
发布于 2020-03-04 17:44:12
这可能比使用SPARQL自己编写代码更好地使用API来实现。参见Using the Ontology API,我们可以在其中修改第一个示例,如下所示:
// 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中填入适合您的应用程序的值。
https://stackoverflow.com/questions/60509854
复制相似问题