|
| 1 | +package fr.insee.vtl.engine; |
| 2 | + |
| 3 | +import fr.insee.vtl.engine.utils.dag.DAGBuilder; |
| 4 | +import fr.insee.vtl.engine.utils.dag.DAGStatement; |
| 5 | +import fr.insee.vtl.engine.visitors.DAGBuildingVisitor; |
| 6 | +import fr.insee.vtl.model.exceptions.VtlMultiErrorScriptException; |
| 7 | +import fr.insee.vtl.model.exceptions.VtlScriptException; |
| 8 | +import fr.insee.vtl.parser.VtlParser; |
| 9 | +import java.util.*; |
| 10 | +import java.util.stream.Collectors; |
| 11 | +import java.util.stream.Stream; |
| 12 | +import org.antlr.v4.runtime.ParserRuleContext; |
| 13 | + |
| 14 | +/** |
| 15 | + * Class for preprocessing the VTL script for resolving script errors and reordering statements |
| 16 | + * based on the variables dependency order |
| 17 | + */ |
| 18 | +public class VtlSyntaxPreprocessor { |
| 19 | + |
| 20 | + private final VtlParser.StartContext startContext; |
| 21 | + private final Set<String> bindingVarIds; |
| 22 | + private final List<DAGStatement> unsortedStatements; |
| 23 | + |
| 24 | + public VtlSyntaxPreprocessor(VtlParser.StartContext startContext, Set<String> bindingVarIds) { |
| 25 | + this.startContext = startContext; |
| 26 | + this.bindingVarIds = bindingVarIds; |
| 27 | + DAGBuildingVisitor visitor = new DAGBuildingVisitor(); |
| 28 | + this.unsortedStatements = visitor.visit(startContext); |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Method to check for multiple assignments of variables and reorder the VTL script according to |
| 33 | + * the variables dependency order as defined by the VTL standard. |
| 34 | + * |
| 35 | + * @return reordered VTL |
| 36 | + * @throws VtlScriptException when variables are assigned multiple times |
| 37 | + */ |
| 38 | + public VtlParser.StartContext checkForMultipleAssignmentsAndReorderScript() |
| 39 | + throws VtlScriptException { |
| 40 | + checkForMultipleAssignments(); |
| 41 | + |
| 42 | + // Create DAG & topological sort |
| 43 | + DAGBuilder dagBuilder = new DAGBuilder(unsortedStatements, startContext); |
| 44 | + List<DAGStatement> sortedStatements = dagBuilder.topologicalSortedStatements(); |
| 45 | + |
| 46 | + VtlParser.StartContext startReordered = |
| 47 | + new VtlParser.StartContext( |
| 48 | + (ParserRuleContext) startContext.getRuleContext(), startContext.invokingState); |
| 49 | + |
| 50 | + // Build a set of unsorted indices that need reordering |
| 51 | + Set<Integer> unsortedIndices = |
| 52 | + sortedStatements.stream().map(DAGStatement::unsortedIndex).collect(Collectors.toSet()); |
| 53 | + |
| 54 | + int sortedIndex = 0; |
| 55 | + for (int i = 0; i < startContext.getChildCount(); i++) { |
| 56 | + if (unsortedIndices.contains(i)) { |
| 57 | + DAGStatement stmt = sortedStatements.get(sortedIndex++); |
| 58 | + startReordered.addAnyChild(startContext.getChild(stmt.unsortedIndex())); |
| 59 | + } else { |
| 60 | + startReordered.addAnyChild(startContext.getChild(i)); |
| 61 | + } |
| 62 | + } |
| 63 | + return startReordered; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Method to check for multiple assignments of variables. |
| 68 | + * |
| 69 | + * @throws VtlScriptException when variables are assigned multiple times. |
| 70 | + */ |
| 71 | + public void checkForMultipleAssignments() throws VtlScriptException { |
| 72 | + List<DAGStatement> bindingPseudoStatements = |
| 73 | + bindingVarIds.stream() |
| 74 | + .map( |
| 75 | + bindingVarId -> |
| 76 | + new DAGStatement( |
| 77 | + DAGStatement.PSEUDO_BINDING_POSITION, |
| 78 | + new DAGStatement.Identifier( |
| 79 | + DAGStatement.Identifier.Type.VARIABLE, bindingVarId), |
| 80 | + Set.of())) |
| 81 | + .toList(); |
| 82 | + Map<DAGStatement.Identifier, List<DAGStatement>> groupedByProducedIdentifier = |
| 83 | + Stream.concat(bindingPseudoStatements.stream(), unsortedStatements.stream()) |
| 84 | + .collect(Collectors.groupingBy(DAGStatement::produces)); |
| 85 | + |
| 86 | + List<VtlScriptException> multiProducedExceptions = |
| 87 | + groupedByProducedIdentifier.entrySet().stream() |
| 88 | + .filter(produced -> produced.getValue().size() > 1) |
| 89 | + .map( |
| 90 | + multiProduced -> |
| 91 | + buildScriptExceptionFromMultipleAssignment( |
| 92 | + multiProduced.getKey(), multiProduced.getValue())) |
| 93 | + .toList(); |
| 94 | + |
| 95 | + if (!multiProducedExceptions.isEmpty()) { |
| 96 | + throw VtlMultiErrorScriptException.of( |
| 97 | + multiProducedExceptions.toArray(new VtlScriptException[] {})); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + private VtlScriptException buildScriptExceptionFromMultipleAssignment( |
| 102 | + DAGStatement.Identifier identifier, List<DAGStatement> statements) { |
| 103 | + final List<DAGStatement> statementsWithoutBinding = |
| 104 | + statements.stream() |
| 105 | + .filter(statement -> statement.unsortedIndex() != DAGStatement.PSEUDO_BINDING_POSITION) |
| 106 | + .toList(); |
| 107 | + |
| 108 | + if (statementsWithoutBinding.size() == 1) { |
| 109 | + return new VtlScriptException( |
| 110 | + "Dataset " |
| 111 | + + identifier.name() |
| 112 | + + " is part of the bindings and therefore cannot be assigned", |
| 113 | + statementsWithoutBinding.get(0).getPosition(startContext)); |
| 114 | + } |
| 115 | + |
| 116 | + return DAGStatement.buildMultiStatementExceptionUsingTheLastDAGStatementAsMainPosition( |
| 117 | + "Dataset " |
| 118 | + + identifier.name() |
| 119 | + + " has already been assigned" |
| 120 | + + (statements.size() == statementsWithoutBinding.size() |
| 121 | + ? "" |
| 122 | + : " and is part of the bindings and therefore cannot be assigned"), |
| 123 | + statementsWithoutBinding, |
| 124 | + startContext); |
| 125 | + } |
| 126 | +} |
0 commit comments