use of org.drools.workbench.models.datamodel.rule.FreeFormLine 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.FreeFormLine in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testMultipleFromKeywords.
@Test
public // https://bugzilla.redhat.com/show_bug.cgi?id=1191737
void testMultipleFromKeywords() throws Exception {
String drl = "package org.test;\n" + "rule \"ToyWithoutName \"\n" + "dialect \"java\"\n" + "when\n" + " $father: Father()\n" + " ($kid: Kid() from $father.kids)\n" + " ($toy: Toy(name == null) from $kid.toys)\n" + "then\n" + " System.out.println(\"blabla\");\n" + "end";
addModelField("org.test.Father", "this", "org.test.Father", DataType.TYPE_THIS);
addModelField("org.test.Father", "kids", "org.test.Kid", DataType.TYPE_COLLECTION);
addModelField("org.test.Kid", "this", "org.test.Kid", DataType.TYPE_THIS);
addModelField("org.test.Kid", "toys", "org.test.Toy", DataType.TYPE_COLLECTION);
addModelField("org.test.Toy", "this", "org.test.Toy", DataType.TYPE_THIS);
addModelField("org.test.Toy", "name", "java.lang.String", DataType.TYPE_STRING);
when(dmo.getPackageName()).thenReturn("org.test");
final RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
assertNotNull(m);
assertEquals(3, m.lhs.length);
final IPattern p0 = m.lhs[0];
assertTrue(p0 instanceof FactPattern);
final FactPattern fp0 = (FactPattern) p0;
assertEquals("$father", fp0.getBoundName());
assertEquals("Father", fp0.getFactType());
final IPattern p1 = m.lhs[1];
assertTrue(p1 instanceof FromCompositeFactPattern);
final FromCompositeFactPattern fp1 = (FromCompositeFactPattern) p1;
assertEquals("$kid", fp1.getFactPattern().getBoundName());
assertEquals("Kid", fp1.getFactType());
final IPattern p2 = m.lhs[2];
assertTrue(p2 instanceof FromCompositeFactPattern);
final FromCompositeFactPattern fp2 = (FromCompositeFactPattern) p2;
assertEquals("$toy", fp2.getFactPattern().getBoundName());
assertEquals("Toy", fp2.getFactType());
assertEquals(1, m.rhs.length);
final IAction a = m.rhs[0];
assertTrue(a instanceof FreeFormLine);
final FreeFormLine affl = (FreeFormLine) a;
assertEquals("System.out.println(\"blabla\");", affl.getText());
// Check round-trip
assertEqualsIgnoreWhitespace(drl, RuleModelDRLPersistenceImpl.getInstance().marshal(m));
}
use of org.drools.workbench.models.datamodel.rule.FreeFormLine in project drools-wb by kiegroup.
the class RuleModelCloneVisitor method visitFreeFormLine.
private FreeFormLine visitFreeFormLine(FreeFormLine ffl) {
FreeFormLine clone = new FreeFormLine();
clone.setText(ffl.getText());
return clone;
}
use of org.drools.workbench.models.datamodel.rule.FreeFormLine in project drools-wb by kiegroup.
the class RuleModelCloneVisitorTest method buildFreeFormLine.
private static FreeFormLine buildFreeFormLine() {
FreeFormLine ffl = new FreeFormLine();
ffl.setText("text");
return ffl;
}
use of org.drools.workbench.models.datamodel.rule.FreeFormLine in project drools-wb by kiegroup.
the class RuleModellerWidgetFactory method getWidget.
public RuleModellerWidget getWidget(RuleModeller ruleModeller, EventBus eventBus, IAction action, Boolean readOnly) {
if (action instanceof ActionCallMethod) {
return new ActionCallMethodWidget(ruleModeller, eventBus, (ActionCallMethod) action, readOnly);
}
if (action instanceof ActionSetField) {
return new ActionSetFieldWidget(ruleModeller, eventBus, (ActionSetField) action, readOnly);
}
if (action instanceof ActionInsertFact) {
return new ActionInsertFactWidget(ruleModeller, eventBus, (ActionInsertFact) action, readOnly);
}
if (action instanceof ActionRetractFact) {
return new ActionRetractFactWidget(ruleModeller, eventBus, (ActionRetractFact) action, readOnly);
}
if (action instanceof DSLSentence) {
RuleModellerWidget w = new DSLSentenceWidget(ruleModeller, eventBus, (DSLSentence) action, readOnly);
return w;
}
if (action instanceof FreeFormLine) {
return new FreeFormLineWidget(ruleModeller, eventBus, (FreeFormLine) action, readOnly);
}
if (action instanceof ActionGlobalCollectionAdd) {
return new GlobalCollectionAddWidget(ruleModeller, eventBus, (ActionGlobalCollectionAdd) action, readOnly);
}
// All hardcoded action widgets have been checked, perform a plugin lookup
List<RuleModellerActionPlugin> matchingActionPlugins = actionPlugins.stream().filter(p -> p.accept(action)).collect(Collectors.toList());
if (matchingActionPlugins.size() > 1) {
throw new IllegalStateException("Ambigious " + RuleModellerActionPlugin.class.getName() + " implementations for action " + action);
}
if (matchingActionPlugins.size() == 1) {
RuleModellerActionPlugin actionPlugin = matchingActionPlugins.get(0);
RuleModellerWidget ruleModellerWidget = actionPlugin.createWidget(ruleModeller, eventBus, action, readOnly);
return ruleModellerWidget;
}
// NON-NLS
throw new RuntimeException("I don't know what type of action is: " + action);
}
Aggregations