Skip to content

Commit

Permalink
Fix sonar issues
Browse files Browse the repository at this point in the history
  • Loading branch information
NicoLaval committed Aug 8, 2023
1 parent 8904a10 commit 3c0b831
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import fr.insee.vtl.engine.exceptions.VtlRuntimeException;
import fr.insee.vtl.engine.visitors.expression.ExpressionVisitor;
import fr.insee.vtl.model.*;
import fr.insee.vtl.model.exceptions.VtlScriptException;
import fr.insee.vtl.parser.VtlBaseVisitor;
import fr.insee.vtl.parser.VtlParser;
import org.antlr.v4.runtime.tree.TerminalNode;
Expand Down Expand Up @@ -148,31 +149,31 @@ public Object visitDefHierarchical(VtlParser.DefHierarchicalContext ctx) {
// Mix variables and valuedomain. Information useless for now, find use case to do so
String variable = ctx.hierRuleSignature().IDENTIFIER().getText();

Set<Class> erCodeTypes = ctx.ruleClauseHierarchical().ruleItemHierarchical().stream().map(c -> {
Set<Class<?>> erCodeTypes = ctx.ruleClauseHierarchical().ruleItemHierarchical().stream().map(c -> {
VtlParser.ErCodeContext erCodeContext = c.erCode();
if (null == erCodeContext) return Object.class;
return expressionVisitor.visit(c.erCode()).getType();
}).collect(Collectors.toSet());
List<Class> filteredErCodeTypes = erCodeTypes.stream().filter(t -> !t.equals(Object.class)).collect(Collectors.toList());
List<Class<?>> filteredErCodeTypes = erCodeTypes.stream().filter(t -> !t.equals(Object.class)).collect(Collectors.toList());
if (filteredErCodeTypes.size() > 1) {
throw new VtlRuntimeException(
new InvalidArgumentException("Error codes of rules have different types", pos)
);
}
Class erCodeType = filteredErCodeTypes.isEmpty() ? String.class : filteredErCodeTypes.iterator().next();
Class<?> erCodeType = filteredErCodeTypes.isEmpty() ? String.class : filteredErCodeTypes.iterator().next();

Set<Class> erLevelTypes = ctx.ruleClauseHierarchical().ruleItemHierarchical().stream().map(c -> {
Set<Class<?>> erLevelTypes = ctx.ruleClauseHierarchical().ruleItemHierarchical().stream().map(c -> {
VtlParser.ErLevelContext erLevelContext = c.erLevel();
if (null == erLevelContext) return Object.class;
return expressionVisitor.visit(c.erLevel()).getType();
}).collect(Collectors.toSet());
List<Class> filteredErLevelTypes = erLevelTypes.stream().filter(t -> !t.equals(Object.class)).collect(Collectors.toList());
List<Class<?>> filteredErLevelTypes = erLevelTypes.stream().filter(t -> !t.equals(Object.class)).collect(Collectors.toList());
if (filteredErLevelTypes.size() > 1) {
throw new VtlRuntimeException(
new InvalidArgumentException("Error levels of rules have different types", pos)
);
}
Class erLevelType = filteredErLevelTypes.isEmpty() ? Long.class : filteredErLevelTypes.iterator().next();
Class<?> erLevelType = filteredErLevelTypes.isEmpty() ? Long.class : filteredErLevelTypes.iterator().next();

AtomicInteger index = new AtomicInteger();
List<HierarchicalRule> rules = ctx.ruleClauseHierarchical().ruleItemHierarchical()
Expand All @@ -196,81 +197,71 @@ public Object visitDefHierarchical(VtlParser.DefHierarchicalContext ctx) {
String rightCodeItem = circ.rightCodeItem.getText();
codeItems.add(rightCodeItem);
if (minus != null)
codeItemExpressionBuilder.append(" -" + rightCodeItem);
codeItemExpressionBuilder.append(" -").append(rightCodeItem);
// plus value or plus null & minus null mean plus
codeItemExpressionBuilder.append(" +" + rightCodeItem);
codeItemExpressionBuilder.append(" +").append(rightCodeItem);
});

String leftExpressionToEval = valueDomainValue;
String rightExpressionToEval = codeItemExpressionBuilder.toString();
String expressionToEval = "bool_var := " +
leftExpressionToEval + " " +
valueDomainValue + " " +
comparisonOperandContext.getText() + " " +
rightExpressionToEval + ";";

ResolvableExpression leftExpression = ResolvableExpression.withType(Double.class)
.withPosition(pos)
.using(context -> {
Bindings bindings = new SimpleBindings(context);
bindings.forEach((k, v) -> {
engine.getContext().setAttribute(k, v, ScriptContext.ENGINE_SCOPE);
});
bindings.forEach((k, v) -> engine.getContext().setAttribute(k, v, ScriptContext.ENGINE_SCOPE));
try {
engine.eval("left := " + leftExpressionToEval + ";");
engine.eval("left := " + valueDomainValue + ";");
Object left = engine.getContext().getAttribute("left");
engine.getContext().removeAttribute("left", ScriptContext.ENGINE_SCOPE);
bindings.keySet().forEach(k -> {
engine.getContext().removeAttribute(k, ScriptContext.ENGINE_SCOPE);
});
bindings.keySet().forEach(k -> engine.getContext().removeAttribute(k, ScriptContext.ENGINE_SCOPE));
if (left.getClass().isAssignableFrom(Double.class)) {
return (Double) left;
}
return ((Long) left).doubleValue();
} catch (ScriptException e) {
throw new RuntimeException(e);
throw new VtlRuntimeException(new VtlScriptException(
"right hierarchical rule has to return long or double", pos));
}
});

ResolvableExpression rightExpression = ResolvableExpression.withType(Double.class)
.withPosition(pos)
.using(context -> {
Bindings bindings = new SimpleBindings(context);
bindings.forEach((k, v) -> {
engine.getContext().setAttribute(k, v, ScriptContext.ENGINE_SCOPE);
});
bindings.forEach((k, v) -> engine.getContext().setAttribute(k, v, ScriptContext.ENGINE_SCOPE));
try {
engine.eval("right := " + rightExpressionToEval + ";");
Object right = engine.getContext().getAttribute("right");
engine.getContext().removeAttribute("right", ScriptContext.ENGINE_SCOPE);
bindings.keySet().forEach(k -> {
engine.getContext().removeAttribute(k, ScriptContext.ENGINE_SCOPE);
});
bindings.keySet().forEach(k -> engine.getContext().removeAttribute(k, ScriptContext.ENGINE_SCOPE));
if (right.getClass().isAssignableFrom(Double.class)) {
return (Double) right;
}
return ((Long) right).doubleValue();
} catch (ScriptException e) {
throw new RuntimeException(e);
throw new VtlRuntimeException(new VtlScriptException(
"right hierarchical rule has to return long or double", pos));
}
});

ResolvableExpression expression = ResolvableExpression.withType(Boolean.class)
.withPosition(pos)
.using(context -> {
Bindings bindings = new SimpleBindings(context);
bindings.forEach((k, v) -> {
engine.getContext().setAttribute(k, v, ScriptContext.ENGINE_SCOPE);
});
bindings.forEach((k, v) -> engine.getContext().setAttribute(k, v, ScriptContext.ENGINE_SCOPE));
try {
engine.eval(expressionToEval);
Boolean boolVar = (Boolean) engine.getContext().getAttribute("bool_var");
engine.getContext().removeAttribute("bool_var", ScriptContext.ENGINE_SCOPE);
bindings.keySet().forEach(k -> {
engine.getContext().removeAttribute(k, ScriptContext.ENGINE_SCOPE);
});
bindings.keySet().forEach(k -> engine.getContext().removeAttribute(k, ScriptContext.ENGINE_SCOPE));
return boolVar;
} catch (ScriptException e) {
throw new RuntimeException(e);
throw new VtlRuntimeException(new VtlScriptException(
"hierarchical rule has to return boolean", pos));
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public void checkHierarchy() throws ScriptException {

engine.eval(hierarchicalRulesetDef);
HierarchicalRuleset hr1 = (HierarchicalRuleset) engine.getContext().getAttribute("HR_1");
assertThat(hr1.getRules().size()).isEqualTo(11);
assertThat(hr1.getRules()).hasSize(11);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ public class HierarchicalRuleset {

private final List<HierarchicalRule> rules;
private final String variable;
private final Class errorCodeType;
private final Class errorLevelType;
private final Class<?> errorCodeType;
private final Class<?> errorLevelType;

/**
* Constructor.
Expand All @@ -26,7 +26,7 @@ public class HierarchicalRuleset {
* @param errorLevelType Type of errorlevel
*/
public HierarchicalRuleset(List<HierarchicalRule> rules, String variable,
Class errorCodeType, Class errorLevelType) {
Class<?> errorCodeType, Class<?> errorLevelType) {
this.rules = rules;
this.variable = variable;
this.errorCodeType = errorCodeType;
Expand All @@ -41,11 +41,11 @@ public String getVariable() {
return variable;
}

public Class getErrorCodeType() {
public Class<?> getErrorCodeType() {
return errorCodeType;
}

public Class getErrorLevelType() {
public Class<?> getErrorLevelType() {
return errorLevelType;
}

Expand Down
Loading

0 comments on commit 3c0b831

Please sign in to comment.