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.utilities.components;
021    
022    import java.net.URL;
023    import java.util.HashSet;
024    import java.util.Set;
025    
026    import org.dllearner.algorithms.ocel.OCEL;
027    import org.dllearner.core.ComponentInitException;
028    import org.dllearner.core.ComponentManager;
029    import org.dllearner.core.AbstractKnowledgeSource;
030    import org.dllearner.core.AbstractCELA;
031    import org.dllearner.core.AbstractLearningProblem;
032    import org.dllearner.core.LearningProblemUnsupportedException;
033    import org.dllearner.core.AbstractReasonerComponent;
034    import org.dllearner.kb.OWLFile;
035    import org.dllearner.learningproblems.PosNegLPStandard;
036    import org.dllearner.reasoning.FastInstanceChecker;
037    
038    /**
039     * A mix of components, which are typically combined to create a full 
040     * learning task.
041     * 
042     * Add more constructors if you like (they should be useful in general,
043     * not just for a very specific scenario).
044     * 
045     * @author Jens Lehmann
046     *
047     */
048    public class ComponentCombo {
049    
050            private Set<AbstractKnowledgeSource> sources;
051            private AbstractReasonerComponent reasoner;
052            private AbstractLearningProblem problem;
053            private AbstractCELA algorithm;
054            
055            /**
056             * Builds a component combination object from the specified components. 
057             * @param source A knowledge source.
058             * @param reasoner A reasoner.
059             * @param problem A learning problem.
060             * @param algorithm A learning algorithm.
061             */
062            public ComponentCombo(AbstractKnowledgeSource source, AbstractReasonerComponent reasoner, AbstractLearningProblem problem, AbstractCELA algorithm) {
063                    this(getSourceSet(source), reasoner, problem, algorithm);
064            }       
065            
066            /**
067             * Builds a component combination object from the specified components. 
068             * @param sources A set of knowledge sources.
069             * @param reasoner A reasoner.
070             * @param problem A learning problem.
071             * @param algorithm A learning algorithm.
072             */     
073            public ComponentCombo(Set<AbstractKnowledgeSource> sources, AbstractReasonerComponent reasoner, AbstractLearningProblem problem, AbstractCELA algorithm) {
074                    this.sources = sources;
075                    this.reasoner = reasoner;
076                    this.problem = problem;
077                    this.algorithm = algorithm;
078            }               
079            
080            private static Set<AbstractKnowledgeSource> getSourceSet(AbstractKnowledgeSource source) {
081                    Set<AbstractKnowledgeSource> sources = new HashSet<AbstractKnowledgeSource>();
082                    sources.add(source);
083                    return sources;
084            }
085    
086            /**
087             * Builds a standard combination of components. Currently, this is an OWL
088             * File, the FastInstanceChecker reasoning algorithm, a definition learning
089             * problem with positive and negative examples, and the example based
090             * refinement algorithm.
091             * @param owlFile URL of an OWL file (background knowledge).
092             * @param posExamples Set of positive examples.
093             * @param negExamples Set of negative examples.
094             */
095            public ComponentCombo(URL owlFile, Set<String> posExamples, Set<String> negExamples) {
096                    ComponentManager cm = ComponentManager.getInstance();
097                    AbstractKnowledgeSource source = cm.knowledgeSource(OWLFile.class);
098                    sources = getSourceSet(source);
099                    reasoner = cm.reasoner(FastInstanceChecker.class, source);
100                    problem = cm.learningProblem(PosNegLPStandard.class, reasoner);
101                    cm.applyConfigEntry(problem, "positiveExamples", posExamples);
102                    cm.applyConfigEntry(problem, "negativeExamples", negExamples);
103                    try {
104                            algorithm = cm.learningAlgorithm(OCEL.class, problem, reasoner);
105                    } catch (LearningProblemUnsupportedException e) {
106                            e.printStackTrace();
107                    }
108            }
109    
110            /**
111             * Initialise all components.
112             * @throws ComponentInitException Thrown if a component could not be initialised properly.
113             */
114            public void initAll() throws ComponentInitException {
115                    for(AbstractKnowledgeSource source : sources) {
116                            source.init();
117                    }
118                    reasoner.init();
119                    problem.init();
120                    algorithm.init();
121            }
122            
123            /**
124             * @return the sources
125             */
126            public Set<AbstractKnowledgeSource> getSources() {
127                    return sources;
128            }
129    
130            /**
131             * @return the reasoner
132             */
133            public AbstractReasonerComponent getReasoner() {
134                    return reasoner;
135            }
136    
137            /**
138             * @return the problem
139             */
140            public AbstractLearningProblem getProblem() {
141                    return problem;
142            }
143    
144            /**
145             * @return the algorithm
146             */
147            public AbstractCELA getAlgorithm() {
148                    return algorithm;
149            }       
150            
151    
152    }