Sparql is the query language of Semantic Web and a standard recommended by w3c. It demonstrates the query of the previously designed ontology file and queries the name of the car owner of "benz.
/*** Semantic Data Query ** @ param spaqrlString * @ param queryModel * @ param var */public void queryBySpaqrl (String spaqrlString, Model queryModel, String var) {query Query query = QueryFactory. create (spaqrlString); QueryExecution queryExecution = QueryExecutionFactory. create (query, queryModel); ResultSet resultSet = queryExecution.exe cSelect (); while (resultSet. hasNext () {QuerySolution querySolution = resultSet. nextSolution (); RDFNode rdfNode = querySolution. get (var); System. out. println (var + "is" + rdfNode. toString ());}}
Sparql statement
@Testpublic void testJenaSparql(){JenaModelSpaqrl jenaModelSpaqrl = new JenaModelSpaqrl();jenaModel= new JenaModel();StringBuffer querystringBuffer = new StringBuffer();querystringBuffer.append("PREFIX s1" + ": <" + defaultNameSpace + ">");querystringBuffer.append("select ?name" +" where { " +"?car s1:hasBrand \"Benz\"."+"?car s1:hasOwner ?person."+"?person s1:hasName ?name}");jenaModelSpaqrl.queryBySpaqrl(querystringBuffer.toString(), jenaModel.createOntologyModel(OntModelSpec.OWL_DL_MEM, pathFile) ,"?name");}
Final Result