Go to the documentation of this file.00001 package edu.tum.cs.ias.knowrob.owl;
00002
00003 import java.util.Hashtable;
00004 import java.util.Random;
00005
00006 public class OWLThing implements Comparable<OWLThing> {
00007
00008
00012 protected static Hashtable<String, OWLThing> identifiers = new Hashtable<String, OWLThing>();
00013
00017 protected String iri;
00018
00022 protected String label;
00023
00027 protected boolean readFromProlog = false;
00028
00029
00033 protected boolean saveToProlog = false;
00034
00035
00036
00046 public static OWLThing getOWLThing(String iri, String label) {
00047
00048 if(identifiers.containsKey(iri)) {
00049
00050 if(identifiers.get(iri).label != label)
00051 identifiers.get(iri).setLabel(label);
00052
00053 return identifiers.get(iri);
00054
00055 } else {
00056 OWLThing res = new OWLThing(iri, label);
00057 identifiers.put(iri, res);
00058 return res;
00059 }
00060 }
00061
00062
00070 protected OWLThing(String iri, String label) {
00071
00072 this.iri = iri;
00073
00074 if(label!=null)
00075 this.label = label;
00076 else
00077 this.label = getShortName();
00078
00079 }
00080
00081
00090 public static OWLThing getOWLThingByShortname(String shortname) {
00091
00092 for(String iri : identifiers.keySet()) {
00093 if(iri.endsWith(shortname)) {
00094 return identifiers.get(iri);
00095 }
00096 }
00097 return null;
00098 }
00099
00105 public static void removeOWLThing(String iri) {
00106 identifiers.remove(iri);
00107 }
00108
00109
00114 public String getIRI() {
00115 return iri;
00116 }
00117
00118
00123 public void setIri(String iri) {
00124 this.iri = iri;
00125 }
00126
00131 public String getLabel() {
00132 return label;
00133 }
00134
00139 public void setLabel(String label) {
00140 this.label = label;
00141 }
00142
00143
00150 public String getIRIPrefix(){
00151 return getPrefixOfIRI(iri);
00152 }
00153
00154
00155
00162 public String getShortName(){
00163 return getShortNameOfIRI(iri);
00164 }
00165
00166
00173 public String getIRIFilename(){
00174 return getFilenameOfIRI(iri);
00175 }
00176
00177
00178
00185 @Override
00186 public int compareTo(OWLThing o) {
00187 return this.iri.compareTo(o.getIRI());
00188 }
00189
00190
00197 public boolean equals(Object b) {
00198 if (b instanceof OWLThing) {
00199 if(this.iri.equals(((OWLThing) b).getIRI()))
00200 return true;
00201 else
00202 return false;
00203 }
00204 return false;
00205 }
00206
00210 @Override
00211 public String toString() {
00212 return this.iri;
00213 }
00214
00215
00225 public static String removeSingleQuotes(String str) {
00226 if(str.startsWith("'"))
00227 str = str.substring(1);
00228
00229 if(str.endsWith("'"))
00230 str = str.substring(0, str.length()-1);
00231 return str;
00232 }
00233
00234
00244 public static String addSingleQuotes(String str) {
00245 return "'"+removeSingleQuotes(str)+"'";
00246 }
00247
00248
00255 public static String getShortNameOfIRI(String iri) {
00256
00257 String[] elem = iri.split("#");
00258 if(elem.length>1) {
00259 String res = elem[1].replaceAll("'", "");
00260 return res;
00261 }
00262 else return iri;
00263 }
00264
00265
00266 public static String getPrefixOfIRI(String iri) {
00267
00268 String[] elem = iri.split("#");
00269 if(elem.length>1) {
00270 String res = elem[0].replaceAll("'", "");
00271 return res;
00272 }
00273 else return iri;
00274 }
00275
00276 public static String getFilenameOfIRI(String iri) {
00277
00278 String[] elem = iri.split("/");
00279 if(elem.length>1) {
00280
00281 String after_slash = elem[elem.length-1].replaceAll("'", "");
00282 String[] elem2 = after_slash.split("#");
00283
00284 if(elem2.length>1) {
00285 String res = elem2[0];
00286 return res;
00287 }
00288 else return after_slash;
00289 }
00290 else return iri;
00291 }
00292
00299 public static String getUniqueID(String iri) {
00300
00301 char[] chars = "abcdefghijklmnopqrstuvwxyzABSDEFGHIJKLMNOPQRSTUVWXYZ1234567890".toCharArray();
00302 Random r = new Random(System.currentTimeMillis());
00303 char[] id = new char[8];
00304 for (int i = 0; i < 8; i++) {
00305 id[i] = chars[r.nextInt(chars.length)];
00306 }
00307 try { Thread.sleep(5); } catch (InterruptedException e) {
00308 e.printStackTrace();
00309 }
00310 return iri + "_" + new String(id);
00311 }
00312
00313 public boolean isReadFromProlog() {
00314 return readFromProlog;
00315 }
00316
00317
00318 public void setReadFromProlog(boolean readFromProlog) {
00319 this.readFromProlog = readFromProlog;
00320 }
00321
00322
00323 public boolean needsSaveToProlog() {
00324 return saveToProlog;
00325 }
00326
00327
00328 public void setSaveToProlog(boolean saveToProlog) {
00329 this.saveToProlog = saveToProlog;
00330 }
00331
00332 }