Skip to content

Package: OWL2Java$4

OWL2Java$4

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