comparison OPPL/src/GalaxyOWLAPI.java @ 11:6ca67b155e32

Imports simplified, new tool for inference added
author Mikel Egaña Aranguren <mikel-egana-aranguren@toolshed.g2.bx.psu.edu>
date Fri, 09 Mar 2012 16:15:27 +0100
parents
children 7e6604a5ee55
comparison
equal deleted inserted replaced
10:3f31c0eb7539 11:6ca67b155e32
1 /**
2 *
3 */
4 package es.upm.fi.dia.oeg.oppl.galaxy;
5
6 import java.io.BufferedWriter;
7 import java.io.File;
8 import java.io.IOException;
9 import java.io.OutputStreamWriter;
10 import java.util.ArrayList;
11 import java.util.List;
12 import java.util.Scanner;
13
14 import org.coode.owlapi.obo.parser.OBOOntologyFormat;
15 import org.semanticweb.HermiT.Reasoner;
16 import org.semanticweb.owlapi.apibinding.OWLManager;
17 import org.semanticweb.owlapi.io.RDFXMLOntologyFormat;
18 import org.semanticweb.owlapi.io.SystemOutDocumentTarget;
19 import org.semanticweb.owlapi.model.AddAxiom;
20 import org.semanticweb.owlapi.model.IRI;
21 import org.semanticweb.owlapi.model.OWLAnnotation;
22 import org.semanticweb.owlapi.model.OWLAnnotationProperty;
23 import org.semanticweb.owlapi.model.OWLAxiom;
24 import org.semanticweb.owlapi.model.OWLClass;
25 import org.semanticweb.owlapi.model.OWLDataFactory;
26 import org.semanticweb.owlapi.model.OWLLiteral;
27 import org.semanticweb.owlapi.model.OWLOntology;
28 import org.semanticweb.owlapi.model.OWLOntologyChange;
29 import org.semanticweb.owlapi.model.OWLOntologyCreationException;
30 import org.semanticweb.owlapi.model.OWLOntologyManager;
31 import org.semanticweb.owlapi.model.OWLOntologyStorageException;
32 import org.semanticweb.owlapi.reasoner.InferenceType;
33 import org.semanticweb.owlapi.reasoner.OWLReasoner;
34 import org.semanticweb.owlapi.reasoner.OWLReasonerFactory;
35 import org.semanticweb.owlapi.util.InferredAxiomGenerator;
36 import org.semanticweb.owlapi.util.InferredClassAssertionAxiomGenerator;
37 import org.semanticweb.owlapi.util.InferredDisjointClassesAxiomGenerator;
38 import org.semanticweb.owlapi.util.InferredOntologyGenerator;
39 import org.semanticweb.owlapi.util.InferredSubClassAxiomGenerator;
40 import org.semanticweb.owlapi.util.InferredSubDataPropertyAxiomGenerator;
41 import org.semanticweb.owlapi.util.InferredSubObjectPropertyAxiomGenerator;
42 import org.semanticweb.owlapi.util.OWLEntityRenamer;
43 import org.semanticweb.owlapi.util.OWLOntologyMerger;
44 import org.semanticweb.owlapi.vocab.OWL2Datatype;
45 import org.semanticweb.owlapi.vocab.OWLRDFVocabulary;
46
47 import uk.ac.manchester.cs.factplusplus.owlapiv3.FaCTPlusPlusReasonerFactory;
48
49 import com.clarkparsia.pellet.owlapiv3.PelletReasonerFactory;
50
51 /**
52 * A wrapper of the OWL API
53 *
54 * @author Mikel EgaƱa Aranguren
55 */
56 public class GalaxyOWLAPI {
57 private OWLOntologyManager manager;
58 private OWLOntology ontology;
59 private OWLReasonerFactory reasonerFactory;
60 private OWLReasoner reasoner;
61
62 public GalaxyOWLAPI (){
63 manager = OWLManager.createOWLOntologyManager();
64 }
65 public OWLOntologyManager getOWLManager(){
66 return manager;
67 }
68 public OWLOntology getMainOntology(){
69 return ontology;
70 }
71 public OWLReasoner getReasoner(){
72 return reasoner;
73 }
74
75 public void loadMainOntology(String OWLFilePath) throws OWLOntologyCreationException{
76 File owl_file = new File(OWLFilePath);
77 ontology = manager.loadOntologyFromOntologyDocument(owl_file);
78 }
79
80 // public void loadImportedOntology(IRI importedOntologyIRI) throws OWLOntologyCreationException{
81 // manager.loadOntologyFromOntologyDocument(importedOntologyIRI);
82 // }
83
84 // OWLLink: The problem is that Racer, for example, listens in 8080, the same port as Galaxy
85 // I have to change Racer settings and OWLLink settings
86 // OWLlinkHTTPXMLReasonerFactory factory = new OWLlinkHTTPXMLReasonerFactory();
87 // reasoner = factory.createReasoner(OWL_ontology);
88
89 public void setReasonerPellet (){
90 reasonerFactory = new PelletReasonerFactory();
91 reasoner = reasonerFactory.createReasoner(ontology);
92 }
93 public void setReasonerFaCT (){
94 reasonerFactory = new FaCTPlusPlusReasonerFactory();
95 reasoner = reasonerFactory.createReasoner(ontology);
96 }
97 public void setReasonerHermit (){
98 reasonerFactory = new Reasoner.ReasonerFactory();
99 reasoner = reasonerFactory.createReasoner(ontology);
100 }
101 public void merge () throws OWLOntologyCreationException{
102 OWLOntologyMerger merger = new OWLOntologyMerger(manager);
103 IRI mergedOntologyIRI = IRI.create("http://oeg-upm.net/oppl_galaxy/merged.owl");
104 ontology = merger.createMergedOntology(manager, mergedOntologyIRI);
105 }
106
107 // CLASS_ASSERTIONS Denotes the computation of the direct types of individuals for each individual in the signature of the imports closure of the root ontology.
108
109 public void addCLASS_ASSERTIONS(){
110 reasoner.precomputeInferences(InferenceType.CLASS_ASSERTIONS);
111 List<InferredAxiomGenerator<? extends OWLAxiom>> gens = new ArrayList<InferredAxiomGenerator<? extends OWLAxiom>>();
112 gens.add(new InferredClassAssertionAxiomGenerator());
113 InferredOntologyGenerator iog = new InferredOntologyGenerator(reasoner, gens);
114 iog.fillOntology(manager, ontology);
115 }
116
117 // CLASS_HIERARCHY Denotes the computation of the class hierarchy.
118
119 public void addCLASS_HIERARCHY(){
120 reasoner.precomputeInferences(InferenceType.CLASS_HIERARCHY);
121 List<InferredAxiomGenerator<? extends OWLAxiom>> gens = new ArrayList<InferredAxiomGenerator<? extends OWLAxiom>>();
122 gens.add(new InferredSubClassAxiomGenerator());
123 InferredOntologyGenerator iog = new InferredOntologyGenerator(reasoner, gens);
124 iog.fillOntology(manager, ontology);
125 }
126
127 // DATA_PROPERTY_ASSERTIONS Denotes the computation of relationships between individuals and data property values for each individual in the signature of the imports closure of the root ontology.
128 // public void addDATA_PROPERTY_ASSERTIONS(){
129 // reasoner.precomputeInferences(InferenceType.DATA_PROPERTY_ASSERTIONS);
130 // List<InferredAxiomGenerator<? extends OWLAxiom>> gens = new ArrayList<InferredAxiomGenerator<? extends OWLAxiom>>();
131 // gens.add(new InferredDataPropertyAxiomGenerator());
132 // InferredOntologyGenerator iog = new InferredOntologyGenerator(reasoner, gens);
133 // iog.fillOntology(manager, ontology);
134 // }
135
136 // DATA_PROPERTY_HIERARCHY Denotes the computation of the data property hierarchy.
137
138 public void addDATA_PROPERTY_HIERARCHY(){
139 reasoner.precomputeInferences(InferenceType.DATA_PROPERTY_HIERARCHY);
140 List<InferredAxiomGenerator<? extends OWLAxiom>> gens = new ArrayList<InferredAxiomGenerator<? extends OWLAxiom>>();
141 gens.add(new InferredSubDataPropertyAxiomGenerator());
142 InferredOntologyGenerator iog = new InferredOntologyGenerator(reasoner, gens);
143 iog.fillOntology(manager, ontology);
144 }
145
146 // DIFFERENT_INDIVIDUALS Denotes the computation of sets of individuals that are different from each individual in the signature of the imports closure of the root ontology.
147
148 // public void addDIFFERENT_INDIVIDUALS(){
149 // reasoner.precomputeInferences(InferenceType.DIFFERENT_INDIVIDUALS);
150 // List<InferredAxiomGenerator<? extends OWLAxiom>> gens = new ArrayList<InferredAxiomGenerator<? extends OWLAxiom>>();
151 // gens.add(new Inferred);
152 // InferredOntologyGenerator iog = new InferredOntologyGenerator(reasoner, gens);
153 // iog.fillOntology(manager, ontology);
154 // }
155
156 // DISJOINT_CLASSES Denotes the computation of sets of classes that are disjoint for each class in the signature of the imports closure of the root ontology.
157
158 public void addDISJOINT_CLASSES(){
159 reasoner.precomputeInferences(InferenceType.DISJOINT_CLASSES);
160 List<InferredAxiomGenerator<? extends OWLAxiom>> gens = new ArrayList<InferredAxiomGenerator<? extends OWLAxiom>>();
161 gens.add(new InferredDisjointClassesAxiomGenerator());
162 InferredOntologyGenerator iog = new InferredOntologyGenerator(reasoner, gens);
163 iog.fillOntology(manager, ontology);
164 }
165
166 // OBJECT_PROPERTY_ASSERTIONS Denotes the computation of relationships between individuals in the signature of the imports closure of the root ontology.
167
168 // public void addOBJECT_PROPERTY_ASSERTIONS(){
169 // reasoner.precomputeInferences(InferenceType.OBJECT_PROPERTY_ASSERTIONS);
170 // List<InferredAxiomGenerator<? extends OWLAxiom>> gens = new ArrayList<InferredAxiomGenerator<? extends OWLAxiom>>();
171 // gens.add(new InferredObjectPropertyAxiomGenerator<OWLAxiom>() {
172 // };());
173 // InferredOntologyGenerator iog = new InferredOntologyGenerator(reasoner, gens);
174 // iog.fillOntology(manager, ontology);
175 // }
176
177 // OBJECT_PROPERTY_HIERARCHY Denotes the computation of the object property hierarchy.
178
179 public void addOBJECT_PROPERTY_HIERARCHY(){
180 reasoner.precomputeInferences(InferenceType.OBJECT_PROPERTY_HIERARCHY);
181 List<InferredAxiomGenerator<? extends OWLAxiom>> gens = new ArrayList<InferredAxiomGenerator<? extends OWLAxiom>>();
182 gens.add(new InferredSubObjectPropertyAxiomGenerator());
183 InferredOntologyGenerator iog = new InferredOntologyGenerator(reasoner, gens);
184 iog.fillOntology(manager, ontology);
185 }
186
187 // SAME_INDIVIDUAL Denotes the computation of individuals that are interpreted as the same object for each individual in the imports closure of the root ontology.
188
189 // public void addSAME_INDIVIDUAL(){
190 // reasoner.precomputeInferences(InferenceType.SAME_INDIVIDUAL);
191 // List<InferredAxiomGenerator<? extends OWLAxiom>> gens = new ArrayList<InferredAxiomGenerator<? extends OWLAxiom>>();
192 // gens.add(new Infer);
193 // InferredOntologyGenerator iog = new InferredOntologyGenerator(reasoner, gens);
194 // iog.fillOntology(manager, ontology);
195 // }
196
197 public void saveOntology (boolean OWL) throws OWLOntologyStorageException, IOException, OWLOntologyCreationException{
198 // OWL format
199 if(OWL){
200 manager.saveOntology(ontology, new RDFXMLOntologyFormat(), new SystemOutDocumentTarget());
201 }
202 // OBO format
203 else{
204 // OWL API generates bad OBO but OBOformat doesn't work either so I correct the OWL API problems (More predictable) in a temporary file
205 // Very inefficient but no time for another solution
206
207 OWLDataFactory factory = manager.getOWLDataFactory();
208 OWLEntityRenamer renamer = new OWLEntityRenamer (manager, manager.getOntologies());
209 OWLAnnotationProperty label = factory.getOWLAnnotationProperty(OWLRDFVocabulary.RDFS_LABEL.getIRI());
210 for(OWLClass cls : ontology.getClassesInSignature()){
211 // Remove annotations (OWL API generates bad OBO annotations)
212
213 // Keep rdfs:label
214 String class_name = null;
215 for (OWLAnnotation annotation : cls.getAnnotations(ontology, label)){
216 if (annotation.getValue() instanceof OWLLiteral) {
217 OWLLiteral val = (OWLLiteral) annotation.getValue();
218 class_name = val.getLiteral();
219 }
220 }
221
222 // I have to remove all the annotations cause I don't know which ones are rendered properly
223 manager.removeAxioms(ontology,ontology.getAnnotationAssertionAxioms(cls.getIRI()));
224
225 // Add rdfs:label again
226 OWLAnnotation labelAnno = factory.getOWLAnnotation(factory.getOWLAnnotationProperty(
227 OWLRDFVocabulary.RDFS_LABEL.getIRI()),factory.getOWLLiteral(class_name,OWL2Datatype.XSD_STRING));
228 OWLAxiom ax = factory.getOWLAnnotationAssertionAxiom(cls.getIRI(), labelAnno);
229 manager.applyChange(new AddAxiom(ontology, ax));
230
231 // Rename entities
232 String cls_IRI = cls.getIRI().toString();
233 String cls_proper_IRI = cls_IRI.replace("_", ":");
234 List<OWLOntologyChange> changes = renamer.changeIRI(cls, IRI.create(cls_proper_IRI));
235 manager.applyChanges(changes);
236 }
237
238 File file = new File("tmp.obo");
239 manager.saveOntology(ontology, new OBOOntologyFormat(), IRI.create(file.toURI()));
240 BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
241 Scanner input = new Scanner(file);
242 String buffer = "";
243
244 while(input.hasNext()){
245 String nextLine = input.nextLine();
246 if(nextLine.contains("[Term]") || nextLine.contains("[Typedef]")){
247 if(buffer.isEmpty()){
248 bw.write(buffer);
249 bw.newLine();
250 }
251 }
252 if(!nextLine.contains("is_a: Thing") && !nextLine.contains("auto-generated-by:") && !nextLine.contains("id_space:") && !nextLine.contains("! ----")){
253 if(!nextLine.isEmpty() && !buffer.contains("relationship:")){
254 bw.write(nextLine);
255 bw.newLine();
256 }
257 }
258 buffer = nextLine;
259 }
260 input.close();
261 bw.close();
262 file.delete();
263 }
264 }
265 }