<commit_before>package no.ssb.vtl.script.visitors;

import no.ssb.vtl.model.Dataset;
import no.ssb.vtl.parser.VTLBaseVisitor;
import no.ssb.vtl.parser.VTLParser;
import no.ssb.vtl.script.operations.CheckSingleRuleOperation;

import javax.script.ScriptContext;
import java.util.Optional;
import java.util.function.Supplier;

import static com.google.common.base.Preconditions.*;
import static java.util.Optional.*;

public class CheckVisitor extends VTLBaseVisitor<Dataset> {

    private final ScriptContext context;
    private final RelationalVisitor relationalVisitor;

    public CheckVisitor(ScriptContext context, RelationalVisitor relationalVisitor) {
        this.context = checkNotNull(context, "the context was null");
        this.relationalVisitor = relationalVisitor;
    }

    @Override
    public Dataset visitCheckExpression(VTLParser.CheckExpressionContext ctx) {
        VTLParser.CheckParamContext checkParamContext = ctx.checkParam();
        Dataset dataset = visit(checkParamContext.datasetExpression());

        Optional<CheckSingleRuleOperation.ComponentsToReturn> componentsToReturnEnum = getComponentsToReturn(checkParamContext.checkColumns());
        Optional<CheckSingleRuleOperation.RowsToReturn> rowsToReturn = getRowsToReturn(checkParamContext.checkRows());

        Optional<String> errorCode = getErrorCode(checkParamContext);
        Optional<Integer> errorLevel = getErrorLevel(checkParamContext);

        return new CheckSingleRuleOperation(dataset,
                rowsToReturn,
                componentsToReturnEnum,
                errorCode,
                errorLevel);
    }

    @Override
    public Dataset visitVariableRef(VTLParser.VariableRefContext ctx) {
        return (Dataset) context.getAttribute(ctx.getText());
    }

    @Override
    public Dataset visitRelationalExpression(VTLParser.RelationalExpressionContext ctx) {
        Supplier<Dataset> datasetSupplier = relationalVisitor.visit(ctx);
        return datasetSupplier.get();
    }


    private Optional<Integer> getErrorLevel(VTLParser.CheckParamContext checkParamContext) {
        Integer errorLevel = null;
        if (checkParamContext.errorLevel() != null) {
            errorLevel = Integer.valueOf(checkParamContext.errorLevel().getText());
        }
        return Optional.ofNullable(errorLevel);
    }

    private Optional<String> getErrorCode(VTLParser.CheckParamContext checkParamContext) {
        String errorCode = null;
        if (checkParamContext.errorCode() != null) {
             errorCode = checkParamContext.errorCode().getText();
        }
        return Optional.ofNullable(errorCode);
    }


    private Optional<CheckSingleRuleOperation.ComponentsToReturn> getComponentsToReturn(VTLParser.CheckColumnsContext checkColumnsContext) {
        Optional<String> componentsToReturn = ofNullable(checkColumnsContext).map(VTLParser.CheckColumnsContext::getText);
        CheckSingleRuleOperation.ComponentsToReturn componentsToReturnEnum = null;
        if (componentsToReturn.isPresent()) {
            switch (componentsToReturn.get()) {
                case "measures":
                    componentsToReturnEnum = CheckSingleRuleOperation.ComponentsToReturn.MEASURES;
                    break;
                case "MEASURE":
                    componentsToReturnEnum = CheckSingleRuleOperation.ComponentsToReturn.MEASURES;
                    break;
                default:
                    throw new RuntimeException("unknown parameter value 'checkColumns' " + componentsToReturn.get());
            }
        }
        return Optional.ofNullable(componentsToReturnEnum);
    }

    private Optional<CheckSingleRuleOperation.RowsToReturn> getRowsToReturn(VTLParser.CheckRowsContext checkColumnsContext) {
        Optional<String> rowsToReturn = ofNullable(checkColumnsContext).map(VTLParser.CheckRowsContext::getText);
        CheckSingleRuleOperation.RowsToReturn rowsToReturnEnum = null;
        if (rowsToReturn.isPresent()) {
            switch (rowsToReturn.get()) {
                case "not_valid":
                    rowsToReturnEnum = CheckSingleRuleOperation.RowsToReturn.NOT_VALID;
                    break;
                case "valid":
                    rowsToReturnEnum = CheckSingleRuleOperation.RowsToReturn.VALID;
                    break;
                case "all":
                    rowsToReturnEnum = CheckSingleRuleOperation.RowsToReturn.ALL;
                    break;
                default:
                    throw new RuntimeException("unknown parameter value 'checkColumns' " + rowsToReturn.get());
            }
        }
        return Optional.ofNullable(rowsToReturnEnum);
    }
}
<commit_msg>KP-2055 Bugfix<commit_after>package no.ssb.vtl.script.visitors;

