use of org.drools.workbench.models.datamodel.rule.FactPattern in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testCompositeFactPatternWithOr.
@Test
public void testCompositeFactPatternWithOr() {
String drl = "rule \"rule1\"\n" + "when\n" + "( Person( age == 42 ) or Person( age == 43 ) )\n" + "then\n" + "end";
RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
assertNotNull(m);
assertEquals("rule1", m.name);
// LHS Pattern
assertEquals(1, m.lhs.length);
IPattern p = m.lhs[0];
assertTrue(p instanceof CompositeFactPattern);
CompositeFactPattern cfp = (CompositeFactPattern) p;
assertEquals(CompositeFactPattern.COMPOSITE_TYPE_OR, cfp.getType());
// LHS sub-patterns
assertEquals(2, cfp.getPatterns().length);
IPattern cfp_p1 = cfp.getPatterns()[0];
assertTrue(cfp_p1 instanceof FactPattern);
FactPattern fp1 = (FactPattern) cfp_p1;
assertEquals("Person", fp1.getFactType());
assertEquals(1, fp1.getConstraintList().getConstraints().length);
assertTrue(fp1.getConstraint(0) instanceof SingleFieldConstraint);
SingleFieldConstraint cfp_sfp1 = (SingleFieldConstraint) fp1.getConstraint(0);
assertEquals("Person", cfp_sfp1.getFactType());
assertEquals("age", cfp_sfp1.getFieldName());
assertEquals("==", cfp_sfp1.getOperator());
assertEquals("42", cfp_sfp1.getValue());
assertEquals(BaseSingleFieldConstraint.TYPE_LITERAL, cfp_sfp1.getConstraintValueType());
IPattern cfp_p2 = cfp.getPatterns()[1];
assertTrue(cfp_p2 instanceof FactPattern);
FactPattern fp2 = (FactPattern) cfp_p2;
assertEquals("Person", fp2.getFactType());
assertTrue(fp2.getConstraint(0) instanceof SingleFieldConstraint);
SingleFieldConstraint cfp_sfp2 = (SingleFieldConstraint) fp2.getConstraint(0);
assertEquals("Person", cfp_sfp2.getFactType());
assertEquals("age", cfp_sfp2.getFieldName());
assertEquals("==", cfp_sfp2.getOperator());
assertEquals("43", cfp_sfp2.getValue());
assertEquals(BaseSingleFieldConstraint.TYPE_LITERAL, cfp_sfp2.getConstraintValueType());
}
use of org.drools.workbench.models.datamodel.rule.FactPattern in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method compositeFieldConstraintWithTwoPredicates.
@Test
public void compositeFieldConstraintWithTwoPredicates() {
String drl = "rule \"rule1\"\n" + "when\n" + "Person( eval( age > 18 ) && eval(age < 45) )\n" + "then\n" + "end";
RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
assertNotNull(m);
assertEquals("rule1", m.name);
// LHS Pattern
assertEquals(1, m.lhs.length);
IPattern p = m.lhs[0];
assertTrue(p instanceof FactPattern);
FactPattern fp = (FactPattern) p;
// LHS sub-patterns
assertEquals(1, fp.getNumberOfConstraints());
assertTrue(fp.getConstraint(0) instanceof CompositeFieldConstraint);
CompositeFieldConstraint fp_cfp = (CompositeFieldConstraint) fp.getConstraint(0);
assertEquals("&&", fp_cfp.getCompositeJunctionType());
assertEquals(2, fp_cfp.getNumberOfConstraints());
assertTrue(fp_cfp.getConstraint(0) instanceof SingleFieldConstraint);
assertTrue(fp_cfp.getConstraint(1) instanceof SingleFieldConstraint);
SingleFieldConstraint fp_cfp_sfp1 = (SingleFieldConstraint) fp_cfp.getConstraint(0);
assertEquals("age > 18", fp_cfp_sfp1.getValue());
assertEquals(BaseSingleFieldConstraint.TYPE_PREDICATE, fp_cfp_sfp1.getConstraintValueType());
SingleFieldConstraint fp_cfp_sfp2 = (SingleFieldConstraint) fp_cfp.getConstraint(1);
assertEquals("age < 45", fp_cfp_sfp2.getValue());
assertEquals(BaseSingleFieldConstraint.TYPE_PREDICATE, fp_cfp_sfp2.getConstraintValueType());
}
use of org.drools.workbench.models.datamodel.rule.FactPattern 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));
}
use of org.drools.workbench.models.datamodel.rule.FactPattern in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testRHS_DataTypeSuffixes.
@Test
public // See https://issues.jboss.org/browse/GUVNOR-2455
void testRHS_DataTypeSuffixes() throws Exception {
String drl = "package org.test;\n" + "rule \"MyRule\"\n" + "dialect \"mvel\"\n" + "when\n" + " $p : Person( )\n" + "then\n" + " modify( $p ) { setDouble( 25.0d ), setFloat( 25.0f ), setLong( 25L ) }\n" + "end";
addModelField("org.test.Person", "this", "org.test.Person", DataType.TYPE_THIS);
addModelField("org.test.Person", "double", Double.class.getName(), DataType.TYPE_NUMERIC_DOUBLE);
addModelField("org.test.Person", "float", Float.class.getName(), DataType.TYPE_NUMERIC_FLOAT);
addModelField("org.test.Person", "long", Long.class.getName(), DataType.TYPE_NUMERIC_LONG);
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(0, fp0.getNumberOfConstraints());
assertEquals(1, m.rhs.length);
assertTrue(m.rhs[0] instanceof ActionUpdateField);
ActionUpdateField auf = (ActionUpdateField) m.rhs[0];
assertEquals("$p", auf.getVariable());
assertEquals(3, auf.getFieldValues().length);
ActionFieldValue afv0 = auf.getFieldValues()[0];
assertEquals("double", afv0.getField());
assertEquals("25.0", afv0.getValue());
assertEquals(FieldNatureType.TYPE_LITERAL, afv0.getNature());
ActionFieldValue afv1 = auf.getFieldValues()[1];
assertEquals("float", afv1.getField());
assertEquals("25.0", afv1.getValue());
assertEquals(FieldNatureType.TYPE_LITERAL, afv1.getNature());
ActionFieldValue afv2 = auf.getFieldValues()[2];
assertEquals("long", afv2.getField());
assertEquals("25", afv2.getValue());
assertEquals(FieldNatureType.TYPE_LITERAL, afv2.getNature());
}
use of org.drools.workbench.models.datamodel.rule.FactPattern in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testInvalidFromSyntax.
@Test
public // https://bugzilla.redhat.com/show_bug.cgi?id=1218308
void testInvalidFromSyntax() throws Exception {
String drl = "rule \"test\"\n" + " dialect \"mvel\"\n" + " when\n" + " (obj : MyClass( ) from my.package)\n" + " then\n" + " System.out.println(\"Test\")\n" + " end";
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 FromCompositeFactPattern);
final FromCompositeFactPattern fp0 = (FromCompositeFactPattern) p0;
assertEquals("MyClass", fp0.getFactType());
final FactPattern fp1 = fp0.getFactPattern();
assertEquals("MyClass", fp1.getFactType());
assertEquals(0, fp1.getNumberOfConstraints());
final ExpressionFormLine efl = fp0.getExpression();
assertNotNull(efl);
assertEquals(2, efl.getParts().size());
assertTrue(efl.getParts().get(0) instanceof ExpressionVariable);
final ExpressionVariable ev = (ExpressionVariable) efl.getParts().get(0);
assertEquals("my", ev.getName());
assertTrue(efl.getParts().get(1) instanceof ExpressionText);
final ExpressionText et = (ExpressionText) efl.getParts().get(1);
assertEquals("package", et.getName());
// Check round-trip
assertEqualsIgnoreWhitespace(drl, RuleModelDRLPersistenceImpl.getInstance().marshal(m));
}
Aggregations