001/**
002 * Copyright (C) 2007 - 2016, Jens Lehmann
003 * <p>
004 * This file is part of DL-Learner.
005 * <p>
006 * DL-Learner is free software; you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation; either version 3 of the License, or
009 * (at your option) any later version.
010 * <p>
011 * DL-Learner is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU General Public License for more details.
015 * <p>
016 * You should have received a copy of the GNU General Public License
017 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
018 */
019package org.dllearner.learningproblems;
020
021import com.google.common.collect.Sets;
022import org.dllearner.core.AbstractReasonerComponent;
023import org.dllearner.core.ComponentAnn;
024import org.dllearner.core.ComponentInitException;
025import org.dllearner.core.EvaluatedDescription;
026import org.dllearner.core.config.ConfigOption;
027import org.dllearner.utilities.Helper;
028import org.semanticweb.owlapi.model.OWLClassExpression;
029import org.semanticweb.owlapi.model.OWLIndividual;
030
031import java.util.Set;
032import java.util.SortedSet;
033import java.util.TreeSet;
034
035/**
036 * A ternary learning problem (positive, negative and uncertain instances) to manage the problem of the Open World Assumption
037 * typically employed for ontologies
038 * @author Utente
039 *
040 */
041@ComponentAnn(name = "PosNegUndLP", shortName = "posNegUndLP", version = 1.0, description = "A learning problem with uncertain-membership instances")
042public class PosNegUndLP extends PosNegLPStandard implements Cloneable {
043
044    @ConfigOption(description = "the uncertain examples", required = true)
045    private Set<OWLIndividual> uncertainExamples;
046
047        public PosNegUndLP() {}
048
049        public PosNegUndLP(AbstractReasonerComponent reasoner) {
050                super(reasoner);
051        }
052
053        public PosNegUndLP(AbstractReasonerComponent reasoningService,
054                                           SortedSet<OWLIndividual> positiveExamples,
055                                           SortedSet<OWLIndividual> negativeExamples,
056                                           SortedSet<OWLIndividual> uncertainExamples) {
057                super(reasoningService, positiveExamples, negativeExamples);
058                this.uncertainExamples = uncertainExamples;
059        }
060
061    public Set<OWLIndividual> getPositiveExamples() {
062        return new TreeSet<>(super.getPositiveExamples());
063    }
064
065    public Set<OWLIndividual> getNegativeExamples() {
066        return new TreeSet<>(super.getNegativeExamples());
067    }
068
069    public Set<OWLIndividual> getUncertainExamples() {
070        return new TreeSet<>(uncertainExamples);
071    }
072
073    public void setUncertainExamples(Set<OWLIndividual> uncertainExample) {
074        this.uncertainExamples = uncertainExample;
075    }
076
077    @Override
078    public void init() throws ComponentInitException {
079        // sanity check whether examples are contained in KB
080        Helper.checkIndividuals(reasoner, Sets.union(Sets.union(positiveExamples, negativeExamples), uncertainExamples));
081
082        initialized = true;
083    }
084
085    //TODO add two methods: the first one performs classification by inducing the derived concept definition (see the source code of PosNegstandard )
086    // the second one performs classification with the induced algorithms.tree.models in order to deal with specific settings such as binary classification or missing values for TDTs and ETDTs
087
088    /**
089     * A method for binarizing a ternary learning problem. This is important to work if you want to run a method
090     * such as CELOE starting from randomly generated queries
091     * @return the pos/neg learning problem
092     */
093    public PosNegLP getPosNegLP() {
094        PosNegLP binaryProblem = new PosNegLPStandard(getReasoner());
095        binaryProblem.setPositiveExamples(getPositiveExamples());
096        SortedSet<OWLIndividual> therestOfWorld = new TreeSet<>();
097        //positive vs. the rest  of world
098        therestOfWorld.addAll(getNegativeExamples());
099        therestOfWorld.addAll(uncertainExamples);
100        binaryProblem.setNegativeExamples(therestOfWorld);
101//          System.out.println(getPositiveExamples().size()+"    "+therestOfWorld.size());
102
103        return binaryProblem;
104    }
105
106}