001    /**
002     * Copyright (C) 2007-2011, Jens Lehmann
003     *
004     * This file is part of DL-Learner.
005     *
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     *
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     *
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     */
019    
020    package org.dllearner.learningproblems;
021    
022    import java.util.Collection;
023    import java.util.LinkedList;
024    import java.util.Set;
025    import java.util.SortedSet;
026    
027    import org.dllearner.core.AbstractLearningProblem;
028    import org.dllearner.core.AbstractReasonerComponent;
029    import org.dllearner.core.options.BooleanConfigOption;
030    import org.dllearner.core.options.CommonConfigMappings;
031    import org.dllearner.core.options.CommonConfigOptions;
032    import org.dllearner.core.options.ConfigEntry;
033    import org.dllearner.core.options.ConfigOption;
034    import org.dllearner.core.options.InvalidConfigOptionValueException;
035    import org.dllearner.core.options.StringConfigOption;
036    import org.dllearner.core.options.StringSetConfigOption;
037    import org.dllearner.core.owl.Description;
038    import org.dllearner.core.owl.Individual;
039    import org.dllearner.utilities.Helper;
040    
041    /**
042     * @author Jens Lehmann
043     *
044     */
045    public abstract class PosNegLP extends AbstractLearningProblem {
046            
047            protected SortedSet<Individual> positiveExamples;
048            protected SortedSet<Individual> negativeExamples;
049            protected SortedSet<Individual> allExamples;
050            
051            protected boolean useRetrievalForClassification = false;
052            protected UseMultiInstanceChecks useMultiInstanceChecks = UseMultiInstanceChecks.TWOCHECKS;
053            protected double percentPerLengthUnit = 0.05;
054    
055            /**
056             * If instance checks are used for testing concepts (e.g. no retrieval), then
057             * there are several options to do this. The enumeration lists the supported
058             * options. These options are only important if the reasoning mechanism 
059             * supports sending several reasoning requests at once as it is the case for
060             * DIG reasoners.
061             * 
062             * @author Jens Lehmann
063             *
064             */
065            public enum UseMultiInstanceChecks {
066                    /**
067                     * Perform a separate instance check for each example.
068                     */
069                    NEVER,
070                    /**
071                     * Perform one instance check for all positive and one instance check
072                     * for all negative examples.
073                     */
074                    TWOCHECKS,
075                    /**
076                     * Perform all instance checks at once.
077                     */
078                    ONECHECK
079            };
080            
081            public PosNegLP(AbstractReasonerComponent reasoningService) {
082                    super(reasoningService);
083            }
084            
085            public static Collection<ConfigOption<?>> createConfigOptions() {
086                    Collection<ConfigOption<?>> options = new LinkedList<ConfigOption<?>>();
087                    options.add(new StringSetConfigOption("positiveExamples",
088                                    "positive examples",null, true, false));
089                    options.add(new StringSetConfigOption("negativeExamples",
090                                    "negative examples",null, true, false));
091                    options.add(new BooleanConfigOption("useRetrievalForClassficiation", 
092                                    "Specifies whether to use retrieval or instance checks for testing a concept. - NO LONGER FULLY SUPPORTED.", false));
093                    options.add(CommonConfigOptions.getPercentPerLenghtUnitOption(0.05));
094                    StringConfigOption multiInstanceChecks = new StringConfigOption("useMultiInstanceChecks", "See UseMultiInstanceChecks enum. - NO LONGER FULLY SUPPORTED.","twoChecks");
095                    multiInstanceChecks.setAllowedValues(new String[] {"never", "twoChecks", "oneCheck"});
096                    options.add(multiInstanceChecks);
097                    return options;
098            }
099            
100            /*
101             * (non-Javadoc)
102             * 
103             * @see org.dllearner.core.Component#applyConfigEntry(org.dllearner.core.ConfigEntry)
104             */
105            @Override
106            @SuppressWarnings( { "unchecked" })
107            public <T> void applyConfigEntry(ConfigEntry<T> entry) throws InvalidConfigOptionValueException {
108                    String name = entry.getOptionName();
109                    if (name.equals("positiveExamples"))
110                            positiveExamples = CommonConfigMappings
111                                            .getIndividualSet((Set<String>) entry.getValue());
112                    else if (name.equals("negativeExamples"))
113                            negativeExamples = CommonConfigMappings
114                                            .getIndividualSet((Set<String>) entry.getValue());
115                    else if (name.equals("useRetrievalForClassficiation")) {
116                            useRetrievalForClassification = (Boolean) entry.getValue();
117                    } else if (name.equals("percentPerLengthUnit"))
118                            percentPerLengthUnit = (Double) entry.getValue();
119                    else if (name.equals("useMultiInstanceChecks")) {
120                            String value = (String) entry.getValue();
121                            if(value.equals("oneCheck"))
122                                    useMultiInstanceChecks = UseMultiInstanceChecks.ONECHECK;
123                            else if(value.equals("twoChecks"))
124                                    useMultiInstanceChecks = UseMultiInstanceChecks.TWOCHECKS;
125                            else
126                                    useMultiInstanceChecks = UseMultiInstanceChecks.NEVER;  
127                    }
128            }
129            
130            /*
131             * (non-Javadoc)
132             * 
133             * @see org.dllearner.core.Component#init()
134             */
135            @Override
136            public void init() {
137                    allExamples = Helper.union(positiveExamples, negativeExamples);
138            }
139            
140            public SortedSet<Individual> getNegativeExamples() {
141                    return negativeExamples;
142            }
143    
144            public SortedSet<Individual> getPositiveExamples() {
145                    return positiveExamples;
146            }
147            
148            public void setNegativeExamples(SortedSet<Individual> set) {
149                    this.negativeExamples=set;
150            }
151    
152            public void setPositiveExamples(SortedSet<Individual> set) {
153                    this.positiveExamples=set;
154            }
155            
156            public abstract int coveredNegativeExamplesOrTooWeak(Description concept);
157    
158            public double getPercentPerLengthUnit() {
159                    return percentPerLengthUnit;
160            }
161            
162    }