Search in sources :

Example 31 with FactPattern

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

the class RuleModelDRLPersistenceUnmarshallingTest method testCollectWithFreeFormDRL_MethodsUndefined.

@Test
public void testCollectWithFreeFormDRL_MethodsUndefined() throws Exception {
    // https://bugzilla.redhat.com/show_bug.cgi?id=1060816
    String drl = "package org.sample.resourceassignment;\n" + "rule \"r1\"\n" + "dialect \"mvel\"\n" + "when\n" + "$trans : Transactions()\n" + "$transactions : java.util.List( eval( size > 0 ) ) from collect ( Transaction() from $trans.getRecCategorization().get(\"APES-01\") )\n" + "then\n" + "end";
    addModelField("Transactions", "recCategorization", "java.util.Map", DataType.TYPE_COLLECTION);
    addMethodInformation("java.util.Map", "get", new ArrayList<String>() {

        {
            add("p0");
        }
    }, "java.lang.String", null, DataType.TYPE_STRING);
    final RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
    assertNotNull(m);
    assertEquals(2, m.lhs.length);
    IPattern p0 = m.lhs[0];
    assertTrue(p0 instanceof FactPattern);
    FactPattern fp0 = (FactPattern) p0;
    assertEquals("Transactions", fp0.getFactType());
    assertEquals("$trans", fp0.getBoundName());
    assertEquals(0, fp0.getNumberOfConstraints());
    IPattern p1 = m.lhs[1];
    assertTrue(p1 instanceof FromCollectCompositeFactPattern);
    FromCollectCompositeFactPattern fp1 = (FromCollectCompositeFactPattern) p1;
    assertEquals("java.util.List", fp1.getFactPattern().getFactType());
    assertEquals("$transactions", fp1.getFactPattern().getBoundName());
    assertEquals(1, fp1.getFactPattern().getNumberOfConstraints());
    assertTrue(fp1.getFactPattern().getConstraint(0) instanceof SingleFieldConstraint);
    SingleFieldConstraint fp1sfc = (SingleFieldConstraint) fp1.getFactPattern().getConstraint(0);
    assertEquals("size > 0", fp1sfc.getValue());
    assertEquals(BaseSingleFieldConstraint.TYPE_PREDICATE, fp1sfc.getConstraintValueType());
    assertTrue(fp1.getRightPattern() instanceof FromCompositeFactPattern);
    FromCompositeFactPattern fp2 = (FromCompositeFactPattern) fp1.getRightPattern();
    assertNotNull(fp2.getFactPattern());
    FactPattern fp3 = fp2.getFactPattern();
    assertEquals("Transaction", fp3.getFactType());
    assertEquals(0, fp3.getNumberOfConstraints());
    assertNotNull(fp2.getExpression());
    ExpressionFormLine efl = fp2.getExpression();
    assertEquals(3, efl.getParts().size());
    assertTrue(efl.getParts().get(0) instanceof ExpressionVariable);
    ExpressionVariable ev = (ExpressionVariable) efl.getParts().get(0);
    assertEquals("$trans", ev.getName());
    assertEquals("Transactions", ev.getClassType());
    assertTrue(efl.getParts().get(1) instanceof ExpressionText);
    ExpressionText et1 = (ExpressionText) efl.getParts().get(1);
    assertEquals("getRecCategorization()", et1.getName());
    assertEquals("java.lang.String", et1.getClassType());
    assertEquals(DataType.TYPE_STRING, et1.getGenericType());
    assertTrue(efl.getParts().get(2) instanceof ExpressionText);
    ExpressionText et2 = (ExpressionText) efl.getParts().get(2);
    assertEquals("get(\"APES-01\")", et2.getName());
    assertEquals("java.lang.String", et2.getClassType());
    assertEquals(DataType.TYPE_STRING, et2.getGenericType());
    assertEquals(0, m.rhs.length);
}
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) ExpressionFormLine(org.drools.workbench.models.datamodel.rule.ExpressionFormLine) FromCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern) 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) ExpressionVariable(org.drools.workbench.models.datamodel.rule.ExpressionVariable) ExpressionText(org.drools.workbench.models.datamodel.rule.ExpressionText) FromCollectCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) Test(org.junit.Test)

