Search in sources :

Example 6 with IAction

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]);
}
Also used : ActionSetField(org.drools.workbench.models.datamodel.rule.ActionSetField) IAction(org.drools.workbench.models.datamodel.rule.IAction) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) Test(org.junit.Test)

Example 7 with IAction

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));
}
Also used : IPattern(org.drools.workbench.models.datamodel.rule.IPattern) IAction(org.drools.workbench.models.datamodel.rule.IAction) ActionFieldValue(org.drools.workbench.models.datamodel.rule.ActionFieldValue) ActionInsertFact(org.drools.workbench.models.datamodel.rule.ActionInsertFact) CompositeFactPattern(org.drools.workbench.models.datamodel.rule.CompositeFactPattern) FromCollectCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern) FactPattern(org.drools.workbench.models.datamodel.rule.FactPattern) FromAccumulateCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern) FromCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 8 with IAction

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());
}
Also used : IPattern(org.drools.workbench.models.datamodel.rule.IPattern) SingleFieldConstraint(org.drools.workbench.models.datamodel.rule.SingleFieldConstraint) BaseSingleFieldConstraint(org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint) IAction(org.drools.workbench.models.datamodel.rule.IAction) ActionFieldValue(org.drools.workbench.models.datamodel.rule.ActionFieldValue) ActionInsertFact(org.drools.workbench.models.datamodel.rule.ActionInsertFact) CompositeFactPattern(org.drools.workbench.models.datamodel.rule.CompositeFactPattern) FromCollectCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern) FactPattern(org.drools.workbench.models.datamodel.rule.FactPattern) FromAccumulateCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern) FromCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) Test(org.junit.Test)

Example 9 with IAction

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);
            }
        }
    }
}
Also used : PluggableIAction(org.drools.workbench.models.datamodel.rule.PluggableIAction) IAction(org.drools.workbench.models.datamodel.rule.IAction) ActionInsertFact(org.drools.workbench.models.datamodel.rule.ActionInsertFact) FieldConstraint(org.drools.workbench.models.datamodel.rule.FieldConstraint) ConnectiveConstraint(org.drools.workbench.models.datamodel.rule.ConnectiveConstraint) CompositeFieldConstraint(org.drools.workbench.models.datamodel.rule.CompositeFieldConstraint) SingleFieldConstraint(org.drools.workbench.models.datamodel.rule.SingleFieldConstraint) BaseSingleFieldConstraint(org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint)

Example 10 with IAction

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);
            }
        }
    }
}
Also used : PluggableIAction(org.drools.workbench.models.datamodel.rule.PluggableIAction) IAction(org.drools.workbench.models.datamodel.rule.IAction) PluggableIAction(org.drools.workbench.models.datamodel.rule.PluggableIAction) ArrayList(java.util.ArrayList) ActionCallMethod(org.drools.workbench.models.datamodel.rule.ActionCallMethod) FieldConstraint(org.drools.workbench.models.datamodel.rule.FieldConstraint) ConnectiveConstraint(org.drools.workbench.models.datamodel.rule.ConnectiveConstraint) CompositeFieldConstraint(org.drools.workbench.models.datamodel.rule.CompositeFieldConstraint) SingleFieldConstraint(org.drools.workbench.models.datamodel.rule.SingleFieldConstraint) BaseSingleFieldConstraint(org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint) ActionSetField(org.drools.workbench.models.datamodel.rule.ActionSetField) ActionUpdateField(org.drools.workbench.models.datamodel.rule.ActionUpdateField) ActionFieldValue(org.drools.workbench.models.datamodel.rule.ActionFieldValue) ArrayList(java.util.ArrayList) StringUtils.splitArgumentsList(org.drools.core.util.StringUtils.splitArgumentsList) List(java.util.List) ActionFieldList(org.drools.workbench.models.datamodel.rule.ActionFieldList)

Aggregations

IAction (org.drools.workbench.models.datamodel.rule.IAction)63 Test (org.junit.Test)44 IPattern (org.drools.workbench.models.datamodel.rule.IPattern)35 SingleFieldConstraint (org.drools.workbench.models.datamodel.rule.SingleFieldConstraint)32 RuleModel (org.drools.workbench.models.datamodel.rule.RuleModel)31 FactPattern (org.drools.workbench.models.datamodel.rule.FactPattern)30 CompositeFactPattern (org.drools.workbench.models.datamodel.rule.CompositeFactPattern)29 ActionFieldValue (org.drools.workbench.models.datamodel.rule.ActionFieldValue)25 BRLActionColumn (org.drools.workbench.models.guided.dtable.shared.model.BRLActionColumn)21 BaseSingleFieldConstraint (org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint)20 FromAccumulateCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern)20 FromCollectCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern)20 FromCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern)20 ArrayList (java.util.ArrayList)19 BRLActionVariableColumn (org.drools.workbench.models.guided.dtable.shared.model.BRLActionVariableColumn)19 ActionInsertFact (org.drools.workbench.models.datamodel.rule.ActionInsertFact)17 FreeFormLine (org.drools.workbench.models.datamodel.rule.FreeFormLine)17 GuidedDecisionTable52 (org.drools.workbench.models.guided.dtable.shared.model.GuidedDecisionTable52)17 DescriptionCol52 (org.drools.workbench.models.guided.dtable.shared.model.DescriptionCol52)15 RowNumberCol52 (org.drools.workbench.models.guided.dtable.shared.model.RowNumberCol52)15