Search in sources :

Example 96 with IPattern

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());
}
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) 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 97 with IPattern

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());
}
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 98 with IPattern

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());
}
Also used : IPattern(org.drools.workbench.models.datamodel.rule.IPattern) SingleFieldConstraintEBLeftSide(org.drools.workbench.models.datamodel.rule.SingleFieldConstraintEBLeftSide) 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) ExpressionField(org.drools.workbench.models.datamodel.rule.ExpressionField) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) ExpressionUnboundFact(org.drools.workbench.models.datamodel.rule.ExpressionUnboundFact) Test(org.junit.Test)

Example 99 with IPattern

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));
}
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) 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 100 with IPattern

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());
}
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) 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) CompositeFieldConstraint(org.drools.workbench.models.datamodel.rule.CompositeFieldConstraint) Test(org.junit.Test)

Aggregations

IPattern (org.drools.workbench.models.datamodel.rule.IPattern)118 FactPattern (org.drools.workbench.models.datamodel.rule.FactPattern)95 Test (org.junit.Test)92 RuleModel (org.drools.workbench.models.datamodel.rule.RuleModel)84 CompositeFactPattern (org.drools.workbench.models.datamodel.rule.CompositeFactPattern)83 FromAccumulateCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern)75 FromCollectCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern)75 FromCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern)75 SingleFieldConstraint (org.drools.workbench.models.datamodel.rule.SingleFieldConstraint)71 BaseSingleFieldConstraint (org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint)58 IAction (org.drools.workbench.models.datamodel.rule.IAction)34 ActionFieldValue (org.drools.workbench.models.datamodel.rule.ActionFieldValue)30 ArrayList (java.util.ArrayList)23 BRLConditionColumn (org.drools.workbench.models.guided.dtable.shared.model.BRLConditionColumn)23 BRLConditionVariableColumn (org.drools.workbench.models.guided.dtable.shared.model.BRLConditionVariableColumn)22 FreeFormLine (org.drools.workbench.models.datamodel.rule.FreeFormLine)20 ActionUpdateField (org.drools.workbench.models.datamodel.rule.ActionUpdateField)16 DescriptionCol52 (org.drools.workbench.models.guided.dtable.shared.model.DescriptionCol52)16 GuidedDecisionTable52 (org.drools.workbench.models.guided.dtable.shared.model.GuidedDecisionTable52)16 RowNumberCol52 (org.drools.workbench.models.guided.dtable.shared.model.RowNumberCol52)16