use of org.drools.workbench.models.datamodel.rule.ActionInsertFact in project drools by kiegroup.
the class RuleModelDRLPersistenceTest method testLiteralBigIntegerJava.
@Test
public void testLiteralBigIntegerJava() {
RuleModel m = new RuleModel();
m.name = "test literal biginteger";
m.addAttribute(new RuleAttribute("dialect", "java"));
FactPattern p = new FactPattern("Person");
SingleFieldConstraint con = new SingleFieldConstraint();
con.setFieldType(DataType.TYPE_NUMERIC_BIGINTEGER);
con.setFieldName("field1");
con.setOperator("==");
con.setValue("44");
con.setConstraintValueType(SingleFieldConstraint.TYPE_LITERAL);
p.addConstraint(con);
m.addLhsItem(p);
ActionInsertFact ai = new ActionInsertFact("Person");
ai.addFieldValue(new ActionFieldValue("field1", "55", DataType.TYPE_NUMERIC_BIGINTEGER));
m.addRhsItem(ai);
String expected = "rule \"test literal biginteger\" \n" + "\tdialect \"java\"\n when \n" + " Person(field1 == 44I ) \n" + " then \n" + "Person fact0 = new Person(); \n" + "fact0.setField1( new java.math.BigInteger( \"55\" ) ); \n" + "insert( fact0 ); \n" + "end";
checkMarshalling(expected, m);
}
use of org.drools.workbench.models.datamodel.rule.ActionInsertFact in project drools by kiegroup.
the class RuleModelDRLPersistenceTest method testCompositeOrConstraintsComplex.
@Test
public void testCompositeOrConstraintsComplex() {
RuleModel m = new RuleModel();
m.name = "or composite complex";
FactPattern p = new FactPattern("Goober");
m.addLhsItem(p);
CompositeFieldConstraint comp = new CompositeFieldConstraint();
comp.setCompositeJunctionType(CompositeFieldConstraint.COMPOSITE_TYPE_OR);
p.addConstraint(comp);
final SingleFieldConstraint sfc1 = new SingleFieldConstraint();
sfc1.setFactType("Goober");
sfc1.setFieldName("gooField");
sfc1.setFieldType(DataType.TYPE_STRING);
sfc1.setConstraintValueType(SingleFieldConstraint.TYPE_LITERAL);
sfc1.setValue("gooValue");
sfc1.setOperator("==");
comp.addConstraint(sfc1);
final SingleFieldConstraint sfc2 = new SingleFieldConstraint();
sfc2.setFactType("Goober");
sfc2.setFieldName("fooField");
sfc2.setFieldType(DataType.TYPE_OBJECT);
sfc2.setConstraintValueType(SingleFieldConstraint.TYPE_LITERAL);
sfc2.setOperator("!= null");
comp.addConstraint(sfc2);
final SingleFieldConstraint sfc3 = new SingleFieldConstraint();
sfc3.setFactType("Bar");
sfc3.setFieldName("barField");
sfc3.setParent(sfc2);
sfc3.setFieldType(DataType.TYPE_STRING);
sfc3.setConstraintValueType(SingleFieldConstraint.TYPE_LITERAL);
sfc3.setValue("barValue");
sfc3.setOperator("==");
comp.addConstraint(sfc3);
final SingleFieldConstraint sfc4 = new SingleFieldConstraint();
sfc4.setFactType("Goober");
sfc4.setFieldName("zooField");
sfc4.setFieldType(DataType.TYPE_STRING);
sfc4.setConstraintValueType(SingleFieldConstraint.TYPE_LITERAL);
sfc4.setValue("zooValue");
sfc4.setOperator("==");
p.addConstraint(sfc4);
ActionInsertFact ass = new ActionInsertFact("Whee");
m.addRhsItem(ass);
String expected = "rule \"or composite complex\"" + "dialect \"mvel\"\n" + "when\n" + "Goober( gooField == \"gooValue\" || fooField != null || fooField.barField == \"barValue\", zooField == \"zooValue\" )\n" + "then\n" + "insert( new Whee() );\n" + "end";
checkMarshalling(expected, m);
}
use of org.drools.workbench.models.datamodel.rule.ActionInsertFact in project drools by kiegroup.
the class RuleModelDRLPersistenceTest method testCompositeConstraints.
@Test
public void testCompositeConstraints() {
RuleModel m = new RuleModel();
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.setFieldType(DataType.TYPE_STRING);
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].setFieldType(DataType.TYPE_STRING);
X.getConnectives()[0].setOperator("|| ==");
X.getConnectives()[0].setValue("bar");
comp.addConstraint(X);
final SingleFieldConstraint Y = new SingleFieldConstraint();
Y.setFieldName("goo2");
Y.setFieldType(DataType.TYPE_STRING);
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.setFieldType(DataType.TYPE_STRING);
Q1.setFieldName("goo");
Q1.setOperator("==");
Q1.setValue("whee");
Q1.setConstraintValueType(BaseSingleFieldConstraint.TYPE_LITERAL);
comp2.addConstraint(Q1);
final SingleFieldConstraint Q2 = new SingleFieldConstraint();
Q2.setFieldType(DataType.TYPE_STRING);
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.setFieldType(DataType.TYPE_STRING);
Z.setValue("foo");
Z.setOperator("==");
p.addConstraint(Z);
ActionInsertFact ass = new ActionInsertFact("Whee");
m.addRhsItem(ass);
String actual = RuleModelDRLPersistenceImpl.getInstance().marshal(m);
String expected = "rule \"with composite\"\n" + "\tdialect \"mvel\"\n" + "\twhen\n" + "\t\tp1 : Person( )\n" + "\t\tGoober( goo == \"foo\" || == \"bar\" || goo2 == \"foo\" || ( goo == \"whee\" && gabba == \"whee\" ), goo3 == \"foo\" )\n" + "\tthen\n" + "\t\tinsert( new Whee() );\n" + "end\n";
assertEqualsIgnoreWhitespace(expected, actual);
checkMarshalling(expected, m);
}
use of org.drools.workbench.models.datamodel.rule.ActionInsertFact in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingI18NTest method testI18N_JP_InsertFact.
@Test
public void testI18N_JP_InsertFact() {
final String drl = "package org.test;\n" + "rule \"r1\"\n" + "dialect \"mvel\"\n" + "when\n" + "then\n" + "帽子 fact0 = new 帽子();\n" + "fact0.setサイズ( 55 );\n" + "insert( fact0 );\n" + "end";
addModelField("帽子", "サイズ", "java.lang.Integer", DataType.TYPE_NUMERIC_INTEGER);
final RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, new ArrayList<String>(), dmo);
assertNotNull(m);
assertEquals(1, m.rhs.length);
assertTrue(m.rhs[0] instanceof ActionInsertFact);
final ActionInsertFact aif = (ActionInsertFact) m.rhs[0];
assertEquals("帽子", aif.getFactType());
assertEquals("fact0", aif.getBoundName());
assertEquals(1, aif.getFieldValues().length);
final ActionFieldValue afv = aif.getFieldValues()[0];
assertEquals("サイズ", afv.getField());
assertEquals("55", afv.getValue());
assertEquals(DataType.TYPE_NUMERIC_INTEGER, afv.getType());
assertEquals(FieldNatureType.TYPE_LITERAL, afv.getNature());
}
use of org.drools.workbench.models.datamodel.rule.ActionInsertFact in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingI18NTest method testI18N_US_InsertFact.
@Test
public void testI18N_US_InsertFact() {
final String drl = "package org.test;\n" + "rule \"r1\"\n" + "dialect \"mvel\"\n" + "when\n" + "then\n" + "Applicant fact0 = new Applicant();\n" + "fact0.setAge( 55 );\n" + "insert( fact0 );\n" + "end";
addModelField("Applicant", "age", "java.lang.Integer", DataType.TYPE_NUMERIC_INTEGER);
final RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, new ArrayList<String>(), dmo);
assertNotNull(m);
assertEquals(1, m.rhs.length);
assertTrue(m.rhs[0] instanceof ActionInsertFact);
final ActionInsertFact aif = (ActionInsertFact) m.rhs[0];
assertEquals("Applicant", aif.getFactType());
assertEquals("fact0", aif.getBoundName());
assertEquals(1, aif.getFieldValues().length);
final ActionFieldValue afv = aif.getFieldValues()[0];
assertEquals("age", afv.getField());
assertEquals("55", afv.getValue());
assertEquals(DataType.TYPE_NUMERIC_INTEGER, afv.getType());
assertEquals(FieldNatureType.TYPE_LITERAL, afv.getNature());
}
Aggregations