001    /**
002     * Copyright (C) 2007-2011, Jens Lehmann
003     *
004     * This file is part of DL-Learner.
005     *
006     * DL-Learner is free software; you can redistribute it and/or modify
007     * it under the terms of the GNU General Public License as published by
008     * the Free Software Foundation; either version 3 of the License, or
009     * (at your option) any later version.
010     *
011     * DL-Learner is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014     * GNU General Public License for more details.
015     *
016     * You should have received a copy of the GNU General Public License
017     * along with this program.  If not, see <http://www.gnu.org/licenses/>.
018     */
019    
020    package org.dllearner.utilities.owl;
021    
022    import java.net.MalformedURLException;
023    import java.net.URL;
024    
025    import org.dllearner.core.owl.Description;
026    import org.dllearner.parser.KBParser;
027    import org.dllearner.parser.ParseException;
028    import org.semanticweb.owlapi.apibinding.OWLManager;
029    import org.semanticweb.owlapi.model.IRI;
030    import org.semanticweb.owlapi.model.OWLClass;
031    import org.semanticweb.owlapi.model.OWLClassExpression;
032    import org.semanticweb.owlapi.model.OWLDataFactory;
033    import org.semanticweb.owlapi.model.OWLOntology;
034    import org.semanticweb.owlapi.model.OWLOntologyCreationException;
035    import org.semanticweb.owlapi.model.OWLOntologyManager;
036    
037    /**
038     * Utility class to replace a definition in an OWL file by a learned
039     * definition.
040     * 
041     * TODO: Class is currently not working. There is still some KAON2 specific
042     * code (commented out), which has to be converted to OWL API code.
043     * 
044     * @author Jens Lehmann
045     *
046     */
047    public class OntologyClassRewriter {
048    
049            public static void main(String[] args) {
050                    String rewrittenOntology =
051                    rewriteOntology(
052                                    // Ontologie
053                                    "http://localhost/jl/dllearnerws/v2/ontologies/father.owl",
054                                    // Klasse, die umgeschrieben wird
055                                    "http://example.com/father#male",
056                                    // neue Definition in DL-Learner-Syntax(die hier ergibt keinen Sinn)
057                                    "((\"http://example.com/father#male\" AND EXISTS \"http://example.com/father#hasChild\".TOP)" +
058                                    "OR ALL \"http://example.com/father#hasChild\".\"http://example.com/father#female\")");
059                    System.out.println(rewrittenOntology);
060            }
061    
062            @SuppressWarnings({"unused"})
063            public static String rewriteOntology(String urlString, String className, String newConceptString) {
064                    
065                    try {
066                            // neue Definition in DL-Learner internes Format parsen
067                            // (Warnung für Web-Service: Parser ist momentan noch statisch, d.h. nicht thread safe)
068                            Description newConceptInternal = KBParser.parseConcept(newConceptString);
069                            
070                            // umwandeln in interne KAON2-Darstellung (bereits im DL-Learner implementiert)
071                            // Description newConceptKAON2 = KAON2Reasoner.getKAON2Description(newConceptInternal);
072                            // OWLDescription newConceptOWLAPI = OWLAPIReasoner.getOWLAPIDescription(newConceptInternal);
073                            OWLClassExpression newConceptOWLAPI = OWLAPIDescriptionConvertVisitor.getOWLClassExpression(newConceptInternal);
074                            
075                            // Umwandlung Klassenname in atomate KAON2-Klasse
076                            // OWLClass classKAON2 = KAON2Manager.factory().owlClass(className);
077                            OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
078                            OWLDataFactory factory = manager.getOWLDataFactory();
079                            OWLClass classOWLAPI = factory.getOWLClass(IRI.create(className));
080                            
081                            // Test, ob es eine richtige URL ist (ansonsten wird Exception geworfen)
082                            new URL(urlString);
083                            
084                            // einlesen der Ontologie
085                            // DefaultOntologyResolver resolver = new DefaultOntologyResolver();
086                            // KAON2Connection connection = KAON2Manager.newConnection();
087                            // connection.setOntologyResolver(resolver);
088                            // Ontology ontology = connection.openOntology(urlString, new HashMap<String,Object>());                  
089                            
090                            OWLOntology ontology = manager.loadOntologyFromOntologyDocument(IRI.create(urlString));
091                            
092                            // TODO
093                            
094                            // suchen von Äquivalenzaxiomen
095    //                      Request<EquivalentClasses> equivalenceAxiomsRequest = ontology.createAxiomRequest(EquivalentClasses.class);
096    //                      Set<EquivalentClasses> equivalenceAxioms = equivalenceAxiomsRequest.get();
097    //                      
098    //                      for(EquivalentClasses eq : equivalenceAxioms) {
099    //                              Set<Description> eqDescriptions = eq.getDescriptions();
100    //                              if(eqDescriptions.size() != 2)
101    //                                      System.out.println("Warning: Rewriting more than two equivalent descriptions not supported yet." +
102    //                                                      " Possibly incorrect ontology returned.");
103    //                              
104    //                              // entfernen aller Äquivalenzaxiome, die die Klasse enthalten
105    //                              if(eqDescriptions.contains(classKAON2))
106    //                                      ontology.removeAxiom(eq);
107    //                      }
108    //                      
109    //                      // hinzufügen des neuen Äquivalenzaxioms
110    //                      EquivalentClasses eqNew = KAON2Manager.factory().equivalentClasses(classKAON2, newConceptKAON2);
111    //                      ontology.addAxiom(eqNew);
112    //                      
113    //                      // umwandeln der Ontologie in einen String
114    //                      ByteArrayOutputStream os = new ByteArrayOutputStream();
115    //                      ontology.saveOntology(OntologyFileFormat.OWL_RDF,os,"ISO-8859-1");
116    //                      
117    //                      return os.toString();
118                            return "";
119                            
120                    // in einigen der folgenden Fälle sollten im Web-Service Exceptions geworfen
121                    // werden (throws ...) z.B. bei ParseException
122                    } catch (ParseException e) {
123                            e.printStackTrace();
124                            System.out.println("New definition could not be parsed (probably a syntax error.");
125                    } catch (MalformedURLException e) {
126                            // TODO Auto-generated catch block
127                            e.printStackTrace();
128                            System.out.println("Syntactically incorrect URL.");
129                    } catch (OWLOntologyCreationException e) {
130                            // TODO Auto-generated catch block
131                            e.printStackTrace();
132                    } 
133            
134                    throw new Error("Ontology could not be rewritten. Exiting.");
135            }
136            
137    }