All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gs.dmn.validation.DefaultDMNValidator Maven / Gradle / Ivy

There is a newer version: 8.7.3
Show newest version
/*
 * Copyright 2016 Goldman Sachs.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
 *
 * You may obtain a copy of the License at
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations under the License.
 */
package com.gs.dmn.validation;

import com.gs.dmn.DMNModelRepository;
import com.gs.dmn.QualifiedName;
import com.gs.dmn.ast.*;
import com.gs.dmn.ast.visitor.TraversalVisitor;
import com.gs.dmn.error.ErrorHandler;
import com.gs.dmn.log.BuildLogger;
import com.gs.dmn.log.Slf4jBuildLogger;
import com.gs.dmn.transformation.AbstractDMNToNativeTransformer;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

public class DefaultDMNValidator extends SimpleDMNValidator {
    public DefaultDMNValidator() {
        this(new Slf4jBuildLogger(LOGGER));
    }

    public DefaultDMNValidator(BuildLogger logger) {
        super(logger);
    }

    @Override
    public List validate(DMNModelRepository repository) {
        ValidationContext context = new ValidationContext(repository);
        if (isEmpty(repository)) {
            this.logger.warn("DMN repository is empty; validator will not run");
            return context.getErrors();
        }

        DefaultDMNValidatorVisitor visitor = new DefaultDMNValidatorVisitor(this.logger, this.errorHandler, this);
        for (TDefinitions definitions: repository.getAllDefinitions()) {
            definitions.accept(visitor, context);
        }

        return context.getErrors();
    }

    protected void validateInputData(TDefinitions definitions, TInputData element, ValidationContext context) {
        validateNamedElement(definitions, element, context);
        validateVariable(definitions, element, element.getVariable(), true, context);
    }

    protected void validateDecision(TDefinitions definitions, TDecision element, ValidationContext context) {
        validateNamedElement(definitions, element, context);
        TInformationItem variable = element.getVariable();
        validateVariable(definitions, element, variable, true, context);
        validateInformationRequirements(definitions, element, element.getInformationRequirement(), context);
        List krs = element.getKnowledgeRequirement().stream().map(TKnowledgeRequirement::getRequiredKnowledge).collect(Collectors.toList());
        validateReferences(definitions, element, krs, context);
        validateExpression(definitions, element, context);
    }

    protected void validateBusinessKnowledgeModel(TDefinitions definitions, TBusinessKnowledgeModel element, ValidationContext context) {
        validateNamedElement(definitions, element, context);
        validateVariable(definitions, element, element.getVariable(), false, context);
        List krs = element.getKnowledgeRequirement().stream().map(TKnowledgeRequirement::getRequiredKnowledge).collect(Collectors.toList());
        validateReferences(definitions, element, krs, context);
    }

    protected void validateDecisionService(TDefinitions definitions, TDecisionService element, ValidationContext context) {
        validateNamedElement(definitions, element, context);
        validateVariable(definitions, element, element.getVariable(), false, context);
        validateReferences(definitions, element, element.getInputData(), context);
        validateReferences(definitions, element, element.getInputDecision(), context);
        validateReferences(definitions, element, element.getOutputDecision(), context);
        validateReferences(definitions, element, element.getEncapsulatedDecision(), context);
    }

    protected void validateUnique(TDefinitions definitions, List elements, String elementType, String property, boolean isOptionalProperty, Function accessor, String errorMessage, ValidationContext context) {
        if (errorMessage == null) {
            errorMessage = String.format("The '%s' of a '%s' must be unique.", property, elementType);
        }
        // Create a map
        Map> map = new LinkedHashMap<>();
        for (TDMNElement element : elements) {
            String key = accessor.apply(element);
            if (!isOptionalProperty || key != null) {
                List list = map.get(key);
                if (list == null) {
                    list = new ArrayList<>();
                    list.add(element);
                    map.put(key, list);
                } else {
                    list.add(element);
                }
            }
        }
        // Find duplicates
        List duplicates = new ArrayList<>();
        for (Map.Entry> entry : map.entrySet()) {
            String key = entry.getKey();
            if(entry.getValue().size() > 1){
                duplicates.add(key);
            }
        }
        // Report error
        if (!duplicates.isEmpty()) {
            String message = String.join(", ", duplicates);
            context.addError(makeError(context.getRepository(), definitions, null, String.format("%s Found duplicates for '%s'.", errorMessage, message)));
        }
    }

