OWLIndividual.java
Go to the documentation of this file.
00001 package edu.tum.cs.ias.knowrob.owl;
00002 
00003 import java.util.Collection;
00004 import java.util.Collections;
00005 import java.util.HashMap;
00006 import java.util.Map;
00007 import java.util.Vector;
00008 
00009 import edu.tum.cs.ias.knowrob.prolog.PrologInterface;
00010 import edu.tum.cs.ias.knowrob.prolog.PrologQueryUtils;
00011 
00012 
00013 public class OWLIndividual extends OWLThing {
00014         
00015 
00019         protected Vector<OWLClass> types;
00020 
00024         protected Map<String, Vector<String>> data_props;
00025         
00029         protected Map<String, Vector<String>> obj_props;
00030         
00031 
00032 
00040         protected OWLIndividual(String iri, String label) {
00041                 
00042                 super(iri, label);
00043                 this.types = new Vector<OWLClass>();
00044                 data_props = Collections.synchronizedMap(new HashMap<String, Vector<String>>());
00045                 obj_props = Collections.synchronizedMap(new HashMap<String, Vector<String>>());
00046         }
00047 
00048 
00054         protected OWLIndividual(OWLThing ind) {
00055                 this(ind.getIRI(), ind.getLabel());
00056         }
00057         
00058         
00068         public static OWLIndividual getOWLIndividual(String iri, String label) {
00069 
00070                 // return exact match if available
00071                 if(identifiers.containsKey(iri) && identifiers.get(iri) instanceof OWLIndividual) {
00072                         return (OWLIndividual) identifiers.get(iri);                    
00073                 }
00074                 
00075                 // create OWLIndividual from higher-level objects if the existing object for this IRI has a more abstract type
00076                 OWLIndividual res = new OWLIndividual(OWLThing.getOWLThing(iri, label));
00077                 identifiers.put(iri, res);
00078                 return res;
00079         }
00080         
00089         public static OWLIndividual getOWLIndividual(String iri) {
00090                 return getOWLIndividual(iri, null); 
00091         }
00092         
00093         
00094         
00102         public static OWLIndividual getOWLIndividualOfClass(String cl) {
00103                 
00104                 OWLIndividual res = getOWLIndividual(getUniqueID(cl), null);
00105                 res.addType(OWLClass.getOWLClass(cl));
00106                 
00107                 return res;
00108         }
00109 
00110 
00115         public Vector<OWLClass> getTypes() {
00116                 return types;
00117         }
00118 
00123         public void setTypes(Vector<OWLClass> types) {
00124                 this.types.clear();
00125                 this.types.addAll(types);
00126         }
00127 
00132         public void addType(OWLClass type) {
00133                 types.add(type);
00134         }
00135 
00140         public void addTypes(Collection<OWLClass> t) {
00141                 types.addAll(t);
00142         }
00143 
00149         public boolean hasType(OWLClass type) {
00150                 
00151                 if(types.contains(type))
00152                         return true;
00153                 else
00154                         return false;
00155         }
00156         
00157         public boolean hasType(String type_iri) {
00158                 
00159                 for(OWLClass c : types) {
00160                         if(c.getIRI().equals(type_iri) || c.getShortName().equals(type_iri))
00161                                 return true;
00162                 }
00163                 return false;
00164         }
00165 
00171         public Map<String, Vector<String>> getDataProperties() {
00172                 return data_props;
00173         }
00174         
00180         public Map<String, Vector<String>> getObjProperties() {
00181                 return obj_props;
00182         }
00183         
00189         public void setDataProperties(Map<String, Vector<String>> properties) {
00190                 this.data_props.clear();
00191                 this.data_props.putAll(properties);
00192         }
00193         
00199         public void setObjProperties(Map<String, Vector<String>> properties) {
00200                 this.obj_props.clear();
00201                 this.obj_props.putAll(properties);
00202         }
00203 
00204         
00211         public boolean hasDataProperty(String property) {
00212                 if(data_props.containsKey(property))
00213                         return true;
00214                 else return false;
00215         }
00216         
00223         public boolean hasObjProperty(String property) {
00224                 if(obj_props.containsKey(property))
00225                         return true;
00226                 else return false;
00227         }
00228 
00235         public Vector<String> getDataPropValues(String property) {
00236                 return data_props.get(property);
00237         }
00238         
00245         public Vector<String> getObjPropValues(String property) {
00246                 return obj_props.get(property);
00247         }
00248         
00255         public void addDataPropValue(String property, String value) {
00256                 
00257                 if(!data_props.containsKey(property))
00258                         data_props.put(property, new Vector<String>());
00259                 
00260                 data_props.get(property).add(value);
00261         }
00262         
00269         public void addObjPropValue(String property, String value) {
00270                 
00271                 if(!obj_props.containsKey(property))
00272                         obj_props.put(property, new Vector<String>());
00273                 
00274                 obj_props.get(property).add(value);
00275         }
00276         
00277         
00278 
00279         public void writeToProlog() {
00280                 
00281                 // check whether instance need to be written to avoid infinite loops
00282                 if(!this.needsSaveToProlog()) {
00283                         return;
00284                 }
00285 
00286                 // set flag that this class has been written 
00287                 // (in the beginning of this method to avoid problems with infinite 
00288                 // recursion due to recursive relations)
00289                 this.setSaveToProlog(false);
00290 
00291 
00292                 // check for deleted types
00293                 for(String t : PrologQueryUtils.readTypesOfInstance(iri)) {
00294                         
00295                         OWLClass cl = OWLClass.getOWLClass(OWLThing.removeSingleQuotes(t));
00296                         if(!types.contains(cl)) {
00297                                 PrologInterface.executeQuery("rdf_retractall('" + iri + "', rdf:type, '" + cl.getIRI() + "')");
00298                         }
00299                 }
00300 
00301                 // set instance types
00302                 for(OWLClass t : types) {
00303                         if(PrologInterface.executeQuery("rdf_has('" + iri + "', rdf:type, '" + t.getIRI() + "')") == null)
00304                                 PrologInterface.executeQuery("rdf_assert('" + iri + "', rdf:type, '" + t.getIRI() + "')");
00305                 }
00306                 
00307                 
00308                 // write object properties
00309                 for(String p : obj_props.keySet()) {
00310                         
00311                         PrologInterface.executeQuery("rdf_retractall('" + iri + "', '"+p+"', _)");
00312                 
00313                         for(String v : obj_props.get(p)) {
00314                                 PrologQueryUtils.assertObjectPropertyForInst(iri, p, v);
00315                         }
00316                 }
00317                 
00318                 // write data properties
00319                 for(String p : data_props.keySet()) {
00320 
00321                         PrologInterface.executeQuery("rdf_retractall('" + iri + "', '"+p+"', _)");
00322 
00323                         for(String v : data_props.get(p)) {                             
00324                                 String type = "http://www.w3.org/2001/XMLSchema#string";
00325                                 try {
00326                                         Float.valueOf(v);
00327                                         type = "http://www.w3.org/2001/XMLSchema#float";
00328                                 } catch(NumberFormatException e) {}
00329                                 PrologQueryUtils.assertDataPropertyForInst(iri, p, v, type);
00330                         }
00331                 }
00332         }
00333 }


knowrob_common
Author(s): Moritz Tenorth
autogenerated on Mon Oct 6 2014 01:29:31