嗨,我想获得子类的所有超类,直到成为根,我使用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.");
}
我尝试使用某些猫头鹰文件,并且可以正常工作,但是当我使用另一只猫头鹰时,出现错误:
错误消息是:
Unable to Cast object of type 'VDS.RDF.Query.Patterns.FixedBlankNodePattern' to type 'VDS.RDF.Query.Patterns.NodeMatchPattern'
这是owl文件:OWL File我不确定,但是可能是Protege 5.5制作的这个owl文件,因为它无法在Protege 4中打开,如何解决这个问题?请帮我。感谢您的帮助
仅使用API而不是尝试自己使用SPARQL对其进行编码可能会更好。参见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
}
只需为您的应用程序填写适当的TODO。