-
Notifications
You must be signed in to change notification settings - Fork 1
add cached annotation #43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
e53cf05
add cached annotation
sebdebros 868399b
clean code
sebdebros e93c134
add mapper
sebdebros a59a385
clean code
sebdebros 68355f8
remove abbreviation (qty -> quantity)
sebdebros 7c8d654
rename test file
sebdebros 749c642
remove exception for duplicated annotation
sebdebros 48b1947
mv cached process mapping test into CoreMapperTest
sebdebros 669f0ca
rename ImpactFactor methods with correct LCA names
sebdebros 6696b1d
move expression mapping logic into CoreMapper
sebdebros 97831a7
transform EMapper into an Object
sebdebros 009507b
add test for cached process resolver
sebdebros File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| #Thu Jul 24 10:18:57 CEST 2025 | ||
| #Mon Aug 11 17:16:17 CEST 2025 | ||
| author=Kleis Technology | ||
| description=LCA as Code CLI | ||
| version=1.7.13 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
core/src/main/kotlin/ch/kleis/lcaac/core/lang/expression/EMapper.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| package ch.kleis.lcaac.core.lang.expression | ||
|
|
||
| import ch.kleis.lcaac.core.lang.value.* | ||
|
|
||
| class EMapper { | ||
| fun <Q> toDataExpression(value: DataValue<Q>): DataExpression<Q> { | ||
| return when (value) { | ||
| is QuantityValue -> value.toEQuantityScale() | ||
| is RecordValue -> value.toERecord() | ||
| is StringValue -> value.toEStringLiteral() | ||
| } | ||
| } | ||
|
|
||
| fun <Q> toFromProcess(value: FromProcessRefValue<Q>): FromProcess<Q> { | ||
| val labels = MatchLabels(value.matchLabels.map { it.key to it.value.toEStringLiteral() }.toMap()) | ||
| val arguments: Map<String, DataExpression<Q>> = value.arguments.map { it.key to toDataExpression(it.value) }.toMap() | ||
| return FromProcess(value.name, labels, arguments) | ||
| } | ||
|
|
||
| fun <Q> toETechnoExchange(quantity: QuantityValue<Q>, product: ProductValue<Q>): ETechnoExchange<Q> { | ||
| return ETechnoExchange( | ||
| quantity = quantity.toEQuantityScale(), | ||
| product = EProductSpec( | ||
| product.name, | ||
| product.referenceUnit.toEUnitLiteral(), | ||
| product.fromProcessRef?.let { toFromProcess(it) } | ||
| ) | ||
| ) | ||
| } | ||
|
|
||
| fun <Q> toETechnoExchange(value: TechnoExchangeValue<Q>): ETechnoExchange<Q> { | ||
| return ETechnoExchange( | ||
| quantity = value.quantity.toEQuantityScale(), | ||
| product = EProductSpec( | ||
| value.product.name, | ||
| value.product.referenceUnit.toEUnitLiteral(), | ||
| value.product.fromProcessRef?.let { toFromProcess(it) } | ||
| ), | ||
| allocation = value.allocation?.toEQuantityScale() | ||
| ) | ||
| } | ||
|
|
||
| fun <Q> toEBioExchange(quantity: QuantityValue<Q>, substance: SubstanceValue<Q>): EBioExchange<Q> { | ||
| return EBioExchange( | ||
| quantity = quantity.toEQuantityScale(), | ||
| substance = when (substance) { | ||
| is FullyQualifiedSubstanceValue -> ESubstanceSpec( | ||
| name = substance.getShortName(), | ||
| displayName = substance.getDisplayName(), | ||
| type = substance.type, | ||
| compartment = substance.compartment, | ||
| subCompartment = substance.subcompartment, | ||
| referenceUnit = substance.referenceUnit.toEUnitLiteral() | ||
| ) | ||
| is PartiallyQualifiedSubstanceValue -> ESubstanceSpec( | ||
| name = substance.getShortName(), | ||
| displayName = substance.getDisplayName(), | ||
| referenceUnit = substance.referenceUnit.toEUnitLiteral() | ||
| ) | ||
| } | ||
| ) | ||
| } | ||
|
|
||
| fun <Q> toEImpact(quantity: QuantityValue<Q>, value: IndicatorValue<Q>): EImpact<Q> { | ||
| return EImpact( | ||
| quantity = quantity.toEQuantityScale(), | ||
| indicator = EIndicatorSpec(value.name, value.referenceUnit.toEUnitLiteral()) | ||
| ) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 90 additions & 29 deletions
119
core/src/main/kotlin/ch/kleis/lcaac/core/lang/resolver/ProcessResolver.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,38 +1,99 @@ | ||
| package ch.kleis.lcaac.core.lang.resolver | ||
|
|
||
| import ch.kleis.lcaac.core.assessment.AnalysisProgram | ||
| import ch.kleis.lcaac.core.datasource.DataSourceOperations | ||
| import ch.kleis.lcaac.core.lang.SymbolTable | ||
| import ch.kleis.lcaac.core.lang.evaluator.EvaluatorException | ||
| import ch.kleis.lcaac.core.lang.expression.EProcessTemplate | ||
| import ch.kleis.lcaac.core.lang.expression.EProductSpec | ||
| import ch.kleis.lcaac.core.lang.expression.EStringLiteral | ||
|
|
||
| class ProcessResolver<Q>( | ||
| private val symbolTable: SymbolTable<Q> | ||
| ) { | ||
| fun resolve(spec: EProductSpec<Q>): EProcessTemplate<Q>? { | ||
| if (spec.fromProcess == null) { | ||
| val matches = symbolTable.getAllTemplatesByProductName(spec.name) | ||
| return when (matches.size) { | ||
| 0 -> null | ||
| 1 -> matches.first() | ||
| else -> throw EvaluatorException("more than one processes found providing ${spec.name}") | ||
| } | ||
| import ch.kleis.lcaac.core.lang.evaluator.EvaluationTrace | ||
| import ch.kleis.lcaac.core.lang.evaluator.Evaluator | ||
| import ch.kleis.lcaac.core.lang.evaluator.step.CompleteTerminals | ||
| import ch.kleis.lcaac.core.lang.evaluator.step.Reduce | ||
| import ch.kleis.lcaac.core.lang.expression.* | ||
| import ch.kleis.lcaac.core.lang.register.ProcessKey | ||
| import ch.kleis.lcaac.core.lang.value.MatrixColumnIndex | ||
| import ch.kleis.lcaac.core.lang.value.QuantityValue | ||
| import ch.kleis.lcaac.core.lang.value.QuantityValueOperations | ||
| import ch.kleis.lcaac.core.lang.value.TechnoExchangeValue | ||
| import ch.kleis.lcaac.core.math.Operations | ||
| import ch.kleis.lcaac.core.matrix.ImpactFactorMatrix | ||
|
|
||
| interface ProcessResolver<Q, M> { | ||
pevab marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| fun resolve(template: EProcessTemplate<Q>, spec: EProductSpec<Q>): EProcess<Q> | ||
| } | ||
|
|
||
| class CachedProcessResolver<Q, M>( | ||
| val symbolTable: SymbolTable<Q>, | ||
| val ops: Operations<Q, M>, | ||
| val sourceOps: DataSourceOperations<Q>, | ||
| ) : ProcessResolver<Q, M> { | ||
| private val eMapper = EMapper() | ||
|
|
||
| override fun resolve(template: EProcessTemplate<Q>, spec: EProductSpec<Q>): EProcess<Q> { | ||
| val trace = getTrace(template, spec) | ||
| val entryPoint = trace.getEntryPoint() | ||
| val analysis = AnalysisProgram(trace.getSystemValue(), entryPoint, ops).run() | ||
| val inputQty = inputQtyAnalysis(entryPoint.products, analysis.impactFactors) | ||
|
|
||
| val inputs = analysis.impactFactors.getInputs().map { | ||
| eMapper.toETechnoExchange(inputQty(it), it) | ||
| } | ||
|
|
||
| val name = spec.fromProcess.name | ||
| val labels = spec.fromProcess.matchLabels.elements.mapValues { | ||
| when (val v = it.value) { | ||
| is EStringLiteral -> v.value | ||
| else -> throw EvaluatorException("$v is not a valid label value") | ||
| } | ||
| } | ||
| return symbolTable.getTemplate(name, labels)?.let { candidate -> | ||
| val providedProducts = candidate.body.products.map { it.product.name } | ||
| if (!providedProducts.contains(spec.name)) { | ||
| val s = if (labels.isEmpty()) name else "$name$labels" | ||
| throw EvaluatorException("no process '$s' providing '${spec.name}' found") | ||
| val biosphere = analysis.impactFactors.getEmissions().map { | ||
| eMapper.toEBioExchange(inputQty(it), it) | ||
| } | ||
|
|
||
| val impacts = analysis.impactFactors.getImpacts().map { | ||
| eMapper.toEImpact(inputQty(it), it) | ||
| } | ||
|
|
||
| return template.body.copy( | ||
| products = entryPoint.products.map { eMapper.toETechnoExchange(it)}, | ||
| inputs = inputs.map { ETechnoBlockEntry(it) }, | ||
| biosphere = biosphere.map { EBioBlockEntry(it) }, | ||
| impacts = impacts.map { EImpactBlockEntry(it)} | ||
| ) | ||
| } | ||
|
|
||
| private fun getTrace(template: EProcessTemplate<Q>, spec: EProductSpec<Q>): EvaluationTrace<Q> { | ||
| val arguments = template.params.plus(spec.fromProcess?.arguments ?: emptyMap()) | ||
| val newAnnotations = template.annotations.filter { it != ProcessAnnotation.CACHED }.toSet() | ||
| val newTemplate = template.copy(annotations = newAnnotations) | ||
|
|
||
| val newSymbolTable = symbolTable.copy(processTemplates = symbolTable.processTemplates.override( | ||
| ProcessKey(newTemplate.body.name, newTemplate.body.labels.mapValues { it.value.value }), | ||
| newTemplate | ||
| )) | ||
| val evaluator = Evaluator(newSymbolTable, ops, sourceOps) | ||
|
|
||
| return evaluator.trace(newTemplate, arguments) | ||
| } | ||
|
|
||
| private fun inputQtyAnalysis( | ||
pevab marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| products: List<TechnoExchangeValue<Q>>, | ||
| impactFactors: ImpactFactorMatrix<Q, M> | ||
| ): (MatrixColumnIndex<Q>) -> QuantityValue<Q> { | ||
| return { inputPort: MatrixColumnIndex<Q> -> | ||
| with(QuantityValueOperations(ops)) { | ||
| products | ||
| .map { impactFactors.characterizationFactor(it.port(), inputPort) * it.quantity } | ||
| .reduce { a, b -> a + b } | ||
| } | ||
| candidate | ||
| } | ||
| } | ||
| } | ||
|
|
||
| class BareProcessResolver<Q, M>( | ||
| val symbolTable: SymbolTable<Q>, | ||
| val ops: Operations<Q, M>, | ||
| val sourceOps: DataSourceOperations<Q>, | ||
| ) : ProcessResolver<Q, M> { | ||
| private val reduceDataExpressions = Reduce(symbolTable, ops, sourceOps) | ||
| private val completeTerminals = CompleteTerminals(ops) | ||
|
|
||
| override fun resolve(template: EProcessTemplate<Q>, spec: EProductSpec<Q>): EProcess<Q> { | ||
| val arguments = template.params.plus(spec.fromProcess?.arguments ?: emptyMap()) | ||
| val expression = EProcessTemplateApplication(template, arguments) | ||
| return expression | ||
| .let(reduceDataExpressions::apply) | ||
| .let(completeTerminals::apply) | ||
| } | ||
| } | ||
38 changes: 38 additions & 0 deletions
38
core/src/main/kotlin/ch/kleis/lcaac/core/lang/resolver/ProcessTemplateResolver.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| package ch.kleis.lcaac.core.lang.resolver | ||
|
|
||
| import ch.kleis.lcaac.core.lang.SymbolTable | ||
| import ch.kleis.lcaac.core.lang.evaluator.EvaluatorException | ||
| import ch.kleis.lcaac.core.lang.expression.EProcessTemplate | ||
| import ch.kleis.lcaac.core.lang.expression.EProductSpec | ||
| import ch.kleis.lcaac.core.lang.expression.EStringLiteral | ||
|
|
||
| class ProcessTemplateResolver<Q>( | ||
| private val symbolTable: SymbolTable<Q> | ||
| ) { | ||
| fun resolve(spec: EProductSpec<Q>): EProcessTemplate<Q>? { | ||
| if (spec.fromProcess == null) { | ||
| val matches = symbolTable.getAllTemplatesByProductName(spec.name) | ||
| return when (matches.size) { | ||
| 0 -> null | ||
| 1 -> matches.first() | ||
| else -> throw EvaluatorException("more than one processes found providing ${spec.name}") | ||
| } | ||
| } | ||
|
|
||
| val name = spec.fromProcess.name | ||
| val labels = spec.fromProcess.matchLabels.elements.mapValues { | ||
| when (val v = it.value) { | ||
| is EStringLiteral -> v.value | ||
| else -> throw EvaluatorException("$v is not a valid label value") | ||
| } | ||
| } | ||
| return symbolTable.getTemplate(name, labels)?.let { candidate -> | ||
| val providedProducts = candidate.body.products.map { it.product.name } | ||
| if (!providedProducts.contains(spec.name)) { | ||
| val s = if (labels.isEmpty()) name else "$name$labels" | ||
| throw EvaluatorException("no process '$s' providing '${spec.name}' found") | ||
| } | ||
| candidate | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.