use of org.drools.workbench.screens.dtablexls.backend.server.conversion.builders.GuidedDecisionTableSourceBuilderIndirect in project drools-wb by kiegroup.
the class GuidedDecisionTablePopulater method addIndirectSourceBuildersColumns.
private void addIndirectSourceBuildersColumns() {
// Extract BRLVariableColumn and SourceBuilders for respective sections of the rule
final List<BRLVariableColumn> variableColumns = new ArrayList<>();
final List<GuidedDecisionTableLHSBuilder> lhsBuilders = new ArrayList<>();
final List<GuidedDecisionTableRHSBuilder> rhsBuilders = new ArrayList<>();
for (GuidedDecisionTableSourceBuilder sb : sourceBuilders) {
if (sb instanceof GuidedDecisionTableSourceBuilderIndirect) {
for (BRLVariableColumn variableColumn : ((GuidedDecisionTableSourceBuilderIndirect) sb).getVariableColumns()) {
variableColumns.add(variableColumn);
}
}
if (sb instanceof GuidedDecisionTableLHSBuilder) {
lhsBuilders.add((GuidedDecisionTableLHSBuilder) sb);
}
if (sb instanceof GuidedDecisionTableRHSBuilder) {
rhsBuilders.add((GuidedDecisionTableRHSBuilder) sb);
}
}
// -- PROCESS LHS PATTERNS
// For each GuidedDecisionTableLHSBuilder gather IPatterns generated
// from each accumulative iteration and convert to RuleModel
int previousPatternCount = 0;
for (int i = 0; i < lhsBuilders.size(); i++) {
// Construct DRL for this and previous builders
final String rule = assembleRuleDRL(i, lhsBuilders, Collections.emptyList());
final RuleModel rm = RuleModelDRLPersistenceImpl.getInstance().unmarshal(rule, Collections.emptyList(), dmo);
if (Objects.nonNull(rm.lhs)) {
final Map<IPattern, Map<InterpolationVariable, Integer>> patternTemplateKeys = new HashMap<>();
final BRLConditionColumn column = new BRLConditionColumn();
dtable.getConditions().add(column);
// Extract Template Keys for Patterns in iteration
boolean allPatternsHaveTemplateKeys = true;
for (int iPattern = previousPatternCount; iPattern < rm.lhs.length; iPattern++) {
final IPattern pattern = rm.lhs[iPattern];
final Map<InterpolationVariable, Integer> templateKeys = new HashMap<>();
final RuleModelVisitor rmv = new RuleModelVisitor(templateKeys);
rmv.visit(pattern);
patternTemplateKeys.put(pattern, templateKeys);
if (templateKeys.isEmpty()) {
allPatternsHaveTemplateKeys = false;
}
}
// If ALL patterns in the iteration have Template keys we can use IPattern otherwise fallback to FreeFormLine
if (allPatternsHaveTemplateKeys) {
for (int iPattern = previousPatternCount; iPattern < rm.lhs.length; iPattern++) {
final IPattern pattern = rm.lhs[iPattern];
column.getDefinition().add(pattern);
}
} else {
final FreeFormLine ffl = new FreeFormLine();
ffl.setText(lhsBuilders.get(i).getResult());
column.setDefinition(Collections.singletonList(ffl));
}
// Copy variables to column
for (int iPattern = previousPatternCount; iPattern < rm.lhs.length; iPattern++) {
final IPattern pattern = rm.lhs[iPattern];
final Map<InterpolationVariable, Integer> templateKeys = patternTemplateKeys.get(pattern);
if (Objects.nonNull(templateKeys)) {
final List<InterpolationVariable> ivs = new ArrayList<>(templateKeys.keySet());
for (BRLVariableColumn variableColumn : variableColumns) {
final Iterator<InterpolationVariable> ivsIts = ivs.iterator();
while (ivsIts.hasNext()) {
final InterpolationVariable iv = ivsIts.next();
if (Objects.equals(iv.getVarName(), variableColumn.getVarName())) {
final BRLConditionVariableColumn source = (BRLConditionVariableColumn) variableColumn;
final BRLConditionVariableColumn target = makeBRLConditionVariableColumn(source, iv);
column.getChildColumns().add(target);
ivsIts.remove();
}
}
}
}
}
// Give the column a header
if (column.getChildColumns().isEmpty()) {
setZeroParameterConditionColumnHeader(column, variableColumns);
} else {
setCompositeColumnHeader(column);
}
}
previousPatternCount = rm.lhs.length;
}
// -- PROCESS RHS ACTIONS
final String rule = assembleRuleDRL(lhsBuilders.size() - 1, lhsBuilders, rhsBuilders);
final RuleModel rm = RuleModelDRLPersistenceImpl.getInstance().unmarshal(rule, Collections.emptyList(), dmo, true);
if (rm.rhs != null) {
for (IAction action : rm.rhs) {
final BRLActionColumn column = new BRLActionColumn();
column.getDefinition().add(action);
dtable.getActionCols().add(column);
if (action instanceof ActionRetractFact) {
final ActionRetractFact arf = (ActionRetractFact) action;
createActionRetractFactChildColumns(arf, column, variableColumns);
} else {
createRHSChildColumns(rm, action, column, variableColumns);
}
if (column.getChildColumns().isEmpty()) {
setZeroParameterActionColumnHeader(column, variableColumns);
} else {
setCompositeColumnHeader(column);
}
}
}
}
use of org.drools.workbench.screens.dtablexls.backend.server.conversion.builders.GuidedDecisionTableSourceBuilderIndirect in project drools-wb by kiegroup.
the class GuidedDecisionTablePopulater method getOrderedValueBuilders.
private List<ParameterizedValueBuilder> getOrderedValueBuilders() {
// Get ordered list of ParameterizedValueBuilder for all GuidedDecisionTableSourceBuilderIndirect instances
// An ordered list of ParameterizedValueBuilder guarantees they are checked in the same order as columns
// were added to the Guided Decision Table.
final List<ParameterizedValueBuilder> valueBuilders = new ArrayList<>();
for (GuidedDecisionTableSourceBuilder sb : sourceBuilders) {
if (sb instanceof GuidedDecisionTableSourceBuilderIndirect) {
final GuidedDecisionTableSourceBuilderIndirect isb = (GuidedDecisionTableSourceBuilderIndirect) sb;
final Set<Integer> sortedIndexes = new TreeSet<>(isb.getValueBuilders().keySet());
for (Integer index : sortedIndexes) {
final ParameterizedValueBuilder vb = isb.getValueBuilders().get(index);
valueBuilders.add(vb);
}
}
}
return valueBuilders;
}
use of org.drools.workbench.screens.dtablexls.backend.server.conversion.builders.GuidedDecisionTableSourceBuilderIndirect in project drools-wb by kiegroup.
the class GuidedDecisionTableGeneratorListener method assertValueBuildersRowData.
private void assertValueBuildersRowData() {
// new row is created the GuidedDecisionTableSourceBuilders contain sufficient row data
if (_sourceBuilders != null) {
final int rowCount = getRowCount();
final int columnCount = _sourceBuilders.size();
for (GuidedDecisionTableSourceBuilder sb : _sourceBuilders) {
if (sb instanceof GuidedDecisionTableSourceBuilderDirect) {
final GuidedDecisionTableSourceBuilderDirect sbd = (GuidedDecisionTableSourceBuilderDirect) sb;
if (sbd.getRowCount() < rowCount) {
sbd.addCellValue(rowCount, columnCount, "");
}
} else if (sb instanceof GuidedDecisionTableSourceBuilderIndirect) {
final GuidedDecisionTableSourceBuilderIndirect sbi = (GuidedDecisionTableSourceBuilderIndirect) sb;
for (ParameterizedValueBuilder pvb : sbi.getValueBuilders().values()) {
if (pvb.getColumnData().size() < rowCount) {
pvb.addCellValue(rowCount, columnCount, "");
}
}
}
}
}
}
Aggregations