use of org.drools.workbench.models.datamodel.rule.FactPattern in project drools by kiegroup.
the class RuleModelDRLPersistenceTest method testRHSChangeMultipleFieldsModifyOneUpdateOther.
@Test
public void testRHSChangeMultipleFieldsModifyOneUpdateOther() {
String expected = "" + "rule \"my rule\" \n" + " dialect \"mvel\"\n" + " when\n" + " $p : Person()\n" + " then\n" + " modify( $p ) {\n" + " setName( \"Fred\" )\n" + " }\n" + " $p.setAge( 55 );\n" + "end\n";
final RuleModel m = new RuleModel();
FactPattern factPattern = new FactPattern();
factPattern.setFactType("Person");
factPattern.setBoundName("$p");
m.lhs = new IPattern[] { factPattern };
ActionUpdateField auf = new ActionUpdateField();
auf.setVariable("$p");
ActionFieldValue afv1 = new ActionFieldValue();
afv1.setField("name");
afv1.setType(DataType.TYPE_STRING);
afv1.setNature(FieldNatureType.TYPE_LITERAL);
afv1.setValue("Fred");
auf.setFieldValues(new ActionFieldValue[] { afv1 });
ActionSetField asf = new ActionSetField();
asf.setVariable("$p");
ActionFieldValue afv2 = new ActionFieldValue();
afv2.setField("age");
afv2.setType(DataType.TYPE_NUMERIC_INTEGER);
afv2.setNature(FieldNatureType.TYPE_LITERAL);
afv2.setValue("55");
asf.setFieldValues(new ActionFieldValue[] { afv2 });
m.rhs = new IAction[] { auf, asf };
m.name = "my rule";
checkMarshalling(expected, m);
}
use of org.drools.workbench.models.datamodel.rule.FactPattern in project drools by kiegroup.
the class RuleModelDRLPersistenceTest method testInOperatorStringCommaInside.
@Test
public void testInOperatorStringCommaInside() {
final RuleModel model = new RuleModel();
model.name = "in";
final FactPattern pattern = new FactPattern("Person");
final SingleFieldConstraint constraint = new SingleFieldConstraint();
constraint.setFieldType(DataType.TYPE_STRING);
constraint.setFieldName("field1");
constraint.setOperator("in");
constraint.setValue("value1, \"value2, value3\"");
constraint.setConstraintValueType(SingleFieldConstraint.TYPE_LITERAL);
pattern.addConstraint(constraint);
model.addLhsItem(pattern);
final String expected = "rule \"in\" \n" + "dialect \"mvel\" \n" + "when \n" + " Person(field1 in ( \"value1\", \"value2, value3\" ) ) \n" + " then \n" + "end";
checkMarshalling(expected, model);
}
use of org.drools.workbench.models.datamodel.rule.FactPattern in project drools by kiegroup.
the class RuleModelDRLPersistenceTest method testBasics2.
@Test
public void testBasics2() {
final RuleModelPersistence p = RuleModelDRLPersistenceImpl.getInstance();
final RuleModel m = new RuleModel();
m.addLhsItem(new FactPattern("Person"));
m.addLhsItem(new FactPattern("Accident"));
m.addAttribute(new RuleAttribute("no-loop", "true"));
m.addRhsItem(new ActionInsertFact("Report"));
ActionGlobalCollectionAdd ag = new ActionGlobalCollectionAdd();
ag.setFactName("x");
ag.setGlobalName("g");
m.addRhsItem(ag);
m.name = "my rule";
final String drl = p.marshal(m);
assertTrue(drl.indexOf("Person( )") > -1);
assertTrue(drl.indexOf("Accident( )") > -1);
assertTrue(drl.indexOf("no-loop true") > -1);
assertTrue(drl.indexOf("org.kie") == -1);
assertTrue(drl.indexOf("g.add( x );") > -1);
PackageDataModelOracle dmo = mock(PackageDataModelOracle.class);
RuleModel rm_ = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.EMPTY_LIST, dmo);
assertEquals(2, rm_.rhs.length);
}
use of org.drools.workbench.models.datamodel.rule.FactPattern in project drools by kiegroup.
the class RuleModelDRLPersistenceTest method testEnumTypeBoolean.
@Test
public void testEnumTypeBoolean() {
// A legacy "Guvnor" enums (i.e pick-list of underlying field data-type)
String expected = "rule \"my rule\"\n\tdialect \"mvel\"\n\twhen\n\t\tCheese( smelly == true )\n" + "\tthen\n\t\tinsert( new Report() );\nend\n";
final RuleModel m = new RuleModel();
final FactPattern pat = new FactPattern("Cheese");
m.addLhsItem(pat);
final SingleFieldConstraint con = new SingleFieldConstraint();
con.setFieldName("smelly");
con.setOperator("==");
con.setValue("true");
con.setFieldType(DataType.TYPE_BOOLEAN);
con.setConstraintValueType(BaseSingleFieldConstraint.TYPE_ENUM);
pat.addConstraint(con);
m.addRhsItem(new ActionInsertFact("Report"));
m.name = "my rule";
checkMarshalling(expected, m);
}
use of org.drools.workbench.models.datamodel.rule.FactPattern in project drools by kiegroup.
the class RuleModelDRLPersistenceTest method testCompositeFactPatternWithFromWithDSL.
@Test
public void testCompositeFactPatternWithFromWithDSL() {
final RuleModel m = new RuleModel();
m.name = "model";
final DSLSentence sen = new DSLSentence();
sen.setDefinition("A DSL phrase");
m.addLhsItem(sen);
final FactPattern fp1 = new FactPattern("Data");
fp1.setBoundName("$d");
m.addLhsItem(fp1);
final CompositeFactPattern cp = new CompositeFactPattern(CompositeFactPattern.COMPOSITE_TYPE_NOT);
final FactPattern fp2 = new FactPattern("Person");
final FromCompositeFactPattern ffp1 = new FromCompositeFactPattern();
ffp1.setExpression(new ExpressionFormLine(new ExpressionVariable(fp1.getBoundName(), fp1.getFactType())));
ffp1.setFactPattern(fp2);
cp.addFactPattern(ffp1);
m.addLhsItem(cp);
final String actual = RuleModelDRLPersistenceImpl.getInstance().marshal(m);
final String expected = "rule \"model\"\n" + "dialect \"mvel\"\n" + "when\n" + "A DSL phrase\n" + ">$d : Data( )\n" + ">not ( Person( ) from $d\n" + ")\n" + "then\n" + "end\n";
assertEqualsIgnoreWhitespace(expected, actual);
}
Aggregations