use of org.drools.workbench.models.datamodel.rule.IPattern 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.IPattern in project drools-wb by kiegroup.
the class BRLConditionColumnSynchronizer method getPatternBindings.
private Set<String> getPatternBindings(final BRLConditionColumn column) {
final Set<String> bindings = new HashSet<>();
final List<IPattern> definition = column.getDefinition();
final RuleModel rm = new RuleModel();
rm.lhs = definition.toArray(new IPattern[definition.size()]);
bindings.addAll(rm.getLHSVariables(true, true));
return bindings;
}
use of org.drools.workbench.models.datamodel.rule.IPattern in project drools-wb by kiegroup.
the class RuleModelCloneVisitorTest method testRuleModelCloning.
@Test
public void testRuleModelCloning() {
RuleModel model = new RuleModel();
// --------------------------------------------------------------------------------------------------------------
// LHS
// --------------------------------------------------------------------------------------------------------------
//
RuleAttribute attribute = new RuleAttribute("att1_name", "att1_val");
model.addAttribute(attribute);
RuleMetadata metadata = new RuleMetadata("meta1_name", "meta1_val");
model.addMetadata(metadata);
FactPattern f1 = buildFactPattern();
FactPattern f2 = new FactPattern();
f2.setBoundName("$f2");
f2.setFactType("Fact2");
f2.addConstraint(buildCompositeFieldConstraint());
CompositeFactPattern composite = new CompositeFactPattern();
composite.setType(CompositeFactPattern.COMPOSITE_TYPE_EXISTS);
composite.addFactPattern(f1);
composite.addFactPattern(f2);
model.addLhsItem(composite);
model.addLhsItem(buildFromAccumulateCompositeFactPattern());
model.addLhsItem(buildFromCollectCompositeFactPattern());
model.addLhsItem(buildFromCompositeFactPattern());
model.addLhsItem(buildFreeFormLine());
model.addLhsItem(buildDslSentence());
ActionGlobalCollectionAdd addGlobal = new ActionGlobalCollectionAdd();
addGlobal.setFactName("MyFact");
addGlobal.setGlobalName("glist");
model.addRhsItem(addGlobal);
// --------------------------------------------------------------------------------------------------------------
// RHS
// --------------------------------------------------------------------------------------------------------------
//
ActionInsertFact aif = new ActionInsertFact();
aif.setBoundName("$f");
aif.setFactType("FactType");
aif.addFieldValue(buildActionFieldValue());
aif.addFieldValue(buildActionFieldValue());
model.addRhsItem(aif);
ActionUpdateField auf = new ActionUpdateField();
auf.setVariable("$var");
auf.addFieldValue(buildActionFieldValue());
auf.addFieldValue(buildActionFieldValue());
model.addRhsItem(auf);
ActionSetField asf = new ActionSetField();
asf.setVariable("$var");
asf.addFieldValue(buildActionFieldValue());
asf.addFieldValue(buildActionFieldValue());
model.addRhsItem(asf);
ActionRetractFact arf = new ActionRetractFact();
arf.setVariableName("$f");
model.addRhsItem(arf);
ActionCallMethod callMethod1 = new ActionCallMethod();
callMethod1.setVariable("var1");
callMethod1.setMethodName("testMethod1 ");
callMethod1.setState(ActionCallMethod.TYPE_DEFINED);
callMethod1.addFieldValue(new ActionFieldFunction("field1", "value1", "type1"));
model.addRhsItem(callMethod1);
ActionCallMethod callMethod2 = new ActionCallMethod();
callMethod2.setVariable("var2");
callMethod2.setMethodName("testMethod2 ");
callMethod2.setState(ActionCallMethod.TYPE_UNDEFINED);
callMethod2.addFieldValue(new ActionFieldFunction("field2", "value2", "type2"));
model.addRhsItem(callMethod2);
model.addRhsItem(buildFreeFormLine());
// --------------------------------------------------------------------------------------------------------------
// Clone and verify
// --------------------------------------------------------------------------------------------------------------
//
RuleModel clone = new RuleModelCloneVisitor().visitRuleModel(model);
assertArrayEquals(model.attributes, clone.attributes);
int attIndex = 0;
for (RuleAttribute att : model.attributes) {
assertNotSame(att, clone.attributes[attIndex++]);
}
assertArrayEquals(model.metadataList, clone.metadataList);
int metIndex = 0;
for (RuleMetadata met : model.metadataList) {
assertNotSame(met, clone.metadataList[metIndex++]);
}
assertArrayEquals(model.lhs, clone.lhs);
int lhsIndex = 0;
for (IPattern pattern : model.lhs) {
assertNotSame(pattern, clone.lhs[lhsIndex++]);
}
assertArrayEquals(model.rhs, clone.rhs);
int rhsIndex = 0;
for (IAction action : model.rhs) {
assertNotSame(action, clone.rhs[rhsIndex++]);
}
}
use of org.drools.workbench.models.datamodel.rule.IPattern in project drools-wb by kiegroup.
the class GuidedRuleModelVisitor method getConsumedModelClasses.
public Set<String> getConsumedModelClasses() {
final Set<String> factTypes = new HashSet<String>();
// Extract Fact Types from model
if (model.lhs != null) {
for (int i = 0; i < model.lhs.length; i++) {
IPattern pattern = model.lhs[i];
factTypes.addAll(visit(pattern));
}
}
if (model.rhs != null) {
for (int i = 0; i < model.rhs.length; i++) {
IAction action = model.rhs[i];
factTypes.addAll(visit(action));
}
}
// Convert Fact Types into Fully Qualified Class Names
final Set<String> fullyQualifiedClassNames = new HashSet<String>();
for (String factType : factTypes) {
fullyQualifiedClassNames.add(convertToFullyQualifiedClassName(factType));
}
return fullyQualifiedClassNames;
}
use of org.drools.workbench.models.datamodel.rule.IPattern in project drools by kiegroup.
the class RuleModelDRLPersistenceImpl method parseLhs.
private Map<String, String> parseLhs(final RuleModel m, final AndDescr lhs, final boolean isJavaDialect, final ExpandedDRLInfo expandedDRLInfo, final PackageDataModelOracle dmo) {
Map<String, String> boundParams = new HashMap<String, String>();
int lineCounter = -1;
for (BaseDescr descr : lhs.getDescrs()) {
lineCounter = parseNonDrlInLhs(m, expandedDRLInfo, lineCounter);
IPattern pattern = parseBaseDescr(m, descr, isJavaDialect, boundParams, dmo);
if (pattern != null) {
m.addLhsItem(pattern);
}
}
parseNonDrlInLhs(m, expandedDRLInfo, lineCounter);
return boundParams;
}
Aggregations