/
CLI.java
272 lines (223 loc) · 8.76 KB
/
CLI.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/**
* Copyright (C) 2007-2011, Jens Lehmann
*
* This file is part of DL-Learner.
*
* DL-Learner is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* DL-Learner is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package org.dllearner.cli;
import org.apache.log4j.Level;
import org.dllearner.algorithms.decisiontrees.dsttdt.DSTTDTClassifier;
import org.dllearner.algorithms.decisiontrees.refinementoperators.DLTreesRefinementOperator;
import org.dllearner.algorithms.decisiontrees.tdt.TDTClassifier;
import org.dllearner.configuration.IConfiguration;
import org.dllearner.configuration.spring.ApplicationContextBuilder;
import org.dllearner.configuration.spring.DefaultApplicationContextBuilder;
import org.dllearner.confparser.ConfParserConfiguration;
import org.dllearner.confparser.ParseException;
import org.dllearner.core.*;
import org.dllearner.core.config.ConfigOption;
import org.dllearner.learningproblems.PosNegLP;
import org.semanticweb.owlapi.model.OWLClass;
import org.semanticweb.owlapi.model.OWLObjectProperty;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
//import org.dllearner.algorithms.qtl.QTL2;
/**
*
* New commandline interface.
*
* @author Jens Lehmann
*
*/
@ComponentAnn(name = "Command Line Interface", version = 0, shortName = "")
public class CLI extends CLIBase2 {
private static Logger logger = LoggerFactory.getLogger(CLI.class);
private LearningAlgorithm algorithm;
private KnowledgeSource knowledgeSource;
// some CLI options
@ConfigOption(defaultValue = "false", description = "Run in Cross-Validation mode")
private boolean performCrossValidation = false;
@ConfigOption(defaultValue = "10", description = "Number of folds in Cross-Validation mode")
private int nrOfFolds = 10;
private AbstractClassExpressionLearningProblem lp;
private AbstractReasonerComponent rs;
private AbstractCELA la;
public CLI() {
}
public CLI(File confFile) {
this();
this.confFile = confFile;
}
// separate init methods, because some scripts may want to just get the application
// context from a conf file without actually running it
@Override
public void init() throws IOException {
if(context == null) {
super.init();
knowledgeSource = context.getBean(KnowledgeSource.class);
rs = getMainReasonerComponent();
la = context.getBean(AbstractCELA.class);
lp = context.getBean(AbstractClassExpressionLearningProblem.class);
}
}
@Override
public void run() {
try {
org.apache.log4j.Logger.getLogger("org.dllearner").setLevel(Level.toLevel(logLevel.toUpperCase()));
} catch (Exception e) {
logger.warn("Error setting log level to " + logLevel);
}
rs = getMainReasonerComponent();
if (performCrossValidation) {
la = context.getBeansOfType(AbstractCELA.class).entrySet().iterator().next().getValue();
PosNegLP lp = context.getBean(PosNegLP.class);
// if(la instanceof QTL2){
// //new SPARQLCrossValidation((QTL2Disjunctive) la,lp,rs,nrOfFolds,false);
// }
if((la instanceof TDTClassifier)||(la instanceof DSTTDTClassifier) ){
//TODO: verify if the quality of the code can be improved
DLTreesRefinementOperator op = context.getBeansOfType(DLTreesRefinementOperator.class).entrySet().iterator().next().getValue();
ArrayList<OWLClass> concepts = new ArrayList<>(rs.getClasses());
op.setAllConcepts(concepts);
ArrayList<OWLObjectProperty> roles = new ArrayList<>(rs.getAtomicRolesList());
op.setAllConcepts(concepts);
op.setAllRoles(roles);
op.setReasoner(getMainReasonerComponent());
if (la instanceof TDTClassifier)
((TDTClassifier)la).setOperator(op);
else
((DSTTDTClassifier)la).setOperator(op);
new CrossValidation2(la,lp,rs,nrOfFolds,false);
}else {
new CrossValidation2(la,lp,rs,nrOfFolds,false);
}
} else {
if(context.getBean(AbstractLearningProblem.class) instanceof AbstractClassExpressionLearningProblem) {
lp = context.getBean(AbstractClassExpressionLearningProblem.class);
} else {
}
Map<String, LearningAlgorithm> learningAlgs = context.getBeansOfType(LearningAlgorithm.class);
// knowledgeSource = context.getBeansOfType(Knowledge1Source.class).entrySet().iterator().next().getValue();
for(Entry<String, LearningAlgorithm> entry : learningAlgs.entrySet()){
algorithm = entry.getValue();
logger.info("Running algorithm instance \"" + entry.getKey() + "\" (" + algorithm.getClass().getSimpleName() + ")");
algorithm.start();
}
}
}
/**
* @return the lp
*/
public AbstractClassExpressionLearningProblem getLearningProblem() {
return lp;
}
/**
* @return the rs
*/
public AbstractReasonerComponent getReasonerComponent() {
return rs;
}
/**
* @return the la
*/
public AbstractCELA getLearningAlgorithm() {
return la;
}
/**
* @param args
* @throws ParseException
* @throws IOException
* @throws ReasoningMethodUnsupportedException
*/
public static void main(String[] args) throws ParseException, IOException, ReasoningMethodUnsupportedException {
// System.out.println("DL-Learner " + Info.build + " [TODO: read pom.version and put it here (make sure that the code for getting the version also works in the release build!)] command line interface");
System.out.println("DL-Learner command line interface");
// currently, CLI has exactly one parameter - the conf file
if(args.length == 0) {
System.out.println("You need to give a conf file as argument.");
System.exit(0);
}
// read file and print and print a message if it does not exist
File file = new File(args[args.length - 1]);
if(!file.exists()) {
System.out.println("File \"" + file + "\" does not exist.");
System.exit(0);
}
Resource confFile = new FileSystemResource(file);
List<Resource> springConfigResources = new ArrayList<>();
try {
//DL-Learner Configuration Object
IConfiguration configuration = new ConfParserConfiguration(confFile);
ApplicationContextBuilder builder = new DefaultApplicationContextBuilder();
ApplicationContext context = builder.buildApplicationContext(configuration,springConfigResources);
// TODO: later we could check which command line interface is specified in the conf file
// for now we just use the default one
CLIBase2 cli;
if(context.containsBean("cli")) {
cli = (CLIBase2) context.getBean("cli");
} else {
cli = new CLI();
}
cli.setContext(context);
cli.setConfFile(file);
cli.run();
} catch (Exception e) {
String stacktraceFileName = "log/error.log";
//Find the primary cause of the exception.
Throwable primaryCause = findPrimaryCause(e);
// Get the Root Error Message
logger.error("An Error Has Occurred During Processing.");
if (primaryCause != null) {
logger.error(primaryCause.getMessage());
}
logger.debug("Stack Trace: ", e);
logger.error("Terminating DL-Learner...and writing stacktrace to: " + stacktraceFileName);
createIfNotExists(new File(stacktraceFileName));
FileOutputStream fos = new FileOutputStream(stacktraceFileName);
PrintStream ps = new PrintStream(fos);
e.printStackTrace(ps);
}
}
public boolean isPerformCrossValidation() {
return performCrossValidation;
}
public void setPerformCrossValidation(boolean performCrossValiation) {
this.performCrossValidation = performCrossValiation;
}
public int getNrOfFolds() {
return nrOfFolds;
}
public void setNrOfFolds(int nrOfFolds) {
this.nrOfFolds = nrOfFolds;
}
// public LearningAlgorithm getLearningAlgorithm() {
// return algorithm;
// }
public KnowledgeSource getKnowledgeSource() {
return knowledgeSource;
}
}