|
| 1 | +package com.github.sszuev.ontconverter |
| 2 | + |
| 3 | +import com.github.owlcs.ontapi.OntFormat |
| 4 | +import com.github.owlcs.ontapi.jena.impl.conf.OntModelConfig |
| 5 | +import com.github.sszuev.ontconverter.utils.supportedReadFormats |
| 6 | +import com.github.sszuev.ontconverter.utils.supportedWriteFormats |
| 7 | +import kotlinx.cli.ArgParser |
| 8 | +import kotlinx.cli.ArgType |
| 9 | +import kotlinx.cli.default |
| 10 | +import kotlinx.cli.required |
| 11 | +import java.nio.file.Files |
| 12 | +import java.nio.file.Path |
| 13 | +import java.nio.file.Paths |
| 14 | +import java.util.* |
| 15 | + |
| 16 | +fun main(argsArray: Array<String>) { |
| 17 | + val args = parse(argsArray) |
| 18 | + println(args.printString()) |
| 19 | +} |
| 20 | + |
| 21 | +private fun parse(args: Array<String>): Args { |
| 22 | + val locale = Locale.ENGLISH |
| 23 | + val parser = ArgParser("java -jar ont-converter.jar") |
| 24 | + val inputStringPath by parser.option( |
| 25 | + ArgType.String, shortName = "i", fullName = "input", |
| 26 | + description = """ |
| 27 | + The file path or not-empty directory to load ontology/ontologies. |
| 28 | + """.trimIndent() |
| 29 | + ).required() |
| 30 | + val inputFormat by parser.option( |
| 31 | + ArgType.Choice(supportedReadFormats(), { e -> OntFormat.valueOf(e) }, { it.name }), |
| 32 | + shortName = "if", fullName = "input-format", |
| 33 | + description = """ |
| 34 | + The input format. If not specified the program will choose |
| 35 | + the most suitable one to load ontology from a file. |
| 36 | + """.trimIndent() |
| 37 | + ) |
| 38 | + val outputStringPath by parser.option( |
| 39 | + ArgType.String, shortName = "o", fullName = "output", |
| 40 | + description = """ |
| 41 | + The file or directory path to store result ontology/ontologies. |
| 42 | + If the --input is a file then this parameter must also be a file. |
| 43 | + """.trimIndent() |
| 44 | + ).required() |
| 45 | + val outputFormat by parser.option( |
| 46 | + ArgType.Choice(supportedWriteFormats(), { OntFormat.valueOf(it.uppercase(locale)) }, { it.name }), |
| 47 | + shortName = "of", fullName = "output-format", |
| 48 | + description = """ |
| 49 | + The format of output ontology/ontologies. |
| 50 | + """.trimIndent() |
| 51 | + ).required() |
| 52 | + |
| 53 | + val punnings by parser.option( |
| 54 | + ArgType.Choice<OntModelConfig.StdMode>({ OntModelConfig.StdMode.valueOf(it.uppercase(locale)) }, { it.name }), |
| 55 | + shortName = "p", fullName = "punnings", |
| 56 | + description = """ |
| 57 | + The punning mode. Could be used in conjunction with --refine option. Must be one of the following: |
| 58 | + Lax mode: allows any punnings, i.e. ontology is allowed to contain multiple entity declarations |
| 59 | + Middle mode: two forbidden intersections: Datatype <-> Class & NamedObjectProperty <-> DatatypeProperty |
| 60 | + Strict mode: all punnings are forbidden, i.e. Datatype <-> Class and rdf:Property intersections |
| 61 | + (any pairs of NamedObjectProperty, DatatypeProperty, AnnotationProperty). |
| 62 | + """.trimIndent() |
| 63 | + ).default(OntModelConfig.StdMode.LAX) |
| 64 | + |
| 65 | + val spin by parser.option( |
| 66 | + ArgType.Boolean, shortName = "s", fullName = "spin", |
| 67 | + description = """ |
| 68 | + Use spin transformation to replace rdf:List based spin-constructs (e.g sp:Select) |
| 69 | + with their text-literal representation to produce compact axioms list. |
| 70 | + """.trimIndent() |
| 71 | + ).default(false) |
| 72 | + |
| 73 | + val refine by parser.option( |
| 74 | + ArgType.Boolean, shortName = "r", fullName = "refine", |
| 75 | + description = """ |
| 76 | + Refine output: |
| 77 | + if specified the resulting ontologies will consist only of the OWL2-DL components (annotations and axioms), |
| 78 | + otherwise there could be some rdf-stuff (in case the output format is provided by jena) |
| 79 | + """.trimIndent() |
| 80 | + ).default(false) |
| 81 | + |
| 82 | + val web by parser.option( |
| 83 | + ArgType.Boolean, shortName = "w", fullName = "web", |
| 84 | + description = """ |
| 85 | + Allow web/ftp diving to retrieve dependent ontologies from imports (owl:imports), |
| 86 | + otherwise the specified directory (see --input) will be used as the only source. |
| 87 | + """.trimIndent() |
| 88 | + ).default(false) |
| 89 | + |
| 90 | + val force by parser.option( |
| 91 | + ArgType.Boolean, shortName = "f", fullName = "force", |
| 92 | + description = "Ignore any exceptions while loading/saving and processing imports." |
| 93 | + ).default(false) |
| 94 | + |
| 95 | + val verbose by parser.option( |
| 96 | + ArgType.Boolean, shortName = "v", fullName = "verbose", |
| 97 | + description = "To print progress messages to console." |
| 98 | + ).default(false) |
| 99 | + |
| 100 | + parser.parse(args) |
| 101 | + |
| 102 | + val input = Paths.get(inputStringPath).toRealPath() |
| 103 | + val output = parseOutput(outputStringPath, input) |
| 104 | + |
| 105 | + return Args(input, inputFormat, output, outputFormat, punnings, spin, refine, web, force, verbose) |
| 106 | +} |
| 107 | + |
| 108 | +private fun parseOutput(outputStringPath: String, input: Path): Path { |
| 109 | + var out: Path = Paths.get(outputStringPath) |
| 110 | + if (out.parent != null) { |
| 111 | + require(Files.exists(out.parent)) { "Directory ${out.parent} does not exist." } |
| 112 | + out = out.parent.toRealPath().resolve(out.fileName) |
| 113 | + } |
| 114 | + if (Files.isDirectory(input) && |
| 115 | + Files.walk(input).filter { Files.isRegularFile(it) }.skip(1).findFirst().isPresent |
| 116 | + ) { |
| 117 | + // if input is a directory with more than one file, then out should be a directory |
| 118 | + if (Files.exists(out)) { |
| 119 | + out = if (!Files.isDirectory(out)) { |
| 120 | + throw IllegalArgumentException("Output parameter is not a directory path: $out") |
| 121 | + } else { |
| 122 | + out.toRealPath() |
| 123 | + } |
| 124 | + } else { |
| 125 | + Files.createDirectory(out) |
| 126 | + } |
| 127 | + } |
| 128 | + return out |
| 129 | +} |
| 130 | + |
0 commit comments