-
Notifications
You must be signed in to change notification settings - Fork 14
/
OwlapiDataPersist.java
40 lines (36 loc) · 2.19 KB
/
OwlapiDataPersist.java
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
package cz.cvut.kbss.jopa.test.environment;
import cz.cvut.kbss.jopa.CommonVocabulary;
import cz.cvut.kbss.jopa.model.EntityManager;
import cz.cvut.kbss.jopa.test.environment.Triple;
import cz.cvut.kbss.ontodriver.owlapi.util.OwlapiUtils;
import org.semanticweb.owlapi.model.*;
import java.net.URI;
import java.util.Collection;
public class OwlapiDataPersist {
public void persistTestData(Collection<Triple> data, EntityManager em) throws Exception {
final OWLOntology ontology = em.unwrap(OWLOntology.class);
final OWLOntologyManager manager = ontology.getOWLOntologyManager();
final OWLDataFactory df = manager.getOWLDataFactory();
for (Triple t : data) {
final OWLNamedIndividual ind = df.getOWLNamedIndividual(IRI.create(t.getSubject()));
final AddAxiom axiom;
if (t.getProperty().toString().equals(CommonVocabulary.RDF_TYPE)) {
final OWLClass cls = df.getOWLClass(IRI.create(t.getValue().toString()));
axiom = new AddAxiom(ontology, df.getOWLClassAssertionAxiom(cls, ind));
} else if (t.getValue() instanceof URI) {
final OWLObjectProperty op = df.getOWLObjectProperty(IRI.create(t.getProperty()));
final OWLNamedIndividual obj = df.getOWLNamedIndividual(IRI.create((URI) t.getValue()));
axiom = new AddAxiom(ontology, df.getOWLObjectPropertyAssertionAxiom(op, ind, obj));
} else if (t.getProperty().toString().equals(CommonVocabulary.RDFS_LABEL)) {
final OWLAnnotationProperty ap = df.getOWLAnnotationProperty(IRI.create(t.getProperty()));
final OWLLiteral value = OwlapiUtils.createOWLLiteralFromValue(t.getValue(), df, "en");
axiom = new AddAxiom(ontology, df.getOWLAnnotationAssertionAxiom(ap, ind.getIRI(), value));
} else {
final OWLDataProperty dp = df.getOWLDataProperty(IRI.create(t.getProperty()));
final OWLLiteral value = OwlapiUtils.createOWLLiteralFromValue(t.getValue(), df, "en");
axiom = new AddAxiom(ontology, df.getOWLDataPropertyAssertionAxiom(dp, ind, value));
}
manager.applyChange(axiom);
}
}
}