From 2aa8094df2eb9fde48c8073fbdbb2ebcc42fdbf0 Mon Sep 17 00:00:00 2001 From: Federico Igne Date: Sun, 3 Oct 2021 10:22:34 +0100 Subject: Move to os-lib for filesystem operations --- src/main/scala/uk/ac/ox/cs/rsacomb/Main.scala | 50 ++++++++++++---------- .../scala/uk/ac/ox/cs/rsacomb/RSAOntology.scala | 4 +- .../uk/ac/ox/cs/rsacomb/ontology/Ontology.scala | 15 ++++--- .../scala/uk/ac/ox/cs/rsacomb/util/RDFoxUtil.scala | 20 +++++---- 4 files changed, 51 insertions(+), 38 deletions(-) (limited to 'src') 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 { sys.exit(1) } + private def getPath(str: String): os.Path = + try { + os.Path(str, base = os.pwd) + } catch { + case e: IllegalArgumentException => + exit(s"'$str' is not a well formed path.") + } + /** Parse arguments with default options * * @param args arguments list @@ -116,26 +124,20 @@ object RSAConfig { } parse(tail, config += ('logger -> level)) } - case flag @ ("-o" | "--output") :: _output :: tail => - try { - val output = Paths.get(_output) - parse(tail, config += ('output -> output)) - } catch { - case e: InvalidPathException => - exit(s"'${_output}' is not a valid filename.") - } + case flag @ ("-o" | "--output") :: output :: tail => + parse(tail, config += ('output -> getPath(output))) case flag @ ("-q" | "--queries") :: _query :: tail => { - val query = new File(_query) - if (!query.isFile) - exit(s"'$query' is not a valid filename.") + val query = getPath(_query) + if (!os.isFile(query)) + exit(s"'${_query}' is not a valid filename.") parse(tail, config += ('queries -> query)) } case _ontology :: _data => { - val ontology = new File(_ontology) - val data = _data.map(new File(_)) - (ontology :: data) foreach { (file) => - if (!file.isFile) - exit(s"'$file' is not a valid filename.") + val ontology = getPath(_ontology) + val data = _data map getPath + (ontology :: data) foreach { (path) => + if (!os.isFile(path)) + exit(s"'$path' is not a valid filename.") } finalise(config += ('ontology -> ontology) += ('data -> data)) } @@ -156,8 +158,8 @@ object RSAComb extends App { /* Load original ontology and normalize it */ val ontology = Ontology( - config('ontology).get[File], - config('data).get[List[File]] + config('ontology).get[os.Path], + config('data).get[List[os.Path]] ).normalize(new Normalizer) //ontology.axioms foreach println @@ -168,14 +170,18 @@ object RSAComb extends App { if (config contains 'queries) { val queries = - RDFoxUtil.loadQueriesFromFile(config('queries).get[File].getAbsoluteFile) + RDFoxUtil.loadQueriesFromFile( + config('queries).get[os.Path] + ) val answers = rsa ask queries /* Write answers to output file */ - val output = new PrintWriter(config('output).get[Path].toFile) - output.write(ujson.write(ujson.Arr(answers.map(_.toJSON)), indent = 4)) - output.close() + os.write( + config('output).get[os.Path], + ujson.write(ujson.Arr(answers.map(_.toJSON)), indent = 4), + createFolders = true + ) // Logger.print(s"$answers", Logger.VERBOSE) // 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 { def apply( axioms: List[OWLLogicalAxiom], - datafiles: List[File] + datafiles: List[os.Path] ): RSAOntology = new RSAOntology(axioms, datafiles) // def apply( @@ -191,7 +191,7 @@ object RSAOntology { * @param ontology the input OWL2 ontology. * @param datafiles additinal data (treated as part of the ABox) */ -class RSAOntology(axioms: List[OWLLogicalAxiom], datafiles: List[File]) +class RSAOntology(axioms: List[OWLLogicalAxiom], datafiles: List[os.Path]) extends Ontology(axioms, datafiles) { /** 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 { */ def dependencyGraph( axioms: List[OWLLogicalAxiom], - datafiles: List[File], + datafiles: List[os.Path], unsafe: List[OWLObjectPropertyExpression] ): DependencyGraph = { @@ -167,10 +167,10 @@ object Ontology { (graph, nodemap) } - def apply(axioms: List[OWLLogicalAxiom], datafiles: List[File]): Ontology = + def apply(axioms: List[OWLLogicalAxiom], datafiles: List[os.Path]): Ontology = new Ontology(axioms, datafiles) - def apply(ontology: OWLOntology, datafiles: List[File]): Ontology = { + def apply(ontology: OWLOntology, datafiles: List[os.Path]): Ontology = { /** TBox axioms */ var tbox: List[OWLLogicalAxiom] = @@ -202,8 +202,8 @@ object Ontology { Ontology(abox ::: tbox ::: rbox, datafiles) } - def apply(ontofile: File, datafiles: List[File]): Ontology = { - val ontology = manager.loadOntologyFromOntologyDocument(ontofile) + def apply(ontofile: os.Path, datafiles: List[os.Path]): Ontology = { + val ontology = manager.loadOntologyFromOntologyDocument(ontofile.toIO) Ontology(ontology, datafiles) } @@ -214,7 +214,10 @@ object Ontology { * @param axioms list of axioms (roughly) corresponding to the TBox. * @param datafiles files containing ABox data. */ -class Ontology(val axioms: List[OWLLogicalAxiom], val datafiles: List[File]) { +class Ontology( + val axioms: List[OWLLogicalAxiom], + val datafiles: List[os.Path] +) { /** Extend OWLAxiom functionalities */ 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 { * @param graph named graph where the data should be uploaded * @param files sequence of files to upload. */ - def addData(data: DataStoreConnection, graph: IRI, files: File*): Unit = + def addData(data: DataStoreConnection, graph: IRI, files: os.Path*): Unit = Logger.timed( - files.foreach { - data.importData(graph.getIRI, UpdateType.ADDITION, RSA.Prefixes, _) + files.foreach { path => + data.importData( + graph.getIRI, + UpdateType.ADDITION, + RSA.Prefixes, + path.toIO + ) }, "Loading data files", Logger.DEBUG @@ -235,11 +240,11 @@ object RDFoxUtil { * @return a list of [[tech.oxfordsemantic.jrdfox.logic.sparql.statement.SelectQuery SelectQuery]] queries. */ def loadQueriesFromFile( - file: File, + path: os.Path, prefixes: Prefixes = new Prefixes() ): List[ConjunctiveQuery] = { - val source = io.Source.fromFile(file) - val queries = source.getLines + val queries = os.read + .lines(path) .map(_.trim.filter(_ >= ' ')) .filterNot(_ == "") .foldRight((List.empty[List[String]], List.empty[String])) { @@ -254,8 +259,7 @@ object RDFoxUtil { .map(_.mkString(" ")) .map(ConjunctiveQuery.parse(_, prefixes)) .collect { case Some(q) => q } - Logger print s"Loaded ${queries.length} queries from ${file.getAbsolutePath}" - source.close() + Logger print s"Loaded ${queries.length} queries from $path" queries } -- cgit v1.2.3