use of org.drools.workbench.models.datamodel.rule.IPattern in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testSingleFieldConstraintCEPOperator.
@Test
public void testSingleFieldConstraintCEPOperator() {
String drl = "rule \"rule1\"\n" + "when\n" + "Applicant( dob after \"26-Jun-2013\" )\n" + "then\n" + "end";
RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
assertNotNull(m);
assertEquals("rule1", m.name);
assertEquals(1, m.lhs.length);
IPattern p = m.lhs[0];
assertTrue(p instanceof FactPattern);
FactPattern fp = (FactPattern) p;
assertEquals("Applicant", fp.getFactType());
assertEquals(1, fp.getNumberOfConstraints());
assertTrue(fp.getConstraint(0) instanceof SingleFieldConstraint);
SingleFieldConstraint sfp = (SingleFieldConstraint) fp.getConstraint(0);
assertEquals("Applicant", sfp.getFactType());
assertEquals("dob", sfp.getFieldName());
assertEquals("after", sfp.getOperator());
assertEquals("26-Jun-2013", 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 testRHSInsertFactWithFieldAsLiteral.
@Test
public void testRHSInsertFactWithFieldAsLiteral() {
String drl = "package org.mortgages\n" + "import org.test.Person\n" + "rule \"variable\"\n" + "dialect \"mvel\"\n" + "when\n" + "Person( field1 == 44 )\n" + "then\n" + "Person fact0 = new Person();\n" + "fact0.setField1( 55 );\n" + "insert( fact0 );\n" + "end";
addModelField("org.test.Person", "field1", "java.lang.Integer", DataType.TYPE_NUMERIC_INTEGER);
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(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_LITERAL, afv.getNature());
assertEquals("55", afv.getValue());
assertEquals(DataType.TYPE_NUMERIC_INTEGER, afv.getType());
}
use of org.drools.workbench.models.datamodel.rule.IPattern in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testNestedFieldConstraintsAsExpression.
@Test
public void testNestedFieldConstraintsAsExpression() {
String drl = "rule \"rule1\"\n" + "when\n" + "Person( contact.telephone > 12345 )\n" + "then\n" + "end";
addModelField("org.test.Person", "contact", "org.test.Contact", "Contact");
addModelField("org.test.Contact", "telephone", "java.lang.Integer", DataType.TYPE_NUMERIC_INTEGER);
RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
assertNotNull(m);
assertEquals("rule1", m.name);
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(((FactPattern) m.lhs[0]).getFieldConstraints()[0] instanceof SingleFieldConstraintEBLeftSide);
SingleFieldConstraintEBLeftSide ebLeftSide = (SingleFieldConstraintEBLeftSide) ((FactPattern) m.lhs[0]).getFieldConstraints()[0];
assertEquals("telephone", ebLeftSide.getFieldName());
assertEquals("java.lang.Integer", ebLeftSide.getFieldType());
assertEquals(">", ebLeftSide.getOperator());
assertEquals("12345", ebLeftSide.getValue());
assertEquals(3, ebLeftSide.getExpressionLeftSide().getParts().size());
assertTrue(ebLeftSide.getExpressionLeftSide().getParts().get(0) instanceof ExpressionUnboundFact);
ExpressionUnboundFact expressionUnboundFact = ((ExpressionUnboundFact) ebLeftSide.getExpressionLeftSide().getParts().get(0));
assertEquals("Person", expressionUnboundFact.getName());
assertEquals("Person", expressionUnboundFact.getClassType());
assertEquals("Person", expressionUnboundFact.getGenericType());
assertEquals(((FactPattern) m.lhs[0]).getFactType(), expressionUnboundFact.getFactType());
assertEquals(null, expressionUnboundFact.getPrevious());
assertEquals(ebLeftSide.getExpressionLeftSide().getParts().get(1), expressionUnboundFact.getNext());
assertTrue(ebLeftSide.getExpressionLeftSide().getParts().get(1) instanceof ExpressionField);
ExpressionField expressionField1 = (ExpressionField) ebLeftSide.getExpressionLeftSide().getParts().get(1);
assertEquals("contact", expressionField1.getName());
assertEquals("org.test.Contact", expressionField1.getClassType());
assertEquals("Contact", expressionField1.getGenericType());
assertEquals(ebLeftSide.getExpressionLeftSide().getParts().get(0), expressionField1.getPrevious());
assertEquals(ebLeftSide.getExpressionLeftSide().getParts().get(2), expressionField1.getNext());
assertTrue(ebLeftSide.getExpressionLeftSide().getParts().get(2) instanceof ExpressionField);
ExpressionField expressionField2 = (ExpressionField) ebLeftSide.getExpressionLeftSide().getParts().get(2);
assertEquals("telephone", expressionField2.getName());
assertEquals("java.lang.Integer", expressionField2.getClassType());
assertEquals(DataType.TYPE_NUMERIC_INTEGER, expressionField2.getGenericType());
assertEquals(ebLeftSide.getExpressionLeftSide().getParts().get(1), expressionField2.getPrevious());
assertNull(expressionField2.getNext());
}
use of org.drools.workbench.models.datamodel.rule.IPattern in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testStringFieldsWithDoubleForwardSlashes.
@Test
public // https://bugzilla.redhat.com/show_bug.cgi?id=1234640
void testStringFieldsWithDoubleForwardSlashes() throws Exception {
String drl = "rule \"test\"\n" + "dialect \"mvel\"\n" + "when\n" + " MyType( url == \"http://www.redhat.com\" )\n" + "then\n" + "end";
addModelField("org.test.MyType", "this", "org.test.MyType", DataType.TYPE_THIS);
addModelField("org.test.MyType", "url", 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("MyType", fp0.getFactType());
assertEquals(1, fp0.getNumberOfConstraints());
assertTrue(fp0.getConstraint(0) instanceof SingleFieldConstraint);
final SingleFieldConstraint fp0sfc0 = (SingleFieldConstraint) fp0.getConstraint(0);
assertEquals("MyType", fp0sfc0.getFactType());
assertEquals("url", fp0sfc0.getFieldName());
assertEquals("==", fp0sfc0.getOperator());
assertEquals(DataType.TYPE_STRING, fp0sfc0.getFieldType());
assertEquals("http://www.redhat.com", fp0sfc0.getValue());
assertEquals(0, m.rhs.length);
// 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 testCompositeFieldConstraintWithNotNullOperator.
@Test
public void testCompositeFieldConstraintWithNotNullOperator() {
String drl = "rule \"rule1\"\n" + "when\n" + "Applicant( age != null && age > 70 )\n" + "then\n" + "end";
RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
assertNotNull(m);
assertEquals("rule1", m.name);
assertEquals(1, m.lhs.length);
IPattern p = m.lhs[0];
assertTrue(p instanceof FactPattern);
FactPattern fp = (FactPattern) p;
assertEquals("Applicant", fp.getFactType());
assertEquals(1, fp.getConstraintList().getConstraints().length);
assertTrue(fp.getConstraint(0) instanceof CompositeFieldConstraint);
CompositeFieldConstraint cfp = (CompositeFieldConstraint) fp.getConstraint(0);
assertEquals("&&", cfp.getCompositeJunctionType());
assertEquals(2, cfp.getNumberOfConstraints());
assertTrue(cfp.getConstraint(0) instanceof SingleFieldConstraint);
assertTrue(cfp.getConstraint(1) instanceof SingleFieldConstraint);
SingleFieldConstraint sfp1 = (SingleFieldConstraint) cfp.getConstraint(0);
assertEquals("Applicant", sfp1.getFactType());
assertEquals("age", sfp1.getFieldName());
assertEquals("!= null", sfp1.getOperator());
assertNull(sfp1.getValue());
assertEquals(BaseSingleFieldConstraint.TYPE_UNDEFINED, sfp1.getConstraintValueType());
SingleFieldConstraint sfp2 = (SingleFieldConstraint) cfp.getConstraint(1);
assertEquals("Applicant", sfp2.getFactType());
assertEquals("age", sfp2.getFieldName());
assertEquals(">", sfp2.getOperator());
assertEquals("70", sfp2.getValue());
assertEquals(BaseSingleFieldConstraint.TYPE_LITERAL, sfp2.getConstraintValueType());
}
Aggregations