001    /***************************************************************************/
002    /*  Copyright (C) 2010-2011, Sebastian Hellmann                            */
003    /*  Note: If you need parts of NLP2RDF in another licence due to licence   */
004    /*  incompatibility, please mail hellmann@informatik.uni-leipzig.de        */
005    /*                                                                         */
006    /*  This file is part of NLP2RDF.                                          */
007    /*                                                                         */
008    /*  NLP2RDF is free software; you can redistribute it and/or modify        */
009    /*  it under the terms of the GNU General Public License as published by   */
010    /*  the Free Software Foundation; either version 3 of the License, or      */
011    /*  (at your option) any later version.                                    */
012    /*                                                                         */
013    /*  NLP2RDF is distributed in the hope that it will be useful,             */
014    /*  but WITHOUT ANY WARRANTY; without even the implied warranty of         */
015    /*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the           */
016    /*  GNU General Public License for more details.                           */
017    /*                                                                         */
018    /*  You should have received a copy of the GNU General Public License      */
019    /*  along with this program. If not, see <http://www.gnu.org/licenses/>.   */
020    /***************************************************************************/
021    
022    package org.nlp2rdf.ontology;
023    
024    import com.hp.hpl.jena.ontology.OntClass;
025    import com.hp.hpl.jena.ontology.OntModel;
026    import com.hp.hpl.jena.ontology.OntModelSpec;
027    import com.hp.hpl.jena.rdf.model.ModelFactory;
028    import org.slf4j.Logger;
029    import org.slf4j.LoggerFactory;
030    
031    import java.util.*;
032    
033    
034    /**
035     * Indexes an Ontology
036     * skips complex classes per default, this does not affect the hierarchy outcome
037     */
038    public class ClassIndexer {
039        private static Logger log = LoggerFactory.getLogger(ClassIndexer.class);
040    
041        //Options
042        private boolean copyLabels = true;
043        private boolean copyComments = true;
044        private String language = null;
045    
046        //Not implemented
047        private Map<String, String> transform = new HashMap<String, String>();
048        //Not implemented
049        private Set<String> remove = new HashSet<String>();
050    
051        //internal variables
052        private Map<String, OntModel> classUriToClassHierarchy = new HashMap<String, OntModel>();
053    
054        public ClassIndexer() {
055        }
056    
057        public void index(OntModel from) {
058            Set<OntClass> classes = from.listClasses().toSet();
059            int i = 0;
060            for (OntClass cl : classes) {
061                Tree t = new Tree(cl);
062                classUriToClassHierarchy.put(cl.getURI(), t.toModel());
063            }
064    
065        }
066    
067        /**
068         * @param classUri
069         * @return a filled OntModel with all superclasses of classUri or null, if no class is found
070         */
071        public OntModel getHierarchyForClassURI(String classUri) {
072            return classUriToClassHierarchy.get(classUri);
073        }
074    
075        /**
076         * transforms namespaces
077         *
078         * @param in
079         * @return
080         */
081        private String transformNamespace(String in) {
082            String ret = in;
083            for (String s : transform.keySet()) {
084                if (in.startsWith(s)) {
085                    return in.replace(s, transform.get(s));
086    
087                }
088            }
089            return ret;
090        }
091    
092        /**
093         * filters out certain namespaces
094         *
095         * @param s
096         * @return
097         */
098        private boolean filterNamespace(String s) {
099            for (String prefix : remove) {
100                if (s.startsWith(prefix)) {
101                    return true;
102                }
103            }
104            return false;
105        }
106    
107    
108        public boolean isCopyLabels() {
109            return copyLabels;
110        }
111    
112        public void setCopyLabels(boolean copyLabels) {
113            this.copyLabels = copyLabels;
114        }
115    
116        public boolean isCopyComments() {
117            return copyComments;
118        }
119    
120        public void setCopyComments(boolean copyComments) {
121            this.copyComments = copyComments;
122        }
123    
124        public String getLanguage() {
125            return language;
126        }
127    
128        public void setLanguage(String language) {
129            this.language = language;
130        }
131    
132        /**
133         * A simple Helper Class to convert the hierarchy
134         */
135        private class Tree {
136            final String uri;
137            List<Tree> parents;
138            final String label;
139            final String comment;
140    
141            public Tree(OntClass me) {
142                this.uri = me.getURI();
143                label = me.getLabel(language);
144                comment = me.getComment(language);
145                parents = new ArrayList<Tree>();
146    
147                Set<OntClass> superClasses = me.listSuperClasses(true).toSet();
148                for (OntClass s : superClasses) {
149                    //this is were complex classes are skipped
150                    if (s.isAnon()) {
151                        continue;
152                    }
153                    log.trace(s.toString());
154                    parents.add(new Tree(s));
155                }
156            }
157    
158            public OntModel toModel() {
159                OntModel model = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM, ModelFactory.createDefaultModel());
160                OntClass me = model.createClass(uri);
161                //TODO test this for <&>
162                if (copyLabels && label != null) {
163                    me.addLabel(label, language);
164                }
165                if (copyComments && comment != null) {
166                    me.addComment(comment, language);
167                }
168                for (Tree p : parents) {
169                    OntClass superClass = model.createClass(p.uri);
170                    me.addSuperClass(superClass);
171                    model.add(p.toModel());
172                }
173                return model;
174            }
175        }
176    
177    }
178    
179    /**
180     public void expandSuperAndCopy(String originalClassUri) {
181    
182     String newClassUri = transform(originalClassUri);
183     if (isRemove(originalClassUri) || isRemove(newClassUri)) {
184     return;
185     }
186    
187    
188     // create initial classes
189     OntClass toClass = toModel.createClass(newClassUri);
190     OntClass fromClass = fromModel.getOntClass(originalClassUri);
191    
192     if(toClass==null || fromClass == null){
193     logger.error("null occured in fromClass "+originalClassUri+" but retrieving yielded: "+fromClass );
194     return;
195     }
196    
197     //System.out.println("begin");
198     //for(OntClass cltest: fromModel.listClasses().toSet()){
199     //  System.out.println(cltest.getURI());
200     // System.out.println(cltest.getClass().getSimpleName());
201     //}
202     //System.out.println("end");
203    
204     if (copyLabelsAndComments ) {
205     String tmp = null;
206    
207     if((tmp=fromClass.getLabel(null))!=null) {toClass.setLabel(tmp, null);}
208     //                     System.out.println(fromClass.getURI()+"has label "+tmp);
209    
210     if((tmp=fromClass.getComment(null))!=null) {toClass.setComment(tmp, null);}
211     //                     System.out.println(fromClass.getURI()+"has comment "+tmp);
212     }
213    
214     // get the superclasses
215     Set<OntClass> fromSuperclasses = fromClass.listSuperClasses(true).toSet();
216    
217     for (OntClass fromSuperclass : fromSuperclasses) {
218     String newFromSuperclassUri = transform(fromSuperclass.getURI());
219     if (isRemove(fromSuperclass.getURI()) || isRemove(newFromSuperclassUri)) {
220     continue;
221     }
222     if(fromSuperclass.isAnon()){
223     continue;
224     }
225    
226     OntClass toSuperclass = toModel.createClass(newFromSuperclassUri);
227     toClass.addSuperClass(toSuperclass);
228    
229     if (copyLabelsAndComments) {
230     String tmp = null;
231     if((tmp=fromSuperclass.getLabel(null))!=null) {toSuperclass.setLabel(tmp, null);}
232     //                             System.out.println(fromSuperclass.getURI()+"has label "+tmp);
233    
234     if((tmp=fromSuperclass.getComment(null))!=null) {toSuperclass.setComment(tmp, null);}
235     //                             System.out.println(fromSuperclass.getURI()+"has comment "+tmp);
236     }
237     // System.out.println(fromSuperclass);
238     expandSuperAndCopy(fromSuperclass.getURI());
239     }
240    
241     }     **/