From: <jen...@us...> - 2008-11-14 11:59:10
|
Revision: 1512 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=1512&view=rev Author: jenslehmann Date: 2008-11-14 11:48:57 +0000 (Fri, 14 Nov 2008) Log Message: ----------- - added reasoning method to get all object property relationships for an individual - added some more methods to OWLAPIConverter Modified Paths: -------------- trunk/src/dl-learner/org/dllearner/core/IndividualReasoner.java trunk/src/dl-learner/org/dllearner/core/ReasonerComponent.java trunk/src/dl-learner/org/dllearner/reasoning/OWLAPIReasoner.java trunk/src/dl-learner/org/dllearner/utilities/owl/OWLAPIConverter.java Modified: trunk/src/dl-learner/org/dllearner/core/IndividualReasoner.java =================================================================== --- trunk/src/dl-learner/org/dllearner/core/IndividualReasoner.java 2008-11-12 16:25:45 UTC (rev 1511) +++ trunk/src/dl-learner/org/dllearner/core/IndividualReasoner.java 2008-11-14 11:48:57 UTC (rev 1512) @@ -106,6 +106,16 @@ public Set<Constant> getRelatedValues(Individual individual, DatatypeProperty datatypeProperty); /** + * A map of properties related to an individual, e.g. + * {hasChild => {eric,anna}, hasSibling => {sebastian}}. + * + * @param individual An individual. + * @return A map of of properties connected to the individual as keys and the individuals + * they point to as values. + */ + public Map<ObjectProperty,Set<Individual>> getObjectPropertyRelationships(Individual individual); + + /** * Computes and returns all connections between individuals through the specified * property, e.g. {eric => {maria, anna}, anna => {eric}}. * @param objectProperty An object property. Modified: trunk/src/dl-learner/org/dllearner/core/ReasonerComponent.java =================================================================== --- trunk/src/dl-learner/org/dllearner/core/ReasonerComponent.java 2008-11-12 16:25:45 UTC (rev 1511) +++ trunk/src/dl-learner/org/dllearner/core/ReasonerComponent.java 2008-11-14 11:48:57 UTC (rev 1512) @@ -398,6 +398,21 @@ } @Override + public final Map<ObjectProperty,Set<Individual>> getObjectPropertyRelationships(Individual individual) { + try { + return getObjectPropertyRelationshipsImpl(individual); + } catch (ReasoningMethodUnsupportedException e) { + handleExceptions(e); + return null; + } + } + + protected Map<ObjectProperty,Set<Individual>> getObjectPropertyRelationshipsImpl(Individual individual) throws ReasoningMethodUnsupportedException { + throw new ReasoningMethodUnsupportedException(); + } + + + @Override public final Set<Individual> getRelatedIndividuals(Individual individual, ObjectProperty objectProperty) { try { Modified: trunk/src/dl-learner/org/dllearner/reasoning/OWLAPIReasoner.java =================================================================== --- trunk/src/dl-learner/org/dllearner/reasoning/OWLAPIReasoner.java 2008-11-12 16:25:45 UTC (rev 1511) +++ trunk/src/dl-learner/org/dllearner/reasoning/OWLAPIReasoner.java 2008-11-14 11:48:57 UTC (rev 1512) @@ -34,12 +34,14 @@ import java.util.SortedSet; import java.util.TreeMap; import java.util.TreeSet; +import java.util.Map.Entry; import org.apache.log4j.Level; import org.apache.log4j.Logger; 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; @@ -64,6 +66,7 @@ import org.dllearner.kb.sparql.SparqlKnowledgeSource; import org.dllearner.utilities.owl.ConceptComparator; import org.dllearner.utilities.owl.OWLAPIAxiomConvertVisitor; +import org.dllearner.utilities.owl.OWLAPIConverter; import org.dllearner.utilities.owl.OWLAPIDescriptionConvertVisitor; import org.dllearner.utilities.owl.RoleComparator; import org.semanticweb.owl.apibinding.OWLManager; @@ -118,7 +121,7 @@ // the data factory is used to generate OWL API objects private OWLDataFactory factory; // static factory - private static OWLDataFactory staticFactory = OWLManager.createOWLOntologyManager().getOWLDataFactory(); +// private static OWLDataFactory staticFactory = OWLManager.createOWLOntologyManager().getOWLDataFactory(); private ConceptComparator conceptComparator = new ConceptComparator(); private RoleComparator roleComparator = new RoleComparator(); @@ -511,7 +514,7 @@ protected TreeSet<ObjectProperty> getSuperPropertiesImpl(ObjectProperty role) { Set<Set<OWLObjectProperty>> properties; try { - properties = reasoner.getSuperProperties(getOWLAPIDescription(role)); + properties = reasoner.getSuperProperties(OWLAPIConverter.getOWLAPIObjectProperty(role)); } catch (OWLReasonerException e) { e.printStackTrace(); throw new Error("OWL API classification error."); @@ -523,7 +526,7 @@ protected TreeSet<ObjectProperty> getSubPropertiesImpl(ObjectProperty role) { Set<Set<OWLObjectProperty>> properties; try { - properties = reasoner.getSubProperties(getOWLAPIDescription(role)); + properties = reasoner.getSubProperties(OWLAPIConverter.getOWLAPIObjectProperty(role)); } catch (OWLReasonerException e) { e.printStackTrace(); throw new Error("OWL API classification error."); @@ -534,7 +537,7 @@ private TreeSet<DatatypeProperty> getMoreGeneralDatatypePropertiesImpl(DatatypeProperty role) { Set<Set<OWLDataProperty>> properties; try { - properties = reasoner.getSuperProperties(getOWLAPIDescription(role)); + properties = reasoner.getSuperProperties(OWLAPIConverter.getOWLAPIDataProperty(role)); } catch (OWLReasonerException e) { e.printStackTrace(); throw new Error("OWL API classification error."); @@ -545,7 +548,7 @@ private TreeSet<DatatypeProperty> getMoreSpecialDatatypePropertiesImpl(DatatypeProperty role) { Set<Set<OWLDataProperty>> properties; try { - properties = reasoner.getSubProperties(getOWLAPIDescription(role)); + properties = reasoner.getSubProperties(OWLAPIConverter.getOWLAPIDataProperty(role)); } catch (OWLReasonerException e) { e.printStackTrace(); throw new Error("OWL API classification error."); @@ -606,7 +609,7 @@ @Override public Description getDomainImpl(ObjectProperty objectProperty) { - OWLObjectProperty prop = getOWLAPIDescription(objectProperty); + OWLObjectProperty prop = OWLAPIConverter.getOWLAPIObjectProperty(objectProperty); try { // TODO: look up why OWL API return a two dimensional set here // instead of only one description (probably there can be several @@ -625,7 +628,7 @@ @Override public Description getDomainImpl(DatatypeProperty datatypeProperty) { - OWLDataProperty prop = getOWLAPIDescription(datatypeProperty); + OWLDataProperty prop = OWLAPIConverter.getOWLAPIDataProperty(datatypeProperty); try { // TODO: look up why OWL API return a two dimensional set here // instead of only one description (probably there can be several @@ -644,7 +647,7 @@ @Override public Description getRangeImpl(ObjectProperty objectProperty) { - OWLObjectProperty prop = getOWLAPIDescription(objectProperty); + OWLObjectProperty prop = OWLAPIConverter.getOWLAPIObjectProperty(objectProperty); try { Set<OWLDescription> set = reasoner.getRanges(prop); if(set.size()==0) @@ -658,7 +661,7 @@ @Override public Map<Individual, SortedSet<Individual>> getPropertyMembersImpl(ObjectProperty atomicRole) { - OWLObjectProperty prop = getOWLAPIDescription(atomicRole); + OWLObjectProperty prop = OWLAPIConverter.getOWLAPIObjectProperty(atomicRole); Map<Individual, SortedSet<Individual>> map = new TreeMap<Individual, SortedSet<Individual>>(); for(Individual i : individuals) { OWLIndividual ind = factory.getOWLIndividual(URI.create(i.getName())); @@ -681,9 +684,27 @@ } @Override + protected Map<ObjectProperty,Set<Individual>> getObjectPropertyRelationshipsImpl(Individual individual) { + OWLIndividual ind = factory.getOWLIndividual(URI.create(individual.getName())); + Map<OWLObjectProperty,Set<OWLIndividual>> mapAPI = null; + try { + mapAPI = reasoner.getObjectPropertyRelationships(ind); + } catch (OWLReasonerException e) { + e.printStackTrace(); + } + Map<ObjectProperty,Set<Individual>> map = new TreeMap<ObjectProperty, Set<Individual>>(); + for(Entry<OWLObjectProperty,Set<OWLIndividual>> entry : mapAPI.entrySet()) { + ObjectProperty prop = OWLAPIConverter.convertObjectProperty(entry.getKey()); + Set<Individual> inds = OWLAPIConverter.convertIndividuals(entry.getValue()); + map.put(prop, inds); + } + return map; + } + + @Override public Set<Individual> getRelatedIndividualsImpl(Individual individual, ObjectProperty objectProperty) { OWLIndividual ind = factory.getOWLIndividual(URI.create(individual.getName())); - OWLObjectProperty prop = getOWLAPIDescription(objectProperty); + OWLObjectProperty prop = OWLAPIConverter.getOWLAPIObjectProperty(objectProperty); Set<OWLIndividual> inds = null; try { inds = reasoner.getRelatedIndividuals(ind, prop); @@ -701,18 +722,18 @@ @Override public Set<Constant> getRelatedValuesImpl(Individual individual, DatatypeProperty datatypeProperty) { OWLIndividual ind = factory.getOWLIndividual(URI.create(individual.getName())); - OWLDataProperty prop = getOWLAPIDescription(datatypeProperty); + OWLDataProperty prop = OWLAPIConverter.getOWLAPIDataProperty(datatypeProperty); Set<OWLConstant> constants = null; try { constants = reasoner.getRelatedValues(ind, prop); } catch (OWLReasonerException e) { e.printStackTrace(); } - return convertConstants(constants); + return OWLAPIConverter.convertConstants(constants); } public Map<Individual, SortedSet<Double>> getDoubleValues(DatatypeProperty datatypeProperty) { - OWLDataProperty prop = getOWLAPIDescription(datatypeProperty); + OWLDataProperty prop = OWLAPIConverter.getOWLAPIDataProperty(datatypeProperty); Map<Individual, SortedSet<Double>> map = new TreeMap<Individual, SortedSet<Double>>(); for(Individual i : individuals) { OWLIndividual ind = factory.getOWLIndividual(URI.create(i.getName())); @@ -738,7 +759,7 @@ @Override public Map<Individual, SortedSet<Constant>> getDatatypeMembersImpl(DatatypeProperty datatypeProperty) { - OWLDataProperty prop = getOWLAPIDescription(datatypeProperty); + OWLDataProperty prop = OWLAPIConverter.getOWLAPIDataProperty(datatypeProperty); Map<Individual, SortedSet<Constant>> map = new TreeMap<Individual, SortedSet<Constant>>(); for(Individual i : individuals) { OWLIndividual ind = factory.getOWLIndividual(URI.create(i.getName())); @@ -757,7 +778,7 @@ // for typed constants we have to figure out the correct // data type and value if(oi instanceof OWLTypedConstant) { - Datatype dt = convertDatatype(((OWLTypedConstant)oi).getDataType()); + Datatype dt = OWLAPIConverter.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) @@ -832,14 +853,10 @@ private Set<Description> owlClassesToAtomicConcepts(Set<OWLClass> owlClasses) { Set<Description> concepts = new HashSet<Description>(); for(OWLClass owlClass : owlClasses) - concepts.add(owlClassToAtomicConcept(owlClass)); + concepts.add(OWLAPIConverter.convertClass(owlClass)); return concepts; } - private Description owlClassToAtomicConcept(OWLClass owlClass) { - return new NamedClass(owlClass.getURI().toString()); - } - public static void exportKBToOWL(File owlOutputFile, KB kb, URI ontologyURI) { OWLOntologyManager manager = OWLManager.createOWLOntologyManager(); //URI ontologyURI = URI.create("http://example.com"); @@ -864,66 +881,6 @@ } } - public static Set<Constant> convertConstants(Set<OWLConstant> constants) { - SortedSet<Constant> is = new TreeSet<Constant>(); - for(OWLConstant oi : constants) { - is.add(convertConstant(oi)); - } - return is; - } - - public static Constant convertConstant(OWLConstant constant) { - Constant c; - // for typed constants we have to figure out the correct - // data type and value - if(constant instanceof OWLTypedConstant) { - Datatype dt = convertDatatype(((OWLTypedConstant)constant).getDataType()); - c = new TypedConstant(constant.getLiteral(),dt); - // for untyped constants we have to figure out the value - // and language tag (if any) - } else { - OWLUntypedConstant ouc = (OWLUntypedConstant) constant; - if(ouc.hasLang()) - c = new UntypedConstant(ouc.getLiteral(), ouc.getLang()); - else - c = new UntypedConstant(ouc.getLiteral()); - } - return c; - } - - 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())); - } - - private static OWLEntity getOWLAPIEntity(Entity entity) { - if(entity instanceof ObjectProperty) { - return staticFactory.getOWLObjectProperty(URI.create(entity.getName())); - } else if(entity instanceof DatatypeProperty) { - return staticFactory.getOWLDataProperty(URI.create(entity.getName())); - } else if(entity instanceof NamedClass) { - return staticFactory.getOWLClass(URI.create(entity.getName())); - } else if(entity instanceof OWLIndividual) { - return staticFactory.getOWLIndividual(URI.create(entity.getName())); - } - throw new Error("OWL API entity conversion for " + entity + " not supported."); - } - /** * Test * @@ -1035,14 +992,14 @@ } @Override - @SuppressWarnings("all") + @SuppressWarnings("unchecked") public Set<Constant> getLabelImpl(Entity entity) { - OWLEntity owlEntity = getOWLAPIEntity(entity); + OWLEntity owlEntity = OWLAPIConverter.getOWLAPIEntity(entity); Set<OWLAnnotation> labelAnnotations = owlEntity.getAnnotations(owlAPIOntologies.get(0), URI.create("http://www.w3.org/2000/01/rdf-schema#label")); Set<Constant> annotations = new HashSet<Constant>(); for(OWLAnnotation label : labelAnnotations) { OWLConstant c = ((OWLLabelAnnotation)label).getAnnotationValue(); - annotations.add(convertConstant(c)); + annotations.add(OWLAPIConverter.convertConstant(c)); } return annotations; } Modified: trunk/src/dl-learner/org/dllearner/utilities/owl/OWLAPIConverter.java =================================================================== --- trunk/src/dl-learner/org/dllearner/utilities/owl/OWLAPIConverter.java 2008-11-12 16:25:45 UTC (rev 1511) +++ trunk/src/dl-learner/org/dllearner/utilities/owl/OWLAPIConverter.java 2008-11-14 11:48:57 UTC (rev 1512) @@ -19,10 +19,37 @@ */ package org.dllearner.utilities.owl; +import java.net.URI; +import java.util.Set; +import java.util.SortedSet; +import java.util.TreeSet; + import org.dllearner.core.owl.Axiom; +import org.dllearner.core.owl.Constant; +import org.dllearner.core.owl.Datatype; +import org.dllearner.core.owl.DatatypeProperty; import org.dllearner.core.owl.Description; +import org.dllearner.core.owl.Entity; +import org.dllearner.core.owl.Individual; +import org.dllearner.core.owl.NamedClass; +import org.dllearner.core.owl.Nothing; +import org.dllearner.core.owl.ObjectProperty; +import org.dllearner.core.owl.Thing; +import org.dllearner.core.owl.TypedConstant; +import org.dllearner.core.owl.UntypedConstant; +import org.semanticweb.owl.apibinding.OWLManager; 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.OWLDataType; import org.semanticweb.owl.model.OWLDescription; +import org.semanticweb.owl.model.OWLEntity; +import org.semanticweb.owl.model.OWLIndividual; +import org.semanticweb.owl.model.OWLObjectProperty; +import org.semanticweb.owl.model.OWLTypedConstant; +import org.semanticweb.owl.model.OWLUntypedConstant; /** * A collection of methods for exchanging objects between OWL API and @@ -33,6 +60,8 @@ */ public final class OWLAPIConverter { + private static OWLDataFactory staticFactory = OWLManager.createOWLOntologyManager().getOWLDataFactory(); + /** * Converts a DL-Learner axiom into an OWL API axiom. * @@ -40,7 +69,7 @@ * @param axiom The axiom to convert. * @return An OWL API axiom. */ - public static OWLAxiom convertAxiom(Axiom axiom) { + public static OWLAxiom getOWLAPIAxiom(Axiom axiom) { return OWLAPIAxiomConvertVisitor.convertAxiom(axiom); } @@ -51,9 +80,103 @@ * @param description DL-Learner description. * @return Corresponding OWL API description. */ - public static OWLDescription getOWLDescription(Description description) { + public static OWLDescription getOWLAPIDescription(Description description) { return OWLAPIDescriptionConvertVisitor.getOWLDescription(description); + } + + public static OWLIndividual getOWLAPIIndividual(Individual individual) { + return staticFactory.getOWLIndividual(URI.create(individual.getName())); } + public static OWLObjectProperty getOWLAPIObjectProperty(ObjectProperty role) { + return staticFactory.getOWLObjectProperty(URI.create(role.getName())); + } + public static OWLDataProperty getOWLAPIDataProperty(DatatypeProperty datatypeProperty) { + return staticFactory.getOWLDataProperty(URI.create(datatypeProperty.getName())); + } + + public static OWLEntity getOWLAPIEntity(Entity entity) { + if(entity instanceof ObjectProperty) { + return staticFactory.getOWLObjectProperty(URI.create(entity.getName())); + } else if(entity instanceof DatatypeProperty) { + return staticFactory.getOWLDataProperty(URI.create(entity.getName())); + } else if(entity instanceof NamedClass) { + return staticFactory.getOWLClass(URI.create(entity.getName())); + } else if(entity instanceof OWLIndividual) { + return staticFactory.getOWLIndividual(URI.create(entity.getName())); + } + // should never happen + throw new Error("OWL API entity conversion for " + entity + " not supported."); + } + + public static Individual convertIndividual(OWLIndividual individual) { + return new Individual(individual.getURI().toString()); + } + + public static Set<Individual> convertIndividuals(Set<OWLIndividual> individuals) { + Set<Individual> inds = new TreeSet<Individual>(); + for(OWLIndividual individual : individuals) { + inds.add(convertIndividual(individual)); + } + return inds; + } + + public static ObjectProperty convertObjectProperty(OWLObjectProperty property) { + return new ObjectProperty(property.getURI().toString()); + } + + public static DatatypeProperty convertIndividual(OWLDataProperty property) { + return new DatatypeProperty(property.getURI().toString()); + } + + public static Description convertClass(OWLClass owlClass) { + if(owlClass.isOWLThing()) { + return Thing.instance; + } else if(owlClass.isOWLNothing()) { + return Nothing.instance; + } else { + return new NamedClass(owlClass.getURI().toString()); + } + } + + public static Constant convertConstant(OWLConstant constant) { + Constant c; + // for typed constants we have to figure out the correct + // data type and value + if(constant instanceof OWLTypedConstant) { + Datatype dt = OWLAPIConverter.convertDatatype(((OWLTypedConstant)constant).getDataType()); + c = new TypedConstant(constant.getLiteral(),dt); + // for untyped constants we have to figure out the value + // and language tag (if any) + } else { + OWLUntypedConstant ouc = (OWLUntypedConstant) constant; + if(ouc.hasLang()) + c = new UntypedConstant(ouc.getLiteral(), ouc.getLang()); + else + c = new UntypedConstant(ouc.getLiteral()); + } + return c; + } + + public static Set<Constant> convertConstants(Set<OWLConstant> constants) { + SortedSet<Constant> is = new TreeSet<Constant>(); + for(OWLConstant oi : constants) { + is.add(convertConstant(oi)); + } + return is; + } + + 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."); + } + } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |