$search
00001 package edu.tum.cs.ias.knowrob.owl; 00002 00003 import java.text.SimpleDateFormat; 00004 import java.util.Calendar; 00005 import java.util.Collection; 00006 import java.util.Collections; 00007 import java.util.HashMap; 00008 import java.util.Map; 00009 import java.util.Random; 00010 import java.util.UUID; 00011 import java.util.Vector; 00012 00013 import edu.tum.cs.ias.knowrob.prolog.PrologInterface; 00014 import edu.tum.cs.ias.knowrob.prolog.PrologQueryUtils; 00015 00016 00017 public class OWLIndividual extends OWLThing { 00018 00019 00023 protected Vector<OWLClass> types; 00024 00028 protected Map<String, Vector<String>> data_props; 00029 00033 protected Map<String, Vector<String>> obj_props; 00034 00035 00036 00044 protected OWLIndividual(String iri, String label) { 00045 00046 super(iri, label); 00047 this.types = new Vector<OWLClass>(); 00048 data_props = Collections.synchronizedMap(new HashMap<String, Vector<String>>()); 00049 obj_props = Collections.synchronizedMap(new HashMap<String, Vector<String>>()); 00050 } 00051 00052 00058 protected OWLIndividual(OWLThing ind) { 00059 this(ind.getIRI(), ind.getLabel()); 00060 } 00061 00062 00072 public static OWLIndividual getOWLIndividual(String iri, String label) { 00073 00074 // return exact match if available 00075 if(identifiers.containsKey(iri) && identifiers.get(iri) instanceof OWLIndividual) { 00076 return (OWLIndividual) identifiers.get(iri); 00077 } 00078 00079 // create OWLIndividual from higher-level objects if the existing object for this IRI has a more abstract type 00080 OWLIndividual res = new OWLIndividual(OWLThing.getOWLThing(iri, label)); 00081 identifiers.put(iri, res); 00082 return res; 00083 } 00084 00093 public static OWLIndividual getOWLIndividual(String iri) { 00094 return getOWLIndividual(iri, null); 00095 } 00096 00097 00098 00106 public static OWLIndividual getOWLIndividualOfClass(String cl) { 00107 00108 char[] chars = "abcdefghijklmnopqrstuvwxyzABSDEFGHIJKLMNOPQRSTUVWXYZ1234567890".toCharArray(); 00109 Random r = new Random(System.currentTimeMillis()); 00110 char[] id = new char[8]; 00111 for (int i = 0; i < 8; i++) { 00112 id[i] = chars[r.nextInt(chars.length)]; 00113 } 00114 00115 String new_iri = cl + "_" + new String(id); 00116 00117 OWLIndividual res = getOWLIndividual(new_iri, null); 00118 res.addType(OWLClass.getOWLClass(cl)); 00119 00120 return res; 00121 } 00122 00123 00124 00129 public Vector<OWLClass> getTypes() { 00130 return types; 00131 } 00132 00137 public void setTypes(Vector<OWLClass> types) { 00138 this.types.clear(); 00139 this.types.addAll(types); 00140 } 00141 00146 public void addType(OWLClass type) { 00147 types.add(type); 00148 } 00149 00154 public void addTypes(Collection<OWLClass> t) { 00155 types.addAll(t); 00156 } 00157 00163 public boolean hasType(OWLClass type) { 00164 00165 if(types.contains(type)) 00166 return true; 00167 else 00168 return false; 00169 } 00170 00171 public boolean hasType(String type_iri) { 00172 00173 for(OWLClass c : types) { 00174 if(c.getIRI().equals(type_iri) || c.getShortName().equals(type_iri)) 00175 return true; 00176 } 00177 return false; 00178 } 00179 00185 public Map<String, Vector<String>> getDataProperties() { 00186 return data_props; 00187 } 00188 00194 public Map<String, Vector<String>> getObjProperties() { 00195 return obj_props; 00196 } 00197 00203 public void setDataProperties(Map<String, Vector<String>> properties) { 00204 this.data_props.clear(); 00205 this.data_props.putAll(properties); 00206 } 00207 00213 public void setObjProperties(Map<String, Vector<String>> properties) { 00214 this.obj_props.clear(); 00215 this.obj_props.putAll(properties); 00216 } 00217 00218 00225 public boolean hasDataProperty(String property) { 00226 if(data_props.containsKey(property)) 00227 return true; 00228 else return false; 00229 } 00230 00237 public boolean hasObjProperty(String property) { 00238 if(obj_props.containsKey(property)) 00239 return true; 00240 else return false; 00241 } 00242 00249 public Vector<String> getDataPropValues(String property) { 00250 return data_props.get(property); 00251 } 00252 00259 public Vector<String> getObjPropValues(String property) { 00260 return obj_props.get(property); 00261 } 00262 00269 public void addDataPropValue(String property, String value) { 00270 00271 if(!data_props.containsKey(property)) 00272 data_props.put(property, new Vector<String>()); 00273 00274 data_props.get(property).add(value); 00275 } 00276 00283 public void addObjPropValue(String property, String value) { 00284 00285 if(!obj_props.containsKey(property)) 00286 obj_props.put(property, new Vector<String>()); 00287 00288 obj_props.get(property).add(value); 00289 } 00290 00291 00292 00293 public void writeToProlog() { 00294 00295 // check whether instance need to be written to avoid infinite loops 00296 if(!this.needsSaveToProlog()) { 00297 return; 00298 } 00299 00300 // set flag that this class has been written 00301 // (in the beginning of this method to avoid problems with infinite 00302 // recursion due to recursive relations) 00303 this.setSaveToProlog(false); 00304 00305 00306 // check for deleted types 00307 for(String t : PrologQueryUtils.readTypesOfInstance(iri)) { 00308 00309 OWLClass cl = OWLClass.getOWLClass(OWLThing.removeSingleQuotes(t)); 00310 if(!types.contains(cl)) { 00311 PrologInterface.executeQuery("rdf_retractall('" + iri + "', rdf:type, '" + cl.getIRI() + "')"); 00312 } 00313 } 00314 00315 // set instance types 00316 for(OWLClass t : types) { 00317 if(PrologInterface.executeQuery("rdf_has('" + iri + "', rdf:type, '" + t.getIRI() + "')") == null) 00318 PrologInterface.executeQuery("rdf_assert('" + iri + "', rdf:type, '" + t.getIRI() + "')"); 00319 } 00320 00321 00322 // write object properties 00323 for(String p : obj_props.keySet()) { 00324 00325 PrologInterface.executeQuery("rdf_retractall('" + iri + "', '"+p+"', _)"); 00326 00327 for(String v : obj_props.get(p)) { 00328 PrologQueryUtils.assertObjectPropertyForInst(iri, p, v); 00329 } 00330 } 00331 00332 // write data properties 00333 for(String p : data_props.keySet()) { 00334 00335 PrologInterface.executeQuery("rdf_retractall('" + iri + "', '"+p+"', _)"); 00336 00337 for(String v : data_props.get(p)) { 00338 String type = "http://www.w3.org/2001/XMLSchema#string"; 00339 try { 00340 Float.valueOf(v); 00341 type = "http://www.w3.org/2001/XMLSchema#float"; 00342 } catch(NumberFormatException e) {} 00343 PrologQueryUtils.assertDataPropertyForInst(iri, p, v, type); 00344 } 00345 } 00346 } 00347 }