use of org.drools.workbench.models.datamodel.rule.RuleModel in project drools by kiegroup.
the class RuleModelTest method testRemoveItemRhs.
@Test
public void testRemoveItemRhs() {
final RuleModel model = new RuleModel();
model.rhs = new IAction[3];
final ActionRetractFact r0 = new ActionRetractFact("x");
final ActionRetractFact r1 = new ActionRetractFact("y");
final ActionRetractFact r2 = new ActionRetractFact("z");
model.rhs[0] = r0;
model.rhs[1] = r1;
model.rhs[2] = r2;
model.removeRhsItem(1);
assertEquals(2, model.rhs.length);
assertEquals(r0, model.rhs[0]);
assertEquals(r2, model.rhs[1]);
}
use of org.drools.workbench.models.datamodel.rule.RuleModel in project drools by kiegroup.
the class RuleModelTest method testRemoveItemLhs.
@Test
public void testRemoveItemLhs() {
final RuleModel model = new RuleModel();
model.lhs = new IPattern[3];
final FactPattern x = new FactPattern("Car");
model.lhs[0] = x;
x.setBoundName("x");
final FactPattern y = new FactPattern("Car");
model.lhs[1] = y;
y.setBoundName("y");
final FactPattern other = new FactPattern("House");
model.lhs[2] = other;
assertEquals(3, model.lhs.length);
assertEquals(x, model.lhs[0]);
model.removeLhsItem(0);
assertEquals(2, model.lhs.length);
assertEquals(y, model.lhs[0]);
}
use of org.drools.workbench.models.datamodel.rule.RuleModel in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testReciprocal_CompositeFactPatternWithOr.
@Test
public void testReciprocal_CompositeFactPatternWithOr() {
// This is the inverse of "CompositeFactPatternWithOr"
String drl = "rule \"rule1\"\n" + "dialect \"mvel\"\n" + "when\n" + "( Person( age == 42 ) or Person( age == 43 ) )\n" + "then\n" + "end";
RuleModel m = new RuleModel();
m.name = "rule1";
// LHS Patterns
CompositeFactPattern cfp = new CompositeFactPattern();
cfp.setType(CompositeFactPattern.COMPOSITE_TYPE_OR);
// LHS sub-patterns
FactPattern fp1 = new FactPattern();
fp1.setFactType("Person");
SingleFieldConstraint cfp_sfp1 = new SingleFieldConstraint();
cfp_sfp1.setFactType("Person");
cfp_sfp1.setFieldName("age");
cfp_sfp1.setOperator("==");
cfp_sfp1.setValue("42");
cfp_sfp1.setFieldType(DataType.TYPE_NUMERIC_INTEGER);
cfp_sfp1.setConstraintValueType(BaseSingleFieldConstraint.TYPE_LITERAL);
fp1.addConstraint(cfp_sfp1);
cfp.addFactPattern(fp1);
FactPattern fp2 = new FactPattern();
fp2.setFactType("Person");
SingleFieldConstraint cfp_sfp2 = new SingleFieldConstraint();
cfp_sfp2.setFactType("Person");
cfp_sfp2.setFieldName("age");
cfp_sfp2.setOperator("==");
cfp_sfp2.setValue("43");
cfp_sfp2.setFieldType(DataType.TYPE_NUMERIC_INTEGER);
cfp_sfp2.setConstraintValueType(BaseSingleFieldConstraint.TYPE_LITERAL);
fp2.addConstraint(cfp_sfp2);
cfp.addFactPattern(fp2);
m.addLhsItem(cfp);
String actualDrl = RuleModelDRLPersistenceImpl.getInstance().marshal(m);
assertEqualsIgnoreWhitespace(drl, actualDrl);
}
use of org.drools.workbench.models.datamodel.rule.RuleModel in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method compositeFieldConstraintWithOnePredicateAndOneLiteral.
@Test
public void compositeFieldConstraintWithOnePredicateAndOneLiteral() {
String drl = "rule \"rule1\"\n" + "when\n" + "Person( eval( age > 18 ) && 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("Person", fp_cfp_sfp2.getFactType());
assertEquals("age", fp_cfp_sfp2.getFieldName());
assertEquals("<", fp_cfp_sfp2.getOperator());
assertEquals("45", fp_cfp_sfp2.getValue());
assertEquals(BaseSingleFieldConstraint.TYPE_LITERAL, fp_cfp_sfp2.getConstraintValueType());
}
use of org.drools.workbench.models.datamodel.rule.RuleModel in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testMethodCall.
@Test
public void testMethodCall() {
// BZ-1042511
String drl = "" + "package org.mortgages;\n" + "import org.mortgages.LoanApplication;\n" + "import java.util.Map;\n" + "rule \"my rule\"\n" + " dialect \"mvel\"\n" + " when\n" + " a : LoanApplication( )\n" + " m : Map()\n" + " then\n" + " m.put(\"key\", a );\n" + "end\n";
HashMap<String, String> globals = new HashMap<>();
when(dmo.getPackageGlobals()).thenReturn(globals);
RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
assertEquals(2, m.getImports().getImports().size());
assertTrue(m.rhs[0] instanceof ActionCallMethod);
ActionCallMethod mc = (ActionCallMethod) m.rhs[0];
assertEquals("put", mc.getMethodName());
assertEquals("m", mc.getVariable());
assertEquals(1, mc.getState());
assertEquals(2, mc.getFieldValues().length);
ActionFieldValue f1 = mc.getFieldValue(0);
assertEquals("key", f1.getValue());
ActionFieldValue f2 = mc.getFieldValue(1);
assertEquals("a", f2.getValue());
String marshalled = RuleModelDRLPersistenceImpl.getInstance().marshal(m);
logger.debug(marshalled);
assertEqualsIgnoreWhitespace(drl, marshalled);
}
Aggregations