我编写了以下代码,它创建了一个父类玩具和子类作为actiofigures,活动,艺术自行车。 Sparql查询没有给出答案作为子类列表请帮我找到解决方案
import com.hp.hpl.jena.ontology.OntClass;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.RDFNode;
public class jenaquery
{
static String defaultNameSpace ="http://semanticweb.org/ontologies";
static Model model = null;
public static void main(String[] args)
{
model = ModelFactory.createOntologyModel()
OntClass Toys = ((OntModel) model).createClass( "Toys" );
OntClass ActionFigures = ((OntModel) model).createClass( "ActionFigures" );
OntClass Arts = ((OntModel) model).createClass( "Arts" );
OntClass Activities = ((OntModel) model).createClass( "Activities" );
OntClass Bikes = ((OntModel) model).createClass( "Bikes" );
ActionFigures.addSuperClass(Toys);
Activities.addSuperClass(Toys);
Arts.addSuperClass(Toys);
Bikes.addSuperClass(Toys);
String queryString ="PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> "+
"PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> "+
"select ?uri "+
"where { "+
"?uri rdfs:subClassOf<http://semanticweb.org/ontologies#Toys> "+
"} \n ";
com.hp.hpl.jena.query.Query query = QueryFactory.create(queryString);
System.out.println("----------------------");
System.out.println("Query Result Sheet");
System.out.println("----------------------");
QueryExecution qe = QueryExecutionFactory.create(query, model);
System.out.print( "Query executed");
ResultSet resultSet = qe.execSelect();
while(resultSet.hasNext())
{
QuerySolution soln = (QuerySolution)resultSet.next();
RDFNode name = soln.get("?name");
if( name!=null)
{
System.out.println( " Subclasses"+name.toString());
}
else
System.out.print("No subclasses");
}
}
qe.close();
}
}
答案 0 :(得分:0)
在创建类时添加命名空间(在您的案例中为默认命名空间)。下面的代码可以工作..它检索给定类的子类。检查链接以获取更多详细信息.. http://opentox.org/data/documents/development/RDF%20files/JavaOnly/query-reasoning-with-jena-and-sparql
import com.hp.hpl.jena.ontology.OntClass;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntModelSpec;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.query.ResultSetFormatter;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
public class Query {
static String defaultNameSpace ="http://semanticweb.org/ontologies#";
static Model model = null;
public static void main(String[] args) {
model = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM);
OntClass Toys = ((OntModel) model).createClass( defaultNameSpace + "Toys" );
OntClass ActionFigures = ((OntModel) model).createClass( defaultNameSpace + "ActionFigures" );
OntClass Arts = ((OntModel) model).createClass( defaultNameSpace + "Arts" );
OntClass Activities = ((OntModel) model).createClass( defaultNameSpace + "Activities" );
OntClass Bikes = ((OntModel) model).createClass( defaultNameSpace + "Bikes" );
ActionFigures.addSuperClass(Toys);
Activities.addSuperClass(Toys);
Arts.addSuperClass(Toys);
Bikes.addSuperClass(Toys);
String queryString ="PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> "+
"PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> "+
"select ?uri "+
"where { "+
"?uri rdfs:subClassOf <http://semanticweb.org/ontologies#Toys> "+
"} \n ";
com.hp.hpl.jena.query.Query query = QueryFactory.create(queryString);
System.out.println("----------------------");
System.out.println("Query Result Sheet");
System.out.println("----------------------");
QueryExecution qe = QueryExecutionFactory.create(query, model);
System.out.println( "Query executed");
ResultSet resultSet = qe.execSelect();
ResultSetFormatter.out(System.out, resultSet, query);
qe.close();
}
}