Search in sources :

Example 1 with ActionInsertFact

use of org.drools.workbench.models.datamodel.rule.ActionInsertFact in project drools by kiegroup.

the class RuleTemplateModelDRLPersistenceTest method testActionInsertFactZeroValues.

@Test
public void testActionInsertFactZeroValues() {
    TemplateModel m = new TemplateModel();
    m.name = "r1";
    FactPattern fp = new FactPattern("Person");
    fp.setBoundName("$p");
    m.addLhsItem(fp);
    ActionInsertFact aif = new ActionInsertFact("Present");
    aif.setBoundName("f0");
    ActionFieldValue afv0 = new ActionFieldValue();
    afv0.setNature(FieldNatureType.TYPE_TEMPLATE);
    afv0.setField("field1");
    afv0.setValue("$f1");
    aif.addFieldValue(afv0);
    ActionFieldValue afv1 = new ActionFieldValue();
    afv1.setNature(FieldNatureType.TYPE_TEMPLATE);
    afv1.setField("field2");
    afv1.setValue("$f2");
    aif.addFieldValue(afv1);
    m.addRhsItem(aif);
    String expected = "rule \"r1_0\"\n" + "dialect \"mvel\"\n" + "when\n" + "$p : Person()\n" + "then\n" + "Present f0 = new Present();\n" + "insert(f0);\n" + "end";
    m.addRow(new String[] { null, null });
    checkMarshall(expected, m);
}
Also used : ActionFieldValue(org.drools.workbench.models.datamodel.rule.ActionFieldValue) ActionInsertFact(org.drools.workbench.models.datamodel.rule.ActionInsertFact) CompositeFactPattern(org.drools.workbench.models.datamodel.rule.CompositeFactPattern) FactPattern(org.drools.workbench.models.datamodel.rule.FactPattern) FromCollectCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern) TemplateModel(org.drools.workbench.models.guided.template.shared.TemplateModel) Test(org.junit.Test)

Example 2 with ActionInsertFact

use of org.drools.workbench.models.datamodel.rule.ActionInsertFact in project drools by kiegroup.

the class RuleModelDRLPersistenceUnmarshallingTest method testBigDecimal3.

@Test
public // https://bugzilla.redhat.com/show_bug.cgi?id=1189930
void testBigDecimal3() 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" + "\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 3 with ActionInsertFact

use of org.drools.workbench.models.datamodel.rule.ActionInsertFact in project drools by kiegroup.

the class RuleModelDRLPersistenceUnmarshallingTest method testBigDecimal1.

@Test
public // Formatted as generated from a Guided Rule in the Workbench
void testBigDecimal1() 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 4 with ActionInsertFact

use of org.drools.workbench.models.datamodel.rule.ActionInsertFact 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 5 with ActionInsertFact

use of org.drools.workbench.models.datamodel.rule.ActionInsertFact 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)

Aggregations

ActionInsertFact (org.drools.workbench.models.datamodel.rule.ActionInsertFact)82 Test (org.junit.Test)64 ActionFieldValue (org.drools.workbench.models.datamodel.rule.ActionFieldValue)50 RuleModel (org.drools.workbench.models.datamodel.rule.RuleModel)47 FactPattern (org.drools.workbench.models.datamodel.rule.FactPattern)43 CompositeFactPattern (org.drools.workbench.models.datamodel.rule.CompositeFactPattern)42 FromCollectCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern)39 FromAccumulateCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern)34 FromCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern)34 SingleFieldConstraint (org.drools.workbench.models.datamodel.rule.SingleFieldConstraint)34 BaseSingleFieldConstraint (org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint)32 FromEntryPointFactPattern (org.drools.workbench.models.datamodel.rule.FromEntryPointFactPattern)24 IAction (org.drools.workbench.models.datamodel.rule.IAction)18 BRLActionColumn (org.drools.workbench.models.guided.dtable.shared.model.BRLActionColumn)15 BRLActionVariableColumn (org.drools.workbench.models.guided.dtable.shared.model.BRLActionVariableColumn)13 IPattern (org.drools.workbench.models.datamodel.rule.IPattern)12 GuidedDecisionTable52 (org.drools.workbench.models.guided.dtable.shared.model.GuidedDecisionTable52)12 ArrayList (java.util.ArrayList)11 CompositeFieldConstraint (org.drools.workbench.models.datamodel.rule.CompositeFieldConstraint)10 RuleAttribute (org.drools.workbench.models.datamodel.rule.RuleAttribute)10