Skip to content

Commit

Permalink
feat: use delegation pattern for JVM runner arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
jenspots committed Jul 26, 2024
1 parent b291f4d commit 5b4b3f6
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 13 deletions.
26 changes: 13 additions & 13 deletions processors/shacl-validator-kt/src/main/kotlin/SHACLValidator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,30 @@ class SHACLValidator(args: Arguments) : Processor(args) {
private val errorIsFatalDefault = false

/** Arguments. */
private val fatal: Boolean? = arguments["validation_is_fatal"]
private val incoming: ReceiveChannel<ByteArray> = arguments["incoming"]
private val outgoing: SendChannel<ByteArray> = arguments["outgoing"]
private val report: SendChannel<ByteArray>? = arguments["report"]
private val path: String = arguments["shapes"]
private val fatal: Boolean? by args
private val incoming: ReceiveChannel<ByteArray> by args
private val outgoing: SendChannel<ByteArray> by args
private val report: SendChannel<ByteArray>? by args
private val shapes: String by args

/** Runtime fields. */
private val shapes: Graph
private val shapesGraph: Graph
private val validator = ShaclValidator.get()

init {
// Initialize the shape graph and validator.
Log.shared.debug { "Loading: $path" }
Log.shared.debug { "Loading: $shapes" }

// Create a new model with the SHACL shapes.
// Create a new model with the SHACL shapesGraph.
val model = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM)
try {
model.read(path, "TURTLE")
model.read(shapes, "TURTLE")
} catch (e: RiotException) {
Log.shared.fatal("Failed to read SHACL shapes from file://$path")
Log.shared.fatal("Failed to read SHACL shapesGraph from file://$shapes")
}

// Assign its graph to the shapes field.
this.shapes = model.graph
// Assign its graph to the shapesGraph field.
this.shapesGraph = model.graph
}

/** Read incoming data, validate it, and output it. */
Expand All @@ -56,7 +56,7 @@ class SHACLValidator(args: Arguments) : Processor(args) {
}

// Validate the model.
val report = validator.validate(shapes, model.graph)
val report = validator.validate(shapesGraph, model.graph)

if (report.conforms()) {
Log.shared.debug { "Validation successful." }
Expand Down
5 changes: 5 additions & 0 deletions src/main/kotlin/runner/impl/jvm/Arguments.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package technology.idlab.runner.impl.jvm

import kotlin.reflect.KProperty
import kotlin.reflect.KType
import kotlin.reflect.full.isSubclassOf
import kotlin.reflect.full.isSuperclassOf
Expand Down Expand Up @@ -105,6 +106,10 @@ data class Arguments(
}
}

inline operator fun <reified T> getValue(thisRef: Any, property: KProperty<*>): T {
return this.get<T>(property.name)
}

companion object {
/**
* Parse a (nested) map into type-safe arguments. This method calls itself recursively for all
Expand Down
19 changes: 19 additions & 0 deletions src/test/kotlin/runner/impl/jvm/ArgumentsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,25 @@ class ArgumentsTest {
Log.shared.setFatalMode(Log.FatalMode.EXCEPTION)
}

@Test
fun delegation() {
val args =
Arguments(
mapOf(
"words" to listOf("the", "a"),
"present" to listOf(true),
))
val obj =
object {
val words: List<String> by args
val present: Boolean? by args
val nulled: Boolean? by args
}
assertEquals(listOf("the", "a"), obj.words)
assertEquals(true, obj.present)
assertEquals(null, obj.nulled)
}

@Test
fun single() {
val args = Arguments(mapOf("key" to listOf("value")))
Expand Down

0 comments on commit 5b4b3f6

Please sign in to comment.