use of org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testFromAccumulate.
@Test
public void testFromAccumulate() {
String drl = "package org.test;\n" + "import java.lang.Number;\n" + "rule \"rule1\"\n" + "when\n" + " total : Number( intValue > 0 ) from accumulate ( Applicant( age < 30 ), count() )\n" + "then\n" + "end";
addModelField("java.lang.Number", "intValue", "java.lang.Integer", DataType.TYPE_NUMERIC_INTEGER);
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");
RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
assertNotNull(m);
assertTrue(m.lhs[0] instanceof FromAccumulateCompositeFactPattern);
FromAccumulateCompositeFactPattern pattern = (FromAccumulateCompositeFactPattern) m.lhs[0];
assertNotNull(pattern.getFactPattern());
FactPattern factPattern = pattern.getFactPattern();
assertEquals("total", factPattern.getBoundName());
assertNotNull(factPattern.getConstraintList());
assertEquals(1, factPattern.getConstraintList().getNumberOfConstraints());
FieldConstraint constraint = factPattern.getConstraintList().getConstraint(0);
assertTrue(constraint instanceof SingleFieldConstraint);
SingleFieldConstraint fieldConstraint = (SingleFieldConstraint) constraint;
assertEquals("Number", fieldConstraint.getFactType());
assertEquals("intValue", fieldConstraint.getFieldName());
assertEquals("Integer", fieldConstraint.getFieldType());
assertEquals(">", fieldConstraint.getOperator());
assertEquals("0", fieldConstraint.getValue());
}
use of org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern in project drools by kiegroup.
the class RuleModelDRLPersistenceImpl method parsePatternSource.
private IFactPattern parsePatternSource(final RuleModel m, final PatternDescr pattern, final PatternSourceDescr patternSource, final boolean isJavaDialect, final Map<String, String> boundParams, final PackageDataModelOracle dmo) {
if (pattern.getIdentifier() != null) {
boundParams.put(pattern.getIdentifier(), pattern.getObjectType());
}
if (patternSource instanceof AccumulateDescr) {
AccumulateDescr accumulate = (AccumulateDescr) patternSource;
FromAccumulateCompositeFactPattern fac = new FromAccumulateCompositeFactPattern();
fac.setSourcePattern(parseBaseDescr(m, accumulate.getInput(), isJavaDialect, boundParams, dmo));
fac.setInitCode(accumulate.getInitCode());
fac.setActionCode(accumulate.getActionCode());
fac.setReverseCode(accumulate.getReverseCode());
fac.setResultCode(accumulate.getResultCode());
FactPattern factPattern = new FactPattern(pattern.getObjectType());
factPattern.setBoundName(pattern.getIdentifier());
parseConstraint(m, factPattern, pattern.getConstraint(), isJavaDialect, boundParams, dmo);
fac.setFactPattern(factPattern);
for (AccumulateDescr.AccumulateFunctionCallDescr func : accumulate.getFunctions()) {
String funcName = func.getFunction();
boolean first = true;
StringBuilder sb = new StringBuilder();
for (String param : func.getParams()) {
if (first) {
first = false;
} else {
sb.append(", ");
}
sb.append(param);
}
fac.setFunction(funcName + "(" + sb + ")");
break;
}
return fac;
} else if (patternSource instanceof CollectDescr) {
CollectDescr collect = (CollectDescr) patternSource;
FromCollectCompositeFactPattern fac = new FromCollectCompositeFactPattern();
fac.setRightPattern(parseBaseDescr(m, collect.getInputPattern(), isJavaDialect, boundParams, dmo));
fac.setFactPattern(getFactPattern(m, pattern, isJavaDialect, boundParams, dmo));
return fac;
} else if (patternSource instanceof EntryPointDescr) {
EntryPointDescr entryPoint = (EntryPointDescr) patternSource;
FromEntryPointFactPattern fep = new FromEntryPointFactPattern();
fep.setEntryPointName(entryPoint.getText());
fep.setFactPattern(getFactPattern(m, pattern, isJavaDialect, boundParams, dmo));
return fep;
} else if (patternSource instanceof FromDescr) {
FromDescr from = (FromDescr) patternSource;
FromCompositeFactPattern fcfp = new FromCompositeFactPattern();
FactPattern factPattern = new FactPattern(pattern.getObjectType());
factPattern.setBoundName(pattern.getIdentifier());
parseConstraint(m, factPattern, pattern.getConstraint(), isJavaDialect, boundParams, dmo);
fcfp.setFactPattern(factPattern);
ExpressionFormLine expression = new ExpressionFormLine();
fcfp.setExpression(expression);
String dataSource = from.getDataSource().toString();
String[] splitSource = dataSource.split("\\.");
ModelField[] fields = null;
for (int i = 0; i < splitSource.length; i++) {
String sourcePart = splitSource[i];
if (i == 0) {
String type = boundParams.get(sourcePart);
expression.appendPart(new ExpressionVariable(sourcePart, type, DataType.TYPE_NUMERIC));
fields = findFields(m, dmo, type);
} else {
ModelField modelField = null;
if (fields != null) {
for (ModelField field : fields) {
if (field.getName().equals(sourcePart)) {
modelField = field;
break;
}
}
}
if (modelField == null) {
final String previousClassName = expression.getClassType();
final List<MethodInfo> mis = dmo.getModuleMethodInformation().get(previousClassName);
boolean isMethod = false;
if (mis != null) {
for (MethodInfo mi : mis) {
if (mi.getName().equals(sourcePart)) {
expression.appendPart(new ExpressionMethod(mi.getName(), mi.getReturnClassType(), mi.getGenericType(), mi.getParametricReturnType()));
isMethod = true;
break;
}
}
}
if (isMethod == false) {
expression.appendPart(new ExpressionText(sourcePart));
}
} else {
expression.appendPart(new ExpressionField(sourcePart, modelField.getClassName(), modelField.getType()));
fields = findFields(m, dmo, modelField.getClassName());
}
}
}
return fcfp;
}
throw new RuntimeException("Unknown pattern source " + patternSource);
}
use of org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern in project drools by kiegroup.
the class RuleModelDRLPersistenceTest method testDSLWithFromAccumulate.
@Test
public void testDSLWithFromAccumulate() {
final RuleModel m = new RuleModel();
m.name = "testDSLWithFromAccumulate";
m.lhs = new IPattern[2];
m.rhs = new IAction[0];
final FromAccumulateCompositeFactPattern from = new FromAccumulateCompositeFactPattern();
final FactPattern person = new FactPattern("Person");
person.setBoundName("$p");
m.lhs[0] = person;
from.setFactPattern(new FactPattern("java.lang.Number"));
from.setSourcePattern(person);
from.setFunction("count($p)");
m.lhs[1] = from;
final RuleModel modelSpy = spy(m);
doReturn(true).when(modelSpy).hasDSLSentences();
final String drl = ruleModelPersistence.marshal(modelSpy);
final RuleModel tempModel = spy(ruleModelPersistence.unmarshalUsingDSL(drl, null, dmo, ""));
doReturn(true).when(tempModel).hasDSLSentences();
final String newDrl = ruleModelPersistence.marshal(tempModel);
final RuleModel newModel = ruleModelPersistence.unmarshalUsingDSL(newDrl, null, dmo, "");
assertTrue(newModel.lhs[0] instanceof FactPattern);
assertTrue(newModel.lhs[1] instanceof FromAccumulateCompositeFactPattern);
assertDSLEscaping(newDrl);
Assertions.assertThat(newDrl).contains(">java.lang.Number( ) from accumulate ( $p : Person( ),\n").contains(">\t\t\tcount($p))");
}
use of org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern in project drools-wb by kiegroup.
the class RuleModelCloneVisitor method visitFromAccumulateCompositeFactPattern.
private FromAccumulateCompositeFactPattern visitFromAccumulateCompositeFactPattern(FromAccumulateCompositeFactPattern pattern) {
FromAccumulateCompositeFactPattern clone = new FromAccumulateCompositeFactPattern();
clone.setActionCode(pattern.getActionCode());
clone.setExpression((ExpressionFormLine) visit(pattern.getExpression()));
clone.setFactPattern((FactPattern) visit(pattern.getFactPattern()));
clone.setFunction(pattern.getFunction());
clone.setInitCode(pattern.getInitCode());
clone.setResultCode(pattern.getResultCode());
clone.setReverseCode(pattern.getReverseCode());
clone.setSourcePattern((IPattern) visit(pattern.getSourcePattern()));
return clone;
}
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();
}
Aggregations