use of org.drools.workbench.models.datamodel.rule.IPattern in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testLHSFormula.
@Test
public // https://issues.jboss.org/browse/DROOLS-713
void testLHSFormula() throws Exception {
String drl = "package org.test;\n" + "rule \"MyRule\"\n" + "dialect \"java\"\n" + "agenda-group \"MyGroup\"\n" + "salience 900\n" + "when\n" + " $bundle : MyClass( $protocolSequence : protocolSequence )\n" + " eval( $protocolSequence != null )\n" + " $followupBundle : MyClass( protocolSequence == ( $protocolSequence + 1 ) )\n" + "then\n" + "end";
addModelField("org.test.MyClass", "this", "org.test.MyClass", DataType.TYPE_THIS);
addModelField("org.test.MyClass", "protocolSequence", 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(3, m.lhs.length);
final IPattern p0 = m.lhs[0];
assertTrue(p0 instanceof FactPattern);
final FactPattern fp0 = (FactPattern) p0;
assertEquals("$bundle", fp0.getBoundName());
assertEquals("MyClass", fp0.getFactType());
final IPattern p1 = m.lhs[1];
assertTrue(p1 instanceof FreeFormLine);
final FreeFormLine ffl1 = (FreeFormLine) p1;
assertEquals("eval( $protocolSequence != null )", ffl1.getText());
final IPattern p2 = m.lhs[2];
assertTrue(p2 instanceof FactPattern);
final FactPattern fp2 = (FactPattern) p2;
assertEquals("$followupBundle", fp2.getBoundName());
assertEquals("MyClass", fp2.getFactType());
assertEquals(1, fp2.getNumberOfConstraints());
assertTrue(fp2.getConstraint(0) instanceof SingleFieldConstraint);
final SingleFieldConstraint sfc1 = (SingleFieldConstraint) fp2.getConstraint(0);
assertEquals("MyClass", sfc1.getFactType());
assertEquals("protocolSequence", sfc1.getFieldName());
assertEquals(DataType.TYPE_NUMERIC_INTEGER, sfc1.getFieldType());
assertEquals("==", sfc1.getOperator());
assertEquals("$protocolSequence + 1", sfc1.getValue());
assertEquals(BaseSingleFieldConstraint.TYPE_RET_VALUE, sfc1.getConstraintValueType());
// Check round-trip
assertEqualsIgnoreWhitespace(drl, RuleModelDRLPersistenceImpl.getInstance().marshal(m));
}
use of org.drools.workbench.models.datamodel.rule.IPattern in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testRHSChainedMethodCalls2.
@Test
public // https://bugzilla.redhat.com/show_bug.cgi?id=1127303
void testRHSChainedMethodCalls2() throws Exception {
String drl = "package org.test;\n" + "rule \"MyRule\"\n" + "dialect \"mvel\"\n" + "when\n" + " Person( $n : name )\n" + "then\n" + " $n.toUpperCase().indexOf(\".\", 1);\n" + "end";
addModelField("org.test.Person", "this", "org.test.Person", DataType.TYPE_THIS);
addModelField("org.test.Person", "name", String.class.getName(), DataType.TYPE_STRING);
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 p0 = m.lhs[0];
assertTrue(p0 instanceof FactPattern);
final FactPattern fp0 = (FactPattern) p0;
assertEquals("Person", fp0.getFactType());
assertEquals(1, fp0.getNumberOfConstraints());
assertTrue(fp0.getConstraint(0) instanceof SingleFieldConstraint);
final SingleFieldConstraint sfc1 = (SingleFieldConstraint) fp0.getConstraint(0);
assertEquals("Person", sfc1.getFactType());
assertEquals("name", sfc1.getFieldName());
assertEquals(DataType.TYPE_STRING, sfc1.getFieldType());
assertEquals(1, m.rhs.length);
final IAction a0 = m.rhs[0];
assertTrue(a0 instanceof FreeFormLine);
final FreeFormLine ffl1 = (FreeFormLine) a0;
assertEquals("$n.toUpperCase().indexOf(\".\", 1);", ffl1.getText());
// Check round-trip
assertEqualsIgnoreWhitespace(drl, RuleModelDRLPersistenceImpl.getInstance().marshal(m));
}
use of org.drools.workbench.models.datamodel.rule.IPattern in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testLHSInOperatorFieldNameContainingNotInLiteral.
@Test
public void testLHSInOperatorFieldNameContainingNotInLiteral() {
String drl = "package org.test\n" + "rule \"in\"\n" + "dialect \"mvel\"\n" + "when\n" + "Person( rating not in (1, 2) )\n" + "then\n" + "end";
addModelField("org.test.Person", "rating", "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("rating", sfp.getFieldName());
assertEquals("not in", sfp.getOperator());
assertEquals("1, 2", sfp.getValue());
assertEquals(BaseSingleFieldConstraint.TYPE_LITERAL, sfp.getConstraintValueType());
}
use of org.drools.workbench.models.datamodel.rule.IPattern in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testLHSValidLiteralFieldName.
@Test
public // https://issues.jboss.org/browse/DROOLS-715
void testLHSValidLiteralFieldName() throws Exception {
String drl = "package org.test;\n" + "rule \"MyRule\"\n" + "dialect \"mvel\"\n" + "when\n" + " MyClass( valid == true )\n" + "then\n" + "end";
addModelField("org.test.MyClass", "this", "org.test.MyClass", DataType.TYPE_THIS);
addModelField("org.test.MyClass", "valid", Boolean.class.getName(), DataType.TYPE_BOOLEAN);
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 p0 = m.lhs[0];
assertTrue(p0 instanceof FactPattern);
final FactPattern fp0 = (FactPattern) p0;
assertEquals("MyClass", fp0.getFactType());
assertEquals(1, fp0.getNumberOfConstraints());
assertTrue(fp0.getConstraint(0) instanceof SingleFieldConstraint);
final SingleFieldConstraint sfc1 = (SingleFieldConstraint) fp0.getConstraint(0);
assertEquals("MyClass", sfc1.getFactType());
assertEquals("valid", sfc1.getFieldName());
assertEquals(DataType.TYPE_BOOLEAN, sfc1.getFieldType());
assertEquals("==", sfc1.getOperator());
assertEquals("true", sfc1.getValue());
assertEquals(BaseSingleFieldConstraint.TYPE_ENUM, sfc1.getConstraintValueType());
// Check round-trip
assertEqualsIgnoreWhitespace(drl, RuleModelDRLPersistenceImpl.getInstance().marshal(m));
}
use of org.drools.workbench.models.datamodel.rule.IPattern 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));
}
Aggregations