Skip to content

Commit

Permalink
plumbing
Browse files Browse the repository at this point in the history
  • Loading branch information
Peva Blanchard committed Jul 22, 2023
1 parent 0ba8f79 commit b5e8077
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 48 deletions.
23 changes: 1 addition & 22 deletions src/main/kotlin/ch/kleis/lcaplugin/actions/ActionHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ package ch.kleis.lcaplugin.actions

import ch.kleis.lcaplugin.core.lang.evaluator.EvaluationTrace
import ch.kleis.lcaplugin.core.lang.evaluator.Evaluator
import ch.kleis.lcaplugin.core.lang.expression.DataExpression
import ch.kleis.lcaplugin.core.lang.expression.EProcessTemplateApplication
import ch.kleis.lcaplugin.core.lang.expression.EQuantityScale
import ch.kleis.lcaplugin.core.math.DualNumber
import ch.kleis.lcaplugin.language.parser.LcaFileCollector
import ch.kleis.lcaplugin.language.parser.LcaLangAbstractParser
import ch.kleis.lcaplugin.language.psi.LcaFile
Expand All @@ -32,24 +29,6 @@ fun traceSystemWithIndicator(
indicator.text = "Solving system"
val template =
symbolTable.getTemplate(processName, matchLabels)!! // We are called from a process, so it must exist
val nParams = template.params
.filter { it.value is EQuantityScale }
.size
val params = template.params
.toList()
.mapIndexed { index: Int, entry: Pair<String, DataExpression> ->
when (val p = entry.second) {
is EQuantityScale -> {
entry.first to EQuantityScale(
p.scale.copy(first = DualNumber.ith(nParams, index)),
p.base,
)
}
else -> entry.first to p
}
}
.toMap()
val entryPoint = EProcessTemplateApplication(template, params)

val entryPoint = EProcessTemplateApplication(template, template.params)
return Evaluator(symbolTable).trace(entryPoint)
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class Assessment(
substanceCharacterizations,
observableProducts,
observableSubstances,
system.effectiveParametersSize,
system.quantitativeArguments.size(),
)

val terminalProducts = processes
Expand All @@ -56,13 +56,13 @@ class Assessment(
terminalProducts,
terminalSubstances,
indicators,
system.effectiveParametersSize,
system.quantitativeArguments.size(),
)

demandMatrix = DemandMatrix(
targetProcess,
observablePorts,
system.effectiveParametersSize,
system.quantitativeArguments.size(),
)
}

