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
| try
{ String URI="http://www.semanticweb.org/ontologies/2012/3/untitled-ontology-59";
OntModel model = ModelFactory.createOntologyModel();
InputStream is = new BufferedInputStream(new FileInputStream("C:\\Users\\SEVEN7\\Desktop\\ontology.owl"));
model.read(is,URI);
model.write(System.out);
FileOutputStream fichierSortie = null ;
model.read(is,URI);
ontClass A=model.getOntClass(URI+"#chercheur");
DatatypeProperty nom= model.getDatatypeProperty(URI+"#Nom");
is.close();
String rdfPrefix = "PREFIX rdf: <"+RDF.getURI()+">" ;
String myOntologyPrefix = "PREFIX "+myOntologyName+": <"+URI+">" ;
// Construction de la requete
String queryString = myOntologyPrefix +rdfPrefix +
"SELECT ?nom ?adresse " +
"WHERE {" + "?individu rdf:type untitled-ontology-59 ?nom. " + " ?individu rdf:type untitled-ontology-59 ?prenom."+
"}";
// Affichage de la requete sur la sortie standard.
Query query = QueryFactory.create(queryString);
System.out.println() ;
QueryExecution qexec = QueryExecutionFactory.create(query, model) ;
// Execution de la requete
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());
}
//sérialiser l'ontology
try {
fichierSortie = new FileOutputStream (new File ("C:\\Users\\SEVEN7\\Desktop\\ontology.owl"));
}
catch (FileNotFoundException ex)
{
Logger.getLogger (Main.class.getName ()).log (Level.SEVERE, null, ex);
}
model.write (fichierSortie);
} // fin du try final
catch (Exception e){System.out.print(e.getCause());}
}
} |
Partager