OWLClass.java
Go to the documentation of this file.
00001 package edu.tum.cs.ias.knowrob.owl;
00002 
00003 import java.io.UnsupportedEncodingException;
00004 import java.net.URLDecoder;
00005 import java.util.Collections;
00006 import java.util.HashMap;
00007 import java.util.HashSet;
00008 import java.util.LinkedHashMap;
00009 import java.util.Map;
00010 import java.util.Vector;
00011 
00012 import edu.tum.cs.ias.knowrob.prolog.PrologInterface;
00013 import edu.tum.cs.ias.knowrob.prolog.PrologQueryUtils;
00014 
00015 
00016 public class OWLClass extends OWLThing {
00017 
00018 
00022         protected Vector<OWLClass> subclasses;
00023 
00027         protected Vector<OWLClass> superclasses;
00028 
00029 
00033         protected Map<String, Vector<String>> some_values_from;
00034 
00038         protected Map<String, Vector<String>> all_values_from;
00039 
00043         protected Map<String, Vector<String>> has_value;
00044 
00045 
00046 
00047 
00055         protected OWLClass(String iri, String label) {
00056 
00057                 super(iri, label);
00058                 this.superclasses = new Vector<OWLClass>();
00059                 this.subclasses = new Vector<OWLClass>();
00060 
00061                 some_values_from = Collections.synchronizedMap(new LinkedHashMap<String, Vector<String>>());
00062                 all_values_from = Collections.synchronizedMap(new LinkedHashMap<String, Vector<String>>());
00063                 has_value = Collections.synchronizedMap(new LinkedHashMap<String, Vector<String>>());
00064         }
00065 
00066 
00072         protected OWLClass(OWLThing ind) {
00073                 this(ind.getIRI(), ind.getLabel());
00074         }
00075 
00076 
00086         public static OWLClass getOWLClass(String iri, String label) {
00087 
00088                 // return exact match if available
00089                 if(identifiers.containsKey(iri) && identifiers.get(iri) instanceof OWLClass) {
00090                         return (OWLClass) identifiers.get(iri);                 
00091                 }
00092 
00093                 OWLClass res = new OWLClass(OWLThing.getOWLThing(iri, label));
00094                 identifiers.put(iri, res);
00095                 return res;
00096         }
00097 
00106         public static OWLClass getOWLClass(String iri) {
00107                 return getOWLClass(iri, null); 
00108         }
00109 
00110 
00111 
00112 
00118         public Vector<OWLClass> getSubclasses() {
00119                 return subclasses;
00120         }
00121 
00122 
00128         public void addSubclass(OWLClass sub) {
00129                 subclasses.add(sub);
00130         }
00131 
00132 
00138         public void removeSubclass(OWLClass sub) {
00139                 subclasses.remove(sub);
00140         }
00141 
00142 
00143 
00149         public void setSuperClasses(Vector<OWLClass> p) {
00150                 this.superclasses.clear();
00151                 this.superclasses.addAll(p);
00152         }
00153 
00159         public void addSuperClass(OWLClass p) {
00160                 
00161                 if(!superclasses.contains(p))
00162                         superclasses.add(p);
00163         }
00164 
00165 
00170         public Vector<OWLClass> getSuperClasses() {
00171                 return superclasses;
00172         }
00173 
00174 
00175 
00176 
00182         public void setSomeValuesFrom(Map<String, Vector<String>> someValuesFrom) {
00183                 this.some_values_from.putAll(someValuesFrom);
00184         }
00185 
00186 
00193         public void addSomeValuesFrom(String prop, String classdef) {
00194 
00195                 if(!some_values_from.containsKey(prop)) {
00196                         some_values_from.put(prop, new Vector<String>());
00197                 }
00198                 some_values_from.get(prop).add(classdef);
00199         }
00200 
00201 
00206         public Map<String, Vector<String>> getSomeValuesFrom() {
00207                 return some_values_from;
00208         }
00209 
00210 
00211 
00212 
00218         public void setAllValuesFrom(Map<String, Vector<String>> allValuesFrom) {
00219                 this.all_values_from.putAll(allValuesFrom);
00220         }
00221 
00222 
00229         public void addAllValuesFrom(String prop, String classdef) {
00230 
00231                 if(!all_values_from.containsKey(prop)) {
00232                         all_values_from.put(prop, new Vector<String>());
00233                 }
00234                 all_values_from.get(prop).add(classdef);
00235         }
00236 
00237 
00243         public Map<String, Vector<String>> getAllValuesFrom() {
00244                 return all_values_from;
00245         }
00246 
00247 
00248 
00249 
00255         public void setHasValue(Map<String, Vector<String>> hasValue) {
00256                 this.has_value.putAll(hasValue);
00257         }
00258 
00259 
00266         public void addHasValue(String prop, String value) {
00267 
00268                 if(!has_value.containsKey(prop)) {
00269                         has_value.put(prop, new Vector<String>());
00270                 }
00271                 has_value.get(prop).add(value);
00272         }
00273 
00274 
00279         public Map<String, Vector<String>> getHasValue() {
00280                 return has_value;
00281         }
00282 
00283 
00284 
00288         public void readFromProlog() {
00289 
00290                 if(isReadFromProlog())
00291                         return;
00292                 
00293                 // Read the action's label if an rdfs:label is set (assuming IRI has been set during initialization) 
00294                 try {
00295                         HashMap<String, Vector<String>> qLabel = PrologInterface.executeQuery("rdf_has('"+iri+"',rdfs:label,L),util:strip_literal_type(L,Label)");
00296 
00297                         if(qLabel.get("Label")!=null && qLabel.get("Label").size()>0) {
00298                                 this.label = OWLThing.removeSingleQuotes(qLabel.get("Label").get(0));
00299                         }                       
00300                 } catch (Exception e) { } // fail silently if no label is set
00301 
00302 
00303                 // Read superclasses 
00304                 HashMap<String, Vector<String>> qSuper = PrologInterface.executeQuery("owl_direct_subclass_of('" + iri + "', Super)");
00305 
00306                 if(qSuper != null) {
00307 
00308                         for(String sup : qSuper.get("Super")) {
00309 
00310                                 if (sup.contains("__Description"))
00311                                         continue;
00312 
00313                                 OWLClass superclass = OWLClass.getOWLClass(OWLThing.removeSingleQuotes(sup));
00314                                 
00315                                 if(!superclasses.contains(superclass))
00316                                         superclasses.add(superclass);
00317                         }
00318                 }
00319 
00320                 // Recursively read subclasses 
00321                 HashMap<String, Vector<String>> subclasses = PrologInterface.executeQuery(
00322                                 "owl_direct_subclass_of(Sub, '" + iri + "')");
00323 
00324                 if(subclasses!=null && subclasses.get("Sub") != null) {
00325 
00326                         this.subclasses.clear();
00327 
00328                         for(String sub_iri : subclasses.get("Sub")) {
00329 
00330                                 OWLClass sub = OWLClass.getOWLClass(OWLThing.removeSingleQuotes(sub_iri));
00331                                 sub.readFromProlog();
00332 
00333                                 this.addSubclass(sub);
00334                                 sub.addSuperClass(this);
00335                         }
00336                 }
00337 
00338                 // Read class properties
00339                 try {
00340 
00341                         HashMap<String, Vector<String>> qProp = 
00342                                 PrologInterface.executeQuery("((class_properties_some('"+iri+"', Prop, V), Type='some'); " +
00343                                                 "(class_properties_all('"+iri+"', Prop, V), Type='all'); " +
00344                                                 "(class_properties_value('"+iri+"', Prop, V), Type='value')), " +
00345                                 "util:strip_literal_type(V,Val)");
00346 
00347                         if(qProp != null) {
00348 
00349                                 Vector<String> prop = qProp.get("Prop");
00350                                 Vector<String> val  = qProp.get("Val");
00351                                 Vector<String> type = qProp.get("Type");
00352 
00353 
00354                                 // Make sure each property is added only once 
00355                                 // (properties may be present two or more times in the result set)
00356 
00357                                 HashSet<String> alreadyAdded = new HashSet<String>();
00358                                 if(prop != null && val != null)
00359 
00360                                         for(int i=0;i<prop.size() && i<val.size();i++) {
00361 
00362                                                 if (alreadyAdded.contains(prop.get(i)+val.get(i)))
00363                                                         continue;
00364 
00365                                                 alreadyAdded.add(prop.get(i)+val.get(i));
00366                                                 String p = OWLThing.removeSingleQuotes(prop.get(i));
00367                                                 String v = OWLThing.removeSingleQuotes(val.get(i));
00368 
00369                                                 // HACK: remove faulty local URLs generated by Prolog export
00370                                                 if(v.startsWith("file:")) {
00371                                                         String[] vs = v.split("/");
00372                                                         v = vs[vs.length-1];
00373                                                         
00374                                                         try {
00375                                                                 v = URLDecoder.decode(URLDecoder.decode(v, "UTF-8"), "UTF-8");
00376                                                         } catch (UnsupportedEncodingException e) {
00377                                                                 e.printStackTrace();
00378                                                         }
00379                                                 }
00380                                                 
00381                                                 if(type.get(i).contains("some")) {
00382                                                         this.addSomeValuesFrom(p, v);
00383 
00384                                                 } else if(type.get(i).contains("all")) {
00385                                                         this.addAllValuesFrom(p, v);
00386 
00387                                                 } else if(type.get(i).contains("value")) {
00388                                                         this.addHasValue(p, v);
00389                                                 }
00390                                         }
00391                         }
00392 
00393                 } catch (Exception e) {
00394                         e.printStackTrace();
00395                 }
00396 
00397                 // set flag that this class has been read
00398                 this.setReadFromProlog(true);
00399         }
00400 
00401 
00405         public void writeToProlog() {
00406 
00407 
00408                 // check whether classes need to be written to avoid infinite loops
00409                 if(!this.needsSaveToProlog()) {
00410                         return;
00411                 }
00412                 
00413                 
00414                 // set flag that this class has been written 
00415                 // (in the beginning of this method to avoid problems with infinite 
00416                 // recursion due to recursive relations)
00417                 this.setSaveToProlog(false);
00418                 
00419                 
00420                 // write label
00421                 PrologInterface.executeQuery("rdf_assert('" + iri + "', rdfs:label, literal(type('http://www.w3.org/2001/XMLSchema#string','"+label+"')))"); 
00422 
00423                 
00424                 // Read class properties and check for removed ones
00425                 HashMap<String, Vector<String>> qProp = 
00426                         PrologInterface.executeQuery("((class_properties_some('"+iri+"', Prop, V), Type='some'); " +
00427                                         "(class_properties_all('"+iri+"', Prop, V), Type='all'); " +
00428                                         "(class_properties_value('"+iri+"', Prop, V), Type='value')), " +
00429                         "util:strip_literal_type(V,Val)");
00430 
00431                 if(qProp != null) {
00432 
00433                         Vector<String> prop = qProp.get("Prop");
00434                         Vector<String> val  = qProp.get("Val");
00435                         Vector<String> type = qProp.get("Type");
00436 
00437                         if(prop != null && val != null) {
00438                                 
00439                                 for(int i=0;i<prop.size() && i<val.size();i++) {
00440 
00441                                         String p = OWLThing.removeSingleQuotes(prop.get(i));
00442                                         String v = OWLThing.removeSingleQuotes(val.get(i));
00443 
00444                                         if(type.get(i).contains("some")) {
00445 
00446                                                 if((!some_values_from.containsKey(p)) || (!some_values_from.get(p).contains(v))) { // TODO: too greedy!, removes all subActions
00447 
00448                                                         // remove p,v pair
00449                                                         PrologInterface.executeQuery("findall(R, (rdfs_subclass_of('"+iri+"', R), " +
00450                                                                         "rdf_has(R, 'http://www.w3.org/2002/07/owl#onProperty','"+p+"'), " +
00451                                                                         "rdf_has(R, 'http://www.w3.org/2002/07/owl#someValuesFrom', '"+v+"')), Rs), " +
00452                                                                         "member(Restr, Rs), rdf_retractall(Restr, _, _), " +
00453                                                                         "rdf_retractall(_, 'http://www.w3.org/2000/01/rdf-schema#subClassOf', Restr)");
00454                                                 }
00455 
00456                                         } else if(type.get(i).contains("all")) {
00457 
00458                                                 if((!all_values_from.containsKey(p)) || (!all_values_from.get(p).contains(v))) {
00459 
00460                                                         // remove p,v pair
00461                                                         PrologInterface.executeQuery("findall(R, (rdfs_subclass_of('"+iri+"', R), " +
00462                                                                         "rdf_has(R, 'http://www.w3.org/2002/07/owl#onProperty','"+p+"'), " +
00463                                                                         "rdf_has(R, 'http://www.w3.org/2002/07/owl#allValuesFrom', '"+v+"')), Rs), " +
00464                                                                         "member(Restr, Rs), rdf_retractall(Restr, _, _), " +
00465                                                                         "rdf_retractall(_, 'http://www.w3.org/2000/01/rdf-schema#subClassOf', Restr)");
00466                                                 }
00467 
00468                                         } else if(type.get(i).contains("value")) {
00469 
00470                                                 if((!has_value.containsKey(p)) || (!has_value.get(p).contains(v))) {
00471 
00472                                                         // remove p,v pair
00473                                                         PrologInterface.executeQuery("findall(R, (rdfs_subclass_of('"+iri+"', R), " + 
00474                                                                         "rdf_has(R, 'http://www.w3.org/2002/07/owl#onProperty','"+p+"'), " +
00475                                                                         "( rdf_has(R, 'http://www.w3.org/2002/07/owl#hasValue', literal(type(_,'"+v+"'))); " +
00476                                                                         "  rdf_has(R, 'http://www.w3.org/2002/07/owl#hasValue', '"+v+"'))), Rs), " +
00477                                                                         "member(Restr, Rs), rdf_retractall(Restr, _, _), " +
00478                                                                         "rdf_retractall(_, 'http://www.w3.org/2000/01/rdf-schema#subClassOf', Restr)");
00479                                                 }
00480                                         }
00481                                 }
00482                         }
00483                 }
00484                 
00485                 
00486                 
00487                 // write class properties by creating appropriate restrictions
00488                 for(String prop : has_value.keySet()) {
00489                         for(String value : has_value.get(prop)) {
00490                                 PrologQueryUtils.createRestriction(iri,prop, value, "http://www.w3.org/2002/07/owl#hasValue", "knowrob_java");
00491                         }
00492                 }
00493                 
00494                 for(String prop : some_values_from.keySet()) {
00495                         for(String value : some_values_from.get(prop)) {
00496                                 PrologQueryUtils.createRestriction(iri, prop, value, "http://www.w3.org/2002/07/owl#someValuesFrom", "knowrob_java");
00497                         }
00498                 }
00499                 
00500                 for(String prop : all_values_from.keySet()) {
00501                         for(String value : all_values_from.get(prop)) {
00502                                 PrologQueryUtils.createRestriction(iri, prop, value, "http://www.w3.org/2002/07/owl#allValuesFrom", "knowrob_java");
00503                         }
00504                 }
00505                 
00506                 // write superclass statement
00507                 for(OWLClass sup : superclasses) {
00508                         PrologQueryUtils.assertSubClassOf(iri, sup.getIRI());
00509                 }
00510                 
00511                 // write subclasses (by triggering their output method)
00512                 for(OWLClass sub : subclasses) {
00513                         sub.writeToProlog();
00514                 }
00515                 
00516         }
00517 }


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