001 package nl.tudelft.tbm.eeni.owl2java.model.jmodel;
002
003 import com.hp.hpl.jena.ontology.OntClass;
004 import com.hp.hpl.jena.ontology.OntModel;
005 import com.hp.hpl.jena.ontology.OntProperty;
006 import nl.tudelft.tbm.eeni.owl2java.model.jenautils.ResourceError;
007 import nl.tudelft.tbm.eeni.owl2java.model.jmodel.utils.LogUtils;
008 import nl.tudelft.tbm.eeni.owl2java.model.jmodel.utils.NamingUtils;
009 import nl.tudelft.tbm.eeni.owl2java.model.ns.NamespaceUtils;
010 import nl.tudelft.tbm.eeni.owl2java.utils.IReporting;
011 import nl.tudelft.tbm.eeni.owl2java.utils.IStatistics;
012 import nl.tudelft.tbm.eeni.owl2java.utils.StringUtils;
013 import org.apache.commons.collections.ListUtils;
014 import org.apache.commons.logging.Log;
015 import org.apache.commons.logging.LogFactory;
016 import org.jgraph.graph.DefaultEdge;
017
018 import java.util.*;
019
020
021 public class JModel implements IReporting, IStatistics {
022
023 private static Log log = LogFactory.getLog(JModel.class);
024
025 private static String THINGNAME = "Thing";
026 public static String BASEPREFIX = "";
027
028 private Map<String, String> ns2prefix = new HashMap<String, String>();
029 private Map<String, String> ns2javaPkgName = new HashMap<String, String>();
030 private Map<String, JPackage> pkgName2Package = new HashMap<String, JPackage>();
031 private Map<String, JClass> uri2class = new HashMap<String, JClass>();
032 private Map<String, JProperty> uri2property = new HashMap<String, JProperty>();
033 private JInheritanceGraph<JProperty, DefaultEdge> propertyGraph;
034 private JInheritanceGraph<JClass, DefaultEdge> classGraph;
035 private List<ResourceError> ontResourceErrors = new ArrayList<ResourceError>();
036
037 private String baseThingUri;
038 private OntModel ontModel;
039
040 public JModel() {
041 propertyGraph = new JInheritanceGraph<JProperty, DefaultEdge>(DefaultEdge.class);
042 classGraph = new JInheritanceGraph<JClass, DefaultEdge>(DefaultEdge.class);
043 }
044
045 public void addOntResourceError(ResourceError error) {
046 ontResourceErrors.add(error);
047 }
048
049 public void addPackage(String uri, String pkgName) {
050 ns2javaPkgName.put(uri, pkgName);
051 }
052
053 public boolean isBaseThing(JClass cls) {
054 String clsUri = cls.getMapUri();
055 String baseUri = getBaseThingUri();
056 return (clsUri.equals(baseUri));
057 }
058
059
060 public void addPackage(String pkgName, JPackage pkg) {
061 if (pkgName2Package.containsKey(pkgName)) {
062 log.warn("Package exists: " + pkgName);
063 return;
064 }
065 this.pkgName2Package.put(pkgName, pkg);
066 }
067
068 public JPackage getJPackage(String packageName) {
069 return this.pkgName2Package.get(packageName);
070 }
071
072 public JClass getAnonymousJClass(JClass.AnonymousClassType type, List<? extends OntClass> operands) {
073 Iterator<JClass> classIterator = uri2class.values().iterator();
074
075 // Loop over all known classes to see whether it matches the signature of the anonymous class we are looking for
076 while (classIterator.hasNext()) {
077 JClass candidate = classIterator.next();
078
079 // Is the candidate class of the type we are looking for
080 if (candidate.getAnonymousClassType() == type) {
081 List<? extends OntClass> candidateOperands = candidate.getAnonymousClassOperands();
082 // See whether either either of the operands lists is null
083 if (operands == null || candidateOperands == null) {
084 // If both operand lists are null, this is the class we are looking for
085 if (operands == candidateOperands) {
086 return candidate;
087 }
088 } else {
089 // If both operand lists contain the same classes, this is the class we are looking for
090 if (operands.size() == candidateOperands.size() && ListUtils.subtract(operands, candidateOperands).isEmpty()) {
091 return candidate;
092 }
093 }
094 }
095 }
096
097 // We did not find any classes matching the signature of the anonymous class we are loking for
098 return null;
099 }
100
101 public boolean hasPackage(String packageName) {
102 return this.pkgName2Package.containsKey(packageName);
103 }
104
105 public static String getBaseThingName() {
106 return THINGNAME;
107 }
108
109 public List<String> listNamespaces() {
110 List<String> nss = new ArrayList<String>();
111 Iterator<String> it = ns2prefix.keySet().iterator();
112 while (it.hasNext()) {
113 String nsUri = (String) it.next();
114 nss.add(nsUri);
115 }
116 return nss;
117 }
118
119 public List<JPackage> listPackages() {
120 List<JPackage> pkgs = new ArrayList<JPackage>();
121 Iterator<String> it = pkgName2Package.keySet().iterator();
122 while (it.hasNext()) {
123 String pkgName = (String) it.next();
124 JPackage pkg = pkgName2Package.get(pkgName);
125 pkgs.add(pkg);
126 }
127 return pkgs;
128 }
129
130 public List<String> listNamespaceURIs() {
131 Set<String> namespaceSet = new HashSet<String>();
132 Iterator<String> nsIt = ns2prefix.keySet().iterator();
133 while (nsIt.hasNext()) {
134 String ns = (String) nsIt.next();
135 if (!NamespaceUtils.defaultNs2UriMapping.containsKey(ns)) {
136 namespaceSet.add(ns);
137 }
138 }
139 return new ArrayList<String>(namespaceSet);
140 }
141
142 public List<String> listImportURIs() {
143 Set<String> importsSet = new HashSet<String>();
144 Iterator<String> nsIt = ns2prefix.keySet().iterator();
145 while (nsIt.hasNext()) {
146 String ns = (String) nsIt.next();
147 if (!NamespaceUtils.defaultNs2UriMapping.containsKey(ns)) {
148 importsSet.add(ns.replaceFirst("[:#]$", ""));
149 }
150 }
151 return new ArrayList<String>(importsSet);
152 }
153
154 public String getJModelReport() {
155 String report = new String();
156 report += StringUtils.toHeader("JModel report");
157
158 report += StringUtils.toSubHeader("Owl Namespaces and Prefixes");
159 Iterator<String> it = ns2prefix.keySet().iterator();
160 while (it.hasNext()) {
161 String key = (String) it.next();
162 report += StringUtils.indentText(key + ": " + ns2prefix.get(key)) + "\n";
163 }
164
165 report += StringUtils.toSubHeader("Owl Namespaces and Java Packages ");
166 it = ns2javaPkgName.keySet().iterator();
167 while (it.hasNext()) {
168 String key = (String) it.next();
169 report += StringUtils.indentText(key + ": " + ns2prefix.get(key)) + "\n";
170 }
171
172 report += StringUtils.toHeader("Classes");
173 it = uri2class.keySet().iterator();
174 while (it.hasNext()) {
175 String key = (String) it.next();
176 JClass cls = uri2class.get(key);
177 report += cls.getJModelReport();
178 }
179
180 report += StringUtils.toHeader("Errors");
181 for (ResourceError res : ontResourceErrors) {
182 report += res.getJModelReport() + "\n";
183 }
184 return report;
185 }
186
187 public String getPrefix(String namespace) {
188 String prefix = ns2prefix.get(namespace);
189 if (prefix == null)
190 return "";
191 return prefix;
192 }
193
194 public String getNamespace(String prefix) {
195 Iterator<String> it = ns2prefix.keySet().iterator();
196 while (it.hasNext()) {
197 String ns = (String) it.next();
198 String value = ns2prefix.get(ns);
199 if (value == prefix)
200 return ns;
201 }
202 return null;
203 }
204
205 public void addNamespacePrefix(String ns, String prefix) {
206 ns2prefix.put(ns, prefix);
207 }
208
209 public String getBaseNamespace() {
210 return getNamespace(BASEPREFIX);
211 }
212
213 public static String getBasePrefix() {
214 return BASEPREFIX;
215 }
216
217 public boolean hasJClass(String uri) {
218 return this.uri2class.containsKey(uri);
219 }
220
221 public JClass getJClass(String uri) {
222 return this.uri2class.get(uri);
223 }
224
225 public List<JClass> listJClasses() {
226 List<JClass> classes = new ArrayList<JClass>();
227 Iterator<String> it = uri2class.keySet().iterator();
228 while (it.hasNext()) {
229 String uri = (String) it.next();
230 JClass cls = uri2class.get(uri);
231 classes.add(cls);
232 }
233 return classes;
234 }
235
236 public List<JProperty> listJProperties() {
237 List<JProperty> props = new ArrayList<JProperty>();
238 Iterator<String> it = uri2property.keySet().iterator();
239 while (it.hasNext()) {
240 String uri = (String) it.next();
241 JProperty prop = uri2property.get(uri);
242 props.add(prop);
243 }
244 return props;
245 }
246
247 public void createJClass(String clsName, String clsUri, String pkgName) {
248 clsName = NamingUtils.getValidJavaName(clsName);
249 JClass cls = new JClass(this, clsName, clsUri);
250 uri2class.put(clsUri, cls);
251
252 getJPackage(pkgName).addClass(cls);
253 log.debug(LogUtils.toLogName(cls) + ": Creating class " + cls.getName() + " in package "
254 + cls.getJavaPackageName());
255 }
256
257 public void createJClass(OntClass ontClass, String basePackage) {
258 // class already exists > return it
259 if (uri2class.containsKey(ontClass.getURI())) {
260 log.warn("Class exists: " + ontClass.getURI());
261 }
262
263 // create an class for the class, add it to our mapping and
264 // also add it to the relevant package
265 String clsName = ontClass.getLocalName();
266 String name = NamingUtils.getValidJavaName(clsName);
267 String uri = ontClass.getURI();
268 String ns = ontClass.getNameSpace();
269 String pkgName = ns2javaPkgName.get(ns);
270
271 // make sure that we have a valid package name; no class for
272 // owl:Thing... created
273 createJClass(name, uri, pkgName);
274
275 }
276
277 public boolean hasJProperty(String uri) {
278 return this.uri2property.containsKey(uri);
279 }
280
281 public boolean hasNamespace(String uri) {
282 return ns2prefix.containsKey(uri);
283 }
284
285 public JProperty getJProperty(String uri) {
286 return this.uri2property.get(uri);
287 }
288
289 public void createJProperty(OntProperty ontProp) {
290 String propName = NamingUtils.getPropertyName(ontProp);
291 JProperty p = new JProperty(this, propName, ontProp.getURI());
292 uri2property.put(ontProp.getURI(), p);
293 log.debug(LogUtils.toLogName(ontProp) + ": Creating property " + p.getName());
294 }
295
296 public String createNewPrefix() {
297 String prefixBase = "p";
298 int id = 1;
299 String prefix = prefixBase + id;
300 while (ns2prefix.containsValue(prefix)) {
301 id++;
302 prefix = prefixBase + id;
303 }
304 return prefix;
305 }
306
307 public String getStatistics() {
308 String ret = new String();
309 ret += StringUtils.toHeader("Statistics");
310 ret += "Classes: " + uri2class.size() + "\n";
311 ret += "Properties: " + uri2property.size() + "\n";
312
313 int restrictionCount = 0;
314 Iterator<String> clsIt = uri2class.keySet().iterator();
315 while (clsIt.hasNext()) {
316 String clsUri = (String) clsIt.next();
317 JClass cls = uri2class.get(clsUri);
318 restrictionCount += cls.listDomainRestrictionContainers().size();
319 }
320 ret += "Restrictions: " + restrictionCount + "\n";
321 ret += "Errors: " + ontResourceErrors.size();
322 return ret;
323 }
324
325 public void setOntModel(OntModel ontModel) {
326 this.ontModel = ontModel;
327 }
328
329 public OntModel getOntModel() {
330 return ontModel;
331 }
332
333 public String getBaseThingUri() {
334 return baseThingUri;
335 }
336
337 public void setBaseThingUri(String baseThingUri) {
338 this.baseThingUri = baseThingUri;
339 }
340
341 public JInheritanceGraph<JProperty, DefaultEdge> getPropertyGraph() {
342 return propertyGraph;
343 }
344
345 public JInheritanceGraph<JClass, DefaultEdge> getClassGraph() {
346 return classGraph;
347 }
348
349 public Map<String, JProperty> getUri2property() {
350 return uri2property;
351 }
352
353 public JClass getBaseThing() {
354 return uri2class.get(baseThingUri);
355 }
356
357 }