Skip to content

Instantly share code, notes, and snippets.

@zyzo zyzo/OntoParser.java

Last active Jan 14, 2020
Embed
What would you like to do?
5 € une copie
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.io.FileDocumentTarget;
import org.semanticweb.owlapi.model.AddAxiom;
import org.semanticweb.owlapi.model.OWLAnnotation;
import org.semanticweb.owlapi.model.OWLAnnotationProperty;
import org.semanticweb.owlapi.model.OWLAxiom;
import org.semanticweb.owlapi.model.OWLClass;
import org.semanticweb.owlapi.model.OWLClassAssertionAxiom;
import org.semanticweb.owlapi.model.OWLDataFactory;
import org.semanticweb.owlapi.model.OWLDataProperty;
import org.semanticweb.owlapi.model.OWLNamedIndividual;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.model.OWLOntologyCreationException;
import org.semanticweb.owlapi.model.OWLOntologyManager;
import org.semanticweb.owlapi.model.OWLOntologyStorageException;
import org.semanticweb.owlapi.model.PrefixManager;
import org.semanticweb.owlapi.util.DefaultPrefixManager;
import org.semanticweb.owlapi.vocab.OWLRDFVocabulary;
public class OntoParser {
private static final String FILENAME = "paris.csv";
private static final String DELIMITER = ";";
private OWLOntology filmOntology = null;
private OWLOntologyManager manager = null;
private BufferedReader readerMR = null;
private OWLDataFactory dataFactory ;
private PrefixManager pm;
private List<String[]> res = null;
private String base;
private void parseFile() {
try {
readerMR = new BufferedReader(new InputStreamReader(new FileInputStream(FILENAME), Charset.forName("UTF-8")));
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
res = readStringFromFile();
for (int i = 0; i < res.size(); i++) {
System.out.println(Arrays.toString(res.get(i)));
try {
addFilm(res.get(i)[0], null);
} catch (ArrayIndexOutOfBoundsException e ) {
System.out.println("Ligne qui déconne : " + i);
throw e;
}
}
System.out.println("Size = " + res.size());
try {
readerMR.close();
} catch (IOException e) {
e.printStackTrace();
}
}
static int counter = 0;
/*
private String getOWLClass(String label) {
OWLAnnotationProperty prop = dataFactory.getOWLAnnotationProperty(":" + label, pm);
return prop.getIRI().toString();
}
*/
private void addFilm(String filmName, String nationalite) {
OWLClass film = dataFactory.getOWLClass(":OWLClass_9e870727_34b3_4693_af26_f45d03dbf91f", pm);
OWLNamedIndividual filmAInserer = dataFactory.getOWLNamedIndividual(":film" + counter, pm);
counter++;
OWLClassAssertionAxiom classAssertion = dataFactory.getOWLClassAssertionAxiom(film, filmAInserer);
manager.addAxiom(filmOntology, classAssertion);
//label
OWLAnnotation commentAnno = dataFactory.getOWLAnnotation(
dataFactory.getOWLAnnotationProperty(
OWLRDFVocabulary.RDFS_LABEL.getIRI()
),
dataFactory.getOWLLiteral(filmName, "fr"));
OWLAxiom ax = dataFactory.getOWLAnnotationAssertionAxiom(filmAInserer.getIRI(),
commentAnno);
manager.addAxiom(filmOntology, ax);
// data property nationalité
OWLDataProperty dp = dataFactory.getOWLDataProperty(":OWLDataProperty_44aaac16_e6e8_4516_9b07_8044b911a192", pm);
OWLAxiom ax2 = dataFactory.getOWLDataPropertyAssertionAxiom(dp, filmAInserer, nationalite);
manager.addAxiom(filmOntology, ax2);
}
public void initOWL() {
manager = OWLManager.createOWLOntologyManager();
File file = new File("TP2.owl");
try {
filmOntology = manager.loadOntologyFromOntologyDocument(file);
} catch (OWLOntologyCreationException e) {
e.printStackTrace();
}
dataFactory = manager.getOWLDataFactory();
base = filmOntology.getOntologyID().getOntologyIRI().toString();
pm = new DefaultPrefixManager(base+"#");
System.out.println("Loaded ontology: " + filmOntology);
}
public void outputOWLToFile() {
try {
manager.saveOntology(filmOntology, new FileDocumentTarget(new File("result.owl")));
} catch (OWLOntologyStorageException e) {
e.printStackTrace();
}
}
private List<String[]> readStringFromFile() {
List<String[]> result = new ArrayList<String[]>();
String line;
try {
readerMR.mark(100000000);
while ((line = readerMR.readLine()) != null) {
result.add(line.split(DELIMITER));
}
} catch (IOException e) {
System.out.println("Cannot read from file ");
e.printStackTrace();
} finally {
try {
readerMR.reset();
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
public static void main(String[] args) {
OntoParser op = new OntoParser();
op.initOWL();
op.addFilm("yolo cooco ", "Paris titi");
//op.parseFile();
op.outputOWLToFile();
System.out.println(op.base);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.