use of org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern in project drools-wb by kiegroup.
the class GuidedRuleEditorValidatorTest method testFromAccumulateCompositePatternMissingValuesWithExistingFrom.
@Test
public void testFromAccumulateCompositePatternMissingValuesWithExistingFrom() throws Exception {
FactPattern pattern1 = new FactPattern("Person");
SingleFieldConstraint constraint1 = new SingleFieldConstraint("name");
constraint1.setOperator("==");
pattern1.addConstraint(constraint1);
FactPattern pattern2 = new FactPattern("Address");
SingleFieldConstraint constraint2 = new SingleFieldConstraint("street");
constraint2.setOperator("!=");
pattern2.addConstraint(constraint2);
FromAccumulateCompositeFactPattern fromAccumulateCompositeFactPattern = new FromAccumulateCompositeFactPattern();
fromAccumulateCompositeFactPattern.setSourcePattern(pattern1);
fromAccumulateCompositeFactPattern.setFactPattern(pattern2);
fromAccumulateCompositeFactPattern.setInitCode("int i = 0");
fromAccumulateCompositeFactPattern.setActionCode(" i++;");
fromAccumulateCompositeFactPattern.setReverseCode("i--;");
fromAccumulateCompositeFactPattern.setResultCode("return i");
model.lhs = new IPattern[] { fromAccumulateCompositeFactPattern };
assertFalse(validator.isValid());
assertEquals(2, validator.getErrors().size());
verify(constants, never()).WhenUsingFromTheSourceNeedsToBeSet();
verify(constants).FactType0HasAField1ThatHasAnOperatorSetButNoValuePleaseAddAValueOrRemoveTheOperator("Person", "name");
verify(constants).FactType0HasAField1ThatHasAnOperatorSetButNoValuePleaseAddAValueOrRemoveTheOperator("Address", "street");
}
use of org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern in project drools-wb by kiegroup.
the class FromAccumulateCompositeFactPatternWidget method showSourcePatternSelector.
/**
* Pops up the fact selector.
*/
protected void showSourcePatternSelector() {
final ListBox box = new ListBox();
AsyncPackageDataModelOracle oracle = this.getModeller().getDataModelOracle();
String[] facts = oracle.getFactTypes();
box.addItem(GuidedRuleEditorResources.CONSTANTS.Choose());
for (int i = 0; i < facts.length; i++) {
box.addItem(facts[i]);
}
box.setSelectedIndex(0);
final FormStylePopup popup = new FormStylePopup(GuidedRuleEditorResources.CONSTANTS.NewFactPattern());
popup.addAttribute(GuidedRuleEditorResources.CONSTANTS.chooseFactType(), box);
box.addChangeHandler(new ChangeHandler() {
public void onChange(ChangeEvent event) {
getFromAccumulatePattern().setSourcePattern(new FactPattern(box.getItemText(box.getSelectedIndex())));
setModified(true);
getModeller().refreshWidget();
popup.hide();
}
});
final Button fromBtn = new Button(HumanReadableConstants.INSTANCE.From());
final Button fromAccumulateBtn = new Button(HumanReadableConstants.INSTANCE.FromAccumulate());
final Button fromCollectBtn = new Button(HumanReadableConstants.INSTANCE.FromCollect());
final Button fromEntryPointBtn = new Button(HumanReadableConstants.INSTANCE.FromEntryPoint());
ClickHandler btnsClickHandler = new ClickHandler() {
public void onClick(ClickEvent event) {
Widget sender = (Widget) event.getSource();
if (sender == fromBtn) {
getFromAccumulatePattern().setSourcePattern(new FromCompositeFactPattern());
} else if (sender == fromAccumulateBtn) {
getFromAccumulatePattern().setSourcePattern(new FromAccumulateCompositeFactPattern());
} else if (sender == fromCollectBtn) {
getFromAccumulatePattern().setSourcePattern(new FromCollectCompositeFactPattern());
} else if (sender == fromEntryPointBtn) {
getFromAccumulatePattern().setSourcePattern(new FromEntryPointFactPattern());
} else {
throw new IllegalArgumentException("Unknown sender: " + sender);
}
setModified(true);
getModeller().refreshWidget();
popup.hide();
}
};
fromBtn.addClickHandler(btnsClickHandler);
fromAccumulateBtn.addClickHandler(btnsClickHandler);
fromCollectBtn.addClickHandler(btnsClickHandler);
fromEntryPointBtn.addClickHandler(btnsClickHandler);
popup.addAttribute("", fromBtn);
popup.addAttribute("", fromAccumulateBtn);
popup.addAttribute("", fromCollectBtn);
popup.addAttribute("", fromEntryPointBtn);
popup.show();
}
use of org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern in project drools by kiegroup.
the class RuleModelDRLPersistenceTest method testFromAccumulateWithEmbeddedFromEntryPoint.
@Test
public void testFromAccumulateWithEmbeddedFromEntryPoint() {
RuleModel m = new RuleModel();
m.name = "r1";
SingleFieldConstraint sfc = new SingleFieldConstraint("bar");
sfc.setFactType(DataType.TYPE_NUMERIC_INTEGER);
sfc.setFieldBinding("$a");
sfc.setOperator("==");
sfc.setValue("777");
FactPattern fp = new FactPattern("Foo");
fp.addConstraint(sfc);
FromEntryPointFactPattern fep = new FromEntryPointFactPattern();
fep.setEntryPointName("ep");
fep.setFactPattern(fp);
FromAccumulateCompositeFactPattern fac = new FromAccumulateCompositeFactPattern();
fac.setSourcePattern(fep);
fac.setFactPattern(new FactPattern("java.util.List"));
fac.setFunction("max($a)");
m.addLhsItem(fac);
String expected = "rule \"r1\"\n" + "dialect \"mvel\"\n" + "when\n" + "java.util.List( ) from accumulate ( Foo( $a : bar == 777 ) from entry-point \"ep\", \n" + "max($a))\n" + "then\n" + "end";
checkMarshalling(expected, m);
}
use of org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testLHSCustomAccumulateFunctions.
@Test
public // https://bugzilla.redhat.com/show_bug.cgi?id=1136100
void testLHSCustomAccumulateFunctions() throws Exception {
String drl = "package org.test;\n" + "rule \"test\"\n" + "dialect \"mvel\"\n" + "when\n" + "Double( ) from accumulate ( Applicant( $a : age != null ),\n" + "init( double total = 0; ),\n" + "action( total += $a; ),\n" + "reverse( total -= $a; ),\n" + "result( new Double( total ) ) )\n" + "then\n" + "end";
addModelField(Double.class.getName(), "this", Double.class.getName(), DataType.TYPE_THIS);
addModelField("org.test.Applicant", "this", "org.test.Applicant", DataType.TYPE_THIS);
addModelField("org.test.Applicant", "age", Integer.class.getName(), DataType.TYPE_NUMERIC_INTEGER);
when(dmo.getPackageName()).thenReturn("org.test");
final RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
assertNotNull(m);
assertEquals(1, m.lhs.length);
assertEquals(0, m.rhs.length);
assertTrue(m.lhs[0] instanceof FromAccumulateCompositeFactPattern);
final FromAccumulateCompositeFactPattern facfp = (FromAccumulateCompositeFactPattern) m.lhs[0];
assertEquals("double total = 0;", facfp.getInitCode());
assertEquals("total += $a;", facfp.getActionCode());
assertEquals("total -= $a;", facfp.getReverseCode());
assertEquals("new Double( total )", facfp.getResultCode());
assertNotNull(facfp.getFactPattern());
final FactPattern fp = facfp.getFactPattern();
assertEquals("Double", fp.getFactType());
assertEquals(0, fp.getNumberOfConstraints());
assertNotNull(facfp.getSourcePattern());
assertTrue(facfp.getSourcePattern() instanceof FactPattern);
final FactPattern fsp = (FactPattern) facfp.getSourcePattern();
assertEquals("Applicant", fsp.getFactType());
assertEquals(1, fsp.getNumberOfConstraints());
assertTrue(fsp.getConstraint(0) instanceof SingleFieldConstraint);
final SingleFieldConstraint sfc = (SingleFieldConstraint) fsp.getConstraint(0);
assertEquals("$a", sfc.getFieldBinding());
assertEquals("Applicant", sfc.getFactType());
assertEquals("!= null", sfc.getOperator());
assertEqualsIgnoreWhitespace(drl, RuleModelDRLPersistenceImpl.getInstance().marshal(m));
}
use of org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern in project drools-wb by kiegroup.
the class RuleModelCloneVisitorTest method buildFromAccumulateCompositeFactPattern.
private static FromAccumulateCompositeFactPattern buildFromAccumulateCompositeFactPattern() {
FromAccumulateCompositeFactPattern facc = new FromAccumulateCompositeFactPattern();
facc.setActionCode("action code; ");
facc.setExpression(buildExpressionFormLine());
facc.setFactPattern(buildFactPattern());
facc.setFunction("function;");
facc.setInitCode("setPlugin code;");
facc.setResultCode("result code;");
facc.setReverseCode("reverse code;");
facc.setSourcePattern(buildExpressionFormLine());
return facc;
}
Aggregations