use of org.drools.workbench.models.datamodel.rule.ActionInsertFact in project drools by kiegroup.
the class RuleTemplateModelDRLPersistenceTest method testRHSInsert.
@Test
public void testRHSInsert() {
TemplateModel m = new TemplateModel();
m.name = "t1";
FactPattern p = new FactPattern("Person");
SingleFieldConstraint con = new SingleFieldConstraint();
con.setFieldType(DataType.TYPE_STRING);
con.setFieldName("field1");
con.setOperator("==");
con.setValue("$f1");
con.setConstraintValueType(SingleFieldConstraint.TYPE_TEMPLATE);
p.addConstraint(con);
m.addLhsItem(p);
ActionInsertFact actionInsertFact = new ActionInsertFact();
actionInsertFact.setFactType("Applicant");
ActionFieldValue actionFieldValue = new ActionFieldValue("age", "123", "Integer");
actionFieldValue.setNature(SingleFieldConstraint.TYPE_LITERAL);
actionInsertFact.addFieldValue(actionFieldValue);
m.addRhsItem(actionInsertFact);
m.addRow(new String[] { "foo" });
String expected = "rule \"t1_0\"" + "dialect \"mvel\"\n" + "when \n" + "Person( field1 == \"foo\" )\n" + "then \n" + " Applicant fact0 = new Applicant(); \n" + " fact0.setAge(123); \n" + " insert(fact0); \n" + "end";
checkMarshall(expected, m);
}
use of org.drools.workbench.models.datamodel.rule.ActionInsertFact in project drools by kiegroup.
the class RuleTemplateModelXMLPersistenceTest method testCompositeConstraintsRoundTrip.
@Test
public void testCompositeConstraintsRoundTrip() throws Exception {
TemplateModel m = new TemplateModel();
m.name = "with composite";
FactPattern p1 = new FactPattern("Person");
p1.setBoundName("p1");
m.addLhsItem(p1);
FactPattern p = new FactPattern("Goober");
m.addLhsItem(p);
CompositeFieldConstraint comp = new CompositeFieldConstraint();
comp.setCompositeJunctionType(CompositeFieldConstraint.COMPOSITE_TYPE_OR);
p.addConstraint(comp);
final SingleFieldConstraint X = new SingleFieldConstraint();
X.setFieldName("goo");
X.setConstraintValueType(SingleFieldConstraint.TYPE_LITERAL);
X.setValue("foo");
X.setOperator("==");
X.setConnectives(new ConnectiveConstraint[1]);
X.getConnectives()[0] = new ConnectiveConstraint();
X.getConnectives()[0].setConstraintValueType(ConnectiveConstraint.TYPE_LITERAL);
X.getConnectives()[0].setOperator("|| ==");
X.getConnectives()[0].setValue("bar");
comp.addConstraint(X);
final SingleFieldConstraint Y = new SingleFieldConstraint();
Y.setFieldName("goo2");
Y.setConstraintValueType(SingleFieldConstraint.TYPE_LITERAL);
Y.setValue("foo");
Y.setOperator("==");
comp.addConstraint(Y);
CompositeFieldConstraint comp2 = new CompositeFieldConstraint();
comp2.setCompositeJunctionType(CompositeFieldConstraint.COMPOSITE_TYPE_AND);
final SingleFieldConstraint Q1 = new SingleFieldConstraint();
Q1.setFieldName("goo");
Q1.setOperator("==");
Q1.setValue("whee");
Q1.setConstraintValueType(BaseSingleFieldConstraint.TYPE_LITERAL);
comp2.addConstraint(Q1);
final SingleFieldConstraint Q2 = new SingleFieldConstraint();
Q2.setFieldName("gabba");
Q2.setOperator("==");
Q2.setValue("whee");
Q2.setConstraintValueType(BaseSingleFieldConstraint.TYPE_LITERAL);
comp2.addConstraint(Q2);
// now nest it
comp.addConstraint(comp2);
final SingleFieldConstraint Z = new SingleFieldConstraint();
Z.setFieldName("goo3");
Z.setConstraintValueType(SingleFieldConstraint.TYPE_LITERAL);
Z.setValue("foo");
Z.setOperator("==");
p.addConstraint(Z);
ActionInsertFact ass = new ActionInsertFact("Whee");
m.addRhsItem(ass);
String xml = RuleTemplateModelXMLPersistenceImpl.getInstance().marshal(m);
// System.err.println(xml);
RuleModel m2 = RuleTemplateModelXMLPersistenceImpl.getInstance().unmarshal(xml);
assertNotNull(m2);
assertEquals("with composite", m2.name);
assertEquals(m2.lhs.length, m.lhs.length);
assertEquals(m2.rhs.length, m.rhs.length);
}
use of org.drools.workbench.models.datamodel.rule.ActionInsertFact in project drools by kiegroup.
the class RuleTemplateModelXMLPersistenceTest method testBasics.
@Test
public void testBasics() {
final RuleTemplateModelPersistence p = RuleTemplateModelXMLPersistenceImpl.getInstance();
final TemplateModel m = new TemplateModel();
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 xml = p.marshal(m);
System.out.println(xml);
assertTrue(xml.indexOf("Person") > -1);
assertTrue(xml.indexOf("Accident") > -1);
assertTrue(xml.indexOf("no-loop") > -1);
assertTrue(xml.indexOf("org.kie") == -1);
assertTrue(xml.indexOf("addToGlobal") > -1);
RuleModel rm_ = RuleTemplateModelXMLPersistenceImpl.getInstance().unmarshal(xml);
assertEquals(2, rm_.rhs.length);
}
use of org.drools.workbench.models.datamodel.rule.ActionInsertFact in project drools by kiegroup.
the class RuleModelDRLPersistenceTest method testEnumTypeString.
@Test
public void testEnumTypeString() {
// 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( type == \"CHEDDAR\" )\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("type");
con.setOperator("==");
con.setValue("CHEDDAR");
con.setFieldType(DataType.TYPE_STRING);
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.ActionInsertFact in project drools by kiegroup.
the class RuleModelDRLPersistenceTest method testActionSetFieldValueWithDSL.
@Test
public void testActionSetFieldValueWithDSL() {
String oldValue = System.getProperty("drools.dateformat");
try {
System.setProperty("drools.dateformat", "dd-MMM-yyyy");
final String dsl = "There is an Applicant";
final String dslDefinition = "[when]" + dsl + "=$a : Applicant();";
final RuleModel m = new RuleModel();
m.name = "r0";
final DSLSentence sen = new DSLSentence();
sen.setDefinition(dsl);
m.addLhsItem(sen);
final ActionInsertFact aif = new ActionInsertFact("Applicant");
aif.addFieldValue(new ActionFieldValue("applicationDate", "13-Mar-2017", DataType.TYPE_DATE));
m.addRhsItem(aif);
final String expected = "rule \"r0\"\n" + "dialect \"mvel\"\n" + "when\n" + "There is an Applicant\n" + "then\n" + ">java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(\"dd-MMM-yyyy\");\n" + ">Applicant fact0 = new Applicant();\n" + ">fact0.setApplicationDate( sdf.parse(\"13-Mar-2017\") );\n" + ">insert( fact0 );\n" + "end";
final String actual = RuleModelDRLPersistenceImpl.getInstance().marshal(m);
assertEqualsIgnoreWhitespace(expected, actual);
PackageDataModelOracle dmo = mock(PackageDataModelOracle.class);
final RuleModel m2 = RuleModelDRLPersistenceImpl.getInstance().unmarshalUsingDSL(actual, Collections.emptyList(), dmo, dslDefinition);
assertNotNull(m2);
// LHS
assertEquals(1, m.lhs.length);
DSLSentence dslSentence = (DSLSentence) m.lhs[0];
assertEquals(dsl, dslSentence.getDefinition());
assertEquals(0, dslSentence.getValues().size());
// RHS
assertEquals(1, m2.rhs.length);
assertTrue(m2.rhs[0] instanceof ActionInsertFact);
final ActionInsertFact a = (ActionInsertFact) m2.rhs[0];
assertEquals("Applicant", a.getFactType());
assertEquals(1, a.getFieldValues().length);
final ActionFieldValue afv = a.getFieldValues()[0];
assertEquals("applicationDate", afv.getField());
assertEquals("13-Mar-2017", afv.getValue());
} finally {
if (oldValue == null) {
System.clearProperty("drools.dateformat");
} else {
System.setProperty("drools.dateformat", oldValue);
}
}
}
Aggregations