Skip to content

Method: getTransformer(CliParams)

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