dotnetrdf - Why does my SPARQL query return the URI of a resource instead of its name? -
i want classes of ontology. part of ontology file in rdf/xml format created protege:
<!-- http://www.w3.org/2002/07/owl#aqua --> <class rdf:about="&owl;aqua"/> <!-- http://www.w3.org/2002/07/owl#varioperfect --> <class rdf:about="&owl;varioperfect"/> i wrote query, works in protege, when use in dotnetrdf returns full uri of class instead of name.
public string[] ontologysearch() { list<string> list = new list<string>(); triplestore store = new triplestore(); graph mygraph = new graph(); mygraph.loadfromfile("d:/msc/search-engine/project/catalogxml.owl"); store.add(mygraph); string sparqlquery1 = "prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>" + "prefix owl: <http://www.w3.org/2002/07/owl#>" + "prefix xsd: <http://www.w3.org/2001/xmlschema#>" + "prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>" + "select distinct ?cls1" + " where{" + " ?cls1 owl:class .}"; sparqlqueryparser sparqlparser = new sparqlqueryparser(); sparqlquery query = sparqlparser.parsefromstring(sparqlquery1); inmemorydataset ds = new inmemorydataset(mygraph); //get query processor isparqlqueryprocessor processor = new leviathanqueryprocessor(ds); object results = processor.processquery(query); if (results sparqlresultset) { sparqlresultset r = results sparqlresultset; foreach (sparqlresult res in r) { list.add(res["cls1"].tostring()); } } return list.toarray(); }
the result expected "aqua" in fact "http://www.w3.org/2002/07/owl#aqua". why happen, , how can retrieve name instead?
non-anonymous resources in rdf , owl identified iris. ontology says http://www.w3.org/2002/07/owl#aqua class. if ask class, that's should get. might protege strips off http://www.w3.org/2002/07/owl# part when displays result, result still iri.
note: should not defining new classes iris begin standard owl namespace. should defining own prefix, typically related ontology iri.
if want string "aqua" result, have 2 options. first (and preferred) approach retrieve rdfs:label of class, if has one, should string name of class. if reason doesn't work, can take string value of uri , strip off string value of prefix. here examples of both approaches on dbpedia sparql endpoint:
select ?class ?label { ?class owl:class ; rdfs:label ?label filter langmatches(lang(?label),'en') } limit 10 sparql results (with rdfs:label)
select ?class ?name { ?class owl:class bind(strafter(str(?class),str(dbpedia-owl:)) ?name) } limit 10 sparql results (by stripping prefix)
stripping off prefix of uri display purposes is, in general, not recommended practice, assumes uri has human-readable form. in case of dbpedia happens work, plenty of datasets have uris internal codes rather human-readable names. if rdfs:label (which explicitly defined human-readable representation of resource) available, should try , use that.
Comments
Post a Comment