diff options
| author | Federico Igne <git@federicoigne.com> | 2021-10-03 10:22:34 +0100 |
|---|---|---|
| committer | Federico Igne <git@federicoigne.com> | 2021-10-03 10:22:34 +0100 |
| commit | 2aa8094df2eb9fde48c8073fbdbb2ebcc42fdbf0 (patch) | |
| tree | 769291c614c2113fb4e8fd10a70cbbd32631602f /src/main/scala | |
| parent | 44b8e1c8c724bf7f62f2b567548b941f88a31dc6 (diff) | |
| download | RSAComb-2aa8094df2eb9fde48c8073fbdbb2ebcc42fdbf0.tar.gz RSAComb-2aa8094df2eb9fde48c8073fbdbb2ebcc42fdbf0.zip | |
Move to os-lib for filesystem operations
Diffstat (limited to 'src/main/scala')
4 files changed, 51 insertions, 38 deletions
diff --git a/src/main/scala/uk/ac/ox/cs/rsacomb/Main.scala b/src/main/scala/uk/ac/ox/cs/rsacomb/Main.scala index 22b1f76..24bda1f 100644 --- a/src/main/scala/uk/ac/ox/cs/rsacomb/Main.scala +++ b/src/main/scala/uk/ac/ox/cs/rsacomb/Main.scala | |||
| @@ -88,6 +88,14 @@ object RSAConfig { | |||
| 88 | sys.exit(1) | 88 | sys.exit(1) |
| 89 | } | 89 | } |
| 90 | 90 | ||
| 91 | private def getPath(str: String): os.Path = | ||
| 92 | try { | ||
| 93 | os.Path(str, base = os.pwd) | ||
| 94 | } catch { | ||
| 95 | case e: IllegalArgumentException => | ||
| 96 | exit(s"'$str' is not a well formed path.") | ||
| 97 | } | ||
| 98 | |||
| 91 | /** Parse arguments with default options | 99 | /** Parse arguments with default options |
| 92 | * | 100 | * |
| 93 | * @param args arguments list | 101 | * @param args arguments list |
| @@ -116,26 +124,20 @@ object RSAConfig { | |||
| 116 | } | 124 | } |
| 117 | parse(tail, config += ('logger -> level)) | 125 | parse(tail, config += ('logger -> level)) |
| 118 | } | 126 | } |
| 119 | case flag @ ("-o" | "--output") :: _output :: tail => | 127 | case flag @ ("-o" | "--output") :: output :: tail => |
| 120 | try { | 128 | parse(tail, config += ('output -> getPath(output))) |
| 121 | val output = Paths.get(_output) | ||
| 122 | parse(tail, config += ('output -> output)) | ||
| 123 | } catch { | ||
| 124 | case e: InvalidPathException => | ||
| 125 | exit(s"'${_output}' is not a valid filename.") | ||
| 126 | } | ||
| 127 | case flag @ ("-q" | "--queries") :: _query :: tail => { | 129 | case flag @ ("-q" | "--queries") :: _query :: tail => { |
| 128 | val query = new File(_query) | 130 | val query = getPath(_query) |
| 129 | if (!query.isFile) | 131 | if (!os.isFile(query)) |
| 130 | exit(s"'$query' is not a valid filename.") | 132 | exit(s"'${_query}' is not a valid filename.") |
| 131 | parse(tail, config += ('queries -> query)) | 133 | parse(tail, config += ('queries -> query)) |
| 132 | } | 134 | } |
| 133 | case _ontology :: _data => { | 135 | case _ontology :: _data => { |
| 134 | val ontology = new File(_ontology) | 136 | val ontology = getPath(_ontology) |
| 135 | val data = _data.map(new File(_)) | 137 | val data = _data map getPath |
| 136 | (ontology :: data) foreach { (file) => | 138 | (ontology :: data) foreach { (path) => |
| 137 | if (!file.isFile) | 139 | if (!os.isFile(path)) |
| 138 | exit(s"'$file' is not a valid filename.") | 140 | exit(s"'$path' is not a valid filename.") |
| 139 | } | 141 | } |
| 140 | finalise(config += ('ontology -> ontology) += ('data -> data)) | 142 | finalise(config += ('ontology -> ontology) += ('data -> data)) |
| 141 | } | 143 | } |
| @@ -156,8 +158,8 @@ object RSAComb extends App { | |||
| 156 | 158 | ||
| 157 | /* Load original ontology and normalize it */ | 159 | /* Load original ontology and normalize it */ |
| 158 | val ontology = Ontology( | 160 | val ontology = Ontology( |
| 159 | config('ontology).get[File], | 161 | config('ontology).get[os.Path], |
| 160 | config('data).get[List[File]] | 162 | config('data).get[List[os.Path]] |
| 161 | ).normalize(new Normalizer) | 163 | ).normalize(new Normalizer) |
| 162 | 164 | ||
| 163 | //ontology.axioms foreach println | 165 | //ontology.axioms foreach println |
| @@ -168,14 +170,18 @@ object RSAComb extends App { | |||
| 168 | 170 | ||
| 169 | if (config contains 'queries) { | 171 | if (config contains 'queries) { |
| 170 | val queries = | 172 | val queries = |
| 171 | RDFoxUtil.loadQueriesFromFile(config('queries).get[File].getAbsoluteFile) | 173 | RDFoxUtil.loadQueriesFromFile( |
| 174 | config('queries).get[os.Path] | ||
| 175 | ) | ||
| 172 | 176 | ||
| 173 | val answers = rsa ask queries | 177 | val answers = rsa ask queries |
| 174 | 178 | ||
| 175 | /* Write answers to output file */ | 179 | /* Write answers to output file */ |
| 176 | val output = new PrintWriter(config('output).get[Path].toFile) | 180 | os.write( |
| 177 | output.write(ujson.write(ujson.Arr(answers.map(_.toJSON)), indent = 4)) | 181 | config('output).get[os.Path], |
| 178 | output.close() | 182 | ujson.write(ujson.Arr(answers.map(_.toJSON)), indent = 4), |
| 183 | createFolders = true | ||
| 184 | ) | ||
| 179 | 185 | ||
| 180 | // Logger.print(s"$answers", Logger.VERBOSE) | 186 | // Logger.print(s"$answers", Logger.VERBOSE) |
| 181 | // Logger print s"Number of answers: ${answers.length} (${answers.lengthWithMultiplicity})" | 187 | // Logger print s"Number of answers: ${answers.length} (${answers.lengthWithMultiplicity})" |
diff --git a/src/main/scala/uk/ac/ox/cs/rsacomb/RSAOntology.scala b/src/main/scala/uk/ac/ox/cs/rsacomb/RSAOntology.scala index 275f523..e2b0e2f 100644 --- a/src/main/scala/uk/ac/ox/cs/rsacomb/RSAOntology.scala +++ b/src/main/scala/uk/ac/ox/cs/rsacomb/RSAOntology.scala | |||
| @@ -123,7 +123,7 @@ object RSAOntology { | |||
| 123 | 123 | ||
| 124 | def apply( | 124 | def apply( |
| 125 | axioms: List[OWLLogicalAxiom], | 125 | axioms: List[OWLLogicalAxiom], |
| 126 | datafiles: List[File] | 126 | datafiles: List[os.Path] |
| 127 | ): RSAOntology = new RSAOntology(axioms, datafiles) | 127 | ): RSAOntology = new RSAOntology(axioms, datafiles) |
| 128 | 128 | ||
| 129 | // def apply( | 129 | // def apply( |
| @@ -191,7 +191,7 @@ object RSAOntology { | |||
| 191 | * @param ontology the input OWL2 ontology. | 191 | * @param ontology the input OWL2 ontology. |
| 192 | * @param datafiles additinal data (treated as part of the ABox) | 192 | * @param datafiles additinal data (treated as part of the ABox) |
| 193 | */ | 193 | */ |
| 194 | class RSAOntology(axioms: List[OWLLogicalAxiom], datafiles: List[File]) | 194 | class RSAOntology(axioms: List[OWLLogicalAxiom], datafiles: List[os.Path]) |
| 195 | extends Ontology(axioms, datafiles) { | 195 | extends Ontology(axioms, datafiles) { |
| 196 | 196 | ||
| 197 | /** Simplify conversion between OWLAPI and RDFox concepts */ | 197 | /** Simplify conversion between OWLAPI and RDFox concepts */ |
diff --git a/src/main/scala/uk/ac/ox/cs/rsacomb/ontology/Ontology.scala b/src/main/scala/uk/ac/ox/cs/rsacomb/ontology/Ontology.scala index 8d46646..9d80dd5 100644 --- a/src/main/scala/uk/ac/ox/cs/rsacomb/ontology/Ontology.scala +++ b/src/main/scala/uk/ac/ox/cs/rsacomb/ontology/Ontology.scala | |||
| @@ -77,7 +77,7 @@ object Ontology { | |||
| 77 | */ | 77 | */ |
| 78 | def dependencyGraph( | 78 | def dependencyGraph( |
| 79 | axioms: List[OWLLogicalAxiom], | 79 | axioms: List[OWLLogicalAxiom], |
| 80 | datafiles: List[File], | 80 | datafiles: List[os.Path], |
| 81 | unsafe: List[OWLObjectPropertyExpression] | 81 | unsafe: List[OWLObjectPropertyExpression] |
| 82 | ): DependencyGraph = { | 82 | ): DependencyGraph = { |
| 83 | 83 | ||
| @@ -167,10 +167,10 @@ object Ontology { | |||
| 167 | (graph, nodemap) | 167 | (graph, nodemap) |
| 168 | } | 168 | } |
| 169 | 169 | ||
| 170 | def apply(axioms: List[OWLLogicalAxiom], datafiles: List[File]): Ontology = | 170 | def apply(axioms: List[OWLLogicalAxiom], datafiles: List[os.Path]): Ontology = |
| 171 | new Ontology(axioms, datafiles) | 171 | new Ontology(axioms, datafiles) |
| 172 | 172 | ||
| 173 | def apply(ontology: OWLOntology, datafiles: List[File]): Ontology = { | 173 | def apply(ontology: OWLOntology, datafiles: List[os.Path]): Ontology = { |
| 174 | 174 | ||
| 175 | /** TBox axioms */ | 175 | /** TBox axioms */ |
| 176 | var tbox: List[OWLLogicalAxiom] = | 176 | var tbox: List[OWLLogicalAxiom] = |
| @@ -202,8 +202,8 @@ object Ontology { | |||
| 202 | Ontology(abox ::: tbox ::: rbox, datafiles) | 202 | Ontology(abox ::: tbox ::: rbox, datafiles) |
| 203 | } | 203 | } |
| 204 | 204 | ||
| 205 | def apply(ontofile: File, datafiles: List[File]): Ontology = { | 205 | def apply(ontofile: os.Path, datafiles: List[os.Path]): Ontology = { |
| 206 | val ontology = manager.loadOntologyFromOntologyDocument(ontofile) | 206 | val ontology = manager.loadOntologyFromOntologyDocument(ontofile.toIO) |
| 207 | Ontology(ontology, datafiles) | 207 | Ontology(ontology, datafiles) |
| 208 | } | 208 | } |
| 209 | 209 | ||
| @@ -214,7 +214,10 @@ object Ontology { | |||
| 214 | * @param axioms list of axioms (roughly) corresponding to the TBox. | 214 | * @param axioms list of axioms (roughly) corresponding to the TBox. |
| 215 | * @param datafiles files containing ABox data. | 215 | * @param datafiles files containing ABox data. |
| 216 | */ | 216 | */ |
| 217 | class Ontology(val axioms: List[OWLLogicalAxiom], val datafiles: List[File]) { | 217 | class Ontology( |
| 218 | val axioms: List[OWLLogicalAxiom], | ||
| 219 | val datafiles: List[os.Path] | ||
| 220 | ) { | ||
| 218 | 221 | ||
| 219 | /** Extend OWLAxiom functionalities */ | 222 | /** Extend OWLAxiom functionalities */ |
| 220 | import uk.ac.ox.cs.rsacomb.implicits.RSAAxiom._ | 223 | import uk.ac.ox.cs.rsacomb.implicits.RSAAxiom._ |
diff --git a/src/main/scala/uk/ac/ox/cs/rsacomb/util/RDFoxUtil.scala b/src/main/scala/uk/ac/ox/cs/rsacomb/util/RDFoxUtil.scala index 217fa7f..aa501cd 100644 --- a/src/main/scala/uk/ac/ox/cs/rsacomb/util/RDFoxUtil.scala +++ b/src/main/scala/uk/ac/ox/cs/rsacomb/util/RDFoxUtil.scala | |||
| @@ -186,10 +186,15 @@ object RDFoxUtil { | |||
| 186 | * @param graph named graph where the data should be uploaded | 186 | * @param graph named graph where the data should be uploaded |
| 187 | * @param files sequence of files to upload. | 187 | * @param files sequence of files to upload. |
| 188 | */ | 188 | */ |
| 189 | def addData(data: DataStoreConnection, graph: IRI, files: File*): Unit = | 189 | def addData(data: DataStoreConnection, graph: IRI, files: os.Path*): Unit = |
| 190 | Logger.timed( | 190 | Logger.timed( |
| 191 | files.foreach { | 191 | files.foreach { path => |
| 192 | data.importData(graph.getIRI, UpdateType.ADDITION, RSA.Prefixes, _) | 192 | data.importData( |
| 193 | graph.getIRI, | ||
| 194 | UpdateType.ADDITION, | ||
| 195 | RSA.Prefixes, | ||
| 196 | path.toIO | ||
| 197 | ) | ||
| 193 | }, | 198 | }, |
| 194 | "Loading data files", | 199 | "Loading data files", |
| 195 | Logger.DEBUG | 200 | Logger.DEBUG |
| @@ -235,11 +240,11 @@ object RDFoxUtil { | |||
| 235 | * @return a list of [[tech.oxfordsemantic.jrdfox.logic.sparql.statement.SelectQuery SelectQuery]] queries. | 240 | * @return a list of [[tech.oxfordsemantic.jrdfox.logic.sparql.statement.SelectQuery SelectQuery]] queries. |
| 236 | */ | 241 | */ |
| 237 | def loadQueriesFromFile( | 242 | def loadQueriesFromFile( |
| 238 | file: File, | 243 | path: os.Path, |
| 239 | prefixes: Prefixes = new Prefixes() | 244 | prefixes: Prefixes = new Prefixes() |
| 240 | ): List[ConjunctiveQuery] = { | 245 | ): List[ConjunctiveQuery] = { |
| 241 | val source = io.Source.fromFile(file) | 246 | val queries = os.read |
| 242 | val queries = source.getLines | 247 | .lines(path) |
| 243 | .map(_.trim.filter(_ >= ' ')) | 248 | .map(_.trim.filter(_ >= ' ')) |
| 244 | .filterNot(_ == "") | 249 | .filterNot(_ == "") |
| 245 | .foldRight((List.empty[List[String]], List.empty[String])) { | 250 | .foldRight((List.empty[List[String]], List.empty[String])) { |
| @@ -254,8 +259,7 @@ object RDFoxUtil { | |||
| 254 | .map(_.mkString(" ")) | 259 | .map(_.mkString(" ")) |
| 255 | .map(ConjunctiveQuery.parse(_, prefixes)) | 260 | .map(ConjunctiveQuery.parse(_, prefixes)) |
| 256 | .collect { case Some(q) => q } | 261 | .collect { case Some(q) => q } |
| 257 | Logger print s"Loaded ${queries.length} queries from ${file.getAbsolutePath}" | 262 | Logger print s"Loaded ${queries.length} queries from $path" |
| 258 | source.close() | ||
| 259 | queries | 263 | queries |
| 260 | } | 264 | } |
| 261 | 265 | ||
