Skip to content

Package: OWL2Java$3

OWL2Java$3

nameinstructionbranchcomplexitylinemethod
{...}
M: 0 C: 96
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 16
100%
M: 0 C: 1
100%

Coverage

1: /**
2: * Copyright (C) 2016 Czech Technical University in Prague
3: * <p>
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: * <p>
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()
50: .ofType(Boolean.class)
51: .defaultsTo(false);
52: accepts(Param.IGNORE_FAILED_IMPORTS.arg, Param.IGNORE_FAILED_IMPORTS.description).withOptionalArg()
53: .ofType(Boolean.class)
54: .defaultsTo(false);
55: }
56: });
57: map.put(Command.vocabulary, new OptionParser() {
58: {
59: accepts(Param.MAPPING_FILE.arg, Param.MAPPING_FILE.description).withRequiredArg().ofType(String.class);
60: accepts(Param.PACKAGE.arg, Param.PACKAGE.description).withRequiredArg().ofType(String.class)
61: .defaultsTo("generated");
62: accepts(Param.CONTEXT.arg, Param.CONTEXT.description).withRequiredArg().ofType(String.class);
63: accepts(Param.WITH_IRIS.arg, Param.WITH_IRIS.description).withRequiredArg().ofType(Boolean.class)
64: .defaultsTo(false);
65: accepts(Param.TARGET_DIR.arg, Param.TARGET_DIR.description).withRequiredArg().ofType(String.class)
66: .defaultsTo("");
67: accepts(Param.WHOLE_ONTOLOGY_AS_IC.arg, Param.WHOLE_ONTOLOGY_AS_IC.description).withOptionalArg()
68: .ofType(Boolean.class)
69: .defaultsTo(false);
70: accepts(Param.IGNORE_FAILED_IMPORTS.arg, Param.IGNORE_FAILED_IMPORTS.description).withOptionalArg()
71: .ofType(Boolean.class)
72: .defaultsTo(false);
73: }
74: });
75: map.put(Command.list, new OptionParser() {
76: {
77: accepts(Param.MAPPING_FILE.arg, Param.MAPPING_FILE.description).withRequiredArg().ofType(String.class);
78: }
79: });
80: map.put(Command.version, new OptionParser() {
81: {
82: // no options
83: }
84: });
85: }
86:
87: private static void printHelp(Command cc) {
88: switch (cc) {
89: case help:
90: System.out
91: .println(
92: "Help command gives hints on how to use other commands. Try 'OWL2Java help <command>' for more specific info.");
93: System.out.println("");
94: System.out.println("Syntax: OWL2Java help <command>");
95: System.out.println("");
96: break;
97: case list:
98: System.out.println("Lists all available IC contexts.");
99: System.out.println("");
100: System.out
101: .println("Syntax: OWL2Java list <ontology_iri> [ <options> ].");
102: System.out.println("");
103: break;
104: case transform:
105: System.out
106: .println("Transforms all ICs into annotated Java classes.");
107: System.out.println("");
108: System.out
109: .println("Syntax: OWL2Java transform <ontology_iri> [ <options> ].");
110: System.out.println("");
111: break;
112: case vocabulary:
113: System.out
114: .println("Generates vocabulary based on the ICs.");
115: System.out.println("");
116: System.out
117: .println("Syntax: OWL2Java vocabulary <ontology_iri> [ <options> ].");
118: System.out.println("");
119: break;
120: case version:
121: System.out.println("Prints the version of the OWL2Java tool.");
122: break;
123: }
124:
125: try {
126: map.get(cc).printHelpOn(System.out);
127: } catch (Exception e) {
128: LOG.error(e.getMessage(), e);
129: }
130: }
131:
132: private static Command getCommandOrNull(String s) {
133: try {
134: return Command.valueOf(s);
135: } catch (IllegalArgumentException e) {
136: return null;
137: }
138: }
139:
140: public static void main(String[] args) {
141:
142: if (args.length == 0) {
143: System.out.println("Syntax: OWL2Java <command> <args>. Run 'OWL2Java help' for more details");
144: return;
145: }
146:
147: final Command c;
148:
149: if ((c = getCommandOrNull(args[0])) == null) {
150: System.err
151: .println("Invalid command " + args[0] + ", try 'OWL2Java help' for the list of available commands");
152: return;
153: }
154:
155: final OptionParser op = map.get(c);
156: final OptionSet os = op.parse(args);
157:
158: final OWL2JavaTransformer oj;
159:
160: switch (c) {
161: case help:
162: if (args.length != 1) {
163: final Command cc;
164: if ((cc = getCommandOrNull(args[1])) != null) {
165: printHelp(cc);
166: } else {
167: System.err.println("Invalid command " + args[0] + " " + args[1] +
168: ", try 'OWL2Java help' for the list of available commands.");
169: return;
170: }
171: } else {
172: System.out.println("Available commands : " + Arrays.asList(Command.values()));
173: }
174: break;
175: case list:
176: if (!verifyArgumentCount(os)) {
177: break;
178: }
179: oj = getTransformer(os);
180:
181: System.out.println("Available contexts: " + oj.listContexts());
182: break;
183: case transform:
184: transformOwlToJava(os);
185: break;
186: case vocabulary:
187: generateVocabulary(os);
188: break;
189: case version:
190: System.out.println("OWL2Java version " + Constants.VERSION);
191: break;
192: default:
193: System.err.println("Unknown command '" + args[0] + "', try 'OWL2Java help.'");
194: }
195: }
196:
197: private static OWL2JavaTransformer getTransformer(OptionSet os) {
198: OWL2JavaTransformer oj;
199: oj = new OWL2JavaTransformer();
200: if (os.has(Param.MAPPING_FILE.arg)) {
201: oj.setOntology(os.nonOptionArguments().get(1), os.valueOf(Param.MAPPING_FILE.arg).toString(), true);
202: } else {
203: oj.setOntology(os.nonOptionArguments().get(1), null, true);
204: }
205: oj.ignoreMissingImports((Boolean) os.valueOf(Param.IGNORE_FAILED_IMPORTS.arg));
206: return oj;
207: }
208:
209: private static boolean verifyArgumentCount(OptionSet os) {
210: if (os.nonOptionArguments().size() != 2) {
211: System.err
212: .println("Exactly one ontology IRI has to be specified, got "
213: + (os.nonOptionArguments().size() - 1)
214: + ", try 'OWL2Java help' for the list of available commands");
215: return false;
216: }
217: return true;
218: }
219:
220: private static void transformOwlToJava(OptionSet os) {
221: boolean whole = (Boolean) os.valueOf(Param.WHOLE_ONTOLOGY_AS_IC.arg);
222:
223: if (!whole && !verifyTransformOptions(os)) {
224: return;
225: }
226:
227: final OWL2JavaTransformer oj = getTransformer(os);
228:
229: oj.transform(whole ? null : os.valueOf(Param.CONTEXT.arg).toString(),
230: os.valueOf(Param.PACKAGE.arg).toString(), os.valueOf(Param.TARGET_DIR.arg).toString(),
231: (Boolean) os.valueOf(Param.WITH_IRIS.arg));
232: }
233:
234: private static boolean verifyTransformOptions(OptionSet os) {
235: if (!verifyArgumentCount(os)) {
236: return false;
237: }
238:
239: if (!os.has(Param.CONTEXT.arg)) {
240: System.err.println("The parameter '-" + Param.CONTEXT.arg +
241: "' is obligatory. Try the 'help' command for more details.");
242: return false;
243: }
244: return true;
245: }
246:
247: private static void generateVocabulary(OptionSet os) {
248: boolean whole = (Boolean) os.valueOf(Param.WHOLE_ONTOLOGY_AS_IC.arg);
249: if (!whole && !verifyTransformOptions(os)) {
250: return;
251: }
252: final OWL2JavaTransformer transformer = getTransformer(os);
253:
254:
255: transformer.generateVocabulary(whole ? null : os.valueOf(Param.CONTEXT.arg).toString(),
256: os.valueOf(Param.PACKAGE.arg).toString(),
257: os.valueOf(Param.TARGET_DIR.arg).toString(), (Boolean) os.valueOf(Param.WITH_IRIS.arg));
258: }
259:
260: private enum Command {
261: help, list, transform, vocabulary, version
262: }
263:
264: private enum Param {
265: MAPPING_FILE("m", "mapping file"), CONTEXT("c", "context name"), WITH_IRIS("w", "with OWLAPI IRIs"), TARGET_DIR(
266: "d", "output directory"), PACKAGE("p", "package"), WHOLE_ONTOLOGY_AS_IC("i",
267: "interpret whole ontology as integrity constraints; this option supersedes the '-c' option."),
268: IGNORE_FAILED_IMPORTS("f", "ignore failed ontology imports");
269:
270: private final String arg;
271: private final String description;
272:
273: Param(String arg, String description) {
274: this.arg = arg;
275: this.description = description;
276: }
277: }
278: }