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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
| package Ontology;
import com.hp.hpl.jena.ontology.OntModelSpec;
import com.hp.hpl.jena.query.Query;
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;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.sparql.util.IndentedWriter;
import com.hp.hpl.jena.util.FileManager;
import com.hp.hpl.jena.vocabulary.RDF;
public class Ontology {
public static final String owlFile = "D:\SMA-SYS-EMBARQUES\article\Nouveau\Nouveau dossier\Java-Projects\src\new-onto3.owl" ;
public static final String NL = System.getProperty("line.separator") ;
public static void main( String[] args ) {
// Creation d'un modele d'ontologie pour une ontologie OWL-DL avec un resonneur RDFS
Model m = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM_RDFS_INF);
// Lecture du fichier OWL. Le Namespace de notre ontologie doit etre specifié
FileManager.get().readModel( m, owlFile );
String myOntologyName = "new-onto3";
String myOntologyNS = "http://www.owl-ontologies.com/Ontology1390165473.owl#";
// Définition de prefixe pour simplifier l'utilisation de SPARQL
String rdfPrefix = "PREFIX rdf: <"+RDF.getURI()+">" ;
String myOntologyPrefix = "PREFIX "+myOntologyName+": <"+myOntologyNS+">" ;
// Construction de la requete
String queryString = myOntologyPrefix + NL
+ rdfPrefix + NL +
"SELECT ?individu WHERE {?individu rdf:type new-onto3:Blood_Sugar}" ;
Query query = QueryFactory.create(queryString) ;
// Affichage de la requete sur la sortie standard.
query.serialize(new IndentedWriter(System.out,true)) ;
System.out.println() ;
// Create a single execution of this query, apply to a model
// which is wrapped up as a Dataset
QueryExecution qexec = QueryExecutionFactory.create(query, m) ;
// Execution de la requete
try {
// Pour l'instant nous nous limitons a des requetes de type SELECT
ResultSet rs = qexec.execSelect() ;
// Affichage des resultats
for ( ; rs.hasNext() ; ){
QuerySolution rb = rs.nextSolution() ;
RDFNode y = rb.get("individu");
System.out.print("uri : "+y+"--- ");
Resource z = (Resource) rb.getResource("individu");
System.out.println("plus simplement "+z.getLocalName());
}
}
finally{
qexec.close() ;
}
}
} |
Partager