Skip to content

Commit

Permalink
feat: updated parser
Browse files Browse the repository at this point in the history
  • Loading branch information
jenspots committed May 2, 2024
1 parent f844b43 commit 3281df1
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 35 deletions.
37 changes: 2 additions & 35 deletions src/main/kotlin/runner/Parser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,45 +39,12 @@ private fun QuerySolution.toProcessor(): Class<Processor> {

private fun QuerySolution.toStage(processors: List<Class<Processor>>): Processor {
val byName = processors.associateBy { it.simpleName }

// Extract the list of arguments.
val name = this["processor"]
.toString()
.substringAfterLast("#")

val values = this["values"].toString().split(";")

val keys = this["keys"]
.toString()
.split(";")
.map { it.substringAfterLast("#") }

val kinds = this["kinds"]
.toString()
.split(";")
.map { it.substringAfterLast("#") }

// Retrieve a class instance of the Processor.
val processor = byName[name] ?: Log.shared.fatal("Processor $name not found")
val args = mutableMapOf<String, Any>()

for (i in keys.indices) {
val key = keys[i]
val value = values[i]
val kind = kinds[i]

Log.shared.debug("$key: $kind = $value")

args[key] = when (kind) {
"integer" -> value.toInt()
"ChannelWriter" -> bridge
"ChannelReader" -> bridge
else -> Log.shared.fatal("Unknown kind $kind")
}
}

val constructor = processor.getConstructor(Map::class.java)
return constructor.newInstance(args)
val constructor = processor.getConstructor(QuerySolution::class.java)
return constructor.newInstance(this)
}

// TODO: Create some sort of a factory.
Expand Down
32 changes: 32 additions & 0 deletions src/main/kotlin/runner/Processor.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package technology.idlab.runner

import org.apache.jena.query.QuerySolution
import technology.idlab.logging.Log
import java.util.Optional

Expand All @@ -18,6 +19,37 @@ abstract class Processor {
*/
private val arguments = mutableMapOf<String, Any>()

constructor(solution: QuerySolution) {
val values = solution["values"]
.toString()
.split(";")

val keys = solution["keys"]
.toString()
.split(";")
.map { it.substringAfterLast("#") }

val kinds = solution["kinds"]
.toString()
.split(";")
.map { it.substringAfterLast("#") }

for (i in keys.indices) {
val key = keys[i]
val value = values[i]
val kind = kinds[i]

Log.shared.debug("$key: $kind = $value")

arguments[key] = when (kind) {
"integer" -> value.toInt()
"ChannelWriter" -> bridge
"ChannelReader" -> bridge
else -> Log.shared.fatal("Unknown kind $kind")
}
}
}

constructor(args: Map<String, Any>) {
arguments.putAll(args)
}
Expand Down

0 comments on commit 3281df1

Please sign in to comment.