    private void validateUniqueReferences(TDefinitions definitions, List elements, String elementType, String property, boolean isOptionalProperty, Function accessor, String errorMessage, ValidationContext context) {
        if (errorMessage == null) {
            errorMessage = String.format("The '%s' of a '%s' must be unique.", property, elementType);
        }
        // Create a map
        Map> map = new LinkedHashMap<>();
        for (TDMNElementReference element : elements) {
            String key = accessor.apply(element);
            if (!isOptionalProperty || key != null) {
                List list = map.get(key);
                if (list == null) {
                    list = new ArrayList<>();
                    list.add(element);
                    map.put(key, list);
                } else {
                    list.add(element);
                }
            }
        }
        // Find duplicates
        List duplicates = new ArrayList<>();
        for (Map.Entry> entry : map.entrySet()) {
            String key = entry.getKey();
            if(entry.getValue().size() > 1){
                duplicates.add(key);
            }
        }
        // Report error
        if (!duplicates.isEmpty()) {
            String message = String.join(", ", duplicates);
            context.addError(makeError(context.getRepository(), definitions, null, String.format("%s Found duplicates for '%s'.", errorMessage, message)));
        }
    }

    private void validateNamedElement(TDefinitions definitions, TNamedElement element, ValidationContext context) {
        if (StringUtils.isBlank(element.getName())) {
            String errorMessage = "Missing name";
            context.addError(makeError(context.getRepository(), definitions, element, errorMessage));
        }
    }

    private void validateVariable(TDefinitions definitions, TNamedElement element, TInformationItem variable, boolean validateTypeRef, ValidationContext context) {
        DMNModelRepository repository = context.getRepository();

        if (variable == null) {
            String errorMessage = "Missing variable";
            context.addError(makeError(repository, definitions, element, errorMessage));
        } else {
            if (variable.getName() == null) {
                String errorMessage = "Missing variable name";
                context.addError(makeError(context.getRepository(), definitions, element, errorMessage));
            } else {
                // element/@name == element/variable/@name
                String variableName = variable.getName();
                String elementName = element.getName();
                if (!elementName.equals(variableName)) {
                    String errorMessage = String.format("DRGElement name and variable name should be the same. Found '%s' and '%s'", elementName, variableName);
                    context.addError(makeError(repository, definitions, element, errorMessage));
                }
                // decision/variable/@typeRef is not null
                if (validateTypeRef) {
                    QualifiedName typeRef = QualifiedName.toQualifiedName(definitions, variable.getTypeRef());
                    if (repository.isNull(typeRef)) {
                        String errorMessage = "Missing typRef in variable";
                        context.addError(makeError(repository, definitions, element, errorMessage));
                    }
                }
            }
        }
    }

    private void validateInformationRequirements(TDefinitions definitions, TNamedElement decision, List informationRequirements, ValidationContext context) {
        // Validate requirements
        Function accessor =
                (TDMNElement e) -> {
                    TInformationRequirement ir = (TInformationRequirement) e;
                    if (ir.getRequiredInput() != null) {
                        return ir.getRequiredInput().getHref();
                    } else {
                        return ir.getRequiredDecision().getHref();
                    }
                };
        validateUnique(definitions, informationRequirements, "TInformationRequirement", "href", false,
                accessor, decision.getName(), context);
    }

    private void validateReferences(TDefinitions definitions, TNamedElement decision, List references, ValidationContext context) {
        // Validate requirements
        Function accessor =
                TDMNElementReference::getHref;
        validateUniqueReferences(definitions, references, "TDMNElementReference", "href", false,
                accessor, decision.getName(), context);
    }

    private void validateExpression(TDefinitions definitions, TDecision decision, ValidationContext context) {
        // Validate expression
        TExpression expression = decision.getExpression();
        if (expression != null) {
            validateExpression(definitions, decision, expression, context);
        }
    }

    private void validateExpression(TDefinitions definitions, TDRGElement element, TExpression expression, ValidationContext context) {
        DMNModelRepository repository = context.getRepository();
        if (expression == null) {
            String errorMessage = "Missing expression";
            context.addError(makeError(repository, definitions, element, errorMessage));
        } else {
            if (expression instanceof TDecisionTable) {
                TDecisionTable decisionTable = (TDecisionTable) expression;
                validateDecisionTable(definitions, element, decisionTable, context);
            } else if (expression instanceof TInvocation) {
                TInvocation invocation = (TInvocation) expression;
                validateExpression(definitions, element, invocation.getExpression(), context);
            } else if (expression instanceof TLiteralExpression) {
                TLiteralExpression literalExpression = (TLiteralExpression) expression;
                String expressionLanguage = ((TLiteralExpression) expression).getExpressionLanguage();
                if (!isSupported(expressionLanguage)) {
                    String errorMessage = String.format("Not supported expression language '%s'", expressionLanguage);
                    context.addError(makeError(repository, definitions, element, errorMessage));
                }
                if (StringUtils.isBlank(literalExpression.getText())) {
                    String errorMessage = "Missing text in literal expressions";
                    context.addError(makeError(repository, definitions, element, errorMessage));
                }
            } else if (expression instanceof TContext) {
                List contextEntryList = ((TContext) expression).getContextEntry();
                if (contextEntryList.isEmpty()) {
                    String errorMessage = "Missing entries in context expression";
                    context.addError(makeError(repository, definitions, element, errorMessage));
                }
            } else if (expression instanceof TRelation) {
                if (((TRelation) expression).getColumn() == null && ((TRelation) expression).getRow() == null) {
                    String errorMessage = "Empty relation";
                    context.addError(makeError(repository, definitions, element, errorMessage));
                }
            } else if (expression instanceof TConditional) {
                checkChildExpression(definitions, element, ((TConditional) expression).getIf(), "conditional", "if", context);
                checkChildExpression(definitions, element, ((TConditional) expression).getThen(), "conditional", "then", context);
                checkChildExpression(definitions, element, ((TConditional) expression).getElse(), "conditional", "else", context);
            } else if (expression instanceof TFilter) {
                checkChildExpression(definitions, element, ((TFilter) expression).getIn(), "filter", "in", context);
                checkChildExpression(definitions, element, ((TFilter) expression).getMatch(), "filter", "match", context);
            } else if (expression instanceof TFor) {
                checkChildExpression(definitions, element, ((TFor) expression).getIn(), "for", "in", context);
                checkChildExpression(definitions, element, ((TFor) expression).getReturn(), "for", "return", context);
            } else if (expression instanceof TSome) {
                String parentName = "some";
                checkChildExpression(definitions, element, ((TQuantified) expression).getIn(), parentName, "in", context);
                checkChildExpression(definitions, element, ((TQuantified) expression).getSatisfies(), parentName, "satisfies", context);
            } else if (expression instanceof TEvery) {
                String parentName = "every";
                checkChildExpression(definitions, element, ((TQuantified) expression).getIn(), parentName, "in", context);
                checkChildExpression(definitions, element, ((TQuantified) expression).getSatisfies(), parentName, "satisfies", context);
            } else {
                throw new UnsupportedOperationException("Not supported DMN expression type " + expression.getClass().getName());
            }
        }
    }


    private void validateDecisionTable(TDefinitions definitions, TDMNElement element, TDecisionTable decisionTable, ValidationContext context) {
        DMNModelRepository repository = context.getRepository();
        List input = decisionTable.getInput();
        if (input == null || input.isEmpty()) {
            String errorMessage = "Missing input clauses";
            context.addError(makeError(repository, definitions, element, errorMessage));
        }
        List output = decisionTable.getOutput();
        if (output == null || output.isEmpty()) {
            String errorMessage = "Missing output clauses";
            context.addError(makeError(repository, definitions, element, errorMessage));
        }
        validateHitPolicy(definitions, element, decisionTable, context);
        List ruleList = decisionTable.getRule();
        if (ruleList == null || ruleList.isEmpty()) {
            String errorMessage = "Missing rules in decision table";
            context.addError(makeError(repository, definitions, element, errorMessage));
        } else {
            for (TDecisionRule rule : ruleList) {
                validateRule(definitions, element, rule, context);
            }
        }
    }

    private void validateHitPolicy(TDefinitions definitions, TDMNElement element, TDecisionTable decisionTable, ValidationContext context) {
        DMNModelRepository repository = context.getRepository();

        List output = decisionTable.getOutput();
        THitPolicy hitPolicy = decisionTable.getHitPolicy();
        TBuiltinAggregator aggregation = decisionTable.getAggregation();
        if (hitPolicy != THitPolicy.COLLECT && aggregation != null) {
            String errorMessage = String.format("Aggregation '%s' not allowed for hit policy '%s'", aggregation, hitPolicy);
            context.addError(makeError(repository, definitions, element, errorMessage));
        }
        if (output != null && output.size() > 1
                && hitPolicy == THitPolicy.COLLECT
                && aggregation != null) {
            String errorMessage = String.format("Collect operator is not defined over multiple outputs for decision table '%s'", decisionTable.getId());
            context.addError(makeError(repository, definitions, element, errorMessage));
        }
    }

    private boolean isSupported(String expressionLanguage) {
        return expressionLanguage == null || AbstractDMNToNativeTransformer.SUPPORTED_LANGUAGES.contains(expressionLanguage);
    }

    private void validateRule(TDefinitions definitions, TDMNElement element, TDecisionRule rule, ValidationContext context) {
        DMNModelRepository repository = context.getRepository();
        List inputEntry = rule.getInputEntry();
        if (inputEntry == null || inputEntry.isEmpty()) {
            String errorMessage = "No input entries for rule " + rule.getId();
            context.addError(makeError(repository, definitions, element, errorMessage));
        }
        List outputEntry = rule.getOutputEntry();
        if (outputEntry == null || outputEntry.isEmpty()) {
            String errorMessage = "No outputEntry entries for rule " + rule.getId();
            context.addError(makeError(repository, definitions, element, errorMessage));
        }
    }

    private void checkChildExpression(TDefinitions definitions, TDRGElement element, TChildExpression childExpression, String parentName, String childName, ValidationContext context) {
        String errorMessage = String.format("Missing '%s' expression in '%s' boxed expression in element '%s'", childName, parentName, element.getName());
        if (childExpression == null || childExpression.getExpression() == null) {
            context.addError(makeError(context.getRepository(), definitions, element, errorMessage));
        }
    }
}

class DefaultDMNValidatorVisitor extends TraversalVisitor {
    private final DefaultDMNValidator validator;

    public DefaultDMNValidatorVisitor(BuildLogger logger, ErrorHandler errorHandler, DefaultDMNValidator validator) {
        super(logger, errorHandler);
        this.validator = validator;
    }

    @Override
    public DMNBaseElement visit(TDefinitions element, ValidationContext context) {
        if (element != null)
        {
            DMNModelRepository repository = context.getRepository();

            logger.debug("Validate unique 'DRGElement.id'");
            this.validator.validateUnique(
                    element, new ArrayList<>(repository.findDRGElements(element)), "DRGElement", "id", false,
                    TDMNElement::getId, null, context
            );

            logger.debug("Validate unique 'DRGElement.name'");
            this.validator.validateUnique(
                    element, new ArrayList<>(repository.findDRGElements(element)), "DRGElement", "name", false,
                    e -> ((TNamedElement)e).getName(), null, context
            );

            logger.debug("Validate unique 'ItemDefinition.name'");
            this.validator.validateUnique(
                    element, new ArrayList<>(repository.findItemDefinitions(element)), "ItemDefinition", "name", false,
                    e -> ((TNamedElement)e).getName(), null, context
            );
        }

        // Visit children
        super.visit(element, context);

        return element;
    }

    @Override
    public DMNBaseElement visit(TInputData element, ValidationContext context) {
        if (element != null) {
            DMNModelRepository repository = context.getRepository();
            TDefinitions definitions = repository.getModel(element);

            this.validator.validateInputData(definitions, element, context);
        }

        return element;
    }

    @Override
    public DMNBaseElement visit(TDecision element, ValidationContext context) {
        if (element != null) {
            DMNModelRepository repository = context.getRepository();
            TDefinitions definitions = repository.getModel(element);

            this.validator.validateDecision(definitions, element, context);
        }

        return element;
    }

    @Override
    public DMNBaseElement visit(TBusinessKnowledgeModel element, ValidationContext context) {
        if (element != null) {
            DMNModelRepository repository = context.getRepository();
            TDefinitions definitions = repository.getModel(element);

            this.validator.validateBusinessKnowledgeModel(definitions, element, context);
        }

        return element;
    }

    @Override
    public DMNBaseElement visit(TDecisionService element, ValidationContext context) {
        if (element != null) {
            DMNModelRepository repository = context.getRepository();
            TDefinitions definitions = repository.getModel(element);
            this.validator.validateDecisionService(definitions, element, context);
        }

        return element;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy