001/** 002 * 003 */ 004package org.dllearner.algorithms.isle; 005 006import java.util.HashSet; 007import java.util.Iterator; 008import java.util.Set; 009 010import org.semanticweb.owlapi.model.AxiomType; 011import org.semanticweb.owlapi.model.OWLAnnotationAssertionAxiom; 012import org.semanticweb.owlapi.model.OWLAnnotationProperty; 013import org.semanticweb.owlapi.model.OWLAxiom; 014import org.semanticweb.owlapi.model.OWLClass; 015import org.semanticweb.owlapi.model.OWLDataFactory; 016import org.semanticweb.owlapi.model.OWLDataProperty; 017import org.semanticweb.owlapi.model.OWLEntity; 018import org.semanticweb.owlapi.model.OWLLiteral; 019import org.semanticweb.owlapi.model.OWLObjectProperty; 020import org.semanticweb.owlapi.model.OWLOntology; 021 022import uk.ac.manchester.cs.owl.owlapi.OWLDataFactoryImpl; 023 024import com.google.common.collect.Sets; 025 026/** 027 * @author Lorenz Buehmann 028 * 029 */ 030public class StructuralEntityContext { 031 032 private static OWLDataFactory df = new OWLDataFactoryImpl(); 033 private static Set<OWLAnnotationProperty> annotationProperties = Sets.newHashSet( 034 df.getRDFSLabel(), 035 df.getRDFSComment()); 036 private static Set<String> languages = Sets.newHashSet("en"); 037 038 /** 039 * Returns a set of words that describe entities related to the given entity. 040 * @param ontology 041 * @param entity 042 * @return 043 */ 044 public static Set<String> getContextInNaturalLanguage(OWLOntology ontology, OWLEntity entity){ 045 Set<String> context = new HashSet<>(); 046 047 Set<OWLEntity> contextEntities = getContext(ontology, entity); 048 //add annotations for each entity 049 for (OWLEntity contextEntity : contextEntities) { 050 context.addAll(getAnnotations(ontology, contextEntity)); 051 } 052 053 return context; 054 } 055 056 /** 057 * Returns a set of entities that are structural related to the given entity. 058 * @param ontology 059 * @param entity 060 * @return 061 */ 062 public static Set<OWLEntity> getContext(OWLOntology ontology, OWLEntity entity){ 063 064 Set<OWLEntity> context; 065 if(entity.isOWLClass()){ 066 context = getContext(ontology, entity.asOWLClass()); 067 } else if(entity.isOWLObjectProperty()){ 068 context = getContext(ontology, entity.asOWLObjectProperty()); 069 } else if(entity.isOWLDataProperty()){ 070 context = getContext(ontology, entity.asOWLDataProperty()); 071 } else { 072 throw new UnsupportedOperationException("Unsupported entity type: " + entity); 073 } 074 075 context.add(entity); 076 077 return context; 078 } 079 080 public static Set<OWLEntity> getContext(OWLOntology ontology, OWLObjectProperty property){ 081 Set<OWLEntity> context = new HashSet<>(); 082 083 Set<OWLAxiom> relatedAxioms = new HashSet<>(); 084 relatedAxioms.addAll(ontology.getObjectSubPropertyAxiomsForSubProperty(property)); 085 relatedAxioms.addAll(ontology.getEquivalentObjectPropertiesAxioms(property)); 086 relatedAxioms.addAll(ontology.getObjectPropertyDomainAxioms(property)); 087 relatedAxioms.addAll(ontology.getObjectPropertyRangeAxioms(property)); 088 089 for (OWLAxiom axiom : relatedAxioms) { 090 context.addAll(axiom.getSignature()); 091 } 092 093 return context; 094 } 095 096 public static Set<OWLEntity> getContext(OWLOntology ontology, OWLDataProperty property){ 097 Set<OWLEntity> context = new HashSet<>(); 098 099 Set<OWLAxiom> relatedAxioms = new HashSet<>(); 100 relatedAxioms.addAll(ontology.getDataSubPropertyAxiomsForSubProperty(property)); 101 relatedAxioms.addAll(ontology.getEquivalentDataPropertiesAxioms(property)); 102 relatedAxioms.addAll(ontology.getDataPropertyDomainAxioms(property)); 103 104 for (OWLAxiom axiom : relatedAxioms) { 105 context.addAll(axiom.getSignature()); 106 } 107 108 return context; 109 } 110 111 public static Set<OWLEntity> getContext(OWLOntology ontology, OWLClass cls){ 112 Set<OWLEntity> context = new HashSet<>(); 113 114 Set<OWLAxiom> relatedAxioms = new HashSet<>(); 115 relatedAxioms.addAll(ontology.getSubClassAxiomsForSubClass(cls)); 116 relatedAxioms.addAll(ontology.getEquivalentClassesAxioms(cls)); 117 118 //axioms where cls is domain of a property 119 Set<OWLAxiom> domainAxioms = new HashSet<>(); 120 domainAxioms.addAll(ontology.getAxioms(AxiomType.OBJECT_PROPERTY_DOMAIN)); 121 domainAxioms.addAll(ontology.getAxioms(AxiomType.DATA_PROPERTY_DOMAIN)); 122 for (Iterator<OWLAxiom> iterator = domainAxioms.iterator(); iterator.hasNext();) { 123 OWLAxiom axiom = iterator.next(); 124 if(!axiom.getSignature().contains(cls)){ 125 iterator.remove(); 126 } 127 } 128 relatedAxioms.addAll(domainAxioms); 129 130 //axioms where cls is range of a object property 131 Set<OWLAxiom> rangeAxioms = new HashSet<>(); 132 rangeAxioms.addAll(ontology.getAxioms(AxiomType.OBJECT_PROPERTY_RANGE)); 133 for (Iterator<OWLAxiom> iterator = rangeAxioms.iterator(); iterator.hasNext();) { 134 OWLAxiom axiom = iterator.next(); 135 if(!axiom.getSignature().contains(cls)){ 136 iterator.remove(); 137 } 138 } 139 relatedAxioms.addAll(rangeAxioms); 140 141 for (OWLAxiom axiom : relatedAxioms) { 142 context.addAll(axiom.getSignature()); 143 } 144 145 return context; 146 } 147 148 private static Set<String> getAnnotations(OWLOntology ontology, OWLEntity entity){ 149 Set<String> annotations = new HashSet<>(); 150 Set<OWLAnnotationAssertionAxiom> axioms = ontology.getAnnotationAssertionAxioms(entity.getIRI()); 151 for (OWLAnnotationAssertionAxiom annotation : axioms) { 152 if(annotationProperties.contains(annotation.getProperty())){ 153 if (annotation.getValue() instanceof OWLLiteral) { 154 OWLLiteral val = (OWLLiteral) annotation.getValue(); 155 if(val.getLang() != null && !val.getLang().isEmpty()){ 156 if(languages.contains(val.getLang())){ 157 if(!val.getLiteral().isEmpty()){ 158 annotations.add(val.getLiteral()); 159 } 160 } 161 } else { 162 if(!val.getLiteral().isEmpty()){ 163 annotations.add(val.getLiteral()); 164 } 165 } 166 } 167 } 168 } 169 return annotations; 170 } 171 172}