use of org.kie.workbench.common.dmn.api.definition.model.DecisionRule in project kie-wb-common by kiegroup.
the class DecisionRuleFactory method makeDecisionRule.
public static DecisionRule makeDecisionRule(final DecisionTable dtable) {
final DecisionRule rule = new DecisionRule();
for (int ie = 0; ie < dtable.getInput().size(); ie++) {
final UnaryTests ut = new UnaryTests();
ut.getText().setValue(DecisionTableDefaultValueUtilities.INPUT_CLAUSE_UNARY_TEST_TEXT);
rule.getInputEntry().add(ut);
ut.setParent(rule);
}
for (int oe = 0; oe < dtable.getOutput().size(); oe++) {
final LiteralExpression le = new LiteralExpression();
le.getText().setValue(DecisionTableDefaultValueUtilities.OUTPUT_CLAUSE_EXPRESSION_TEXT);
rule.getOutputEntry().add(le);
le.setParent(rule);
}
for (int index = 0; index < dtable.getAnnotations().size(); index++) {
final RuleAnnotationClauseText ruleAnnotationClauseText = new RuleAnnotationClauseText();
ruleAnnotationClauseText.getText().setValue(DecisionTableDefaultValueUtilities.RULE_ANNOTATION_CLAUSE_EXPRESSION_TEXT);
rule.getAnnotationEntry().add(ruleAnnotationClauseText);
ruleAnnotationClauseText.setParent(rule);
}
rule.setParent(dtable);
return rule;
}
use of org.kie.workbench.common.dmn.api.definition.model.DecisionRule in project kie-wb-common by kiegroup.
the class MoveColumnsCommand method newGraphCommand.
@Override
protected Command<GraphCommandExecutionContext, RuleViolation> newGraphCommand(final AbstractCanvasHandler context) {
return new AbstractGraphCommand() {
@Override
protected CommandResult<RuleViolation> check(final GraphCommandExecutionContext context) {
return isColumnInValidSection() ? GraphCommandResultBuilder.SUCCESS : GraphCommandResultBuilder.failed();
}
private boolean isColumnInValidSection() {
final DecisionTableSection section = DecisionTableUIModelMapperHelper.getSection(dtable, index);
return section == DecisionTableSection.INPUT_CLAUSES || section == DecisionTableSection.OUTPUT_CLAUSES || section == DecisionTableSection.ANNOTATION_CLAUSES;
}
@Override
public CommandResult<RuleViolation> execute(final GraphCommandExecutionContext context) {
return moveClauses(index);
}
@Override
public CommandResult<RuleViolation> undo(final GraphCommandExecutionContext context) {
return moveClauses(oldIndex);
}
private CommandResult<RuleViolation> moveClauses(final int index) {
final DecisionTableSection section = DecisionTableUIModelMapperHelper.getSection(dtable, index);
if (section == DecisionTableSection.INPUT_CLAUSES) {
final int oldIndex = uiModel.getColumns().indexOf(columns.get(0));
final int relativeIndex = DecisionTableUIModelMapperHelper.getInputEntryIndex(dtable, index);
final int relativeOldIndex = DecisionTableUIModelMapperHelper.getInputEntryIndex(dtable, oldIndex);
final List<Integer> uiColumnIndexesToMove = columns.stream().map(c -> uiModel.getColumns().indexOf(c)).collect(Collectors.toList());
final List<Integer> inputClauseIndexesToMove = uiColumnIndexesToMove.stream().map(i -> DecisionTableUIModelMapperHelper.getInputEntryIndex(dtable, i)).collect(Collectors.toList());
moveClauses(relativeIndex, relativeOldIndex, dtable.getInput(), inputClauseIndexesToMove);
CommandUtils.moveComponentWidths(index, oldIndex, dtable.getComponentWidths(), uiColumnIndexesToMove);
final List<List<UnaryTests>> decisionRulesInputEntries = dtable.getRule().stream().map(DecisionRule::getInputEntry).collect(Collectors.toList());
updateDecisionRules(relativeIndex, relativeOldIndex, decisionRulesInputEntries, inputClauseIndexesToMove);
return GraphCommandResultBuilder.SUCCESS;
} else if (section == DecisionTableSection.OUTPUT_CLAUSES) {
final int oldIndex = uiModel.getColumns().indexOf(columns.get(0));
final int relativeIndex = DecisionTableUIModelMapperHelper.getOutputEntryIndex(dtable, index);
final int relativeOldIndex = DecisionTableUIModelMapperHelper.getOutputEntryIndex(dtable, oldIndex);
final List<Integer> uiColumnIndexesToMove = columns.stream().map(c -> uiModel.getColumns().indexOf(c)).collect(Collectors.toList());
final List<Integer> outputClauseIndexesToMove = uiColumnIndexesToMove.stream().map(i -> DecisionTableUIModelMapperHelper.getOutputEntryIndex(dtable, i)).collect(Collectors.toList());
moveClauses(relativeIndex, relativeOldIndex, dtable.getOutput(), outputClauseIndexesToMove);
CommandUtils.moveComponentWidths(index, oldIndex, dtable.getComponentWidths(), uiColumnIndexesToMove);
final List<List<LiteralExpression>> decisionRulesOutputEntries = dtable.getRule().stream().map(DecisionRule::getOutputEntry).collect(Collectors.toList());
updateDecisionRules(relativeIndex, relativeOldIndex, decisionRulesOutputEntries, outputClauseIndexesToMove);
return GraphCommandResultBuilder.SUCCESS;
} else if (section == DecisionTableSection.ANNOTATION_CLAUSES) {
final int oldIndex = uiModel.getColumns().indexOf(columns.get(0));
final int relativeIndex = DecisionTableUIModelMapperHelper.getAnnotationEntryIndex(dtable, index);
final int relativeOldIndex = DecisionTableUIModelMapperHelper.getAnnotationEntryIndex(dtable, oldIndex);
final List<Integer> uiColumnIndexesToMove = columns.stream().map(c -> uiModel.getColumns().indexOf(c)).collect(Collectors.toList());
final List<Integer> annotationClauseIndexesToMove = uiColumnIndexesToMove.stream().map(i -> DecisionTableUIModelMapperHelper.getAnnotationEntryIndex(dtable, i)).collect(Collectors.toList());
moveClauses(relativeIndex, relativeOldIndex, dtable.getAnnotations(), annotationClauseIndexesToMove);
CommandUtils.moveComponentWidths(index, oldIndex, dtable.getComponentWidths(), uiColumnIndexesToMove);
final List<List<RuleAnnotationClauseText>> decisionRulesAnnotationEntries = dtable.getRule().stream().map(DecisionRule::getAnnotationEntry).collect(Collectors.toList());
updateDecisionRules(relativeIndex, relativeOldIndex, decisionRulesAnnotationEntries, annotationClauseIndexesToMove);
return GraphCommandResultBuilder.SUCCESS;
} else {
return GraphCommandResultBuilder.failed();
}
}
private <T> void moveClauses(final int relativeIndex, final int relativeOldIndex, final List<T> clauses, final List<Integer> clauseIndexesToMove) {
final List<T> clausesToMove = clauseIndexesToMove.stream().map(clauses::get).collect(Collectors.toList());
clauses.removeAll(clausesToMove);
if (relativeIndex < relativeOldIndex) {
clauses.addAll(relativeIndex, clausesToMove);
} else if (relativeIndex > relativeOldIndex) {
clauses.addAll(relativeIndex - clausesToMove.size() + 1, clausesToMove);
}
}
private <T> void updateDecisionRules(final int relativeIndex, final int relativeOldIndex, final List<List<T>> clauses, final List<Integer> clauseIndexesToMove) {
clauses.forEach(row -> moveClauses(relativeIndex, relativeOldIndex, row, clauseIndexesToMove));
}
};
}
use of org.kie.workbench.common.dmn.api.definition.model.DecisionRule in project kie-wb-common by kiegroup.
the class DecisionTableEditorDefinitionEnricher method buildOutputClausesByDataType.
void buildOutputClausesByDataType(final HasExpression hasExpression, final DecisionTable dTable, final DecisionRule decisionRule) {
final HasTypeRef hasTypeRef = getHasTypeRef(hasExpression, dTable);
final QName typeRef = !Objects.isNull(hasTypeRef) ? hasTypeRef.getTypeRef() : BuiltInType.UNDEFINED.asQName();
final String name = DecisionTableDefaultValueUtilities.getNewOutputClauseName(dTable);
final List<ClauseRequirement> outputClausesRequirement = generateOutputClauseRequirements(dmnGraphUtils.getModelDefinitions(), typeRef, name);
if (outputClausesRequirement.isEmpty()) {
dTable.getOutput().add(buildOutputClause(dTable, typeRef, name));
populateOutputEntries(decisionRule);
} else {
outputClausesRequirement.stream().sorted(Comparator.comparing(outputClauseRequirement -> outputClauseRequirement.text)).map(outputClauseRequirement -> buildOutputClause(dTable, outputClauseRequirement.typeRef, outputClauseRequirement.text)).forEach(outputClause -> {
dTable.getOutput().add(outputClause);
populateOutputEntries(decisionRule);
});
}
}
use of org.kie.workbench.common.dmn.api.definition.model.DecisionRule in project kie-wb-common by kiegroup.
the class DecisionRuleFactory method duplicateDecisionRule.
public static DecisionRule duplicateDecisionRule(final int index, final DecisionTable dtable) {
final DecisionRule rule = new DecisionRule();
final DecisionRule source = dtable.getRule().get(index);
for (UnaryTests ie : source.getInputEntry()) {
final UnaryTests ut = new UnaryTests();
ut.getText().setValue(ie.getText().getValue());
ut.setConstraintType(ie.getConstraintType());
rule.getInputEntry().add(ut);
ut.setParent(rule);
}
for (LiteralExpression oe : source.getOutputEntry()) {
final LiteralExpression le = new LiteralExpression();
le.getText().setValue(oe.getText().getValue());
rule.getOutputEntry().add(le);
le.setParent(rule);
}
for (final RuleAnnotationClauseText text : source.getAnnotationEntry()) {
final RuleAnnotationClauseText copy = new RuleAnnotationClauseText();
copy.getText().setValue(text.getText().getValue());
copy.setParent(rule);
rule.getAnnotationEntry().add(copy);
}
rule.setParent(dtable);
return rule;
}
use of org.kie.workbench.common.dmn.api.definition.model.DecisionRule in project kie-wb-common by kiegroup.
the class DecisionTableGrid method duplicateDecisionRule.
void duplicateDecisionRule(final int index) {
getExpression().get().ifPresent(dtable -> {
final GridRow decisionTableRow = new LiteralExpressionGridRow();
final DecisionRule decisionRule = DecisionRuleFactory.duplicateDecisionRule(index, dtable);
sessionCommandManager.execute((AbstractCanvasHandler) sessionManager.getCurrentSession().getCanvasHandler(), new AddDecisionRuleCommand(dtable, decisionRule, model, decisionTableRow, index, uiModelMapper, () -> resize(BaseExpressionGrid.RESIZE_EXISTING)));
});
}
Aggregations