Expand All @@ -74,6 +74,6 @@ class Assessment(
?.transpose()
?.let { SupplyMatrix(observablePorts, it) }
?: throw EvaluatorException("The system cannot be solved")
return Inventory(impactFactorMatrix, supplyMatrix)
return Inventory(allocatedSystem.quantitativeArguments, impactFactorMatrix, supplyMatrix)
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package ch.kleis.lcaplugin.core.assessment

import ch.kleis.lcaplugin.core.lang.evaluator.ArgumentName
import ch.kleis.lcaplugin.core.lang.value.MatrixColumnIndex
import ch.kleis.lcaplugin.core.matrix.ImpactFactorMatrix
import ch.kleis.lcaplugin.core.matrix.IndexedCollection
import ch.kleis.lcaplugin.core.matrix.SupplyMatrix

data class Inventory(
val arguments: IndexedCollection<ArgumentName>,
val impactFactors: ImpactFactorMatrix,
val supply: SupplyMatrix,
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package ch.kleis.lcaplugin.core.lang.evaluator

import ch.kleis.lcaplugin.core.HasUID

data class ArgumentName(val uid: String) : HasUID {
override fun getUID(): String {
return uid
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package ch.kleis.lcaplugin.core.lang.evaluator

import ch.kleis.lcaplugin.core.lang.value.*
import ch.kleis.lcaplugin.core.matrix.IndexedCollection

class EvaluationTrace(
private val parameters: Map<String, DataValue>
private val quantitativeArguments: IndexedCollection<ArgumentName>
) {
private val stages = ArrayList<HashSet<MatrixRowIndex>>()
private var currentStage = HashSet<MatrixRowIndex>()
Expand All @@ -12,8 +13,8 @@ class EvaluationTrace(
private val productDepthMap = HashMap<MatrixColumnIndex, Int>()

companion object {
fun empty(parameters: Map<String, DataValue>): EvaluationTrace {
return EvaluationTrace(parameters)
fun empty(quantitativeArguments: IndexedCollection<ArgumentName>): EvaluationTrace {
return EvaluationTrace(quantitativeArguments)
}
}

Expand Down Expand Up @@ -67,7 +68,7 @@ class EvaluationTrace(

fun getSystemValue(): SystemValue {
return SystemValue(
parameters,
quantitativeArguments,
processes,
substanceCharacterizations,
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ch.kleis.lcaplugin.core.lang.evaluator

import arrow.core.filterIsInstance
import arrow.optics.Every
import arrow.optics.PEvery
import ch.kleis.lcaplugin.core.lang.SymbolTable
Expand All @@ -12,6 +13,8 @@ import ch.kleis.lcaplugin.core.lang.expression.*
import ch.kleis.lcaplugin.core.lang.resolver.ProcessResolver
import ch.kleis.lcaplugin.core.lang.resolver.SubstanceCharacterizationResolver
import ch.kleis.lcaplugin.core.lang.value.SystemValue
import ch.kleis.lcaplugin.core.math.DualNumber
import ch.kleis.lcaplugin.core.matrix.IndexedCollection
import com.intellij.openapi.diagnostic.Logger

class Evaluator(
Expand Down Expand Up @@ -41,11 +44,13 @@ class Evaluator(
fun trace(expression: EProcessTemplateApplication): EvaluationTrace {
LOG.info("Start recursive Compile")
try {
val arguments = expression.arguments.mapValues {
dataReducer.reduce(it.value)
}
val result = EvaluationTrace.empty(arguments.mapValues { it.value.toValue() })
recursiveCompile(result, HashSet(), HashSet(setOf(EProcessTemplateApplication(expression.template, arguments))))
val (modifiedQuantitativeArgumentCollection, modifiedArguments) = prepareArguments(expression)
val result = EvaluationTrace.empty(modifiedQuantitativeArgumentCollection)
recursiveCompile(
result,
HashSet(),
HashSet(setOf(EProcessTemplateApplication(expression.template, modifiedArguments)))
)
LOG.info("End recursive Compile, found ${result.getNumberOfProcesses()} processes and ${result.getNumberOfSubstanceCharacterizations()} substances")
return result
} catch (e: Exception) {
Expand All @@ -54,6 +59,35 @@ class Evaluator(
}
}

private fun prepareArguments(expression: EProcessTemplateApplication): Pair<IndexedCollection<ArgumentName>, Map<String, DataExpression>> {
val arguments = expression.arguments
.mapValues { dataReducer.reduce(it.value) }
val quantitativeArguments = arguments
.filterIsInstance<String, EQuantityScale>()
val nonQuantitativeArguments = arguments.minus(quantitativeArguments.keys)
val nbQuantitativeArguments = quantitativeArguments.size
val modifiedQuantitativeArguments = quantitativeArguments
.toList()
.mapIndexed { i, p ->
val modifiedScale = p.second.scale
.copy(
first = DualNumber.ith(nbQuantitativeArguments, i)
)
val modifiedQuantity = p.second
.copy(
scale = modifiedScale
)
p.first to modifiedQuantity
}
val modifiedArguments = modifiedQuantitativeArguments
.toMap()
.plus(nonQuantitativeArguments)
val modifiedQuantitativeArgumentsCollection = modifiedQuantitativeArguments
.map { ArgumentName(it.first) }
.let { IndexedCollection(it) }
return Pair(modifiedQuantitativeArgumentsCollection, modifiedArguments)
}

fun eval(expression: EProcessTemplateApplication): SystemValue {
return trace(expression).getSystemValue()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
package ch.kleis.lcaplugin.core.lang.value

import arrow.core.filterIsInstance
import ch.kleis.lcaplugin.core.HasUID
import ch.kleis.lcaplugin.core.lang.evaluator.ArgumentName
import ch.kleis.lcaplugin.core.matrix.IndexedCollection


data class SystemValue(
val parameters: Map<String, DataValue>,
val quantitativeArguments: IndexedCollection<ArgumentName>,
val processes: Set<ProcessValue>,
val substanceCharacterizations: Set<SubstanceCharacterizationValue>,
) : Value, HasUID {
val effectiveParametersSize = parameters
.filterIsInstance<String, QuantityValue>()
.filter { it.value.amount.first.isNotEmpty() }
.size


val productToProcessMap: Map<ProductValue, ProcessValue> =
processes.flatMap { process -> process.products.map { it.product to process } }.toMap()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class AllocationTest {
fun apply_when_coProducts_should_duplicate_process() {
// Given
val system = SystemValue(
parameters = emptyMap(),
quantitativeArguments = emptyMap(),
mutableSetOf(
ProcessValue(
"carrot",
Expand Down Expand Up @@ -64,7 +64,7 @@ class AllocationTest {
fun apply_when_coProducts_should_keep_only_one_product_per_process() {
// Given
val system = SystemValue(
parameters = emptyMap(),
quantitativeArguments = emptyMap(),
mutableSetOf(
ProcessValue(
"carrot",
Expand Down Expand Up @@ -98,7 +98,7 @@ class AllocationTest {
fun apply_when_allocation_should_divide_inputs_quantities() {
// Given
val system = SystemValue(
parameters = emptyMap(),
quantitativeArguments = emptyMap(),
mutableSetOf(
ProcessValue(
"carrot",
Expand Down Expand Up @@ -138,7 +138,7 @@ class AllocationTest {
fun apply_when_allocation_should_divide_biosphere_quantities() {
// given
val system = SystemValue(
parameters = emptyMap(),
quantitativeArguments = emptyMap(),
mutableSetOf(
ProcessValue(
"carrot",
Expand Down Expand Up @@ -259,7 +259,7 @@ class AllocationTest {
// given
val allocation = Allocation()
val system = SystemValue(
parameters = emptyMap(),
quantitativeArguments = emptyMap(),
mutableSetOf(
ProcessValue(
"carrot",
Expand Down Expand Up @@ -302,7 +302,7 @@ class AllocationTest {
fun apply_shouldKeepAllocation() {
// given
val system = SystemValue(
parameters = emptyMap(),
quantitativeArguments = emptyMap(),
mutableSetOf(ProcessValue("", emptyMap(), listOf(), listOf(), listOf())),
mutableSetOf(propanolCharacterization)
)
Expand Down

0 comments on commit b5e8077

Please sign in to comment.