Skip to content

Commit

Permalink
fix(roundabout): disabled condition (#1078)
Browse files Browse the repository at this point in the history
  • Loading branch information
nsenave authored Jul 15, 2024
1 parent fd68517 commit 3daa426
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 7 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ tasks.withType(JavaCompile).configureEach {

allprojects {
group = 'fr.insee.eno'
version = '3.23.6'
version = '3.23.7'
}

subprojects {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@

import fr.insee.eno.core.exceptions.technical.MappingException;
import fr.insee.eno.core.model.EnoQuestionnaire;
import fr.insee.eno.core.model.navigation.Filter;
import fr.insee.eno.core.model.navigation.Loop;
import fr.insee.eno.core.model.sequence.ItemReference;
import fr.insee.eno.core.model.sequence.RoundaboutSequence;
import fr.insee.eno.core.processing.ProcessingStep;
import fr.insee.eno.core.utils.VtlSyntaxUtils;

import java.util.Optional;

/**
* Temporary processing step to mark filters that correspond to a roundabout filter.
* This processing also inverts the roundabout occurrence filter expression (which is in the opposite way in DDI
* compared to Pogues and Lunatic).
* @see fr.insee.eno.core.model.navigation.Filter
*/
public class DDIMarkRoundaboutFilters implements ProcessingStep<EnoQuestionnaire> {
Expand All @@ -29,7 +33,10 @@ private static void markRoundaboutFilter(EnoQuestionnaire enoQuestionnaire, Roun
return;
enoQuestionnaire.getFilters().stream()
.filter(enoFilter -> itemReference.getId().equals(enoFilter.getId()))
.forEach(enoFilter -> enoFilter.setRoundaboutFilter(true));
.forEach(enoFilter -> {
invertFilterExpression(enoFilter);
enoFilter.setRoundaboutFilter(true);
});
}

private static Loop findRoundaboutLoop(EnoQuestionnaire enoQuestionnaire, RoundaboutSequence roundaboutSequence) {
Expand All @@ -42,4 +49,11 @@ private static Loop findRoundaboutLoop(EnoQuestionnaire enoQuestionnaire, Rounda
return enoLoop.get();
}

private static void invertFilterExpression(Filter enoFilter) {
if (enoFilter.getExpression() == null)
throw new MappingException("Eno filter object '" + enoFilter.getId() + "' has no expression.");
String stringExpression = enoFilter.getExpression().getValue();
enoFilter.getExpression().setValue(VtlSyntaxUtils.invertBooleanExpression(stringExpression));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,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
15 changes: 15 additions & 0 deletions eno-core/src/main/java/fr/insee/eno/core/utils/VtlSyntaxUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,19 @@ public static String concatenateStrings(String vtlString1, String vtlString2) {
return vtlString1 + " " + VTL_CONCATENATION_OPERATOR + " " + vtlString2;
}

/**
* Inverts the given expression (which is supposed to be a VTL expression that returns a boolean)
* by adding a 'not()' around it, or eventually removing one if the entire expression is in a "not".
* @param expression VTL boolean expression.
* @return The inverted VTL expression.
*/
public static String invertBooleanExpression(String expression) {
String trimmed = expression.trim();
// If the whole expression is within a not(), remove it
if (trimmed.startsWith("not(") && trimmed.endsWith(")"))
return trimmed.substring(4, trimmed.length() - 1);
// Otherwise, add a not() around the expression
return "not(" + trimmed + ")";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import fr.insee.eno.core.exceptions.business.DDIParsingException;
import fr.insee.eno.core.mappers.DDIMapper;
import fr.insee.eno.core.model.EnoQuestionnaire;
import fr.insee.eno.core.model.navigation.Filter;
import fr.insee.eno.core.serialize.DDIDeserializer;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.*;

class DDIMarkRoundaboutFiltersTest {

Expand All @@ -25,7 +25,9 @@ void mapDDIWithRoundabout() throws DDIParsingException {
// Then
// this questionnaire has no filters except the "occurrence" filter added for the roundabout
assertEquals(1, enoQuestionnaire.getFilters().size());
assertTrue(enoQuestionnaire.getFilters().getFirst().isRoundaboutFilter());
Filter enoFilter = enoQuestionnaire.getFilters().getFirst();
assertTrue(enoFilter.isRoundaboutFilter());
assertFalse(enoFilter.getExpression().getValue().startsWith("not"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
import java.util.Optional;

import static fr.insee.lunatic.model.flat.ComponentTypeEnum.*;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class LunaticRoundaboutLoopsTest {
Expand Down Expand Up @@ -81,7 +82,7 @@ void roundaboutItem() {
assertEquals("\"Occurrence description of \" || FIRST_NAME",
roundaboutItem.getDescription().getValue().stripTrailing());
assertEquals(LabelTypeEnum.VTL_MD, roundaboutItem.getDescription().getType());
assertEquals("not(FIRST_NAME <> FIRST_NAME_REF)",
assertEquals("FIRST_NAME <> FIRST_NAME_REF",
roundaboutItem.getDisabled().getValue().stripTrailing());
assertEquals(LabelTypeEnum.VTL, roundaboutItem.getDisabled().getType());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package fr.insee.eno.core.utils;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

class VtlSyntaxUtilsTest {

@Test
void invertExpressions() {
assertEquals("FOO", VtlSyntaxUtils.invertBooleanExpression("not(FOO)"));
assertEquals("not(FOO)", VtlSyntaxUtils.invertBooleanExpression("FOO"));
assertEquals("not(FOO)", VtlSyntaxUtils.invertBooleanExpression("not(not(FOO))"));
assertEquals("FOO = 1", VtlSyntaxUtils.invertBooleanExpression("not(FOO = 1)"));
assertEquals("not(FOO = 1)", VtlSyntaxUtils.invertBooleanExpression("FOO = 1"));
}

}

0 comments on commit 3daa426

Please sign in to comment.