From: <jen...@us...> - 2008-11-14 15:09:18
|
Revision: 1514 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=1514&view=rev Author: jenslehmann Date: 2008-11-14 15:09:11 +0000 (Fri, 14 Nov 2008) Log Message: ----------- started class for detecting relevant entities at a certain position in a class description for a given individual Modified Paths: -------------- trunk/src/dl-learner/org/dllearner/algorithms/el/SearchTreeNode.java trunk/src/dl-learner/org/dllearner/reasoning/FastInstanceChecker.java trunk/src/dl-learner/org/dllearner/reasoning/OWLAPIReasoner.java trunk/src/dl-learner/org/dllearner/test/junit/TestOntologies.java Added Paths: ----------- trunk/src/dl-learner/org/dllearner/test/junit/UtilitiesTests.java trunk/src/dl-learner/org/dllearner/utilities/learn/UsedEntitiesDetection.java Modified: trunk/src/dl-learner/org/dllearner/algorithms/el/SearchTreeNode.java =================================================================== --- trunk/src/dl-learner/org/dllearner/algorithms/el/SearchTreeNode.java 2008-11-14 15:04:06 UTC (rev 1513) +++ trunk/src/dl-learner/org/dllearner/algorithms/el/SearchTreeNode.java 2008-11-14 15:09:11 UTC (rev 1514) @@ -22,8 +22,6 @@ import java.util.LinkedList; import java.util.List; -import org.dllearner.algorithms.refinement.Node; - /** * A node in the search tree of an EL algorithm. * Modified: trunk/src/dl-learner/org/dllearner/reasoning/FastInstanceChecker.java =================================================================== --- trunk/src/dl-learner/org/dllearner/reasoning/FastInstanceChecker.java 2008-11-14 15:04:06 UTC (rev 1513) +++ trunk/src/dl-learner/org/dllearner/reasoning/FastInstanceChecker.java 2008-11-14 15:09:11 UTC (rev 1514) @@ -681,6 +681,11 @@ } @Override + protected Map<ObjectProperty,Set<Individual>> getObjectPropertyRelationshipsImpl(Individual individual) { + return rc.getObjectPropertyRelationships(individual); + } + + @Override public Set<Constant> getRelatedValuesImpl(Individual individual, DatatypeProperty datatypeProperty) throws ReasoningMethodUnsupportedException { return rc.getRelatedValues(individual, datatypeProperty); } Modified: trunk/src/dl-learner/org/dllearner/reasoning/OWLAPIReasoner.java =================================================================== --- trunk/src/dl-learner/org/dllearner/reasoning/OWLAPIReasoner.java 2008-11-14 15:04:06 UTC (rev 1513) +++ trunk/src/dl-learner/org/dllearner/reasoning/OWLAPIReasoner.java 2008-11-14 15:09:11 UTC (rev 1514) @@ -41,7 +41,6 @@ import org.dllearner.core.ComponentInitException; import org.dllearner.core.KnowledgeSource; import org.dllearner.core.ReasonerComponent; -import org.dllearner.core.ReasoningMethodUnsupportedException; import org.dllearner.core.configurators.OWLAPIReasonerConfigurator; import org.dllearner.core.options.ConfigEntry; import org.dllearner.core.options.ConfigOption; Modified: trunk/src/dl-learner/org/dllearner/test/junit/TestOntologies.java =================================================================== --- trunk/src/dl-learner/org/dllearner/test/junit/TestOntologies.java 2008-11-14 15:04:06 UTC (rev 1513) +++ trunk/src/dl-learner/org/dllearner/test/junit/TestOntologies.java 2008-11-14 15:09:11 UTC (rev 1514) @@ -36,7 +36,7 @@ */ public final class TestOntologies { - public enum TestOntology { EMPTY, SIMPLE, SIMPLE2, R1SUBR2 }; + public enum TestOntology { EMPTY, SIMPLE, SIMPLE2, R1SUBR2, DATA1 }; public static ReasonerComponent getTestOntology(TestOntology ont) { String kbString = ""; @@ -59,6 +59,14 @@ kbString += "a2 SUB a4"; } else if(ont.equals(TestOntology.R1SUBR2)) { kbString += "Subrole(r1,r2).\n"; + } else if(ont.equals(TestOntology.DATA1)) { + kbString += "man SUB person.\n"; + kbString += "woman SUB person.\n"; + kbString += "man(eric).\n"; + kbString += "woman(diana).\n"; + kbString += "married(eric,diana).\n"; + kbString += "hasChild(eric,frank).\n"; + kbString += "hasChild(eric,tim).\n"; } try { Added: trunk/src/dl-learner/org/dllearner/test/junit/UtilitiesTests.java =================================================================== --- trunk/src/dl-learner/org/dllearner/test/junit/UtilitiesTests.java (rev 0) +++ trunk/src/dl-learner/org/dllearner/test/junit/UtilitiesTests.java 2008-11-14 15:09:11 UTC (rev 1514) @@ -0,0 +1,49 @@ +/** + * Copyright (C) 2007-2008, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ +package org.dllearner.test.junit; + +import java.util.Set; +import java.util.TreeSet; + +import org.dllearner.core.ReasonerComponent; +import org.dllearner.core.owl.Individual; +import org.dllearner.test.junit.TestOntologies.TestOntology; +import org.dllearner.utilities.learn.UsedEntitiesDetection; +import org.junit.Test; + +/** + * Various tests for methods/classes in the utilities package. + * + * @author Jens Lehmann + * + */ +public class UtilitiesTests { + + @Test + public void entityDetection() { + ReasonerComponent reasoner = TestOntologies.getTestOntology(TestOntology.DATA1); + int maxDepth = 2; + Set<Individual> individuals = new TreeSet<Individual>(); + individuals.add(new Individual("http://localhost/foo#tim")); + UsedEntitiesDetection detection = new UsedEntitiesDetection(reasoner, individuals, maxDepth); + System.out.println(detection); + } + +} Added: trunk/src/dl-learner/org/dllearner/utilities/learn/UsedEntitiesDetection.java =================================================================== --- trunk/src/dl-learner/org/dllearner/utilities/learn/UsedEntitiesDetection.java (rev 0) +++ trunk/src/dl-learner/org/dllearner/utilities/learn/UsedEntitiesDetection.java 2008-11-14 15:09:11 UTC (rev 1514) @@ -0,0 +1,175 @@ +/** + * Copyright (C) 2007-2008, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ +package org.dllearner.utilities.learn; + +import java.util.Comparator; +import java.util.Iterator; +import java.util.Map; +import java.util.Set; +import java.util.TreeMap; +import java.util.TreeSet; +import java.util.Map.Entry; + +import org.dllearner.core.ReasonerComponent; +import org.dllearner.core.owl.Individual; +import org.dllearner.core.owl.NamedClass; +import org.dllearner.core.owl.ObjectProperty; + +/** + * This class takes a reasoner and individuals as input and detects + * the relevant (wrt. a learning process) classes and properties + * at a certain distance of the examples. + * + * @author Jens Lehmann + * + */ +public class UsedEntitiesDetection { + + Comparator<Set<ObjectProperty>> keyComp = new Comparator<Set<ObjectProperty>>() { + + @Override + public int compare(Set<ObjectProperty> key1, Set<ObjectProperty> key2) { + // first criterion: size of key + int sizeDiff = key1.size() - key2.size(); + if(sizeDiff == 0) { + Iterator<ObjectProperty> it1 = key1.iterator(); + Iterator<ObjectProperty> it2 = key2.iterator(); + // compare elements one by one (assumes that both use the same + // ordering, which is the case) + while(it1.hasNext()) { + ObjectProperty prop1 = it1.next(); + ObjectProperty prop2 = it2.next(); + int comp = prop1.compareTo(prop2); + if(comp != 0) { + return comp; + } + } + // all elements of the set are equal + return 0; + } else { + return sizeDiff; + } + } + + }; + + private Map<Set<ObjectProperty>,Set<NamedClass>> usedClasses; + + private Map<Set<ObjectProperty>,Set<ObjectProperty>> usedObjectProperties; + + private ReasonerComponent reasoner; + private int maxDepth; + + /** + * Computes used properties in classes. + * TODO more explanation + * + * @param reasoner A reasoner. + * @param individuals A set of individuals to start from. + * @param depth The maximum depth for the search. + */ + public UsedEntitiesDetection(ReasonerComponent reasoner, Set<Individual> individuals, int maxDepth) { + this.reasoner = reasoner; + this.maxDepth = maxDepth; + usedClasses = new TreeMap<Set<ObjectProperty>,Set<NamedClass>>(keyComp); + usedObjectProperties = new TreeMap<Set<ObjectProperty>,Set<ObjectProperty>>(keyComp); + + Set<ObjectProperty> startKey = new TreeSet<ObjectProperty>(); + computeUsedEntitiesRec(startKey, individuals); + + } + + private void computeUsedEntitiesRec(Set<ObjectProperty> key, Set<Individual> individuals) { + Set<NamedClass> types = new TreeSet<NamedClass>(); +// Set<ObjectProperty> properties = new TreeSet<ObjectProperty>(); + // we must use the object property comparator to avoid double occurences of properties + Map<ObjectProperty,Set<Individual>> relations = new TreeMap<ObjectProperty,Set<Individual>>(); + + for(Individual individual : individuals) { + // add all types + types.addAll(reasoner.getTypes(individual)); + + // compute outgoing properties + Map<ObjectProperty,Set<Individual>> map = reasoner.getObjectPropertyRelationships(individual); + for(Entry<ObjectProperty,Set<Individual>> entry : map.entrySet()) { + ObjectProperty prop = entry.getKey(); + // we must use the individual comparator to avoid + // multiple occurrences of the same individual + Set<Individual> inds = new TreeSet<Individual>(entry.getValue()); + + // if property exists, add the found individuals + if(relations.containsKey(prop)) { + relations.get(prop).addAll(inds); + // if property not encountered before, add it + } else { + relations.put(prop, inds); + } + } + } + + // store all found relations + usedClasses.put(key, types); + usedObjectProperties.put(key, relations.keySet()); + + // recurse if limit not reached yet + if(key.size() < maxDepth) { + for(Entry<ObjectProperty,Set<Individual>> entry : relations.entrySet()) { + // construct new key (copy and add) + Set<ObjectProperty> newKey = new TreeSet<ObjectProperty>(key); + newKey.add(entry.getKey()); + + // recursion + computeUsedEntitiesRec(newKey, entry.getValue()); + } + } + + } + + public Set<Set<ObjectProperty>> getKeys() { + return usedClasses.keySet(); + } + + /** + * @return the usedClasses + */ + public Map<Set<ObjectProperty>, Set<NamedClass>> getUsedClasses() { + return usedClasses; + } + + /** + * @return the usedObjectProperties + */ + public Map<Set<ObjectProperty>, Set<ObjectProperty>> getUsedObjectProperties() { + return usedObjectProperties; + } + + @Override + public String toString() { + String str = ""; + Set<Set<ObjectProperty>> keys = getKeys(); + for(Set<ObjectProperty> key : keys) { + str += key.toString() + ": \n"; + str += " classes: " + usedClasses.get(key) + "\n"; + str += " object properties: " + usedObjectProperties.get(key) + "\n"; + } + return str; + } + +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |