Skip to content

Commit

Permalink
refactor: split files for IR arguments and parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
jenspots committed Oct 3, 2024
1 parent 0a2eea7 commit 7a8f258
Show file tree
Hide file tree
Showing 21 changed files with 219 additions and 147 deletions.
4 changes: 2 additions & 2 deletions orchestrator/src/main/kotlin/RDFCException.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package technology.idlab

import technology.idlab.intermediate.IRRunner
import technology.idlab.intermediate.runner.RunnerType

abstract class RDFCException : Exception()

Expand Down Expand Up @@ -111,7 +111,7 @@ class NoSuchRunnerException(private val runnerUri: String) : RDFCException()
*
* @param type The type which was used, but not implemented.
*/
class UnsupportedRunnerTypeException(private val type: IRRunner.Type) : RDFCException()
class UnsupportedRunnerTypeException(private val type: RunnerType) : RDFCException()

/**
* Metadata was expected, but not found.
Expand Down
2 changes: 1 addition & 1 deletion orchestrator/src/main/kotlin/extensions/PropertyShape.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import org.apache.jena.shacl.parser.PropertyShape
import org.apache.jena.shacl.parser.Shape
import org.apache.jena.shacl.vocabulary.SHACLM
import technology.idlab.RDFC
import technology.idlab.intermediate.LiteralParameterType
import technology.idlab.intermediate.parameter.LiteralParameterType
import technology.idlab.parser.ParserException

/**
Expand Down
6 changes: 3 additions & 3 deletions orchestrator/src/main/kotlin/extensions/Shape.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import org.apache.jena.rdf.model.Resource
import org.apache.jena.shacl.parser.PropertyShape
import org.apache.jena.shacl.parser.Shape
import org.apache.jena.shacl.vocabulary.SHACLM
import technology.idlab.intermediate.LiteralParameter
import technology.idlab.intermediate.NestedParameter
import technology.idlab.intermediate.Parameter
import technology.idlab.intermediate.parameter.LiteralParameter
import technology.idlab.intermediate.parameter.NestedParameter
import technology.idlab.intermediate.parameter.Parameter

/**
* Check if the shape is closed.
Expand Down
67 changes: 19 additions & 48 deletions orchestrator/src/main/kotlin/intermediate/IRArgument.kt
Original file line number Diff line number Diff line change
@@ -1,58 +1,29 @@
package technology.idlab.intermediate

sealed interface Argument {
val parameter: Parameter

fun findAll(type: LiteralParameterType, parameter: Parameter): List<String>
}

class LiteralArgument(
override val parameter: LiteralParameter,
val values: MutableList<String> = mutableListOf(),
) : Argument {
override fun findAll(type: LiteralParameterType, parameter: Parameter): List<String> {
if (parameter !is LiteralParameter) {
return emptyList()
}

return if (parameter.type == type) {
this.values
} else {
emptyList()
}
}
}

class NestedArgument(
override val parameter: NestedParameter,
val values: MutableList<Map<String, Argument>> = mutableListOf(),
) : Argument {
override fun findAll(type: LiteralParameterType, parameter: Parameter): List<String> {
if (parameter !is NestedParameter) {
throw IllegalStateException()
}

val result = mutableListOf<String>()

for (value in this.values) {
for ((key, argument) in value) {
result.addAll(argument.findAll(type, parameter[key]))
}
}

return result
}
}
import technology.idlab.intermediate.argument.Argument
import technology.idlab.intermediate.parameter.LiteralParameterType

class IRArgument(
val parameter: IRParameter,
val values: Map<String, Argument> = mutableMapOf(),
val root: Map<String, Argument> = mutableMapOf(),
) {
/**
* Get the argument for a given key.
*
* @param key The key of the argument.
* @return The argument.
* @throws IllegalArgumentException If the argument is not found.
*/
operator fun get(key: String): Argument {
return values[key] ?: throw IllegalArgumentException("Argument $key not found.")
return root[key] ?: throw IllegalArgumentException("Argument $key not found.")
}

fun findAll(type: LiteralParameterType, parameter: Parameter): List<String> {
return this.values.values.map { it.findAll(type, parameter) }.flatten()
/**
* Find all values of a certain type for a given parameter.
*
* @param type The type of the literal parameter to find.
* @result A flattened list of all values of the given type for the given parameter.
*/
fun findAll(type: LiteralParameterType): List<String> {
return root.values.map { it.findAll(type) }.flatten()
}
}
47 changes: 5 additions & 42 deletions orchestrator/src/main/kotlin/intermediate/IRParameter.kt
Original file line number Diff line number Diff line change
@@ -1,55 +1,18 @@
package technology.idlab.intermediate

class IRParameter(
uri: String,
type: Map<String, Parameter>,
) :
NestedParameter(
uri,
type,
single = false,
optional = false,
)

sealed interface Parameter {
val uri: String
val single: Boolean
val optional: Boolean
}
import technology.idlab.intermediate.parameter.Parameter

