Sparql Query With Inferencing

I have some rdf and rdfs files, and I want to use the jena sparql implementation for the query, and my code looks like this:

//model of my rdf file Model model = ModelFactory.createMemModelMaker().createDefaultModel(); model.read(inputStream1, null); //model of my ontology (word net) file Model onto = ModelFactory.createOntologyModel( OntModelSpec.RDFS_MEM_RDFS_INF); onto.read( inputStream2,null); String queryString = "PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#> " + "PREFIX wn:<http://www.webkb.org/theKB_terms.rdf/wn#> " + "SELECT ?person " + "WHERE {" + " ?person rdf:type wn:Person . " + " }"; Query query = QueryFactory.create(queryString); QueryExecution qe = QueryExecutionFactory.create(query, ????); ResultSet results = qe.execSelect(); ResultSetFormatter.out(System.out, results, query); qe.close(); 

and I have the wordNet ontology in the rdf file, and I want to use this ontology in my request to make automatic determination (when I ask the person to whom the request should return, for example, Man, Woman) so how to connect the ontology with my request? please help me.

update: Now I have tow models: from what should I run my request?

  QueryExecution qe = QueryExecutionFactory.create(query, ????); 

early.

+4
source share
1 answer

The key is to recognize that in Yen, Model is one of the central abstractions. The output model is just a Model , in which some of the triples are present, because they are associated with the output rules, and are not read from the source document. Thus, you only need to change the first line of your example, where you first create the model.

While you can create output models directly, it is often easiest to create an OntModel with the required degree of output support:

 Model model = ModelFactory.createOntologyModel( OntModelSpec.RDFS_MEM_RDFS_INF ); 

If you need another argument or OWL support, you can choose another OntModelSpec constant. Keep in mind that large and / or complex models can perform slow queries.

Refresh (after editing the original question)

To reason on two models, you need an alliance. You can do this with OntModel submodeling. I would modify your example as follows (note: I have not tested this code, but it should work):

 String rdfFile = "... your RDF file location ..."; Model source = FileManager.get().loadModel( rdfFile ); String ontFile = "... your ontology file location ..."; Model ont = FileManager.get().loadModel( ontFile ); Model m = ModelFactory.createOntologyModel( OntModelSpec.RDFS_MEM_RDFS_INF, ont ); m.addSubModel( source ); String queryString = "PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#> " + "PREFIX wn:<http://www.webkb.org/theKB_terms.rdf/wn#> " + "SELECT ?person " + "WHERE {" + " ?person rdf:type wn:Person . " + " }"; Query query = QueryFactory.create(queryString); QueryExecution qe = QueryExecutionFactory.create(query, m); ResultSet results = qe.execSelect(); ResultSetFormatter.out(System.out, results, query); qe.close(); 
+6
source

All Articles