001 /**
002 * Copyright (C) 2007-2008, 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.gui;
021
022 import java.io.File;
023 import java.io.FileWriter;
024 import java.io.IOException;
025 import java.io.PrintWriter;
026 import java.net.URL;
027 import java.util.List;
028
029 import org.dllearner.cli.ConfMapper;
030 import org.dllearner.core.Component;
031 import org.dllearner.core.ComponentManager;
032 import org.dllearner.core.KnowledgeSource;
033 import org.dllearner.core.LearningAlgorithm;
034 import org.dllearner.core.LearningProblem;
035 import org.dllearner.core.ReasonerComponent;
036 import org.dllearner.core.options.ConfigEntry;
037 import org.dllearner.core.options.ConfigOption;
038 import org.dllearner.core.options.InvalidConfigOptionValueException;
039 import org.dllearner.kb.KBFile;
040 import org.dllearner.kb.OWLFile;
041 import org.dllearner.kb.sparql.SparqlKnowledgeSource;
042
043 /**
044 * Save a conf file.
045 *
046 * @author Jens Lehmann
047 * @author Tilo Hielscher
048 */
049 public class ConfigSave {
050
051 private ConfMapper confMapper = new ConfMapper();
052 private ComponentManager cm = ComponentManager.getInstance();
053
054 // private File file;
055 private Config config;
056
057 /**
058 * Creates object to save config.
059 *
060 * @param config The central configuration handler.
061 */
062 public ConfigSave(Config config) {
063 this.config = config;
064 }
065
066 /**
067 * Save current configuration to a conf file.
068 * @param file The conf file to save to.
069 * @throws IOException Thrown if any IO errors occurr while writing the file.
070 */
071 public void saveFile(File file) throws IOException {
072 PrintWriter out = new PrintWriter(new FileWriter(file));
073 out.println("/**");
074 out.println(" * Conf file generated by DL-Learner GUI.");
075 out.println(" */");
076
077 // knowledge source
078 out.println("\n// knowledge source");
079 KnowledgeSource ks = config.getKnowledgeSource();
080 if (ks != null) {
081 // String ksString = confMapper.getComponentString(config.getKnowledgeSource().getClass());
082 URL url = (URL) cm.getConfigOptionValue(ks, "url");
083 if(ks instanceof OWLFile || ks instanceof KBFile) {
084 out.println("import(\"" + url + "\");");
085 } else if(ks instanceof SparqlKnowledgeSource) {
086 out.println("import(\"" + url + "\",\"SPARQL\");");
087 }
088 writeConfigEntries(ks, out);
089 }
090
091 // reasoner
092 out.println("\n// reasoner");
093 ReasonerComponent rc = config.getReasoner();
094 if (rc != null) {
095 String typeString = confMapper.getComponentTypeString(ReasonerComponent.class);
096 String componentString = confMapper.getComponentString(rc.getClass());
097 out.println(typeString + " = " + componentString + ";");
098
099 writeConfigEntries(config.getReasoner(), out);
100 }
101
102 // learning problem
103 out.println("\n// learning problem");
104 LearningProblem lp = config.getLearningProblem();
105 if (lp != null) {
106 String typeString = confMapper.getComponentTypeString(LearningProblem.class);
107 String componentString = confMapper.getComponentString(lp.getClass());
108 out.println(typeString + " = " + componentString + ";");
109
110 writeConfigEntries(config.getLearningProblem(), out);
111 }
112
113 // learning algorithm
114 out.println("\n// learning algorithm");
115 LearningAlgorithm la = config.getLearningAlgorithm();
116 if (la != null) {
117 String typeString = confMapper.getComponentTypeString(LearningAlgorithm.class);
118 String componentString = confMapper.getComponentString(la.getClass());
119 out.println(typeString + " = " + componentString + ";");
120
121 writeConfigEntries(config.getLearningAlgorithm(), out);
122 }
123
124 out.flush();
125 out.close();
126 }
127
128 /**
129 * Writes all entries of the given component.
130 *
131 * @param component
132 * i.e. config.getKnowledgeSource(), config.getResaoner(), ...
133 */
134 @SuppressWarnings("unchecked")
135 private void writeConfigEntries(Component component, PrintWriter out) {
136
137 // prefix (left hand side of all entries except special cases)
138 String prefix = confMapper.getComponentString(component.getClass());
139 Class<? extends Component> componentClass = component.getClass();
140
141 List<ConfigOption<?>> optionList = ComponentManager.getConfigOptions(componentClass);
142 for (int i = 0; i < optionList.size(); i++) {
143 try {
144 // get value and default value of option
145 Object defaultValue = optionList.get(i).getDefaultValue();
146 Object value = config.getComponentManager().getConfigOptionValue(component,
147 optionList.get(i).getName());
148
149 // we do not write option "url" as it is used in imports already;
150 // TODO this is not very clean - maybe the special import construct
151 // should be remove to avoid this special case;
152 if (optionList.get(i).getName() != "url" && value != null) {
153 if (value != null) {
154 // only write values which are not equal to the default value
155 if (!value.equals(defaultValue)) {
156 ConfigOption option = cm.getConfigOption(componentClass, optionList.get(i).getName());
157 ConfigEntry entry = new ConfigEntry(option, value);
158 // call toConfString method of ConfigEntry
159 out.println(entry.toConfString(prefix));
160 }
161 }
162 }
163 } catch (InvalidConfigOptionValueException e) {
164 e.printStackTrace();
165 }
166 }
167 }
168 }