001package conexp.fx.core.dl.deprecated;
002
003/*
004 * #%L
005 * Concept Explorer FX
006 * %%
007 * Copyright (C) 2010 - 2019 Francesco Kriegel
008 * %%
009 * This program is free software: you can redistribute it and/or modify
010 * it under the terms of the GNU General Public License as
011 * published by the Free Software Foundation, either version 3 of the
012 * License, or (at your option) any later version.
013 * 
014 * This program is distributed in the hope that it will be useful,
015 * but WITHOUT ANY WARRANTY; without even the implied warranty of
016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
017 * GNU General Public License for more details.
018 * 
019 * You should have received a copy of the GNU General Public
020 * License along with this program.  If not, see
021 * <http://www.gnu.org/licenses/gpl-3.0.html>.
022 * #L%
023 */
024
025import java.util.Set;
026import java.util.function.BiPredicate;
027import java.util.stream.Collectors;
028
029import org.semanticweb.HermiT.Reasoner;
030import org.semanticweb.owlapi.apibinding.OWLManager;
031import org.semanticweb.owlapi.model.OWLClassExpression;
032import org.semanticweb.owlapi.model.OWLObjectAllValuesFrom;
033import org.semanticweb.owlapi.model.OWLObjectComplementOf;
034import org.semanticweb.owlapi.model.OWLObjectExactCardinality;
035import org.semanticweb.owlapi.model.OWLObjectIntersectionOf;
036import org.semanticweb.owlapi.model.OWLObjectMaxCardinality;
037import org.semanticweb.owlapi.model.OWLObjectMinCardinality;
038import org.semanticweb.owlapi.model.OWLObjectSomeValuesFrom;
039import org.semanticweb.owlapi.model.OWLOntologyCreationException;
040
041@Deprecated
042public class OWLMinimizer {
043
044  public static final Reasoner r = initializeReasoner();
045
046  private static final Reasoner initializeReasoner() {
047    try {
048      return new Reasoner(OWLManager.createOWLOntologyManager().createOntology());
049    } catch (OWLOntologyCreationException e) {
050      e.printStackTrace();
051      return null;
052    }
053  }
054
055  public static final boolean isSubsumedBy(final OWLClassExpression c1, final OWLClassExpression c2) {
056    return r.isEntailed(OWLManager.getOWLDataFactory().getOWLSubClassOfAxiom(c1, c2));
057  }
058
059  public static final boolean subsumes(final OWLClassExpression c1, final OWLClassExpression c2) {
060    return isSubsumedBy(c2, c1);
061  }
062
063  public static final <T> Set<T> filterMinimal(final Set<T> elements, BiPredicate<T, T> predicate) {
064    return elements
065        .stream()
066        .filter(x -> !elements.stream().anyMatch(y -> !x.equals(y) && predicate.test(x, y)))
067        .collect(Collectors.toSet());
068  }
069
070  public static final <T> Set<T> filterMinimalParallel(final Set<T> elements, BiPredicate<T, T> predicate) {
071    return elements
072        .parallelStream()
073        .filter(x -> !elements.parallelStream().anyMatch(y -> !x.equals(y) && predicate.test(x, y)))
074        .collect(Collectors.toSet());
075  }
076
077  public static final OWLClassExpression minimizeConjunction(final OWLClassExpression classExpression) {
078    if (classExpression instanceof OWLObjectIntersectionOf) {
079      final OWLObjectIntersectionOf c = (OWLObjectIntersectionOf) classExpression;
080      return OWLManager
081          .getOWLDataFactory()
082          .getOWLObjectIntersectionOf(filterMinimal(c.getOperands(), OWLMinimizer::subsumes));
083    } else if (classExpression instanceof OWLObjectComplementOf) {
084      final OWLObjectComplementOf c = (OWLObjectComplementOf) classExpression;
085      return OWLManager.getOWLDataFactory().getOWLObjectComplementOf(minimizeConjunction(c.getOperand()));
086    } else if (classExpression instanceof OWLObjectSomeValuesFrom) {
087      final OWLObjectSomeValuesFrom c = (OWLObjectSomeValuesFrom) classExpression;
088      return OWLManager
089          .getOWLDataFactory()
090          .getOWLObjectSomeValuesFrom(c.getProperty(), minimizeConjunction(c.getFiller()));
091    } else if (classExpression instanceof OWLObjectAllValuesFrom) {
092      final OWLObjectAllValuesFrom c = (OWLObjectAllValuesFrom) classExpression;
093      return OWLManager
094          .getOWLDataFactory()
095          .getOWLObjectSomeValuesFrom(c.getProperty(), minimizeConjunction(c.getFiller()));
096    } else if (classExpression instanceof OWLObjectMinCardinality) {
097      final OWLObjectMinCardinality c = (OWLObjectMinCardinality) classExpression;
098      return OWLManager
099          .getOWLDataFactory()
100          .getOWLObjectMinCardinality(c.getCardinality(), c.getProperty(), minimizeConjunction(c.getFiller()));
101    } else if (classExpression instanceof OWLObjectMaxCardinality) {
102      final OWLObjectMaxCardinality c = (OWLObjectMaxCardinality) classExpression;
103      return OWLManager
104          .getOWLDataFactory()
105          .getOWLObjectMaxCardinality(c.getCardinality(), c.getProperty(), minimizeConjunction(c.getFiller()));
106    } else if (classExpression instanceof OWLObjectExactCardinality) {
107      final OWLObjectExactCardinality c = (OWLObjectExactCardinality) classExpression;
108      return OWLManager
109          .getOWLDataFactory()
110          .getOWLObjectExactCardinality(c.getCardinality(), c.getProperty(), minimizeConjunction(c.getFiller()));
111    }
112    return classExpression;
113  }
114}