/
RelaxCommand.java
134 lines (116 loc) · 3.55 KB
/
RelaxCommand.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
package org.obolibrary.robot;
import java.util.Map;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Options;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.reasoner.OWLReasonerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Handles inputs and outputs for the {@link relaxOperation}.
*
* @author <a href="mailto:cjmungall@lbl.gov">Chris Mungall</a>
*/
public class RelaxCommand implements Command {
/**
* Logger.
*/
private static final Logger logger =
LoggerFactory.getLogger(RelaxCommand.class);
/**
* Store the command-line options for the command.
*/
private Options options;
/**
* Initialize the command.
*/
public RelaxCommand() {
Options o = CommandLineHelper.getCommonOptions();
o.addOption("i", "input", true, "relax ontology from a file");
o.addOption("I", "input-iri", true, "relax ontology from an IRI");
o.addOption("o", "output", true, "save relaxed ontology to a file");
options = o;
}
/**
* Name of the command.
*
* @return name
*/
public String getName() {
return "relax";
}
/**
* Brief description of the command.
*
* @return description
*/
public String getDescription() {
return "relax ontology";
}
/**
* Command-line usage for the command.
*
* @return usage
*/
public String getUsage() {
return "robot relax --input <file> "
+ "--reasoner <name> "
+ "[options] "
+ "--output <file>";
}
/**
* Command-line options for the command.
*
* @return options
*/
public Options getOptions() {
return options;
}
/**
* Handle the command-line and file operations for the relaxOperation.
*
* @param args strings to use as arguments
*/
public void main(String[] args) {
try {
execute(null, args);
} catch (Exception e) {
CommandLineHelper.handleException(getUsage(), getOptions(), e);
}
}
/**
* Given an input state and command line arguments,
* run a reasoner, and add axioms to the input ontology,
* returning a state with the updated ontology.
*
* @param state the state from the previous command, or null
* @param args the command-line arguments
* @return the state with inferred axioms added to the ontology
* @throws Exception on any problem
*/
public CommandState execute(CommandState state, String[] args)
throws Exception {
CommandLine line = CommandLineHelper
.getCommandLine(getUsage(), getOptions(), args);
if (line == null) {
return null;
}
if (state == null) {
state = new CommandState();
}
IOHelper ioHelper = CommandLineHelper.getIOHelper(line);
state = CommandLineHelper.updateInputOntology(ioHelper, state, line);
OWLOntology ontology = state.getOntology();
// Override default reasoner options with command-line options
Map<String, String> relaxOptions =
RelaxOperation.getDefaultOptions();
for (String option: relaxOptions.keySet()) {
if (line.hasOption(option)) {
relaxOptions.put(option, line.getOptionValue(option));
}
}
RelaxOperation.relax(ontology, relaxOptions);
CommandLineHelper.maybeSaveOutput(line, ontology);
return state;
}
}