Example 32 with FactPattern

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

the class RuleModelTest method testAddItemLhsAtSpecificPosition.

@Test
public void testAddItemLhsAtSpecificPosition() {
    final RuleModel model = new RuleModel();
    final FactPattern a = new FactPattern();
    model.addLhsItem(a);
    assertEquals(1, model.lhs.length);
    final FactPattern b = new FactPattern();
    model.addLhsItem(b);
    assertEquals(2, model.lhs.length);
    final FactPattern c = new FactPattern();
    model.addLhsItem(c, true);
    assertEquals(3, model.lhs.length);
    assertEquals(a, model.lhs[0]);
    assertEquals(b, model.lhs[1]);
    assertEquals(c, model.lhs[2]);
    final FactPattern d = new FactPattern();
    model.addLhsItem(d, false);
    assertEquals(4, model.lhs.length);
    assertEquals(d, model.lhs[0]);
    assertEquals(a, model.lhs[1]);
    assertEquals(b, model.lhs[2]);
    assertEquals(c, model.lhs[3]);
    final FactPattern e = new FactPattern();
    model.addLhsItem(e, 2);
    assertEquals(5, model.lhs.length);
    assertEquals(d, model.lhs[0]);
    assertEquals(a, model.lhs[1]);
    assertEquals(e, model.lhs[2]);
    assertEquals(b, model.lhs[3]);
    assertEquals(c, model.lhs[4]);
    // test auto-bound
    final FactPattern f = new FactPattern();
    final FactPattern g = new FactPattern();
    model.addLhsItem(f, -1);
    model.addLhsItem(g, 100);
    assertEquals(7, model.lhs.length);
    assertEquals(f, model.lhs[0]);
    assertEquals(d, model.lhs[1]);
    assertEquals(a, model.lhs[2]);
    assertEquals(e, model.lhs[3]);
    assertEquals(b, model.lhs[4]);
    assertEquals(c, model.lhs[5]);
    assertEquals(g, model.lhs[6]);
    model.moveLhsItemDown(5);
    model.moveLhsItemUp(3);
    assertEquals(7, model.lhs.length);
    assertEquals(f, model.lhs[0]);
    assertEquals(d, model.lhs[1]);
    assertEquals(e, model.lhs[2]);
    assertEquals(a, model.lhs[3]);
    assertEquals(b, model.lhs[4]);
    assertEquals(g, model.lhs[5]);
    assertEquals(c, model.lhs[6]);
    model.moveLhsItemUp(0);
    model.moveLhsItemDown(6);
    assertEquals(7, model.lhs.length);
    assertEquals(f, model.lhs[0]);
    assertEquals(d, model.lhs[1]);
    assertEquals(e, model.lhs[2]);
    assertEquals(a, model.lhs[3]);
    assertEquals(b, model.lhs[4]);
    assertEquals(g, model.lhs[5]);
    assertEquals(c, model.lhs[6]);
}
Also used : FactPattern(org.drools.workbench.models.datamodel.rule.FactPattern) CompositeFactPattern(org.drools.workbench.models.datamodel.rule.CompositeFactPattern) FromCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) Test(org.junit.Test)

Example 33 with FactPattern

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

the class RuleModelTest method testAddItemLhs.

@Test
public void testAddItemLhs() {
    final RuleModel model = new RuleModel();
    final FactPattern x = new FactPattern();
    model.addLhsItem(x);
    assertEquals(1, model.lhs.length);
    final FactPattern y = new FactPattern();
    model.addLhsItem(y);
    assertEquals(2, model.lhs.length);
    assertEquals(x, model.lhs[0]);
    assertEquals(y, model.lhs[1]);
}
Also used : FactPattern(org.drools.workbench.models.datamodel.rule.FactPattern) CompositeFactPattern(org.drools.workbench.models.datamodel.rule.CompositeFactPattern) FromCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) Test(org.junit.Test)

Example 34 with FactPattern

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

the class RuleModelTest method testBoundVariableFromOR.

