Skip to content

Commit

Permalink
core: refactor evaluator: via template name and args and labels
Browse files Browse the repository at this point in the history
  • Loading branch information
Peva Blanchard committed Oct 27, 2023
1 parent c5eadab commit 29bd64e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,18 @@ class Evaluator<Q>(
}
}

fun trace(template: EProcessTemplate<Q>, arguments: Map<String, DataExpression<Q>> = emptyMap()): EvaluationTrace<Q> {
fun trace(
templateName: String,
arguments: Map<String, DataExpression<Q>> = emptyMap(),
labels: Map<String, String> = emptyMap(),
): EvaluationTrace<Q> {
val template = pkg.getTemplate(templateName, labels)
?: throw EvaluatorException("unknown process template $templateName$labels")
return prepareRequests(template, arguments)
.let(this::trace)
}

private fun <Q> prepareRequests(
private fun prepareRequests(
template: EProcessTemplate<Q>,
arguments: Map<String, DataExpression<Q>> = emptyMap(),
): Set<EProductSpec<Q>> {
Expand All @@ -48,7 +54,8 @@ class Evaluator<Q>(
fromProcess = FromProcess(
body.name,
MatchLabels(body.labels),
template.params.plus(arguments)
template.params.plus(arguments),
pkg = pkg,
)
)
}.toSet()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class EvaluatorTest {
)

// when
val actual = evaluator.trace(template).getEntryPoint().impacts.first()
val actual = evaluator.trace("eProcess").getEntryPoint().impacts.first()

// then
assertEquals(expected, actual)
Expand Down Expand Up @@ -95,7 +95,7 @@ class EvaluatorTest {
)

// when
val actual = evaluator.trace(template).getEntryPoint().biosphere.first().substance
val actual = evaluator.trace("eProcess").getEntryPoint().biosphere.first().substance

// then
assertEquals(expected, actual)
Expand All @@ -115,9 +115,9 @@ class EvaluatorTest {
val evaluator = Evaluator(pkg, PkgResolverFixture.alwaysResolveTo(pkg), ops)

// when
val p1 = evaluator.trace(template, mapOf("q_water" to QuantityFixture.oneLitre))
val p1 = evaluator.trace("carrot_production", mapOf("q_water" to QuantityFixture.oneLitre))
.getEntryPoint().products.first().product
val p2 = evaluator.trace(template, mapOf("q_water" to QuantityFixture.twoLitres))
val p2 = evaluator.trace("carrot_production", mapOf("q_water" to QuantityFixture.twoLitres))
.getEntryPoint().products.first().product

// then
Expand All @@ -138,7 +138,7 @@ class EvaluatorTest {

// when

val p1 = evaluator.trace(template).getEntryPoint().products.first().product
val p1 = evaluator.trace("carrot_production").getEntryPoint().products.first().product

// then
assertEquals("carrot", p1.name)
Expand Down Expand Up @@ -177,7 +177,7 @@ class EvaluatorTest {
val evaluator = Evaluator(pkg, PkgResolverFixture.alwaysResolveTo(pkg), ops)

// when
val actual = evaluator.trace(template).getSystemValue().processes
val actual = evaluator.trace("salad_production").getSystemValue().processes

// then
val expected = setOf(
Expand Down Expand Up @@ -276,7 +276,7 @@ class EvaluatorTest {
val evaluator = Evaluator(pkg, PkgResolverFixture.alwaysResolveTo(pkg), ops)

// when
val actual = evaluator.trace(template).getSystemValue().processes
val actual = evaluator.trace("salad_production").getSystemValue().processes

// then
val expected = setOf(
Expand Down Expand Up @@ -376,7 +376,7 @@ class EvaluatorTest {
// when/then
val e = assertFailsWith(
EvaluatorException::class,
) { evaluator.trace(template) }
) { evaluator.trace("salad_production") }
assertEquals("no process 'carrot_production' providing 'irrelevant_product' found", e.message)
}

Expand Down Expand Up @@ -422,7 +422,7 @@ class EvaluatorTest {
val evaluator = Evaluator(pkg, PkgResolverFixture.alwaysResolveTo(pkg), ops)

// when
val actual = evaluator.trace(template).getSystemValue().substanceCharacterizations
val actual = evaluator.trace("carrot_production").getSystemValue().substanceCharacterizations

// then
val expected = setOf(
Expand Down Expand Up @@ -464,7 +464,7 @@ class EvaluatorTest {
// when/then
val e = assertFailsWith(
EvaluatorException::class,
) { evaluator.trace(template) }
) { evaluator.trace("salad_production") }
assertEquals("incompatible dimensions: length³ vs mass for product carrot", e.message)
}
}

0 comments on commit 29bd64e

Please sign in to comment.