1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
package mypkg;
public class TestQuery {
public static void main(String[] args) {
String monontologie=("C:\\Documents and Settings\\sabrina.SAHLI-8CD27CF73\\workspace\\requettSparql\\GestionHopital.owl");
OntModel model = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM_RDFS_INF);
InputStream reader=FileManager.get().open(monontologie);
if (reader == null) {
throw new IllegalArgumentException("File: " + monontologie+ " not found");
}
model.read(reader , "","RDF/XML");
// the query: afficher les medicament d'un patient donnée
String queryString="PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>"+
"PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#>"+
"PREFIX GestionHopital:<http://semanticweb.org/ontologies/2010/10/Ontology1291143729468.owl>"+
"SELECT ?CODE_MEDICAMENT WHERE{"+
"?P rdf:type GestionHopital:Patient."+
"?P GestionHopital:prend ?M."+
"?P GestionHopital:CodeP $Code.FILTER($Code < 100)" +
"?M rdf:type GestionHopital:medication."+
"?M GestionHopital:codeM ?CODE_MEDICAMENT."+
"?P rdfs:subClassOf GestionHopital:Personne."+
"?M rdfs:subClassOf GestionHopital:Soin."+
"}" ;
Query query =QueryFactory.create(queryString) ;
Dataset dataset=DatasetFactory.create(model);
QueryExecution qexec = QueryExecutionFactory.create(query,dataset) ;
ResultSet resultset = qexec.execSelect() ;
ResultSetFormatter.out(System.out,resultset,query) ;
//afficher les resultats
for ( ;resultset.hasNext(); ){
QuerySolution row1 =(QuerySolution) resultset.nextSolution() ;
RDFNode code = row1.get("CODE_MEDICAMENT");
System.out.print(code.toString());
}
} |