-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
192 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package technology.idlab.std | ||
|
||
import bridge.Writer | ||
import java.io.File | ||
import technology.idlab.runner.Processor | ||
|
||
class FileReader(args: Map<String, Any>) : Processor(args) { | ||
/** Arguments */ | ||
private val path: String = this.getArgument("path") | ||
private val output: Writer = this.getArgument("output") | ||
|
||
/** Read the file as a single byte array and push it down the pipeline. */ | ||
override fun exec() { | ||
val file = File(path) | ||
val bytes = file.readBytes() | ||
output.pushSync(bytes) | ||
output.close() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package technology.idlab.std | ||
|
||
import bridge.Reader | ||
import java.io.File | ||
import technology.idlab.runner.Processor | ||
|
||
class FileWriter(args: Map<String, Any>) : Processor(args) { | ||
/** Processor default values. */ | ||
private val overwriteDefault = true | ||
private val appendDefault = false | ||
|
||
/** Arguments */ | ||
private val file = File(this.getArgument<String>("path")) | ||
private val input: Reader = this.getArgument("input") | ||
private val overwrite = this.getOptionalArgument<Boolean>("overwrite") | ||
private val append = this.getOptionalArgument<Boolean>("append") | ||
|
||
init { | ||
// Sanity check. | ||
if (overwrite.orElse(false) && append.orElse(false)) { | ||
log.fatal("Cannot overwrite and append at the same time") | ||
} | ||
|
||
// Do not overwrite the file if it exists. | ||
if (file.exists() && !overwrite.orElse(overwriteDefault)) { | ||
log.fatal("File ${file.path} already exists") | ||
} | ||
|
||
// Overwrite file if not exists. | ||
if (file.exists() && !append.orElse(appendDefault)) { | ||
file.writeBytes(ByteArray(0)) | ||
} | ||
} | ||
|
||
/** All incoming values are parsed as byte and appended onto the file. */ | ||
override fun exec() { | ||
while (true) { | ||
// Read the next incoming value. | ||
val result = input.readSync() | ||
if (result.isClosed()) { | ||
break | ||
} | ||
|
||
// Append it to the file. | ||
file.appendBytes(result.value) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
@prefix jvm: <https://w3id.org/conn/jvm#>. | ||
@prefix owl: <http://www.w3.org/2002/07/owl#>. | ||
@prefix sh: <http://www.w3.org/ns/shacl#>. | ||
@prefix xsd: <http://www.w3.org/2001/XMLSchema#>. | ||
|
||
<> owl:imports <../pipeline.ttl>. | ||
|
||
jvm:FileReader a jvm:Processor; | ||
jvm:file <../../kotlin/std/FileReader.kt>; | ||
jvm:language "Kotlin". | ||
|
||
[] a sh:NodeShape; | ||
sh:targetClass jvm:FileReader; | ||
sh:property [ | ||
sh:path jvm:path; | ||
sh:name "path"; | ||
sh:datatype xsd:string; | ||
], [ | ||
sh:path jvm:output; | ||
sh:name "output"; | ||
sh:class jvm:ChannelWriter; | ||
sh:minCount 1; | ||
sh:maxCount 1; | ||
]; | ||
sh:closed true; | ||
sh:ignoredProperties (rdf:type). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
@prefix jvm: <https://w3id.org/conn/jvm#>. | ||
@prefix owl: <http://www.w3.org/2002/07/owl#>. | ||
@prefix sh: <http://www.w3.org/ns/shacl#>. | ||
@prefix xsd: <http://www.w3.org/2001/XMLSchema#>. | ||
|
||
<> owl:imports <../pipeline.ttl>. | ||
|
||
jvm:FileWriter a jvm:Processor; | ||
jvm:file <../../kotlin/std/FileWriter.kt>; | ||
jvm:language "Kotlin". | ||
|
||
[] a sh:NodeShape; | ||
sh:targetClass jvm:FileWriter; | ||
sh:property [ | ||
sh:path jvm:path; | ||
sh:name "path"; | ||
sh:datatype xsd:string; | ||
], [ | ||
sh:path jvm:input; | ||
sh:name "input"; | ||
sh:class jvm:ChannelReader; | ||
sh:minCount 1; | ||
sh:maxCount 1; | ||
], [ | ||
sh:path jvm:overwrite; | ||
sh:name "overwrite"; | ||
sh:datatype xsd:boolean; | ||
sh:minCount 0; | ||
sh:maxCount 1; | ||
], [ | ||
sh:path jvm:append; | ||
sh:name "append"; | ||
sh:datatype xsd:boolean; | ||
sh:minCount 0; | ||
sh:maxCount 1; | ||
]; | ||
sh:closed true; | ||
sh:ignoredProperties (rdf:type). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package std | ||
|
||
import java.io.File | ||
import java.util.* | ||
import kotlin.concurrent.thread | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlinx.coroutines.channels.Channel | ||
import technology.idlab.bridge.MemoryReader | ||
import technology.idlab.bridge.MemoryWriter | ||
import technology.idlab.std.FileReader | ||
import technology.idlab.std.FileWriter | ||
|
||
class FileUtilities { | ||
@Test | ||
fun e2e() { | ||
val input = File.createTempFile("input", "txt") | ||
val output = File.createTempFile("output", "txt") | ||
|
||
// Write to the input file. | ||
input.writeText("Hello, World!") | ||
|
||
// Configure the FileReader processor. | ||
val channel = Channel<ByteArray>(1) | ||
val reader = MemoryReader() | ||
val writer = MemoryWriter() | ||
|
||
reader.setChannel(channel) | ||
writer.setChannel(channel) | ||
|
||
val fileReader = | ||
FileReader( | ||
mapOf( | ||
"path" to input.path, | ||
"output" to writer, | ||
)) | ||
|
||
val fileWriter = | ||
FileWriter( | ||
mapOf( | ||
"path" to output.path, | ||
"input" to reader, | ||
"overwrite" to Optional.of(true), | ||
"append" to Optional.of(false), | ||
)) | ||
|
||
// Execute the FileReader processor. | ||
listOf(fileReader, fileWriter).map { thread { it.exec() } }.forEach { it.join() } | ||
|
||
// Check if the output is correct. | ||
val result = output.readText() | ||
assertEquals("Hello, World!", result) | ||
} | ||
} |