From: <jen...@us...> - 2007-08-26 11:09:13
|
Revision: 58 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=58&view=rev Author: jenslehmann Date: 2007-08-25 09:04:07 -0700 (Sat, 25 Aug 2007) Log Message: ----------- several changes to enable role hierarchy DIG reasoning Modified Paths: -------------- trunk/src/dl-learner/org/dllearner/Main.java trunk/src/dl-learner/org/dllearner/reasoning/AbstractReasoner.java trunk/src/dl-learner/org/dllearner/reasoning/DIGReasoner.java trunk/src/dl-learner/org/dllearner/reasoning/FastRetrievalReasoner.java trunk/src/dl-learner/org/dllearner/reasoning/Reasoner.java trunk/src/dl-learner/org/dllearner/reasoning/RoleHierarchy.java Modified: trunk/src/dl-learner/org/dllearner/Main.java =================================================================== --- trunk/src/dl-learner/org/dllearner/Main.java 2007-08-25 16:02:32 UTC (rev 57) +++ trunk/src/dl-learner/org/dllearner/Main.java 2007-08-25 16:04:07 UTC (rev 58) @@ -1,3 +1,23 @@ +/** + * Copyright (C) 2007, 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; import java.io.BufferedReader; @@ -265,6 +285,21 @@ System.out.println("OK (" + Helper.prettyPrintNanoSeconds(subHierTime, true, false) + ")"); + // prepare role hierarchy + System.out.print("Preparing Role Hierarchy ... "); + long roleHierTimeStart = System.nanoTime(); + try { + reasoner.prepareRoleHierarchy(); + // System.out.println(); + // System.out.println(reasoner.getRoleHierarchy()); + } catch (ReasoningMethodUnsupportedException e1) { + System.out.println("Tried to construct the role hierarchy, but the reasoner " + + "does not support it. Currently only DIG reasoners support this feature."); + } + long roleHierTime = System.nanoTime() - roleHierTimeStart; + System.out.println("OK (" + + Helper.prettyPrintNanoSeconds(roleHierTime, true, false) + ")"); + // Beispiele anzeigen boolean oneLineExampleInfo = true; int maxExampleStringLength = posExamples.toString().length(); Modified: trunk/src/dl-learner/org/dllearner/reasoning/AbstractReasoner.java =================================================================== --- trunk/src/dl-learner/org/dllearner/reasoning/AbstractReasoner.java 2007-08-25 16:02:32 UTC (rev 57) +++ trunk/src/dl-learner/org/dllearner/reasoning/AbstractReasoner.java 2007-08-25 16:04:07 UTC (rev 58) @@ -90,6 +90,10 @@ throw new ReasoningMethodUnsupportedException(); } + public void prepareRoleHierarchy() throws ReasoningMethodUnsupportedException { + throw new ReasoningMethodUnsupportedException(); + } + public RoleHierarchy getRoleHierarchy() throws ReasoningMethodUnsupportedException { throw new ReasoningMethodUnsupportedException(); } Modified: trunk/src/dl-learner/org/dllearner/reasoning/DIGReasoner.java =================================================================== --- trunk/src/dl-learner/org/dllearner/reasoning/DIGReasoner.java 2007-08-25 16:02:32 UTC (rev 57) +++ trunk/src/dl-learner/org/dllearner/reasoning/DIGReasoner.java 2007-08-25 16:04:07 UTC (rev 58) @@ -1,3 +1,23 @@ +/** + * Copyright (C) 2007, 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.reasoning; import java.io.File; @@ -25,6 +45,7 @@ import org.dllearner.dl.Top; import org.dllearner.utilities.ConceptComparator; import org.dllearner.utilities.Helper; +import org.dllearner.utilities.RoleComparator; import org.kr.dl.dig.v1_1.Concepts; import org.kr.dl.dig.v1_1.Csynonyms; import org.kr.dl.dig.v1_1.IdType; @@ -35,6 +56,12 @@ import org.kr.dl.dig.v1_1.Rsynonyms; import org.kr.dl.dig.v1_1.IndividualSetDocument.IndividualSet; +/** + * DIG 1.1 implementation of the reasoner interface. + * + * @author Jens Lehmann + * + */ public class DIGReasoner extends AbstractReasoner { // Variablen für Reasoner @@ -54,11 +81,19 @@ // unterschiedlich sind; // alternativ wäre auch eine Indizierung über Strings möglich ConceptComparator conceptComparator = new ConceptComparator(); + RoleComparator roleComparator = new RoleComparator(); SubsumptionHierarchy subsumptionHierarchy; + RoleHierarchy roleHierarchy; // enthält atomare Konzepte, sowie Top und Bottom Set<Concept> allowedConceptsInSubsumptionHierarchy; - // imports not supported yet + /** + * Creates a DIG reasoner object. + * + * @param kb Internal knowledge base. + * @param url URL of the DIG reasoner e.g. http://localhost:8081. + * @param imports Files to import (format and physical location of each file). + */ public DIGReasoner(KB kb, URL url, Map<URL,OntologyFileFormat> imports) { this.imports = imports; this.kb = kb; @@ -136,6 +171,10 @@ } + /** + * Construct a subsumption hierarchy using DIG queries. After calling this + * method one can ask for children or parents in the subsumption hierarchy. + */ public void prepareSubsumptionHierarchy() { allowedConceptsInSubsumptionHierarchy = new TreeSet<Concept>(conceptComparator); allowedConceptsInSubsumptionHierarchy.addAll(Config.Refinement.allowedConcepts); @@ -172,6 +211,26 @@ subsumptionHierarchy = new SubsumptionHierarchy(Config.Refinement.allowedConcepts, subsumptionHierarchyUp, subsumptionHierarchyDown); } + /** + * Constructs a role hierarchy using DIG queries. After calling this method, + * one can query parents or children of roles. + * + * @todo Does not yet take ignored roles into account. + */ + @Override + public void prepareRoleHierarchy() { + TreeMap<AtomicRole,TreeSet<AtomicRole>> roleHierarchyUp = new TreeMap<AtomicRole,TreeSet<AtomicRole>>(roleComparator); + TreeMap<AtomicRole,TreeSet<AtomicRole>> roleHierarchyDown = new TreeMap<AtomicRole,TreeSet<AtomicRole>>(roleComparator); + + // Refinement atomarer Konzepte + for(AtomicRole role : atomicRoles) { + roleHierarchyDown.put(role, getMoreSpecialRolesDIG(role)); + roleHierarchyUp.put(role, getMoreGeneralRolesDIG(role)); + } + + roleHierarchy = new RoleHierarchy(Config.Refinement.allowedRoles, roleHierarchyUp, roleHierarchyDown); + } + // eigentlich müsste man klonen um sicherzustellen, dass der parent-Link // bei null bleibt; bei der aktuellen Implementierung ist der parent-Link // nicht immer null, was bei GP negative Auswirkungen haben könnte @@ -371,6 +430,11 @@ return subsumptionHierarchy; } + @Override + public RoleHierarchy getRoleHierarchy() { + return roleHierarchy; + } + private TreeSet<Concept> getMoreGeneralConceptsDIG(Concept concept) { String moreGeneralDIG = asksPrefix; moreGeneralDIG += "<parents id=\"query_parents\">"; @@ -448,6 +512,54 @@ return resultsSet; } + private TreeSet<AtomicRole> getMoreGeneralRolesDIG(AtomicRole role) { + String moreGeneralRolesDIG = asksPrefix; + moreGeneralRolesDIG += "<rparents id=\"query_parents\">"; + moreGeneralRolesDIG += "<ratom name=\"" + role.getName() + "\" />"; + moreGeneralRolesDIG += "</rparents></asks>"; + + ResponsesDocument rd = connector.asks(moreGeneralRolesDIG); + TreeSet<AtomicRole> resultsSet = new TreeSet<AtomicRole>(roleComparator); + Roles[] rolesArray = rd.getResponses().getRoleSetArray()[0].getSynonymsArray(); + + for(int i=0; i<rolesArray.length; i++) { + Named[] atoms = rolesArray[i].getRatomArray(); + + for(Named atom : atoms) { + AtomicRole ar = new AtomicRole(atom.getName()); + //if(Config.Refinement.allowedRoles.contains(ar)) + resultsSet.add(ar); + } + } + + // System.out.println(rd); + + return resultsSet; + } + + private TreeSet<AtomicRole> getMoreSpecialRolesDIG(AtomicRole role) { + String moreSpecialRolesDIG = asksPrefix; + moreSpecialRolesDIG += "<rchildren id=\"query_children\">"; + moreSpecialRolesDIG += "<ratom name=\"" + role.getName() + "\" />"; + moreSpecialRolesDIG += "</rchildren></asks>"; + + ResponsesDocument rd = connector.asks(moreSpecialRolesDIG); + TreeSet<AtomicRole> resultsSet = new TreeSet<AtomicRole>(roleComparator); + Roles[] rolesArray = rd.getResponses().getRoleSetArray()[0].getSynonymsArray(); + + for(int i=0; i<rolesArray.length; i++) { + Named[] atoms = rolesArray[i].getRatomArray(); + + for(Named atom : atoms) { + AtomicRole ar = new AtomicRole(atom.getName()); + //if(Config.Refinement.allowedRoles.contains(ar)) + resultsSet.add(ar); + } + } + + return resultsSet; + } + public boolean instanceCheck(Concept concept, Individual individual) { String instanceCheckDIG = asksPrefix; instanceCheckDIG += "<instance id= \"query_instance\">"; Modified: trunk/src/dl-learner/org/dllearner/reasoning/FastRetrievalReasoner.java =================================================================== --- trunk/src/dl-learner/org/dllearner/reasoning/FastRetrievalReasoner.java 2007-08-25 16:02:32 UTC (rev 57) +++ trunk/src/dl-learner/org/dllearner/reasoning/FastRetrievalReasoner.java 2007-08-25 16:04:07 UTC (rev 58) @@ -76,5 +76,4 @@ public void prepareSubsumptionHierarchy() { // hier muss nichts getan werden } - } Modified: trunk/src/dl-learner/org/dllearner/reasoning/Reasoner.java =================================================================== --- trunk/src/dl-learner/org/dllearner/reasoning/Reasoner.java 2007-08-25 16:02:32 UTC (rev 57) +++ trunk/src/dl-learner/org/dllearner/reasoning/Reasoner.java 2007-08-25 16:04:07 UTC (rev 58) @@ -1,3 +1,23 @@ +/** + * Copyright (C) 2007, 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.reasoning; import java.util.Map; @@ -11,9 +31,9 @@ import org.dllearner.utilities.SortedSetTuple; /** - * Auflisten aller Reasoning-Methoden. + * Reasoner Interface. Lists all available reasoning methods. * - * @author jl + * @author Jens Lehmann * */ public interface Reasoner { @@ -25,6 +45,7 @@ // => erstellt auch vereinfachte Sichten auf Subsumptionhierarchie // (siehe einfacher Traversal in Diplomarbeit) public void prepareSubsumptionHierarchy(); + public void prepareRoleHierarchy() throws ReasoningMethodUnsupportedException; public boolean subsumes(Concept superConcept, Concept subConcept) throws ReasoningMethodUnsupportedException; Modified: trunk/src/dl-learner/org/dllearner/reasoning/RoleHierarchy.java =================================================================== --- trunk/src/dl-learner/org/dllearner/reasoning/RoleHierarchy.java 2007-08-25 16:02:32 UTC (rev 57) +++ trunk/src/dl-learner/org/dllearner/reasoning/RoleHierarchy.java 2007-08-25 16:04:07 UTC (rev 58) @@ -20,27 +20,74 @@ package org.dllearner.reasoning; import java.util.Set; +import java.util.SortedSet; import java.util.TreeMap; import java.util.TreeSet; -import org.dllearner.dl.AtomicConcept; import org.dllearner.dl.AtomicRole; -import org.dllearner.dl.Concept; import org.dllearner.utilities.RoleComparator; /** * Represents a hierarchy of roles. * + * @todo Currently, the role hierarchy pruning algorithm (analogous to the + * subsumption hierarchy) is not implemented. + * * @author Jens Lehmann * */ public class RoleHierarchy { RoleComparator rc = new RoleComparator(); + TreeMap<AtomicRole,TreeSet<AtomicRole>> roleHierarchyUp; + TreeMap<AtomicRole,TreeSet<AtomicRole>> roleHierarchyDown; + TreeSet<AtomicRole> mostGeneralRoles = new TreeSet<AtomicRole>(rc); + TreeSet<AtomicRole> mostSpecialRoles = new TreeSet<AtomicRole>(rc); public RoleHierarchy(Set<AtomicRole> atomicRoles, TreeMap<AtomicRole,TreeSet<AtomicRole>> roleHierarchyUp , TreeMap<AtomicRole,TreeSet<AtomicRole>> roleHierarchyDown) { + this.roleHierarchyUp = roleHierarchyUp; + this.roleHierarchyDown = roleHierarchyDown; - + // find most general and most special roles + for(AtomicRole role : atomicRoles) { + if(getMoreGeneralRoles(role).size()==0) + mostGeneralRoles.add(role); + if(getMoreSpecialRoles(role).size()==0) + mostSpecialRoles.add(role); + } } + @SuppressWarnings("unchecked") + public SortedSet<AtomicRole> getMoreGeneralRoles(AtomicRole role) { + // we clone all concepts before returning them such that they cannot be + // modified externally + return (TreeSet<AtomicRole>) roleHierarchyUp.get(role).clone(); + } + + @SuppressWarnings("unchecked") + public SortedSet<AtomicRole> getMoreSpecialRoles(AtomicRole role) { + return (TreeSet<AtomicRole>) roleHierarchyDown.get(role).clone(); + } + + @Override + public String toString() { + String str = ""; + for(AtomicRole role : mostGeneralRoles) { + str += toString(roleHierarchyDown, role, 0); + } + return str; + } + + private String toString(TreeMap<AtomicRole,TreeSet<AtomicRole>> hierarchy, AtomicRole role, int depth) { + String str = ""; + for(int i=0; i<depth; i++) + str += " "; + str += role.toString() + "\n"; + Set<AtomicRole> tmp = hierarchy.get(role); + if(tmp!=null) { + for(AtomicRole c : tmp) + str += toString(hierarchy, c, depth+1); + } + return str; + } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |