Skip to content

Package: OWL2Java

OWL2Java

nameinstructionbranchcomplexitylinemethod
OWL2Java()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
generateVocabulary(CliParams)
M: 21 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
getCommand(String)
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
getTransformer(CliParams)
M: 39 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
invalidArgumentCount(CliParams)
M: 25 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
invalidTransformationOptions(CliParams)
M: 27 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
main(String[])
M: 154 C: 0
0%
M: 20 C: 0
0%
M: 13 C: 0
0%
M: 38 C: 0
0%
M: 1 C: 0
0%
printHelp(Command)
M: 18 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
transformOwlToJava(CliParams)
M: 21 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /**
2: * Copyright (C) 2022 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 cz.cvut.kbss.jopa.owl2java.cli.CliParams;
18: import cz.cvut.kbss.jopa.owl2java.cli.Command;
19: import cz.cvut.kbss.jopa.owl2java.cli.Option;
20: import cz.cvut.kbss.jopa.owl2java.config.Defaults;
21: import cz.cvut.kbss.jopa.owl2java.config.TransformationConfiguration;
22: import joptsimple.OptionParser;
23: import joptsimple.OptionSet;
24: import org.slf4j.Logger;
25: import org.slf4j.LoggerFactory;
26:
27: import java.io.PrintStream;
28: import java.util.Arrays;
29: import java.util.Optional;
30:
31: public class OWL2Java {
32:
33: private static final Logger LOG = LoggerFactory.getLogger(OWL2Java.class);
34:
35: private static void printHelp(Command cc) {
36: final PrintStream os = System.out;
37:
38: os.print(cc.helpText);
39: try {
40: cc.parser.printHelpOn(os);
41: } catch (Exception e) {
42: LOG.error(e.getMessage(), e);
43: }
44: }
45:
46: private static Optional<Command> getCommand(String s) {
47: try {
48: return Optional.of(Command.valueOf(s));
49: } catch (IllegalArgumentException e) {
50: return Optional.empty();
51: }
52: }
53:
54: public static void main(String[] args) {
55:
56:• if (args.length == 0) {
57: System.out.println("Syntax: OWL2Java <command> <args>. Run 'OWL2Java help' for more details");
58: return;
59: }
60:
61: final Optional<Command> c = getCommand(args[0]);
62:
63:• if (!c.isPresent()) {
64: System.err
65: .println("Invalid command " + args[0] + ", try 'OWL2Java help' for the list of available commands");
66: return;
67: }
68:
69: final OptionParser op = c.get().parser;
70: final OptionSet os = op.parse(args);
71: final CliParams input = new CliParams(os);
72:
73: final OWL2JavaTransformer oj;
74:
75:• switch (c.get()) {
76: case help:
77:• if (args.length != 1) {
78: final Optional<Command> cc = getCommand(args[1]);
79:• if (cc.isPresent()) {
80: printHelp(cc.get());
81: } else {
82: System.err.println("Invalid command " + args[0] + " " + args[1] +
83: ", try 'OWL2Java help' for the list of available commands.");
84: return;
85: }
86: } else {
87: System.out.println("Available commands : " + Arrays.asList(Command.values()));
88: }
89: break;
90: case list:
91:• if (invalidArgumentCount(input)) {
92: break;
93: }
94: oj = getTransformer(input);
95:
96: System.out.println("Available contexts: " + oj.listContexts());
97: break;
98: case transform:
99:• if (invalidArgumentCount(input)) {
100: break;
101: }
102: transformOwlToJava(input);
103: break;
104: case vocabulary:
105:• if (invalidArgumentCount(input)) {
106: break;
107: }
108: generateVocabulary(input);
109: break;
110: case version:
111: System.out.println("OWL2Java version " + Constants.VERSION);
112: break;
113: default:
114: System.err.println("Unknown command '" + args[0] + "', try 'OWL2Java help'.");
115: }
116: }
117:
118: private static OWL2JavaTransformer getTransformer(CliParams input) {
119: OWL2JavaTransformer oj;
120: oj = new OWL2JavaTransformer();
121:• if (input.has(Option.MAPPING_FILE.arg)) {
122: oj.setOntology(input.nonOptionArguments().get(1), input.valueOf(Option.MAPPING_FILE.arg).toString());
123: } else {
124: oj.setOntology(input.nonOptionArguments().get(1), null);
125: }
126: oj.ignoreMissingImports(input.is(Option.IGNORE_FAILED_IMPORTS.arg, Defaults.IGNORE_FAILED_IMPORTS));
127: return oj;
128: }
129:
130: private static boolean invalidArgumentCount(CliParams input) {
131:• if (input.nonOptionArguments().size() != 2) {
132: System.err
133: .println("Exactly one ontology IRI has to be specified, got "
134: + (input.nonOptionArguments().size() - 1)
135: + ", try 'OWL2Java help' for the list of available commands");
136: return true;
137: }
138: return false;
139: }
140:
141: private static void transformOwlToJava(CliParams input) {
142: boolean whole = input.is(Option.WHOLE_ONTOLOGY_AS_IC.arg);
143:
144:• if (!whole && invalidTransformationOptions(input)) {
145: return;
146: }
147:
148: final TransformationConfiguration config = TransformationConfiguration.config(input);
149:
150: final OWL2JavaTransformer transformer = getTransformer(input);
151:
152: transformer.transform(config);
153: }
154:
155: private static boolean invalidTransformationOptions(CliParams input) {
156:• if (invalidArgumentCount(input)) {
157: return true;
158: }
159:
160:• if (!input.has(Option.CONTEXT.arg)) {
161: System.err.println("The parameter '-" + Option.CONTEXT.arg +
162: "' is obligatory. Try the 'help' command for more details.");
163: return true;
164: }
165: return false;
166: }
167:
168: private static void generateVocabulary(CliParams input) {
169: final boolean whole = input.is(Option.WHOLE_ONTOLOGY_AS_IC.arg);
170:• if (!whole && invalidTransformationOptions(input)) {
171: return;
172: }
173: final OWL2JavaTransformer transformer = getTransformer(input);
174:
175: final TransformationConfiguration config = TransformationConfiguration.config(input);
176:
177: transformer.generateVocabulary(config);
178: }
179: }