use of org.drools.workbench.models.datamodel.rule.IAction in project drools by kiegroup.
the class RuleModelTest method testAddItemRhs.
@Test
public void testAddItemRhs() {
final RuleModel model = new RuleModel();
final IAction a0 = new ActionSetField();
final IAction a1 = new ActionSetField();
model.addRhsItem(a0);
assertEquals(1, model.rhs.length);
model.addRhsItem(a1);
assertEquals(2, model.rhs.length);
assertEquals(a0, model.rhs[0]);
assertEquals(a1, model.rhs[1]);
}
use of org.drools.workbench.models.datamodel.rule.IAction in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testBigDecimal2.
@Test
public // https://bugzilla.redhat.com/show_bug.cgi?id=1189930
void testBigDecimal2() throws Exception {
String drl = "package org.test;\n" + "import java.lang.Number;\n" + "import java.math.BigDecimal;\n" + "import java.util.Calendar;\n" + "rule \"BigDecimalRule\"\n" + " dialect \"java\"\n" + "when\n" + " $bd : BigDecimal( )\n" + "then\n" + " LastRunInformation lastRun = new LastRunInformation();\n" + " lastRun.setLastNumber($bd);\n" + " insert(lastRun);\n" + "end";
addModelField("org.test.LastRunInformation", "this", "org.test.LastRunInformation", DataType.TYPE_THIS);
addModelField("org.test.LastRunInformation", "lastNumber", BigDecimal.class.getName(), DataType.TYPE_NUMERIC_BIGDECIMAL);
when(dmo.getPackageName()).thenReturn("org.test");
final RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
assertNotNull(m);
assertEquals(1, m.lhs.length);
final IPattern p = m.lhs[0];
assertTrue(p instanceof FactPattern);
final FactPattern fp = (FactPattern) p;
assertEquals("$bd", fp.getBoundName());
assertEquals("BigDecimal", fp.getFactType());
assertEquals(1, m.rhs.length);
final IAction a = m.rhs[0];
assertTrue(a instanceof ActionInsertFact);
final ActionInsertFact aif = (ActionInsertFact) a;
assertEquals("lastRun", aif.getBoundName());
assertEquals("LastRunInformation", aif.getFactType());
assertEquals(1, aif.getFieldValues().length);
final ActionFieldValue afv0 = aif.getFieldValues()[0];
assertEquals("lastNumber", afv0.getField());
assertEquals(FieldNatureType.TYPE_VARIABLE, afv0.getNature());
assertEquals("=$bd", afv0.getValue());
// Check round-trip
assertEqualsIgnoreWhitespace(drl, RuleModelDRLPersistenceImpl.getInstance().marshal(m));
}
use of org.drools.workbench.models.datamodel.rule.IAction in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testRHSInsertFactWithFieldAsVariableSamePackage.
@Test
public void testRHSInsertFactWithFieldAsVariableSamePackage() {
// https://bugzilla.redhat.com/show_bug.cgi?id=1077212
String drl = "package org.test\n" + "rule \"variable\"\n" + "dialect \"mvel\"\n" + "when\n" + "Person( $f : field1 == 44 )\n" + "then\n" + "Person fact0 = new Person();\n" + "fact0.setField1( $f );\n" + "insert( fact0 );\n" + "end";
addModelField("org.test.Person", "field1", "java.lang.Integer", 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);
IPattern p = m.lhs[0];
assertTrue(p instanceof FactPattern);
FactPattern fp = (FactPattern) p;
assertEquals("Person", fp.getFactType());
assertEquals(1, fp.getConstraintList().getConstraints().length);
assertTrue(fp.getConstraint(0) instanceof SingleFieldConstraint);
SingleFieldConstraint sfp = (SingleFieldConstraint) fp.getConstraint(0);
assertEquals("Person", sfp.getFactType());
assertEquals("field1", sfp.getFieldName());
assertEquals("==", sfp.getOperator());
assertEquals("44", sfp.getValue());
assertEquals(BaseSingleFieldConstraint.TYPE_LITERAL, sfp.getConstraintValueType());
assertEquals("$f", sfp.getFieldBinding());
assertEquals(1, m.rhs.length);
IAction a = m.rhs[0];
assertTrue(a instanceof ActionInsertFact);
ActionInsertFact ap = (ActionInsertFact) a;
assertEquals("Person", ap.getFactType());
assertEquals("fact0", ap.getBoundName());
assertEquals(1, ap.getFieldValues().length);
ActionFieldValue afv = ap.getFieldValues()[0];
assertEquals("field1", afv.getField());
assertEquals(FieldNatureType.TYPE_VARIABLE, afv.getNature());
assertEquals("=$f", afv.getValue());
assertEquals(DataType.TYPE_NUMERIC_INTEGER, afv.getType());
}
use of org.drools.workbench.models.datamodel.rule.IAction in project drools by kiegroup.
the class RuleModelDRLPersistenceImpl method fixActionInsertFactBindings.
protected void fixActionInsertFactBindings(final IAction[] rhs) {
final Set<String> existingBindings = extractExistingActionBindings(rhs);
for (IAction action : rhs) {
if (action instanceof ActionInsertFact) {
final ActionInsertFact aif = (ActionInsertFact) action;
if (aif.getFieldValues().length > 0 && aif.getBoundName() == null) {
int idx = 0;
String binding = "fact" + idx;
while (existingBindings.contains(binding)) {
idx++;
binding = "fact" + idx;
}
existingBindings.add(binding);
aif.setBoundName(binding);
}
}
}
}
use of org.drools.workbench.models.datamodel.rule.IAction in project drools by kiegroup.
the class RuleModelDRLPersistenceImpl method marshalRHS.
protected void marshalRHS(final StringBuilder buf, final RuleModel model, final boolean isDSLEnhanced, final RHSGeneratorContextFactory generatorContextFactory) {
String indentation = "\t\t";
if (model.rhs != null) {
// Add boiler-plate for actions operating on Dates
Map<String, List<ActionFieldValue>> classes = getRHSClassDependencies(model);
if (classes.containsKey(DataType.TYPE_DATE)) {
buf.append(indentation);
if (isDSLEnhanced) {
buf.append(">");
}
buf.append("java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(\"" + DateUtils.getDateFormatMask() + "\");\n");
}
// Add boiler-plate for actions operating on WorkItems
if (!getRHSWorkItemDependencies(model).isEmpty()) {
buf.append(indentation);
buf.append("org.drools.core.process.instance.WorkItemManager wim = (org.drools.core.process.instance.WorkItemManager) drools.getWorkingMemory().getWorkItemManager();\n");
}
// Marshall the model itself
RHSActionVisitor actionVisitor = getRHSActionVisitor(isDSLEnhanced, buf, indentation, generatorContextFactory);
// Reconcile ActionSetField and ActionUpdateField calls
final List<IAction> actions = new ArrayList<IAction>();
for (IAction action : model.rhs) {
if (action instanceof ActionCallMethod) {
actions.add(action);
} else if (action instanceof ActionSetField) {
final ActionSetField asf = (ActionSetField) action;
final ActionSetFieldWrapper afw = findExistingAction(asf, actions);
if (afw == null) {
actions.add(new ActionSetFieldWrapper(asf, (asf instanceof ActionUpdateField)));
} else {
final List<ActionFieldValue> existingActionFieldValue = new ArrayList<ActionFieldValue>(Arrays.asList(afw.getAction().getFieldValues()));
for (ActionFieldValue afv : asf.getFieldValues()) {
existingActionFieldValue.add(afv);
}
final ActionFieldValue[] temp = new ActionFieldValue[existingActionFieldValue.size()];
afw.getAction().setFieldValues(existingActionFieldValue.toArray(temp));
}
} else {
actions.add(action);
}
}
model.rhs = new IAction[actions.size()];
for (int i = 0; i < actions.size(); i++) {
final IAction action = actions.get(i);
if (action instanceof ActionSetFieldWrapper) {
model.rhs[i] = ((ActionSetFieldWrapper) action).getAction();
} else {
model.rhs[i] = action;
}
}
for (IAction action : model.rhs) {
if (action instanceof PluggableIAction) {
PluggableIAction processedIAction = (PluggableIAction) actionVisitor.preProcessIActionForExtensions(action);
buf.append(indentation).append(processedIAction.getStringRepresentation()).append(";\n");
} else {
actionVisitor.visit(action);
}
}
}
}
Aggregations