-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeOntologies.java
More file actions
90 lines (65 loc) · 3.04 KB
/
MergeOntologies.java
File metadata and controls
90 lines (65 loc) · 3.04 KB
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
package obanminter;
import com.sun.prism.shader.Solid_ImagePattern_Loader;
import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.io.RDFXMLOntologyFormat;
import org.semanticweb.owlapi.model.*;
import org.semanticweb.owlapi.util.OWLOntologyMerger;
import org.semanticweb.owlapi.util.OWLOntologyURIChanger;
import java.io.*;
import java.util.Collection;
import java.util.List;
import java.util.Set;
/**
* Created by malone on 24/03/2015.
*/
public class MergeOntologies {
/**
* Merge multiple ontologies such as multiple OBAN OWL files into one
* @param ontologyIRI the IRI for the new, merged ontology
* @param outputLocation path to the file to save single merged ontology to
* @param ontologyLocations one or more paths to the ontology files which are to be merged into one
*
*/
public void mergeOntologies(String ontologyIRI, String outputLocation, String ... ontologyLocations) {
try{
// specify the URI of the new ontology that will be created.
IRI mergedOntologyIRI = IRI
.create(ontologyIRI);
OWLOntologyManager man = OWLManager.createOWLOntologyManager();
int i = 0;
//load each ontology into manager
for(String s : ontologyLocations ) {
File f = new File(s);
man.loadOntologyFromOntologyDocument(f);
System.out.println("Loaded ontology: " + f.toString());
Set<OWLOntology> ontologies = man.getOntologies();
for(OWLOntology o : ontologies){
IRI iri = o.getOntologyID().getOntologyIRI();
IRI newiri = IRI.create(iri.toString()+i);
OWLOntologyURIChanger changer = new OWLOntologyURIChanger(man);
List<OWLOntologyChange> changes = changer.getChanges(o, newiri);
man.applyChanges(changes);
}
i++;
}
OWLOntologyMerger merger = new OWLOntologyMerger(man);
//merged ontology
OWLOntology merged = null;// man.createOntology(mergedOntologyIRI);
merged = merger.createMergedOntology(man, mergedOntologyIRI);
//create location to save ontology to
File outputFile = new File(outputLocation);
FileOutputStream fs = new FileOutputStream(outputFile);
//save ontology to file as rdf/xml
man.saveOntology(merged, new RDFXMLOntologyFormat(), fs);
System.out.println("New merged ontology file saved.");
}
catch(OWLOntologyCreationException e){
System.out.println("Error occured creating ontology during merge " + e.toString());
}
catch(OWLOntologyStorageException e){
System.out.println("Error has occurred when trying to save to file " + e.toString());
} catch (FileNotFoundException e) {
System.out.println("Error has occurred when trying to save to file: file not found " + e.toString());
}
}
}