001 package org.nlp2rdf.ontology;
002
003 import com.hp.hpl.jena.ontology.OntClass;
004 import com.hp.hpl.jena.ontology.OntModel;
005 import com.hp.hpl.jena.ontology.OntModelSpec;
006 import com.hp.hpl.jena.rdf.model.ModelFactory;
007 import org.slf4j.Logger;
008 import org.slf4j.LoggerFactory;
009
010 import java.util.*;
011
012
013 /**
014 * Indexes an Ontology
015 * skips complex classes per default, this does not affect the hierarchy outcome
016 */
017 public class ClassIndexer {
018 private static Logger log = LoggerFactory.getLogger(ClassIndexer.class);
019
020 //Options
021 private boolean copyLabels = true;
022 private boolean copyComments = true;
023 private String language = null;
024
025 //Not implemented
026 private Map<String, String> transform = new HashMap<String, String>();
027 //Not implemented
028 private Set<String> remove = new HashSet<String>();
029
030 //internal variables
031 private Map<String, OntModel> classUriToClassHierarchy = new HashMap<String, OntModel>();
032
033 public ClassIndexer() {
034 }
035
036 public void index(OntModel from) {
037 Set<OntClass> classes = from.listClasses().toSet();
038 int i = 0;
039 for (OntClass cl : classes) {
040 Tree t = new Tree(cl);
041 classUriToClassHierarchy.put(cl.getURI(), t.toModel());
042 }
043
044 }
045
046 /**
047 * @param classUri
048 * @return a filled OntModel with all superclasses of classUri or null, if no class is found
049 */
050 public OntModel getHierarchyForClassURI(String classUri) {
051 return classUriToClassHierarchy.get(classUri);
052 }
053
054 /**
055 * transforms namespaces
056 *
057 * @param in
058 * @return
059 */
060 private String transformNamespace(String in) {
061 String ret = in;
062 for (String s : transform.keySet()) {
063 if (in.startsWith(s)) {
064 return in.replace(s, transform.get(s));
065
066 }
067 }
068 return ret;
069 }
070
071 /**
072 * filters out certain namespaces
073 *
074 * @param s
075 * @return
076 */
077 private boolean filterNamespace(String s) {
078 for (String prefix : remove) {
079 if (s.startsWith(prefix)) {
080 return true;
081 }
082 }
083 return false;
084 }
085
086
087 public boolean isCopyLabels() {
088 return copyLabels;
089 }
090
091 public void setCopyLabels(boolean copyLabels) {
092 this.copyLabels = copyLabels;
093 }
094
095 public boolean isCopyComments() {
096 return copyComments;
097 }
098
099 public void setCopyComments(boolean copyComments) {
100 this.copyComments = copyComments;
101 }
102
103 public String getLanguage() {
104 return language;
105 }
106
107 public void setLanguage(String language) {
108 this.language = language;
109 }
110
111 /**
112 * A simple Helper Class to convert the hierarchy
113 */
114 private class Tree {
115 final String uri;
116 List<Tree> parents;
117 final String label;
118 final String comment;
119
120 public Tree(OntClass me) {
121 this.uri = me.getURI();
122 label = me.getLabel(language);
123 comment = me.getComment(language);
124 parents = new ArrayList<Tree>();
125
126 Set<OntClass> superClasses = me.listSuperClasses(true).toSet();
127 for (OntClass s : superClasses) {
128 //this is were complex classes are skipped
129 if (s.isAnon()) {
130 continue;
131 }
132 log.trace(s.toString());
133 parents.add(new Tree(s));
134 }
135 }
136
137 public OntModel toModel() {
138 OntModel model = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM, ModelFactory.createDefaultModel());
139 OntClass me = model.createClass(uri);
140 //TODO test this for <&>
141 if (copyLabels && label != null) {
142 me.addLabel(label, language);
143 }
144 if (copyComments && comment != null) {
145 me.addComment(comment, language);
146 }
147 for (Tree p : parents) {
148 OntClass superClass = model.createClass(p.uri);
149 me.addSuperClass(superClass);
150 model.add(p.toModel());
151 }
152 return model;
153 }
154 }
155
156 }
157
158 /**
159 public void expandSuperAndCopy(String originalClassUri) {
160
161 String newClassUri = transform(originalClassUri);
162 if (isRemove(originalClassUri) || isRemove(newClassUri)) {
163 return;
164 }
165
166
167 // create initial classes
168 OntClass toClass = toModel.createClass(newClassUri);
169 OntClass fromClass = fromModel.getOntClass(originalClassUri);
170
171 if(toClass==null || fromClass == null){
172 logger.error("null occured in fromClass "+originalClassUri+" but retrieving yielded: "+fromClass );
173 return;
174 }
175
176 //System.out.println("begin");
177 //for(OntClass cltest: fromModel.listClasses().toSet()){
178 // System.out.println(cltest.getURI());
179 // System.out.println(cltest.getClass().getSimpleName());
180 //}
181 //System.out.println("end");
182
183 if (copyLabelsAndComments ) {
184 String tmp = null;
185
186 if((tmp=fromClass.getLabel(null))!=null) {toClass.setLabel(tmp, null);}
187 // System.out.println(fromClass.getURI()+"has label "+tmp);
188
189 if((tmp=fromClass.getComment(null))!=null) {toClass.setComment(tmp, null);}
190 // System.out.println(fromClass.getURI()+"has comment "+tmp);
191 }
192
193 // get the superclasses
194 Set<OntClass> fromSuperclasses = fromClass.listSuperClasses(true).toSet();
195
196 for (OntClass fromSuperclass : fromSuperclasses) {
197 String newFromSuperclassUri = transform(fromSuperclass.getURI());
198 if (isRemove(fromSuperclass.getURI()) || isRemove(newFromSuperclassUri)) {
199 continue;
200 }
201 if(fromSuperclass.isAnon()){
202 continue;
203 }
204
205 OntClass toSuperclass = toModel.createClass(newFromSuperclassUri);
206 toClass.addSuperClass(toSuperclass);
207
208 if (copyLabelsAndComments) {
209 String tmp = null;
210 if((tmp=fromSuperclass.getLabel(null))!=null) {toSuperclass.setLabel(tmp, null);}
211 // System.out.println(fromSuperclass.getURI()+"has label "+tmp);
212
213 if((tmp=fromSuperclass.getComment(null))!=null) {toSuperclass.setComment(tmp, null);}
214 // System.out.println(fromSuperclass.getURI()+"has comment "+tmp);
215 }
216 // System.out.println(fromSuperclass);
217 expandSuperAndCopy(fromSuperclass.getURI());
218 }
219
220 } **/