Skip to content

Commit

Permalink
Merge pull request #10 from kleis-technology/e2e
Browse files Browse the repository at this point in the history
e2e
  • Loading branch information
pevab authored Jan 21, 2024
2 parents 3d90b93 + 4240b6c commit 1717c8d
Show file tree
Hide file tree
Showing 12 changed files with 353 additions and 2 deletions.
7 changes: 6 additions & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,13 @@ jobs:
java-version: 17
cache: gradle

- name: Run Tests
- name: Run Kotlin Tests
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: ./gradlew test

- name: Run LCAAC Tests
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: ./tutorials/run.sh

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ The package `cli` contains the code for the command-line interface.

For users
- [Walkthrough](https://lca-as-code.com/book)
- Code samples *\[coming soon\]*
- [Code samples](./tutorials/README.md)

For developers
- Core concepts *\[coming soon\]*
Expand Down
27 changes: 27 additions & 0 deletions tutorials/01-basics/01-getting-started/main.lca
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
process bake {
products {
1 kg bread
}
inputs {
1 kg flour
}
}

process mill {
products {
1 kg flour
}
inputs {
2 kg wheat
}
}

test bake {
given {
1 kg bread
}
assert {
wheat between 2 kg and 2 kg
}
}

31 changes: 31 additions & 0 deletions tutorials/01-basics/02-biosphere/main.lca
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
process bake {
products {
1 kg bread
}
inputs {
1 kg flour
}
emissions {
1 m3 wood_smoke (compartment="air")
}
}

process mill {
products {
1 kg flour
}
inputs {
2 kg wheat
}
}

test bake {
given {
1 kg bread
}
assert {
wheat between 2 kg and 2 kg
wood_smoke between 1 m3 and 1 m3 // TODO: Reference should include compartment
}
}

41 changes: 41 additions & 0 deletions tutorials/01-basics/03-impacts/main.lca
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
process bake {
products {
1 kg bread
}
inputs {
1 kg flour
}
emissions {
1 m3 wood_smoke (compartment="air")
}
}

substance wood_smoke {
name = "Wood fire Smoke"
type = Emission
compartment = "air"
// optional: sub_compartment = "low altitude"
reference_unit = m3
impacts {
1 kg_CO2_Eq climate_change
}
}

process mill {
products {
1 kg flour
}
inputs {
2 kg wheat
}
}

test bake {
given {
1 kg bread
}
assert {
climate_change between 1 kg_CO2_Eq and 1 kg_CO2_Eq
}
}

41 changes: 41 additions & 0 deletions tutorials/02-advanced/01-parametrized-process/main.lca
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
process bake {
params {
salt_amount = 30 g
}
products {
1 kg bread
}
inputs {
1 kg flour
salt_amount salt
}
}

test bake {
given {
1 kg bread from bake(salt_amount = 10 g)
}
assert {
salt between 10 g and 10 g
}
}


process produce_sandwich {
products {
5 p sandwich
}
inputs {
1 kg bread from bake (salt_amount = 20 g)
}
}

test sandwich {
given {
1 p sandwich from produce_sandwich // TODO: separate tests and process references
}
assert {
flour between 0.2 kg and 0.2 kg
salt between 4 g and 4 g
}
}
30 changes: 30 additions & 0 deletions tutorials/02-advanced/02-variables/main.lca
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
process consumption {
params {
budget = 200 u
}
products {
1 u consumer
}
variables {
a_choco = 2 u
p_choco = 4 u
a_candy = 1 u
p_candy = 2 u
q_choco = budget * a_choco / p_choco
q_candy = budget * a_candy / p_candy
}
inputs {
q_choco chocolate_bar
q_candy candy
}
}

test consumption {
given {
1 u consumer
}
assert {
chocolate_bar between 100 u and 100 u
candy between 100 u and 100 u
}
}
23 changes: 23 additions & 0 deletions tutorials/02-advanced/03-units/01-unit-aliases.lca
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
unit oil_barrel {
symbol = "oil_barrel"
alias_for = 1700 kWh
}

process oil_burning {
products {
1700 kWh electricity
}
impacts {
1 p physical_oil_barrel
}
}

test unit_conversion {
given {
1 oil_barrel electricity from oil_burning
}
assert {
physical_oil_barrel between 1 p and 1 p
}
}

48 changes: 48 additions & 0 deletions tutorials/02-advanced/03-units/02-custom-dimensions.lca
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
unit night {
symbol = "night"
dimension = "night"
}

unit guest {
symbol = "guest"
dimension = "guest"
}

unit dollar {
symbol = "dollar"
dimension = "dollar"
}


process hotel {
products {
1 night night
}
impacts {
1 piece night_stay
}
}

process customer {
variables {
budget = 200 dollar
price_per_guest_night = 100 dollar / (guest * night)
nb_guests = 2 guest

/*
The following formula has the wrong dimension.
Assessing the process should yield a dimension error.
The correct formula is
nb_nights = budget / (price_per_guest_night * nb_guests)
*/
nb_nights = budget / price_per_guest_night * nb_guests
}

products {
1 piece customer
}

inputs {
nb_nights night from hotel
}
}
54 changes: 54 additions & 0 deletions tutorials/02-advanced/04-labels/main.lca
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
process electric_vehicle {
params {
geo = "switzerland" // or "france"
}
products {
1 person * km transport
}
inputs {
1 kWh electricity from electricity_production match (geo = geo)
}
}

process electricity_production {
labels {
geo = "switzerland"
}
products {
1 kWh electricity
}
impacts {
1 kg_CO2_Eq GWP
}
}

process electricity_production {
labels {
geo = "france"
}
products {
1 kWh electricity
}
impacts {
2 kg_CO2_Eq GWP
}
}

test electric_vehicle_ch {
given {
1 person * km transport from electric_vehicle(geo = "switzerland")
}
assert {
GWP between 1 kg_CO2_Eq and 1 kg_CO2_Eq
}
}

test electric_vehicle_fr {
given {
1 person * km transport from electric_vehicle(geo = "france")
}
assert {
GWP between 2 kg_CO2_Eq and 2 kg_CO2_Eq
}
}

14 changes: 14 additions & 0 deletions tutorials/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Tutorials

This folder contains multiple code samples covering the main LCAAC language features.
Each code sample takes the form a `.lca` file.
Use the [cli](../cli/README.md) to interact with the code.
Moreover, each code sample contains tests. You can run the tests manually, e.g.,
```bash
lcaac test -p 01-basics/01-getting-started
```

The script `run.sh` runs all the tests.
```bash
./run.sh
```
37 changes: 37 additions & 0 deletions tutorials/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env bash

set -euxo

export GIT_ROOT=$(git rev-parse --show-toplevel)

LCAAC_PATH=$GIT_ROOT/cli/build/install/lcaac/bin
TUTORIALS_PATH=$GIT_ROOT/tutorials

function setup() {
$GIT_ROOT/gradlew :cli:installDist
}

function lcaac() {
$LCAAC_PATH/lcaac $@
}

if ! [ -f $LCAAC_PATH/lcaac ]; then
setup
fi

# Check all lca tests
lcaac test -p $TUTORIALS_PATH/01-basics/01-getting-started
lcaac test -p $TUTORIALS_PATH/01-basics/02-biosphere
lcaac test -p $TUTORIALS_PATH/01-basics/03-impacts
lcaac test -p $TUTORIALS_PATH/02-advanced/01-parametrized-process
lcaac test -p $TUTORIALS_PATH/02-advanced/02-variables
lcaac test -p $TUTORIALS_PATH/02-advanced/03-units
lcaac test -p $TUTORIALS_PATH/02-advanced/04-labels

# Check custom dimensions tutorial
set -euo

# The following assessment is expected to fail.
if lcaac assess -p $TUTORIALS_PATH/02-advanced/03-units customer; then
exit 0
fi

0 comments on commit 1717c8d

Please sign in to comment.