-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: generate description for lunatic input numbers (#1004)
- Loading branch information
Showing
5 changed files
with
151 additions
and
10 deletions.
There are no files selected for viewing
This file contains 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 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 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
67 changes: 67 additions & 0 deletions
67
...in/java/fr/insee/eno/core/processing/out/steps/lunatic/LunaticInputNumberDescription.java
This file contains 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,67 @@ | ||
package fr.insee.eno.core.processing.out.steps.lunatic; | ||
|
||
import fr.insee.eno.core.parameter.EnoParameters; | ||
import fr.insee.eno.core.processing.ProcessingStep; | ||
import fr.insee.lunatic.model.flat.*; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* Generate a description for input number components. | ||
* Note: this processing must be called before wrapping components in Question components. | ||
*/ | ||
public class LunaticInputNumberDescription implements ProcessingStep<Questionnaire> { | ||
|
||
private static final Map<EnoParameters.Language, String> INPUT_NUMBER_DESCRIPTION_CANVAS = Map.of( | ||
EnoParameters.Language.FR, "Format attendu : un nombre%s entre %s et %s", | ||
EnoParameters.Language.EN, "Expected format: a number%s between %s and %s"); | ||
private static final Map<EnoParameters.Language, String> UNIT_DESCRIPTION_PREFIX = Map.of( | ||
EnoParameters.Language.FR, " en ", | ||
EnoParameters.Language.EN, " in "); | ||
private static final Map<EnoParameters.Language, String> NUMBER_COMMA = Map.of( | ||
EnoParameters.Language.FR, ",", | ||
EnoParameters.Language.EN, "."); | ||
|
||
private final EnoParameters.Language language; | ||
|
||
public LunaticInputNumberDescription(EnoParameters.Language language) { | ||
this.language = language; | ||
} | ||
|
||
@Override | ||
public void apply(Questionnaire lunaticQuestionnaire) { | ||
// | ||
generateInputNumberDescription(lunaticQuestionnaire.getComponents()); | ||
// | ||
lunaticQuestionnaire.getComponents().stream() | ||
.filter(Loop.class::isInstance).map(Loop.class::cast) | ||
.map(Loop::getComponents) | ||
.forEach(this::generateInputNumberDescription); | ||
} | ||
|
||
private void generateInputNumberDescription(List<ComponentType> lunaticComponents) { | ||
lunaticComponents.stream() | ||
.filter(InputNumber.class::isInstance).map(InputNumber.class::cast) | ||
.forEach(this::generateInputNumberDescription); | ||
} | ||
|
||
private void generateInputNumberDescription(InputNumber inputNumber) { | ||
// | ||
String unit = inputNumber.getUnit(); | ||
int decimals = inputNumber.getDecimals() != null ? inputNumber.getDecimals().intValue() : 0; | ||
int minValue = inputNumber.getMin().intValue(); | ||
int maxValue = inputNumber.getMax().intValue(); | ||
// | ||
String unitDescription = unit != null ? UNIT_DESCRIPTION_PREFIX.get(language) + unit : ""; | ||
String afterComma = decimals > 0 ? NUMBER_COMMA.get(language) + "0".repeat(decimals) : ""; | ||
String generatedDescription = String.format(INPUT_NUMBER_DESCRIPTION_CANVAS.get(language), | ||
unitDescription, minValue + afterComma, maxValue + afterComma); | ||
// | ||
LabelType description = new LabelType(); | ||
description.setValue(generatedDescription); | ||
description.setType(LabelTypeEnum.VTL_MD); | ||
inputNumber.setDescription(description); | ||
} | ||
|
||
} |
67 changes: 67 additions & 0 deletions
67
...ava/fr/insee/eno/core/processing/out/steps/lunatic/LunaticInputNumberDescriptionTest.java
This file contains 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,67 @@ | ||
package fr.insee.eno.core.processing.out.steps.lunatic; | ||
|
||
import fr.insee.eno.core.parameter.EnoParameters; | ||
import fr.insee.lunatic.model.flat.InputNumber; | ||
import fr.insee.lunatic.model.flat.Questionnaire; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.math.BigInteger; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
class LunaticInputNumberDescriptionTest { | ||
|
||
@Test | ||
void noUnitNoDecimals() { | ||
// | ||
Questionnaire lunaticQuestionnaire = new Questionnaire(); | ||
InputNumber inputNumber = new InputNumber(); | ||
inputNumber.setMin(0d); | ||
inputNumber.setMax(100d); | ||
inputNumber.setDecimals(BigInteger.ZERO); | ||
lunaticQuestionnaire.getComponents().add(inputNumber); | ||
// | ||
LunaticInputNumberDescription processing = new LunaticInputNumberDescription(EnoParameters.Language.FR); | ||
processing.apply(lunaticQuestionnaire); | ||
// | ||
assertEquals("Format attendu : un nombre entre 0 et 100", | ||
lunaticQuestionnaire.getComponents().getFirst().getDescription().getValue()); | ||
} | ||
|
||
@Test | ||
void withUnitAndDecimal() { | ||
// | ||
Questionnaire lunaticQuestionnaire = new Questionnaire(); | ||
InputNumber inputNumber = new InputNumber(); | ||
inputNumber.setMin(0d); | ||
inputNumber.setMax(10d); | ||
inputNumber.setDecimals(BigInteger.ONE); | ||
inputNumber.setUnit("€"); | ||
lunaticQuestionnaire.getComponents().add(inputNumber); | ||
// | ||
LunaticInputNumberDescription processing = new LunaticInputNumberDescription(EnoParameters.Language.FR); | ||
processing.apply(lunaticQuestionnaire); | ||
// | ||
assertEquals("Format attendu : un nombre en € entre 0,0 et 10,0", | ||
lunaticQuestionnaire.getComponents().getFirst().getDescription().getValue()); | ||
} | ||
|
||
@Test | ||
void withUnitNoDecimal() { | ||
// | ||
Questionnaire lunaticQuestionnaire = new Questionnaire(); | ||
InputNumber inputNumber = new InputNumber(); | ||
inputNumber.setMin(20d); | ||
inputNumber.setMax(1000d); | ||
inputNumber.setDecimals(BigInteger.ZERO); | ||
inputNumber.setUnit("k€"); | ||
lunaticQuestionnaire.getComponents().add(inputNumber); | ||
// | ||
LunaticInputNumberDescription processing = new LunaticInputNumberDescription(EnoParameters.Language.FR); | ||
processing.apply(lunaticQuestionnaire); | ||
// | ||
assertEquals("Format attendu : un nombre en k€ entre 20 et 1000", | ||
lunaticQuestionnaire.getComponents().getFirst().getDescription().getValue()); | ||
} | ||
|
||
} |