From: <jen...@us...> - 2008-02-26 13:25:40
|
Revision: 641 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=641&view=rev Author: jenslehmann Date: 2008-02-26 05:25:32 -0800 (Tue, 26 Feb 2008) Log Message: ----------- implemented datatype ABox reasoning/querying using OWL API Modified Paths: -------------- trunk/src/dl-learner/org/dllearner/core/Reasoner.java trunk/src/dl-learner/org/dllearner/core/ReasonerComponent.java trunk/src/dl-learner/org/dllearner/core/owl/DatatypeProperty.java trunk/src/dl-learner/org/dllearner/reasoning/FastInstanceChecker.java trunk/src/dl-learner/org/dllearner/reasoning/OWLAPIReasoner.java Modified: trunk/src/dl-learner/org/dllearner/core/Reasoner.java =================================================================== --- trunk/src/dl-learner/org/dllearner/core/Reasoner.java 2008-02-26 10:47:09 UTC (rev 640) +++ trunk/src/dl-learner/org/dllearner/core/Reasoner.java 2008-02-26 13:25:32 UTC (rev 641) @@ -24,6 +24,7 @@ import java.util.Set; import java.util.SortedSet; +import org.dllearner.core.owl.Constant; import org.dllearner.core.owl.DatatypeProperty; import org.dllearner.core.owl.NamedClass; import org.dllearner.core.owl.Description; @@ -71,6 +72,8 @@ public Map<Individual, SortedSet<Individual>> getRoleMembers(ObjectProperty atomicRole) throws ReasoningMethodUnsupportedException; + public Map<Individual, SortedSet<Constant>> getDatatypeMembers(DatatypeProperty datatypeProperty) throws ReasoningMethodUnsupportedException; + public boolean instanceCheck(Description concept, Individual individual) throws ReasoningMethodUnsupportedException; // mehrere instance checks für ein Konzept - spart bei DIG Anfragen Modified: trunk/src/dl-learner/org/dllearner/core/ReasonerComponent.java =================================================================== --- trunk/src/dl-learner/org/dllearner/core/ReasonerComponent.java 2008-02-26 10:47:09 UTC (rev 640) +++ trunk/src/dl-learner/org/dllearner/core/ReasonerComponent.java 2008-02-26 13:25:32 UTC (rev 641) @@ -23,8 +23,11 @@ import java.util.Map; import java.util.Set; import java.util.SortedSet; +import java.util.TreeMap; import java.util.TreeSet; +import java.util.Map.Entry; +import org.dllearner.core.owl.Constant; import org.dllearner.core.owl.DatatypeProperty; import org.dllearner.core.owl.NamedClass; import org.dllearner.core.owl.Description; @@ -75,6 +78,75 @@ throw new ReasoningMethodUnsupportedException(); } + public Map<Individual, SortedSet<Constant>> getDatatypeMembers(DatatypeProperty datatypeProperty) + throws ReasoningMethodUnsupportedException { + throw new ReasoningMethodUnsupportedException(); + } + + // convenience method to get int value mappings of a datatype property + public Map<Individual, SortedSet<Integer>> getIntDatatypeMembers(DatatypeProperty datatypeProperty) throws ReasoningMethodUnsupportedException { + Map<Individual, SortedSet<Constant>> mapping = getDatatypeMembers(datatypeProperty); + Map<Individual, SortedSet<Integer>> ret = new TreeMap<Individual, SortedSet<Integer>>(); + for(Entry<Individual, SortedSet<Constant>> e : mapping.entrySet()) { + SortedSet<Constant> values = e.getValue(); + SortedSet<Integer> valuesInt = new TreeSet<Integer>(); + for(Constant c : values) { + int v = Integer.parseInt(c.getLiteral()); + valuesInt.add(v); + } + ret.put(e.getKey(),valuesInt); + } + return ret; + } + + // convenience method to get double value mappings of a datatype property + public Map<Individual, SortedSet<Double>> getDoubleDatatypeMembers(DatatypeProperty datatypeProperty) throws ReasoningMethodUnsupportedException { + Map<Individual, SortedSet<Constant>> mapping = getDatatypeMembers(datatypeProperty); + Map<Individual, SortedSet<Double>> ret = new TreeMap<Individual, SortedSet<Double>>(); + for(Entry<Individual, SortedSet<Constant>> e : mapping.entrySet()) { + SortedSet<Constant> values = e.getValue(); + SortedSet<Double> valuesDouble = new TreeSet<Double>(); + for(Constant c : values) { + double v = Double.parseDouble(c.getLiteral()); + valuesDouble.add(v); + } + ret.put(e.getKey(),valuesDouble); + } + return ret; + } + + // convenience method to get boolean value mappings of a datatype property + public Map<Individual, SortedSet<Boolean>> getBooleanDatatypeMembers(DatatypeProperty datatypeProperty) throws ReasoningMethodUnsupportedException { + Map<Individual, SortedSet<Constant>> mapping = getDatatypeMembers(datatypeProperty); + Map<Individual, SortedSet<Boolean>> ret = new TreeMap<Individual, SortedSet<Boolean>>(); + for(Entry<Individual, SortedSet<Constant>> e : mapping.entrySet()) { + SortedSet<Constant> values = e.getValue(); + SortedSet<Boolean> valuesBoolean = new TreeSet<Boolean>(); + for(Constant c : values) { + boolean v = Boolean.parseBoolean(c.getLiteral()); + valuesBoolean.add(v); + } + ret.put(e.getKey(),valuesBoolean); + } + return ret; + } + + // convenience method returning those values which have value "true" for this + // datatype property + public SortedSet<Individual> getTrueDatatypeMembers(DatatypeProperty datatypeProperty) throws ReasoningMethodUnsupportedException { + Map<Individual, SortedSet<Constant>> mapping = getDatatypeMembers(datatypeProperty); + SortedSet<Individual> ret = new TreeSet<Individual>(); + for(Entry<Individual, SortedSet<Constant>> e : mapping.entrySet()) { + SortedSet<Constant> values = e.getValue(); + for(Constant c : values) { + boolean v = Boolean.parseBoolean(c.getLiteral()); + if(v == true) + ret.add(e.getKey()); + } + } + return ret; + } + public boolean instanceCheck(Description concept, Individual individual) throws ReasoningMethodUnsupportedException { throw new ReasoningMethodUnsupportedException(); Modified: trunk/src/dl-learner/org/dllearner/core/owl/DatatypeProperty.java =================================================================== --- trunk/src/dl-learner/org/dllearner/core/owl/DatatypeProperty.java 2008-02-26 10:47:09 UTC (rev 640) +++ trunk/src/dl-learner/org/dllearner/core/owl/DatatypeProperty.java 2008-02-26 13:25:32 UTC (rev 641) @@ -27,7 +27,7 @@ * @author Jens Lehmann * */ -public class DatatypeProperty implements Property, NamedKBElement { +public class DatatypeProperty implements Comparable<DatatypeProperty>, Property, NamedKBElement { protected String name; @@ -46,14 +46,23 @@ return name; } - /* (non-Javadoc) - * @see org.dllearner.core.dl.KBElement#toString(java.lang.String, java.util.Map) - */ + @Override + public String toString() { + return toString(null, null); + } + public String toString(String baseURI, Map<String, String> prefixes) { return "\"" + Helper.getAbbreviatedString(name, baseURI, prefixes) + "\""; } public void accept(KBElementVisitor visitor) { visitor.visit(this); + } + + /* (non-Javadoc) + * @see java.lang.Comparable#compareTo(java.lang.Object) + */ + public int compareTo(DatatypeProperty o) { + return name.compareTo(o.name); } } Modified: trunk/src/dl-learner/org/dllearner/reasoning/FastInstanceChecker.java =================================================================== --- trunk/src/dl-learner/org/dllearner/reasoning/FastInstanceChecker.java 2008-02-26 10:47:09 UTC (rev 640) +++ trunk/src/dl-learner/org/dllearner/reasoning/FastInstanceChecker.java 2008-02-26 13:25:32 UTC (rev 641) @@ -36,6 +36,7 @@ import org.dllearner.core.ReasoningService; import org.dllearner.core.config.ConfigEntry; import org.dllearner.core.config.InvalidConfigOptionValueException; +import org.dllearner.core.owl.BooleanValueRestriction; import org.dllearner.core.owl.DatatypeProperty; import org.dllearner.core.owl.Description; import org.dllearner.core.owl.Individual; @@ -56,24 +57,24 @@ import org.dllearner.parser.ParseException; /** - * Reasoner for fast instance checks. It works by completely dematerialising the knowledge - * base to speed up later reasoning requests. It then continues by only considering one - * model of the knowledge base (TODO: more explanation), which is neither correct nor - * complete, but sufficient in many cases. A big advantage of the algorithm is that it - * does not need even need to perform any set modifications (union, intersection, difference), - * so it avoids any Java object creation, which makes it extremely fast compared to standard + * Reasoner for fast instance checks. It works by completely dematerialising the + * knowledge base to speed up later reasoning requests. It then continues by + * only considering one model of the knowledge base (TODO: more explanation), + * which is neither correct nor complete, but sufficient in many cases. A big + * advantage of the algorithm is that it does not need even need to perform any + * set modifications (union, intersection, difference), so it avoids any Java + * object creation, which makes it extremely fast compared to standard * reasoners. * * Note: This algorithm works only on concepts in negation normal form! * * @author Jens Lehmann - * + * */ public class FastInstanceChecker extends ReasonerComponent { - private static Logger logger = Logger - .getLogger(FastInstanceChecker.class); - + private static Logger logger = Logger.getLogger(FastInstanceChecker.class); + private Set<NamedClass> atomicConcepts; private Set<ObjectProperty> atomicRoles; private Set<DatatypeProperty> datatypeProperties; @@ -81,26 +82,36 @@ private Set<DatatypeProperty> doubleDatatypeProperties = new TreeSet<DatatypeProperty>(); private Set<DatatypeProperty> intDatatypeProperties = new TreeSet<DatatypeProperty>(); private SortedSet<Individual> individuals; - + private ReasoningService rs; private OWLAPIReasoner rc; private Set<KnowledgeSource> sources; - + // we use sorted sets (map indices) here, because they have only log(n) // complexity for checking whether an element is contained in them // instances of classes - private Map<NamedClass,SortedSet<Individual>> classInstancesPos = new TreeMap<NamedClass,SortedSet<Individual>>(); - private Map<NamedClass,SortedSet<Individual>> classInstancesNeg = new TreeMap<NamedClass,SortedSet<Individual>>(); - // object property mappings - private Map<ObjectProperty,Map<Individual,SortedSet<Individual>>> opPos = new TreeMap<ObjectProperty,Map<Individual,SortedSet<Individual>>>(); + private Map<NamedClass, SortedSet<Individual>> classInstancesPos = new TreeMap<NamedClass, SortedSet<Individual>>(); + private Map<NamedClass, SortedSet<Individual>> classInstancesNeg = new TreeMap<NamedClass, SortedSet<Individual>>(); + // object property mappings + private Map<ObjectProperty, Map<Individual, SortedSet<Individual>>> opPos = new TreeMap<ObjectProperty, Map<Individual, SortedSet<Individual>>>(); + // datatype property mappings + // (for booleans we assume that just one mapping exists, e.g. + // hasValue(object,true) and hasValue(object,false) will + // lead to undefined behaviour (they are logical contradictions) + private Map<DatatypeProperty, SortedSet<Individual>> bd = new TreeMap<DatatypeProperty, SortedSet<Individual>>(); + // for int and double we assume that a property can have several values, + // althoug this should be rare, + // e.g. hasValue(object,2) and hasValue(object,3) + private Map<DatatypeProperty, Map<Individual, SortedSet<Double>>> dd = new TreeMap<DatatypeProperty, Map<Individual, SortedSet<Double>>>(); + private Map<DatatypeProperty, Map<Individual, SortedSet<Integer>>> id = new TreeMap<DatatypeProperty, Map<Individual, SortedSet<Integer>>>(); - // TODO: datatype properties - public FastInstanceChecker(Set<KnowledgeSource> sources) { this.sources = sources; } - - /* (non-Javadoc) + + /* + * (non-Javadoc) + * * @see org.dllearner.core.Component#applyConfigEntry(org.dllearner.core.config.ConfigEntry) */ @Override @@ -108,7 +119,9 @@ } - /* (non-Javadoc) + /* + * (non-Javadoc) + * * @see org.dllearner.core.Component#init() */ @Override @@ -118,121 +131,152 @@ try { atomicConcepts = rc.getAtomicConcepts(); - datatypeProperties = rc.getDatatypeProperties(); + datatypeProperties = rc.getDatatypeProperties(); booleanDatatypeProperties = rc.getBooleanDatatypeProperties(); doubleDatatypeProperties = rc.getDoubleDatatypeProperties(); intDatatypeProperties = rc.getIntDatatypeProperties(); atomicRoles = rc.getAtomicRoles(); - individuals = rc.getIndividuals(); - } catch (ReasoningMethodUnsupportedException e) { - throw new ComponentInitException("Underlying reasoner does not support all necessary reasoning methods.", e); - } + individuals = rc.getIndividuals(); - rs = new ReasoningService(rc); - - // TODO: some code taken from Helper.createFlatABox, but pasted here because additional things need to - // be done (maybe this can be merge again with the FastRetrievalReasoner later) - long dematStartTime = System.currentTimeMillis(); + rs = new ReasoningService(rc); - for (NamedClass atomicConcept : rs.getAtomicConcepts()) { - classInstancesPos.put(atomicConcept, rs.retrieval(atomicConcept)); - Negation negatedAtomicConcept = new Negation(atomicConcept); - classInstancesNeg.put(atomicConcept, rs.retrieval(negatedAtomicConcept)); - } + // TODO: some code taken from Helper.createFlatABox, but pasted here + // because additional things need to + // be done (maybe this can be merge again with the + // FastRetrievalReasoner later) + long dematStartTime = System.currentTimeMillis(); - for (ObjectProperty atomicRole : rs.getAtomicRoles()) { - opPos.put(atomicRole, rc.getRoleMembers(atomicRole)); + for (NamedClass atomicConcept : rs.getAtomicConcepts()) { + classInstancesPos.put(atomicConcept, rs.retrieval(atomicConcept)); + Negation negatedAtomicConcept = new Negation(atomicConcept); + classInstancesNeg.put(atomicConcept, rs.retrieval(negatedAtomicConcept)); + } + + for (ObjectProperty atomicRole : atomicRoles) { + opPos.put(atomicRole, rc.getRoleMembers(atomicRole)); + } + + for (DatatypeProperty dp : booleanDatatypeProperties) { + bd.put(dp, rc.getTrueDatatypeMembers(dp)); + } + + for (DatatypeProperty dp : intDatatypeProperties) { + id.put(dp, rc.getIntDatatypeMembers(dp)); + } + + for (DatatypeProperty dp : doubleDatatypeProperties) { + dd.put(dp, rc.getDoubleDatatypeMembers(dp)); + } + + long dematDuration = System.currentTimeMillis() - dematStartTime; + logger.info("TBox dematerialised in " + dematDuration + " ms"); + + } catch (ReasoningMethodUnsupportedException e) { + throw new ComponentInitException( + "Underlying reasoner does not support all necessary reasoning methods.", e); } - - long dematDuration = System.currentTimeMillis() - dematStartTime; - logger.info("TBox dematerialised in " + dematDuration + " ms"); - } @Override - public boolean instanceCheck(Description description, Individual individual) throws ReasoningMethodUnsupportedException { - if(description instanceof NamedClass) { - return classInstancesPos.get((NamedClass)description).contains(individual); - } else if(description instanceof Negation) { + public boolean instanceCheck(Description description, Individual individual) + throws ReasoningMethodUnsupportedException { + if (description instanceof NamedClass) { + return classInstancesPos.get((NamedClass) description).contains(individual); + } else if (description instanceof Negation) { Description child = description.getChild(0); - if(child instanceof NamedClass) { - return classInstancesNeg.get((NamedClass)child).contains(individual); + if (child instanceof NamedClass) { + return classInstancesNeg.get((NamedClass) child).contains(individual); } else { - throw new ReasoningMethodUnsupportedException("Instance check for description " + description + " unsupported. Description needs to be in negation normal form."); + throw new ReasoningMethodUnsupportedException("Instance check for description " + + description + + " unsupported. Description needs to be in negation normal form."); } - } else if(description instanceof Thing) { + } else if (description instanceof Thing) { return true; - } else if(description instanceof Nothing) { + } else if (description instanceof Nothing) { return false; - } else if(description instanceof Union) { - // if the individual is instance of any of the subdescription of + } else if (description instanceof Union) { + // if the individual is instance of any of the subdescription of // the union, we return true List<Description> children = description.getChildren(); - for(Description child : children) { - if(instanceCheck(child, individual)) + for (Description child : children) { + if (instanceCheck(child, individual)) return true; } return false; - } else if(description instanceof Intersection) { - // if the individual is instance of all of the subdescription of - // the union, we return true + } else if (description instanceof Intersection) { + // if the individual is instance of all of the subdescription of + // the union, we return true List<Description> children = description.getChildren(); - for(Description child : children) { - if(!instanceCheck(child, individual)) + for (Description child : children) { + if (!instanceCheck(child, individual)) return false; } return true; - } else if(description instanceof ObjectSomeRestriction) { - ObjectPropertyExpression ope = ((ObjectSomeRestriction)description).getRole(); - if(!(ope instanceof ObjectProperty)) - throw new ReasoningMethodUnsupportedException("Instance check for description " + description + " unsupported. Inverse object properties not supported."); + } else if (description instanceof ObjectSomeRestriction) { + ObjectPropertyExpression ope = ((ObjectSomeRestriction) description).getRole(); + if (!(ope instanceof ObjectProperty)) + throw new ReasoningMethodUnsupportedException("Instance check for description " + + description + " unsupported. Inverse object properties not supported."); ObjectProperty op = (ObjectProperty) ope; Description child = description.getChild(0); - Map<Individual,SortedSet<Individual>> mapping = opPos.get(op);; - if(mapping == null) { - logger.warn("Instance check of a description with an undefinied property (" + op + ")."); + Map<Individual, SortedSet<Individual>> mapping = opPos.get(op); + ; + if (mapping == null) { + logger.warn("Instance check of a description with an undefinied property (" + op + + ")."); return false; } SortedSet<Individual> roleFillers = opPos.get(op).get(individual); - if(roleFillers == null) + if (roleFillers == null) return false; - for(Individual roleFiller : roleFillers) { - if(instanceCheck(child, roleFiller)) + for (Individual roleFiller : roleFillers) { + if (instanceCheck(child, roleFiller)) return true; } return false; - } else if(description instanceof ObjectAllRestriction) { - ObjectPropertyExpression ope = ((ObjectAllRestriction)description).getRole(); - if(!(ope instanceof ObjectProperty)) - throw new ReasoningMethodUnsupportedException("Instance check for description " + description + " unsupported. Inverse object properties not supported."); + } else if (description instanceof ObjectAllRestriction) { + ObjectPropertyExpression ope = ((ObjectAllRestriction) description).getRole(); + if (!(ope instanceof ObjectProperty)) + throw new ReasoningMethodUnsupportedException("Instance check for description " + + description + " unsupported. Inverse object properties not supported."); ObjectProperty op = (ObjectProperty) ope; Description child = description.getChild(0); - Map<Individual,SortedSet<Individual>> mapping = opPos.get(op);; - if(mapping == null) { - logger.warn("Instance check of a description with an undefinied property (" + op + ")."); + Map<Individual, SortedSet<Individual>> mapping = opPos.get(op); + ; + if (mapping == null) { + logger.warn("Instance check of a description with an undefinied property (" + op + + ")."); return true; } SortedSet<Individual> roleFillers = opPos.get(op).get(individual); - if(roleFillers == null) + if (roleFillers == null) return true; - for(Individual roleFiller : roleFillers) { - if(!instanceCheck(child, roleFiller)) + for (Individual roleFiller : roleFillers) { + if (!instanceCheck(child, roleFiller)) return false; } return true; + } else if (description instanceof BooleanValueRestriction) { + } - - throw new ReasoningMethodUnsupportedException("Instance check for description " + description + " unsupported."); - } - - /* (non-Javadoc) + + throw new ReasoningMethodUnsupportedException("Instance check for description " + + description + " unsupported."); + } + + /* + * (non-Javadoc) + * * @see org.dllearner.core.Reasoner#getAtomicConcepts() */ public Set<NamedClass> getAtomicConcepts() { return atomicConcepts; } - /* (non-Javadoc) + /* + * (non-Javadoc) + * * @see org.dllearner.core.Reasoner#getAtomicRoles() */ public Set<ObjectProperty> getAtomicRoles() { @@ -242,38 +286,44 @@ @Override public Set<DatatypeProperty> getDatatypeProperties() { return datatypeProperties; - } - + } + @Override public Set<DatatypeProperty> getBooleanDatatypeProperties() { return booleanDatatypeProperties; - } - + } + @Override public Set<DatatypeProperty> getDoubleDatatypeProperties() { return doubleDatatypeProperties; - } - + } + @Override public Set<DatatypeProperty> getIntDatatypeProperties() { return intDatatypeProperties; - } - - /* (non-Javadoc) + } + + /* + * (non-Javadoc) + * * @see org.dllearner.core.Reasoner#getIndividuals() */ public SortedSet<Individual> getIndividuals() { return individuals; } - /* (non-Javadoc) + /* + * (non-Javadoc) + * * @see org.dllearner.core.Reasoner#getReasonerType() */ public ReasonerType getReasonerType() { return ReasonerType.FAST_INSTANCE_CHECKER; } - /* (non-Javadoc) + /* + * (non-Javadoc) + * * @see org.dllearner.core.Reasoner#prepareSubsumptionHierarchy(java.util.Set) */ public void prepareSubsumptionHierarchy(Set<NamedClass> allowedConcepts) { @@ -283,34 +333,37 @@ @Override public SubsumptionHierarchy getSubsumptionHierarchy() { return rs.getSubsumptionHierarchy(); - } - + } + @Override public void prepareRoleHierarchy(Set<ObjectProperty> allowedRoles) { rs.prepareRoleHierarchy(allowedRoles); - } - + } + @Override public ObjectPropertyHierarchy getRoleHierarchy() { return rs.getRoleHierarchy(); } - + @Override public boolean subsumes(Description superConcept, Description subConcept) { -// Negation neg = new Negation(subConcept); -// Intersection c = new Intersection(neg,superConcept); -// return fastRetrieval.calculateSets(c).getPosSet().isEmpty(); + // Negation neg = new Negation(subConcept); + // Intersection c = new Intersection(neg,superConcept); + // return fastRetrieval.calculateSets(c).getPosSet().isEmpty(); return rs.subsumes(superConcept, subConcept); - } - + } + /** * Test method for fast instance checker. - * @param args No arguments supported. - * @throws ComponentInitException - * @throws ParseException - * @throws ReasoningMethodUnsupportedException + * + * @param args + * No arguments supported. + * @throws ComponentInitException + * @throws ParseException + * @throws ReasoningMethodUnsupportedException */ - public static void main(String[] args) throws ComponentInitException, ParseException, ReasoningMethodUnsupportedException { + public static void main(String[] args) throws ComponentInitException, ParseException, + ReasoningMethodUnsupportedException { ComponentManager cm = ComponentManager.getInstance(); OWLFile owl = cm.knowledgeSource(OWLFile.class); String owlFile = new File("examples/family/father.owl").toURI().toString(); @@ -319,7 +372,7 @@ ReasonerComponent reasoner = cm.reasoner(FastInstanceChecker.class, owl); cm.reasoningService(reasoner); reasoner.init(); - + KBParser.internalNamespace = "http://example.com/father#"; String query = "(male AND EXISTS hasChild.TOP)"; Description d = KBParser.parseConcept(query); Modified: trunk/src/dl-learner/org/dllearner/reasoning/OWLAPIReasoner.java =================================================================== --- trunk/src/dl-learner/org/dllearner/reasoning/OWLAPIReasoner.java 2008-02-26 10:47:09 UTC (rev 640) +++ trunk/src/dl-learner/org/dllearner/reasoning/OWLAPIReasoner.java 2008-02-26 13:25:32 UTC (rev 641) @@ -43,6 +43,7 @@ import org.dllearner.core.config.StringConfigOption; import org.dllearner.core.owl.AssertionalAxiom; import org.dllearner.core.owl.ClassAssertionAxiom; +import org.dllearner.core.owl.Constant; import org.dllearner.core.owl.Datatype; import org.dllearner.core.owl.DatatypeProperty; import org.dllearner.core.owl.Description; @@ -68,7 +69,9 @@ import org.dllearner.core.owl.TerminologicalAxiom; import org.dllearner.core.owl.Thing; import org.dllearner.core.owl.TransitiveObjectPropertyAxiom; +import org.dllearner.core.owl.TypedConstant; import org.dllearner.core.owl.Union; +import org.dllearner.core.owl.UntypedConstant; import org.dllearner.kb.OWLFile; import org.dllearner.utilities.ConceptComparator; import org.dllearner.utilities.RoleComparator; @@ -78,6 +81,7 @@ import org.semanticweb.owl.model.AddAxiom; import org.semanticweb.owl.model.OWLAxiom; import org.semanticweb.owl.model.OWLClass; +import org.semanticweb.owl.model.OWLConstant; import org.semanticweb.owl.model.OWLDataFactory; import org.semanticweb.owl.model.OWLDataProperty; import org.semanticweb.owl.model.OWLDataRange; @@ -91,6 +95,8 @@ import org.semanticweb.owl.model.OWLOntologyCreationException; import org.semanticweb.owl.model.OWLOntologyManager; import org.semanticweb.owl.model.OWLOntologyStorageException; +import org.semanticweb.owl.model.OWLTypedConstant; +import org.semanticweb.owl.model.OWLUntypedConstant; import org.semanticweb.owl.model.UnknownOWLOntologyException; import org.semanticweb.owl.util.SimpleURIMapper; @@ -537,8 +543,47 @@ } return map; } - + @Override + public Map<Individual, SortedSet<Constant>> getDatatypeMembers(DatatypeProperty datatypeProperty) { + OWLDataProperty prop = getOWLAPIDescription(datatypeProperty); + Map<Individual, SortedSet<Constant>> map = new TreeMap<Individual, SortedSet<Constant>>(); + for(Individual i : individuals) { + OWLIndividual ind = factory.getOWLIndividual(URI.create(i.getName())); + + // get all related values via OWL API + Set<OWLConstant> constants = null; + try { + constants = reasoner.getRelatedValues(ind, prop); + } catch (OWLReasonerException e) { + e.printStackTrace(); + } + + // convert data back to DL-Learner structures + SortedSet<Constant> is = new TreeSet<Constant>(); + for(OWLConstant oi : constants) { + // for typed constants we have to figure out the correct + // data type and value + if(oi instanceof OWLTypedConstant) { + Datatype dt = convertDatatype(((OWLTypedConstant)oi).getDataType()); + is.add(new TypedConstant(oi.getLiteral(),dt)); + // for untyped constants we have to figure out the value + // and language tag (if any) + } else { + OWLUntypedConstant ouc = (OWLUntypedConstant) oi; + if(ouc.hasLang()) + is.add(new UntypedConstant(ouc.getLiteral(), ouc.getLang())); + else + is.add(new UntypedConstant(ouc.getLiteral())); + } + } + // only add individuals using the datatype property + if(is.size()>0) + map.put(i, is); + } + return map; + } + // OWL API often returns a set of sets of classes, where each inner // set consists of equivalent classes; this method picks one class // from each inner set to flatten the set of sets @@ -618,10 +663,26 @@ } } + public static Datatype convertDatatype(OWLDataType dataType) { + URI uri = dataType.getURI(); + if(uri.equals(Datatype.BOOLEAN.getURI())) + return Datatype.BOOLEAN; + else if(uri.equals(Datatype.DOUBLE.getURI())) + return Datatype.DOUBLE; + else if(uri.equals(Datatype.INT.getURI())) + return Datatype.INT; + + throw new Error("Unsupported datatype " + dataType + ". Please inform a DL-Learner developer to add it."); + } + private static OWLObjectProperty getOWLAPIDescription(ObjectProperty role) { return staticFactory.getOWLObjectProperty(URI.create(role.getName())); } + private static OWLDataProperty getOWLAPIDescription(DatatypeProperty datatypeProperty) { + return staticFactory.getOWLDataProperty(URI.create(datatypeProperty.getName())); + } + @Deprecated public static OWLDescription getOWLAPIDescription(Description concept) { if (concept instanceof NamedClass) { @@ -773,4 +834,28 @@ } } + /** + * @return the booleanDatatypeProperties + */ + @Override + public Set<DatatypeProperty> getBooleanDatatypeProperties() { + return booleanDatatypeProperties; + } + + /** + * @return the doubleDatatypeProperties + */ + @Override + public Set<DatatypeProperty> getDoubleDatatypeProperties() { + return doubleDatatypeProperties; + } + + /** + * @return the intDatatypeProperties + */ + @Override + public Set<DatatypeProperty> getIntDatatypeProperties() { + return intDatatypeProperties; + } + } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |