org.powerflows.dmn.io.yaml.YamlDecisionConverter Maven / Gradle / Ivy
/*
* Copyright (c) 2018-present PowerFlows.org - all rights reserved.
*
* 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 org.powerflows.dmn.io.yaml;
import org.powerflows.dmn.engine.model.decision.Decision;
import org.powerflows.dmn.engine.model.decision.EvaluationMode;
import org.powerflows.dmn.engine.model.decision.HitPolicy;
import org.powerflows.dmn.engine.model.decision.expression.ExpressionType;
import org.powerflows.dmn.engine.model.decision.field.Input;
import org.powerflows.dmn.engine.model.decision.field.Output;
import org.powerflows.dmn.engine.model.decision.rule.Rule;
import org.powerflows.dmn.engine.model.decision.rule.entry.InputEntry;
import org.powerflows.dmn.engine.model.decision.rule.entry.OutputEntry;
import org.powerflows.dmn.io.DecisionToExternalModelConverter;
import org.powerflows.dmn.io.yaml.model.YamlDecision;
import org.powerflows.dmn.io.yaml.model.field.YamlFields;
import org.powerflows.dmn.io.yaml.model.field.YamlInput;
import org.powerflows.dmn.io.yaml.model.field.YamlOutput;
import org.powerflows.dmn.io.yaml.model.rule.YamlRule;
import org.powerflows.dmn.io.yaml.model.rule.entry.YamlInputEntry;
import org.powerflows.dmn.io.yaml.model.rule.entry.YamlOutputEntry;
import java.io.Serializable;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
public class YamlDecisionConverter implements DecisionToExternalModelConverter {
@Override
public YamlDecision to(final Decision decision) {
final ExpressionType decisionExpressionType = findDecisionExpressionType(decision);
final EvaluationMode decisionEvaluationMode = Decision.DEFAULT_EVALUATION_MODE.equals(decision.getEvaluationMode()) ? null : decision.getEvaluationMode();
final HitPolicy decisionHitPolicy = Decision.DEFAULT_HIT_POLICY.equals(decision.getHitPolicy()) ? null : decision.getHitPolicy();
final YamlDecision yamlDecision = new YamlDecision();
yamlDecision.setId(decision.getId());
yamlDecision.setName(decision.getName());
yamlDecision.setExpressionType(Decision.DEFAULT_EXPRESSION_TYPE.equals(decisionExpressionType) ? null : decisionExpressionType);
yamlDecision.setEvaluationMode(decisionEvaluationMode);
yamlDecision.setHitPolicy(decisionHitPolicy);
yamlDecision.setFields(createFields(decision.getInputs(), decision.getOutputs(), decisionEvaluationMode, decisionExpressionType));
final Map inputsMap = decision.getInputs()
.stream()
.collect(Collectors.toMap(Input::getName, Function.identity()));
yamlDecision.setRules(decision.getRules()
.stream()
.map(rule -> ruleToYamlRule(rule, inputsMap, decisionExpressionType))
.collect(Collectors.toList()));
return yamlDecision;
}
private ExpressionType findDecisionExpressionType(final Decision decision) {
final ExpressionType expressionType;
if (decision.getRules().isEmpty()) {
expressionType = decision.getExpressionType();
} else if (decision.getRules().get(0).getInputEntries().isEmpty()) {
expressionType = decision.getExpressionType();
} else {
expressionType = decision.getRules().get(0).getInputEntries().get(0).getExpression().getType();
}
for (Rule rule : decision.getRules()) {
for (InputEntry inputEntry : rule.getInputEntries()) {
if (!expressionType.equals(inputEntry.getExpression().getType())) {
return decision.getExpressionType();
}
}
for (OutputEntry outputEntry : rule.getOutputEntries()) {
if (!expressionType.equals(outputEntry.getExpression().getType())) {
return decision.getExpressionType();
}
}
}
return expressionType;
}
private YamlFields createFields(final List inputs,
final List