Skip to content

Package: OWL2Java$Command

OWL2Java$Command

nameinstructionbranchcomplexitylinemethod
static {...}
M: 0 C: 54
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%

Coverage

1: /**
2: * Copyright (C) 2016 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.jopa.owl2java;
16:
17: import joptsimple.OptionParser;
18: import joptsimple.OptionSet;
19: import org.slf4j.Logger;
20: import org.slf4j.LoggerFactory;
21:
22: import java.util.Arrays;
23: import java.util.HashMap;
24: import java.util.Map;
25:
26: public class OWL2Java {
27:
28: private static final Logger LOG = LoggerFactory.getLogger(OWL2Java.class);
29:
30: // CLI map
31: private static final Map<Command, OptionParser> map = new HashMap<>();
32:
33: static {
34: map.put(Command.help, new OptionParser() {
35: {
36: // no options
37: }
38: });
39: map.put(Command.transform, new OptionParser() {
40: {
41: accepts(Param.MAPPING_FILE.arg, Param.MAPPING_FILE.description).withRequiredArg().ofType(String.class);
42: accepts(Param.PACKAGE.arg, Param.PACKAGE.description).withRequiredArg().ofType(String.class)
43: .defaultsTo("generated");
44: accepts(Param.CONTEXT.arg, Param.CONTEXT.description).withOptionalArg().ofType(String.class);
45: accepts(Param.WITH_IRIS.arg, Param.WITH_IRIS.description).withRequiredArg().ofType(Boolean.class)
46: .defaultsTo(false);
47: accepts(Param.TARGET_DIR.arg, Param.TARGET_DIR.description).withRequiredArg().ofType(String.class)
48: .defaultsTo("");
49: accepts(Param.WHOLE_ONTOLOGY_AS_IC.arg, Param.WHOLE_ONTOLOGY_AS_IC.description).withOptionalArg().ofType(Boolean.class)
50: .defaultsTo(false);
51: }
52: });
53: map.put(Command.vocabulary, new OptionParser() {
54: {
55: accepts(Param.MAPPING_FILE.arg, Param.MAPPING_FILE.description).withRequiredArg().ofType(String.class);
56: accepts(Param.PACKAGE.arg, Param.PACKAGE.description).withRequiredArg().ofType(String.class)
57: .defaultsTo("generated");
58: accepts(Param.CONTEXT.arg, Param.CONTEXT.description).withRequiredArg().ofType(String.class);
59: accepts(Param.WITH_IRIS.arg, Param.WITH_IRIS.description).withRequiredArg().ofType(Boolean.class)
60: .defaultsTo(false);
61: accepts(Param.TARGET_DIR.arg, Param.TARGET_DIR.description).withRequiredArg().ofType(String.class)
62: .defaultsTo("");
63: accepts(Param.WHOLE_ONTOLOGY_AS_IC.arg, Param.WHOLE_ONTOLOGY_AS_IC.description).withOptionalArg().ofType(Boolean.class)
64: .defaultsTo(false);
65: }
66: });
67: map.put(Command.list, new OptionParser() {
68: {
69: accepts(Param.MAPPING_FILE.arg, Param.MAPPING_FILE.description).withRequiredArg().ofType(String.class);
70: }
71: });
72: map.put(Command.version, new OptionParser() {
73: {
74: // no options
75: }
76: });
77: }
78:
79: private enum Command {
80: help, list, transform, vocabulary, version
81: }
82:
83: private enum Param {
84: MAPPING_FILE("m", "mapping file"), CONTEXT("c", "context name"), WITH_IRIS("w", "with OWLAPI IRIs"), TARGET_DIR(
85: "d", "output directory"), PACKAGE("p", "package"), WHOLE_ONTOLOGY_AS_IC("i","interpret whole ontology as integrity constraints; this option supersedes the '-c' option.");
86:
87: private final String arg;
88: private final String description;
89:
90: Param(String arg, String description) {
91: this.arg = arg;
92: this.description = description;
93: }
94: }
95:
96: private static void printHelp(Command cc) {
97: switch (cc) {
98: case help:
99: System.out
100: .println(
101: "Help command gives hints on how to use other commands. Try 'OWL2Java help <command>' for more specific info.");
102: System.out.println("");
103: System.out.println("Syntax: OWL2Java help <command>");
104: System.out.println("");
105: break;
106: case list:
107: System.out.println("Lists all available IC contexts.");
108: System.out.println("");
109: System.out
110: .println("Syntax: OWL2Java list <ontology_iri> [ <options> ].");
111: System.out.println("");
112: break;
113: case transform:
114: System.out
115: .println("Transforms all ICs into annotated Java classes.");
116: System.out.println("");
117: System.out
118: .println("Syntax: OWL2Java transform <ontology_iri> [ <options> ].");
119: System.out.println("");
120: break;
121: case vocabulary:
122: System.out
123: .println("Generates vocabulary based on the ICs.");
124: System.out.println("");
125: System.out
126: .println("Syntax: OWL2Java vocabulary <ontology_iri> [ <options> ].");
127: System.out.println("");
128: break;
129: case version:
130: System.out.println("Prints the version of the OWL2Java tool.");
131: break;
132: }
133:
134: try {
135: map.get(cc).printHelpOn(System.out);
136: } catch (Exception e) {
137: LOG.error(e.getMessage(), e);
138: }
139: }
140:
141: private static Command getCommandOrNull(String s) {
142: try {
143: return Command.valueOf(s);
144: } catch (IllegalArgumentException e) {
145: return null;
146: }
147: }
148:
149: public static void main(String[] args) {
150:
151: if (args.length == 0) {
152: System.out.println("Syntax: OWL2Java <command> <args>. Run 'OWL2Java help' for more details");
153: return;
154: }
155:
156: final Command c;
157:
158: if ((c = getCommandOrNull(args[0])) == null) {
159: System.err
160: .println("Invalid command " + args[0] + ", try 'OWL2Java help' for the list of available commands");
161: return;
162: }
163:
164: final OptionParser op = map.get(c);
165: final OptionSet os = op.parse(args);
166:
167: final OWL2JavaTransformer oj;
168:
169: switch (c) {
170: case help:
171: if (args.length != 1) {
172: final Command cc;
173: if ((cc = getCommandOrNull(args[1])) != null) {
174: printHelp(cc);
175: } else {
176: System.err.println("Invalid command " + args[0] + " " + args[1] +
177: ", try 'OWL2Java help' for the list of available commands.");
178: return;
179: }
180: } else {
181: System.out.println("Available commands : " + Arrays.asList(Command.values()));
182: }
183: break;
184: case list:
185: if (!verifyArgumentCount(os)) {
186: break;
187: }
188: oj = getTransformer(os);
189:
190: System.out.println("Available contexts: " + oj.listContexts());
191: break;
192: case transform:
193: transformOwlToJava(os);
194: break;
195: case vocabulary:
196: generateVocabulary(os);
197: break;
198: case version:
199: System.out.println("OWL2Java version " + Constants.VERSION);
200: break;
201: default:
202: System.err.println("Unknown command '" + args[0] + "', try 'OWL2Java help.'");
203: }
204: }
205:
206: private static OWL2JavaTransformer getTransformer(OptionSet os) {
207: OWL2JavaTransformer oj;
208: oj = new OWL2JavaTransformer();
209: if (os.has(Param.MAPPING_FILE.arg)) {
210: oj.setOntology(os.nonOptionArguments().get(1), os.valueOf(Param.MAPPING_FILE.arg).toString(), true);
211: } else {
212: oj.setOntology(os.nonOptionArguments().get(1), null, true);
213: }
214: return oj;
215: }
216:
217: private static boolean verifyArgumentCount(OptionSet os) {
218: if (os.nonOptionArguments().size() != 2) {
219: System.err
220: .println("Exactly one ontology IRI has to be specified, got "
221: + (os.nonOptionArguments().size() - 1)
222: + ", try 'OWL2Java help' for the list of available commands");
223: return false;
224: }
225: return true;
226: }
227:
228: private static void transformOwlToJava(OptionSet os) {
229: boolean whole = (Boolean) os.valueOf(Param.WHOLE_ONTOLOGY_AS_IC.arg);
230:
231: if (!whole && !verifyTransformOptions(os)) {
232: return;
233: }
234:
235: final OWL2JavaTransformer oj = getTransformer(os);
236:
237: oj.transform(whole ? null : os.valueOf(Param.CONTEXT.arg).toString(),
238: os.valueOf(Param.PACKAGE.arg).toString(), os.valueOf(Param.TARGET_DIR.arg).toString(),
239: (Boolean) os.valueOf(Param.WITH_IRIS.arg));
240: }
241:
242: private static boolean verifyTransformOptions(OptionSet os) {
243: if (!verifyArgumentCount(os)) {
244: return false;
245: }
246:
247: if (!os.has(Param.CONTEXT.arg)) {
248: System.err.println("The parameter '-" + Param.CONTEXT.arg +
249: "' is obligatory. Try the 'help' command for more details.");
250: return false;
251: }
252: return true;
253: }
254:
255: private static void generateVocabulary(OptionSet os) {
256: boolean whole = (Boolean) os.valueOf(Param.WHOLE_ONTOLOGY_AS_IC.arg);
257: if (!whole && !verifyTransformOptions(os)) {
258: return;
259: }
260: final OWL2JavaTransformer transformer = getTransformer(os);
261:
262:
263: transformer.generateVocabulary(whole ? null : os.valueOf(Param.CONTEXT.arg).toString(), os.valueOf(Param.PACKAGE.arg).toString(),
264: os.valueOf(Param.TARGET_DIR.arg).toString(), (Boolean) os.valueOf(Param.WITH_IRIS.arg));
265: }
266: }