use of org.drools.workbench.models.datamodel.rule.visitors.RuleModelVisitor in project drools-wb by kiegroup.
the class GuidedDecisionTablePopulater method addIndirectSourceBuildersColumns.
private void addIndirectSourceBuildersColumns() {
final List<BRLVariableColumn> variableColumns = new ArrayList<BRLVariableColumn>();
for (GuidedDecisionTableSourceBuilder sb : sourceBuilders) {
if (sb instanceof GuidedDecisionTableSourceBuilderIndirect) {
for (BRLVariableColumn variableColumn : ((GuidedDecisionTableSourceBuilderIndirect) sb).getVariableColumns()) {
variableColumns.add(variableColumn);
}
}
}
// Convert the DRL to a RuleModel from which we construct BRLFragment columns
final StringBuilder rule = new StringBuilder();
if (!(dmo.getPackageName() == null || dmo.getPackageName().isEmpty())) {
rule.append("package ").append(dmo.getPackageName()).append("\n");
}
rule.append("rule 'temp' \n").append("when \n");
for (GuidedDecisionTableSourceBuilder sb : sourceBuilders) {
if (sb instanceof GuidedDecisionTableLHSBuilder) {
rule.append(sb.getResult());
}
}
rule.append("\nthen \n");
for (GuidedDecisionTableSourceBuilder sb : sourceBuilders) {
if (sb instanceof GuidedDecisionTableRHSBuilder) {
rule.append(sb.getResult());
}
}
rule.append("end");
final RuleModel rm = RuleModelDRLPersistenceImpl.getInstance().unmarshal(rule.toString(), Collections.emptyList(), dmo);
if (rm.lhs != null) {
for (IPattern pattern : rm.lhs) {
final BRLConditionColumn column = new BRLConditionColumn();
column.getDefinition().add(pattern);
dtable.getConditions().add(column);
final Map<InterpolationVariable, Integer> templateKeys = new HashMap<>();
final RuleModelVisitor rmv = new RuleModelVisitor(templateKeys);
rmv.visit(pattern);
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 (iv.getVarName().equals(variableColumn.getVarName())) {
final BRLConditionVariableColumn source = (BRLConditionVariableColumn) variableColumn;
final BRLConditionVariableColumn target = makeBRLConditionVariableColumn(source, iv);
column.getChildColumns().add(target);
ivsIts.remove();
}
}
}
if (column.getChildColumns().isEmpty()) {
setZeroParameterConditionColumnHeader(column, variableColumns);
} else {
setCompositeColumnHeader(column);
}
}
}
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.models.datamodel.rule.visitors.RuleModelVisitor in project drools by kiegroup.
the class GuidedDTDRLPersistence method hasVariables.
private boolean hasVariables(BRLActionColumn column) {
Map<InterpolationVariable, Integer> ivs = new HashMap<InterpolationVariable, Integer>();
RuleModel rm = new RuleModel();
for (IAction action : column.getDefinition()) {
rm.addRhsItem(action);
}
RuleModelVisitor rmv = new RuleModelVisitor(ivs);
rmv.visit(rm);
return ivs.size() > 0;
}
use of org.drools.workbench.models.datamodel.rule.visitors.RuleModelVisitor in project drools by kiegroup.
the class GuidedDTDRLPersistence method doAction.
private void doAction(List<BaseColumn> allColumns, BRLActionColumn column, List<LabelledAction> actions, TemplateDataProvider rowDataProvider, List<DTCellValue52> row, RuleModel rm) {
// Check whether the parameter-less BRL fragment needs inclusion
if (!hasVariables(column)) {
final BRLActionVariableColumn variableColumn = column.getChildColumns().get(0);
final int index = allColumns.indexOf(variableColumn);
final DTCellValue52 dcv = row.get(index);
if (dcv.getBooleanValue()) {
for (IAction action : column.getDefinition()) {
addAction(action, actions);
}
}
} else {
for (IAction action : column.getDefinition()) {
boolean addAction = false;
// Get interpolation variables used by the Action
Map<InterpolationVariable, Integer> ivs = new HashMap<InterpolationVariable, Integer>();
RuleModelVisitor rmv = new RuleModelVisitor(action, ivs);
rmv.visit(action);
if (ivs.size() == 0) {
addAction = true;
} else if (ivs.size() > 0) {
// Ensure every key has a value and substitute keys for values
int templateKeyCount = 0;
for (InterpolationVariable variable : ivs.keySet()) {
String value = rowDataProvider.getTemplateKeyValue(variable.getVarName());
if (!"".equals(value)) {
templateKeyCount++;
}
}
// Ensure at least one key has a value (FreeFormLines need all values to be provided)
if (action instanceof FreeFormLine) {
addAction = templateKeyCount == ivs.size();
} else if (templateKeyCount > 0) {
addAction = true;
}
}
if (addAction) {
addAction(action, actions);
}
}
}
}
use of org.drools.workbench.models.datamodel.rule.visitors.RuleModelVisitor in project drools-wb by kiegroup.
the class DefaultGuidedDecisionTableLinkManager method hasTemplateKeys.
private boolean hasTemplateKeys(final BRLActionColumn column) {
final Map<InterpolationVariable, Integer> ivs = new HashMap<>();
final RuleModel rm = new RuleModel();
column.getDefinition().forEach(rm::addRhsItem);
final RuleModelVisitor rmv = new RuleModelVisitor(ivs);
rmv.visit(rm);
return ivs.size() > 0;
}
use of org.drools.workbench.models.datamodel.rule.visitors.RuleModelVisitor in project drools-wb by kiegroup.
the class GuidedDecisionTablePopulater method createRHSChildColumns.
private void createRHSChildColumns(final RuleModel rm, final IAction action, final BRLActionColumn column, final List<BRLVariableColumn> variableColumns) {
final Map<InterpolationVariable, Integer> templateKeys = new HashMap<>();
final RuleModelVisitor rmv = new RuleModelVisitor(rm.lhs, templateKeys);
rmv.visit(action);
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 (iv.getVarName().equals(variableColumn.getVarName())) {
final BRLActionVariableColumn source = (BRLActionVariableColumn) variableColumn;
final BRLActionVariableColumn target = makeBRLActionVariableColumn(source, iv);
column.getChildColumns().add(target);
ivsIts.remove();
}
}
}
}
Aggregations