@Test
public void testBoundVariableFromOR() throws Exception {
    final RuleModel model = new RuleModel();
    model.lhs = new IPattern[1];
    CompositeFactPattern compositeFactPattern = new CompositeFactPattern(CompositeFactPattern.COMPOSITE_TYPE_OR);
    model.lhs[0] = compositeFactPattern;
    FactPattern pattern1 = new FactPattern();
    pattern1.setBoundName("test");
    compositeFactPattern.addFactPattern(pattern1);
    FactPattern pattern2 = new FactPattern();
    SingleFieldConstraint constraint = new SingleFieldConstraint();
    pattern2.addConstraint(constraint);
    compositeFactPattern.addFactPattern(pattern2);
    List<String> boundVariablesInScope = model.getBoundVariablesInScope(constraint);
    assertEquals(1, boundVariablesInScope.size());
}
Also used : SingleFieldConstraint(org.drools.workbench.models.datamodel.rule.SingleFieldConstraint) CompositeFactPattern(org.drools.workbench.models.datamodel.rule.CompositeFactPattern) FromCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern) FactPattern(org.drools.workbench.models.datamodel.rule.FactPattern) CompositeFactPattern(org.drools.workbench.models.datamodel.rule.CompositeFactPattern) FromCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) Test(org.junit.Test)

Example 35 with FactPattern

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

the class RuleModelTest method testAllVariableBindings2.

@Test
public void testAllVariableBindings2() {
    final RuleModel model = new RuleModel();
    model.lhs = new IPattern[1];
    final FactPattern fp = new FactPattern("Car");
    model.lhs[0] = fp;
    fp.setBoundName("$c");
    SingleFieldConstraint sfc = new SingleFieldConstraintEBLeftSide("make");
    sfc.getExpressionValue().appendPart(new ExpressionField("make", "java.lang.String", "String"));
    sfc.setFieldBinding("$m");
    fp.addConstraint(sfc);
    List<String> vars = model.getAllVariables();
    assertEquals(2, vars.size());
    assertEquals("$c", vars.get(0));
    assertEquals("$m", vars.get(1));
}
Also used : SingleFieldConstraint(org.drools.workbench.models.datamodel.rule.SingleFieldConstraint) SingleFieldConstraintEBLeftSide(org.drools.workbench.models.datamodel.rule.SingleFieldConstraintEBLeftSide) FactPattern(org.drools.workbench.models.datamodel.rule.FactPattern) CompositeFactPattern(org.drools.workbench.models.datamodel.rule.CompositeFactPattern) FromCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern) ExpressionField(org.drools.workbench.models.datamodel.rule.ExpressionField) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) Test(org.junit.Test)

Aggregations

FactPattern (org.drools.workbench.models.datamodel.rule.FactPattern)399 Test (org.junit.Test)360 CompositeFactPattern (org.drools.workbench.models.datamodel.rule.CompositeFactPattern)335 FromCollectCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern)315 SingleFieldConstraint (org.drools.workbench.models.datamodel.rule.SingleFieldConstraint)255 FromCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern)249 FromAccumulateCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern)230 BaseSingleFieldConstraint (org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint)222 RuleModel (org.drools.workbench.models.datamodel.rule.RuleModel)221 FromEntryPointFactPattern (org.drools.workbench.models.datamodel.rule.FromEntryPointFactPattern)120 IPattern (org.drools.workbench.models.datamodel.rule.IPattern)94 TemplateModel (org.drools.workbench.models.guided.template.shared.TemplateModel)89 ActionFieldValue (org.drools.workbench.models.datamodel.rule.ActionFieldValue)71 CompositeFieldConstraint (org.drools.workbench.models.datamodel.rule.CompositeFieldConstraint)55 ActionInsertFact (org.drools.workbench.models.datamodel.rule.ActionInsertFact)42 ActionUpdateField (org.drools.workbench.models.datamodel.rule.ActionUpdateField)37 SingleFieldConstraintEBLeftSide (org.drools.workbench.models.datamodel.rule.SingleFieldConstraintEBLeftSide)30 BRLConditionColumn (org.drools.workbench.models.guided.dtable.shared.model.BRLConditionColumn)30 IAction (org.drools.workbench.models.datamodel.rule.IAction)28 ExpressionField (org.drools.workbench.models.datamodel.rule.ExpressionField)24