Skip to content

Commit

Permalink
fix: roundabout controls (#1150)
Browse files Browse the repository at this point in the history
* fix(roundabout): reverse controls in lunatic

* fix(roundabout): locked property when controls are present

* chore: bump version
  • Loading branch information
nsenave committed Nov 8, 2024
1 parent 4ecb932 commit 608fcac
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 5 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ java {

allprojects {
group = "fr.insee.eno"
version = "3.28.0-SNAPSHOT"
version = "3.28.0-SNAPSHOT.1"
}

subprojects {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package fr.insee.eno.core.model.sequence;

import fr.insee.ddi.lifecycle33.datacollection.ComputationItemType;
import fr.insee.ddi.lifecycle33.datacollection.SequenceType;
import fr.insee.eno.core.annotations.Contexts.Context;
import fr.insee.eno.core.annotations.DDI;
import fr.insee.eno.core.model.navigation.Control;
import fr.insee.eno.core.parameter.Format;
import fr.insee.eno.core.reference.DDIIndex;
import lombok.Getter;
import lombok.Setter;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
* Special kind of sequence that describes a "roundabout".
Expand All @@ -22,14 +25,16 @@
@Context(format = Format.DDI, type = SequenceType.class)
public class RoundaboutSequence extends AbstractSequence {

private static final String DDI_LOCKED_CONTROL_TYPE = "roundabout-locked";

/** DDI reference of the loop.
* Note: mapped as the id of the first control construct reference. */
@DDI("getControlConstructReferenceArray(0).getIDArray(0).getStringValue()")
private String loopReference;

/** Boolean that describes if the completed occurrences should be locked or not.
* In DDI, this is modeled by the presence or not of a ComputationItem among the control construct references. */
@DDI("!getControlConstructReferenceList().?[#this.getTypeOfObject().toString() == 'ComputationItem'].isEmpty()")
@DDI("T(fr.insee.eno.core.model.sequence.RoundaboutSequence).ddiLockedProperty(#this, #index)")
private Boolean locked;

/**
Expand All @@ -41,4 +46,15 @@ public class RoundaboutSequence extends AbstractSequence {
*/
private List<Control> controls = new ArrayList<>();

public static boolean ddiLockedProperty(SequenceType ddiRoundaboutSequence, DDIIndex ddiIndex) {
return ddiRoundaboutSequence.getControlConstructReferenceList().stream()
.filter(reference -> "ComputationItem".equals(reference.getTypeOfObject().toString()))
.map(reference -> ddiIndex.get(reference.getIDArray(0).getStringValue()))
.filter(Objects::nonNull)
.filter(ComputationItemType.class::isInstance)
.map(ComputationItemType.class::cast)
.anyMatch(computationItem ->
DDI_LOCKED_CONTROL_TYPE.equals(computationItem.getTypeOfComputationItem().getStringValue()));
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fr.insee.eno.core.processing.out.steps.lunatic;

import fr.insee.eno.core.processing.ProcessingStep;
import fr.insee.eno.core.utils.VtlSyntaxUtils;
import fr.insee.lunatic.model.flat.*;

import java.util.Collection;
Expand All @@ -27,12 +28,13 @@ private void processComponents(List<ComponentType> components) {
.filter(control -> control.getTypeOfControl().equals(ControlTypeEnum.CONSISTENCY))
.forEach(control -> {
LabelType label = control.getControl();
label.setValue("not(" + label.getValue() + ")");
label.setValue(VtlSyntaxUtils.invertBooleanExpression(label.getValue()));
});

components.stream()
.filter(ComponentNestingType.class::isInstance)
.map(ComponentNestingType.class::cast)
.forEach(componentNesting -> processComponents(componentNesting.getComponents()));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import fr.insee.eno.core.model.navigation.LinkedLoop;
import fr.insee.eno.core.model.sequence.RoundaboutSequence;
import fr.insee.eno.core.processing.ProcessingStep;
import fr.insee.eno.core.utils.VtlSyntaxUtils;
import fr.insee.lunatic.model.flat.*;
import fr.insee.lunatic.model.flat.variable.CollectedVariableType;
import fr.insee.lunatic.model.flat.variable.CollectedVariableValues;
Expand Down Expand Up @@ -167,6 +168,9 @@ private Roundabout createRoundabout(RoundaboutSequence roundaboutSequence, Loop
ControlType lunaticControl = new ControlType();
lunaticMapper.mapEnoObject(enoControl, lunaticControl);
lunaticRoundabout.getControls().add(lunaticControl);
// Control expressions have to be inverted in Lunatic
lunaticControl.getControl().setValue(
VtlSyntaxUtils.invertBooleanExpression(lunaticControl.getControl().getValue()));
});
//
Roundabout.Item lunaticRoundaboutItem = new Roundabout.Item();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,11 @@ void ddiToLunatic() throws DDIParsingException {
roundabout = roundabouts.getFirst();
}

@Test
void lockedPropertyTest() {
assertFalse(roundabout.getLocked());
}

@Test
void controlsCount() {
assertEquals(2, roundabout.getControls().size());
Expand All @@ -221,7 +226,7 @@ void roundaboutControlTest() {
.toList();
assertEquals(1, roundaboutControls.size());
ControlType roundaboutControl = roundaboutControls.getFirst();
assertEquals("count(Q2) < 3", roundaboutControl.getControl().getValue());
assertEquals("not(count(Q2) < 3)", roundaboutControl.getControl().getValue());
assertEquals(LabelTypeEnum.VTL, roundaboutControl.getControl().getType());
assertEquals("\"There is less than 3 answers in the roundabout.\"",
roundaboutControl.getErrorMessage().getValue());
Expand All @@ -237,7 +242,7 @@ void occurrenceControlTest() {
.toList();
assertEquals(1, occurrenceControls.size());
ControlType occurrenceControl = occurrenceControls.getFirst();
assertEquals("Q2 = \"bar\"", occurrenceControl.getControl().getValue());
assertEquals("not(Q2 = \"bar\")", occurrenceControl.getControl().getValue());
assertEquals(LabelTypeEnum.VTL, occurrenceControl.getControl().getType());
assertEquals("\"Occurrence with question 1 = '\" || Q1 || \"' answered 'bar' at question 2.\"",
occurrenceControl.getErrorMessage().getValue().stripTrailing());
Expand Down

0 comments on commit 608fcac

Please sign in to comment.