Insérer des individus dans une ontologie
Je voulais insérer des instances dans une ontologie. Voici le code
Code:
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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
| package conn;
import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import org.omg.CORBA.NameValuePair;
import com.hp.hpl.jena.ontology.DatatypeProperty;
import com.hp.hpl.jena.ontology.Individual;
import com.hp.hpl.jena.ontology.OntClass;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntModelSpec;
import com.hp.hpl.jena.query.Dataset;
import com.hp.hpl.jena.query.DatasetFactory;
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.query.ResultSetFormatter;
import com.hp.hpl.jena.rdf.model.Literal;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.util.FileManager;
import com.hp.hpl.jena.util.FileUtils;
import com.hp.hpl.jena.vocabulary.RDF;
import edu.stanford.smi.protegex.owl.jena.JenaOWLModel;
import edu.stanford.smi.protegex.owl.model.RDFIndividual;
import edu.stanford.smi.protegex.owl.model.RDFProperty;
import edu.stanford.smi.protegex.owl.model.RDFSNamedClass;
public class Recupererinstance {
public static void main(String[] args)
{
//charger le fichier de l'ontologie
String monontologie="file:///C:/Program Files/Protege_3.4.7/examples/Krs1.owl";
//creer un modele d'ontologie
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");
}
String ontologyns="file:///C:/ProgramFiles/Protege_3.4.7/examples/Krs1.owl";
String reuses = "reuses: <"+RDF.getURI()+">" ;
//String ontologyns="http://www.owl-ontologies.com/reuses.owl#";
String MyOntologyPrefix="PREFIX OntologyTest:<"+ontologyns+">";
String myOntologyName = "ProjetHela";
String uri="file:/C:/Program20%Files/Protege_3.4.7/Krs1.owl";
// Définition de prefixe pour simplifier l'utilisation de SPARQL
// String myOntologyPrefix = "PREFIX "+myOntologyName+": <"+myOntologyNS+">" ;
String myOntologyPrefix = "PREFIX "+myOntologyName+": <"+uri+">" ;
//construire la requette
//MyOntologyPrefix+"\n"+ rdfPrefix+"\n"+
String queryString= "PREFIX reuses:<http://www.owl-ontologies.com/reuses.owl#>"
+ "INSERT DATA {\n"
+ " <http://www.owl-ontologies.com/reuses.owl#Activite> a reuses:Activite;\n"
+ " reuses:nom activité" + hasnameactivite).getText() + ";\n"
+ "}";
Query query = QueryFactory.create(queryString) ;
QueryExecution qexec = QueryExecutionFactory.create(query, model) ;
try {
// Pour l'instant nous nous limitons a des requetes de type SELECT
ResultSet rs = qexec.execSelect() ;
// Affichage des resultats
for ( ; rs.hasNext() ; ){
//System.out.print("");
QuerySolution rb = rs.nextSolution() ;
String y = rb.getLiteral("hasnamebesoin").getString();
System.out.print("besoin : "+y.toString()+"/n ");
}
}
finally{
qexec.close() ;
}
}} |
run
Citation:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
hasnameactivite cannot be resolved
Syntax error on token ")", delete this token
at conn.Recupererinstance.main(Recupererinstance.java:91)
Merci