open class NestedParameter(
override val uri: String,
class IRParameter(
val type: Map<String, Parameter>,
override val single: Boolean = false,
override val optional: Boolean = false
) : Parameter {
) {
/**
* Get the parameter with the given key.
*
* @param key The key of the parameter.
* @return The parameter.
* @throws IllegalArgumentException If the parameter is not found.
*/
operator fun get(key: String): Parameter {
return type[key] ?: throw IllegalArgumentException("Parameter $key not found.")
return type[key] ?: throw IllegalArgumentException("No such parameter: $key")
}
}

enum class LiteralParameterType {
BOOLEAN,
BYTE,
DATE,
DOUBLE,
FLOAT,
INT,
LONG,
STRING,
WRITER,
READER,
}

data class LiteralParameter(
override val uri: String,
val type: LiteralParameterType,
override val single: Boolean = false,
override val optional: Boolean = false,
) : Parameter
2 changes: 1 addition & 1 deletion orchestrator/src/main/kotlin/intermediate/IRProcessor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class IRProcessor(
/** The entrypoint. */
val entrypoint: String,
/** Processor parameters. */
val parameters: IRParameter = IRParameter(uri, emptyMap()),
val parameters: IRParameter,
/** Additional parameters. These may be used by the runner for any reason. */
val metadata: Map<String, String> = emptyMap()
)
10 changes: 3 additions & 7 deletions orchestrator/src/main/kotlin/intermediate/IRRunner.kt
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
package technology.idlab.intermediate

import java.io.File
import technology.idlab.intermediate.runner.RunnerType

data class IRRunner(
val uri: String,
val directory: File? = null,
val entrypoint: String? = null,
val type: Type,
) {
enum class Type {
GRPC,
BUILT_IN,
}
}
val type: RunnerType,
)
6 changes: 4 additions & 2 deletions orchestrator/src/main/kotlin/intermediate/IRStage.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package technology.idlab.intermediate

import technology.idlab.intermediate.parameter.LiteralParameterType

data class IRStage(
// The URI of the stage.
val uri: String,
Expand All @@ -9,10 +11,10 @@ data class IRStage(
val arguments: IRArgument,
) {
fun readers(): List<String> {
return arguments.findAll(LiteralParameterType.READER, processor.parameters)
return arguments.findAll(LiteralParameterType.READER)
}

fun writers(): List<String> {
return arguments.findAll(LiteralParameterType.WRITER, processor.parameters)
return arguments.findAll(LiteralParameterType.WRITER)
}
}
18 changes: 18 additions & 0 deletions orchestrator/src/main/kotlin/intermediate/argument/Argument.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package technology.idlab.intermediate.argument

import technology.idlab.intermediate.parameter.LiteralParameterType
import technology.idlab.intermediate.parameter.Parameter

/** IR representation of a concrete argument. It holds it's corresponding parameter as a field. */
sealed interface Argument {
// The parameter that this argument corresponds to.
val parameter: Parameter

/**
* Find all values of a certain type for a given parameter.
*
* @param type The type of the literal parameter to find.
* @return A flattened list of all values of the given type for the given parameter.
*/
fun findAll(type: LiteralParameterType): List<String>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package technology.idlab.intermediate.argument

import technology.idlab.intermediate.parameter.LiteralParameter
import technology.idlab.intermediate.parameter.LiteralParameterType

/** Representation of a literal argument in IR as a list of strings. */
class LiteralArgument(
override val parameter: LiteralParameter,
val values: MutableList<String> = mutableListOf(),
) : Argument {
/*
* If the argument holds the correct type, we can simply return the whole list.
*/
override fun findAll(type: LiteralParameterType): List<String> {
return if (parameter.type == type) {
values
} else {
emptyList()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package technology.idlab.intermediate.argument

import technology.idlab.intermediate.parameter.LiteralParameterType
import technology.idlab.intermediate.parameter.NestedParameter

/** Representation of a single nested argument in IR as a list of key-value pairs. */
class NestedArgument(
override val parameter: NestedParameter,
val values: MutableList<Map<String, Argument>> = mutableListOf(),
) : Argument {
/*
* The function simply loops over all values in the list and recursively calls findAll on each of
* them.
*/
override fun findAll(type: LiteralParameterType): List<String> {
val result = mutableListOf<String>()

for (value in this.values) {
for ((_, argument) in value) {
result.addAll(argument.findAll(type))
}
}

return result
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package technology.idlab.intermediate.parameter

/** Representation of a (list of) literal parameter(s) in IR. */
data class LiteralParameter(
// The path of the parameter as specified in the config.
override val path: String,
// The datatype of the parameter.
val type: LiteralParameterType,
// True if there is only one instance of the parameter at most.
override val single: Boolean = false,
// True if there may be zero instances of the parameter.
override val optional: Boolean = false,
) : Parameter
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package technology.idlab.intermediate.parameter

/** Specifies the type of literal parameter. */
enum class LiteralParameterType {
BOOLEAN,
BYTE,
DATE,
DOUBLE,
FLOAT,
INT,
LONG,
STRING,
WRITER,
READER,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package technology.idlab.intermediate.parameter

/** Representation of nested parameter in IR. */
open class NestedParameter(
override val path: String,
val type: Map<String, Parameter>,
override val single: Boolean = false,
override val optional: Boolean = false
) : Parameter {
/**
* Get the parameter with the given key.
*
* @param key The key of the parameter.
* @return The parameter.
*/
operator fun get(key: String): Parameter {
return type[key] ?: throw IllegalArgumentException("Parameter $key not found.")
}
}
20 changes: 20 additions & 0 deletions orchestrator/src/main/kotlin/intermediate/parameter/Parameter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package technology.idlab.intermediate.parameter

/**
* Generic representation of a parameter in IR, which may be either a literal or a nested parameter.
*/
sealed interface Parameter {
// The path of the parameter as specified in the config.
val path: String
// True if there is only one instance of the parameter at most.
val single: Boolean
// True if there may be zero instances of the parameter.
val optional: Boolean
// True if there can be more than one instance of the parameter.
val list: Boolean
get() = !single

// True if the parameter is not optional.
val required: Boolean
get() = !optional
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package technology.idlab.intermediate.runner

/** The types of runner supported by the Orchestrator. */
enum class RunnerType {
// A runner which communicates over GRPC.
GRPC,
// A runner which is built into the Orchestrator.
BuiltIn,
}
Loading

0 comments on commit 7a8f258

Please sign in to comment.