001package org.dllearner.algorithms.pattern; 002 003import org.semanticweb.owlapi.model.*; 004import org.semanticweb.owlapi.util.OWLAxiomVisitorExAdapter; 005import uk.ac.manchester.cs.owl.owlapi.OWLDataFactoryImpl; 006 007import javax.annotation.Nonnull; 008import java.util.Collections; 009import java.util.Set; 010import java.util.TreeSet; 011import java.util.stream.Collectors; 012 013/** 014 * @author Lorenz Buehmann 015 */ 016public class OWLAxiomGeneralizer extends OWLAxiomVisitorExAdapter<Set<OWLAxiom>> { 017 018 private OWLDataFactory df = new OWLDataFactoryImpl(); 019 020 OWLClassExpressionGeneralizer ceGeneralizer = new OWLClassExpressionGeneralizer(df); 021 022 public OWLAxiomGeneralizer() { 023 super(Collections.EMPTY_SET); 024 } 025 026 public Set<OWLAxiom> generalize(OWLAxiom ax) { 027 return ax.accept(this); 028 } 029 030 @Nonnull 031 @Override 032 protected Set<OWLAxiom> doDefault(@Nonnull OWLAxiom axiom) { 033 return Collections.EMPTY_SET; 034// return Collections.singleton(axiom); 035 } 036 037 @Override 038 public Set<OWLAxiom> visit(OWLSubClassOfAxiom axiom) { 039 OWLClassExpression sub = axiom.getSubClass(); 040 OWLClassExpression sup = axiom.getSuperClass(); 041 042 Set<OWLClassExpression> supGens = ceGeneralizer.generalize(sup); 043 044 return supGens.stream() 045 .map(supGen -> df.getOWLSubClassOfAxiom(sub, supGen)) 046 .collect(Collectors.toCollection(TreeSet::new)); 047 } 048 049 @Override 050 public Set<OWLAxiom> visit(OWLObjectPropertyDomainAxiom axiom) { 051 return ceGeneralizer.generalize(axiom.getDomain()).stream() 052 .map(dom -> df.getOWLObjectPropertyDomainAxiom(axiom.getProperty(), dom)) 053 .collect(Collectors.toCollection(TreeSet::new)); 054 } 055 056 @Override 057 public Set<OWLAxiom> visit(OWLObjectPropertyRangeAxiom axiom) { 058 return ceGeneralizer.generalize(axiom.getRange()).stream() 059 .map(dom -> df.getOWLObjectPropertyRangeAxiom(axiom.getProperty(), dom)) 060 .collect(Collectors.toCollection(TreeSet::new)); 061 } 062 063 @Override 064 public Set<OWLAxiom> visit(OWLDataPropertyDomainAxiom axiom) { 065 return ceGeneralizer.generalize(axiom.getDomain()).stream() 066 .map(dom -> df.getOWLDataPropertyDomainAxiom(axiom.getProperty(), dom)) 067 .collect(Collectors.toCollection(TreeSet::new)); 068 } 069}