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);
}
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]);
}
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]);
}
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());
}
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));
}
Aggregations