import no.ssb.vtl.model.Dataset;
import no.ssb.vtl.parser.VTLBaseVisitor;
import no.ssb.vtl.parser.VTLParser;
import no.ssb.vtl.script.operations.CheckSingleRuleOperation;

import javax.script.ScriptContext;
import java.util.Optional;
import java.util.function.Supplier;

import static com.google.common.base.Preconditions.*;
import static java.util.Optional.*;

public class CheckVisitor extends VTLBaseVisitor<Dataset> {

    private final ScriptContext context;
    private final RelationalVisitor relationalVisitor;

    public CheckVisitor(ScriptContext context, RelationalVisitor relationalVisitor) {
        this.context = checkNotNull(context, "the context was null");
        this.relationalVisitor = relationalVisitor;
    }

    @Override
    public Dataset visitCheckExpression(VTLParser.CheckExpressionContext ctx) {
        VTLParser.CheckParamContext checkParamContext = ctx.checkParam();
        Dataset dataset = visit(checkParamContext.datasetExpression());

        Optional<CheckSingleRuleOperation.ComponentsToReturn> componentsToReturnEnum = getComponentsToReturn(checkParamContext.checkColumns());
        Optional<CheckSingleRuleOperation.RowsToReturn> rowsToReturn = getRowsToReturn(checkParamContext.checkRows());

        Optional<String> errorCode = getErrorCode(checkParamContext);
        Optional<Integer> errorLevel = getErrorLevel(checkParamContext);

        return new CheckSingleRuleOperation(dataset,
                rowsToReturn,
                componentsToReturnEnum,
                errorCode,
                errorLevel);
    }

    @Override
    public Dataset visitVariableRef(VTLParser.VariableRefContext ctx) {
        return (Dataset) context.getAttribute(ctx.getText());
    }

    @Override
    public Dataset visitRelationalExpression(VTLParser.RelationalExpressionContext ctx) {
        Supplier<Dataset> datasetSupplier = relationalVisitor.visit(ctx);
        return datasetSupplier.get();
    }


    private Optional<Integer> getErrorLevel(VTLParser.CheckParamContext checkParamContext) {
        Integer errorLevel = null;
        if (checkParamContext.errorLevel() != null) {
            errorLevel = Integer.valueOf(checkParamContext.errorLevel().getText());
        }
        return Optional.ofNullable(errorLevel);
    }

    private Optional<String> getErrorCode(VTLParser.CheckParamContext checkParamContext) {
        String errorCode = null;
        if (checkParamContext.errorCode() != null) {
             errorCode = checkParamContext.errorCode().getText();
        }
        return Optional.ofNullable(errorCode);
    }


    private Optional<CheckSingleRuleOperation.ComponentsToReturn> getComponentsToReturn(VTLParser.CheckColumnsContext checkColumnsContext) {
        Optional<String> componentsToReturn = ofNullable(checkColumnsContext).map(VTLParser.CheckColumnsContext::getText);
        CheckSingleRuleOperation.ComponentsToReturn componentsToReturnEnum = null;
        if (componentsToReturn.isPresent()) {
            switch (componentsToReturn.get()) {
                case "measures":
                    componentsToReturnEnum = CheckSingleRuleOperation.ComponentsToReturn.MEASURES;
                    break;
                case "condition":
                    componentsToReturnEnum = CheckSingleRuleOperation.ComponentsToReturn.CONDITION;
                    break;
                default:
                    throw new RuntimeException("unknown parameter value 'checkColumns' " + componentsToReturn.get());
            }
        }
        return Optional.ofNullable(componentsToReturnEnum);
    }

    private Optional<CheckSingleRuleOperation.RowsToReturn> getRowsToReturn(VTLParser.CheckRowsContext checkColumnsContext) {
        Optional<String> rowsToReturn = ofNullable(checkColumnsContext).map(VTLParser.CheckRowsContext::getText);
        CheckSingleRuleOperation.RowsToReturn rowsToReturnEnum = null;
        if (rowsToReturn.isPresent()) {
            switch (rowsToReturn.get()) {
                case "not_valid":
                    rowsToReturnEnum = CheckSingleRuleOperation.RowsToReturn.NOT_VALID;
                    break;
                case "valid":
                    rowsToReturnEnum = CheckSingleRuleOperation.RowsToReturn.VALID;
                    break;
                case "all":
                    rowsToReturnEnum = CheckSingleRuleOperation.RowsToReturn.ALL;
                    break;
                default:
                    throw new RuntimeException("unknown parameter value 'checkColumns' " + rowsToReturn.get());
            }
        }
        return Optional.ofNullable(rowsToReturnEnum);
    }
}

{'fraction_non_alphanumeric': 0.07942430703624734, 'fraction_numerical': 0.00042643923240938164, 'mean_word_length': 2.980059397539245, 'pattern_counts': {'":': 10, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6442163', 'n_tokens_mistral': 2311, 'n_tokens_neox': 2234, 'n_words': 395}