use of org.drools.workbench.models.datamodel.rule.ActionInsertFact in project drools by kiegroup.
the class RuleModelDRLPersistenceImpl method fixActionInsertFactBindings.
protected void fixActionInsertFactBindings(final IAction[] rhs) {
final Set<String> existingBindings = extractExistingActionBindings(rhs);
for (IAction action : rhs) {
if (action instanceof ActionInsertFact) {
final ActionInsertFact aif = (ActionInsertFact) action;
if (aif.getFieldValues().length > 0 && aif.getBoundName() == null) {
int idx = 0;
String binding = "fact" + idx;
while (existingBindings.contains(binding)) {
idx++;
binding = "fact" + idx;
}
existingBindings.add(binding);
aif.setBoundName(binding);
}
}
}
}
use of org.drools.workbench.models.datamodel.rule.ActionInsertFact in project drools by kiegroup.
the class RuleModelDRLPersistenceTest method testCompositeAndConstraints.
@Test
public void testCompositeAndConstraints() {
RuleModel m = new RuleModel();
m.name = "and composite";
FactPattern p = new FactPattern("Goober");
m.addLhsItem(p);
CompositeFieldConstraint comp = new CompositeFieldConstraint();
comp.setCompositeJunctionType(CompositeFieldConstraint.COMPOSITE_TYPE_AND);
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);
ActionInsertFact ass = new ActionInsertFact("Whee");
m.addRhsItem(ass);
String expected = "rule \"and composite\"" + "dialect \"mvel\"\n" + "when\n" + "Goober( gooField == \"gooValue\" && fooField != null && fooField.barField == \"barValue\" )\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 testEnumTypeNumeric.
@Test
public void testEnumTypeNumeric() {
// 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( age == 100 )\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("age");
con.setOperator("==");
con.setValue("100");
con.setFieldType(DataType.TYPE_NUMERIC_INTEGER);
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 testCompositeConstraintsRoundTrip.
@Test
public void testCompositeConstraintsRoundTrip() throws Exception {
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.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 drl = RuleModelDRLPersistenceImpl.getInstance().marshal(m);
PackageDataModelOracle dmo = mock(PackageDataModelOracle.class);
RuleModel m2 = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.EMPTY_LIST, dmo);
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 RuleModelDRLPersistenceTest method testRHSDateInsertActionWithoutSystemProperty.
@Test
public void testRHSDateInsertActionWithoutSystemProperty() {
// RHBRMS-3034
String oldValue = System.getProperty("drools.dateformat");
try {
System.clearProperty("drools.dateformat");
RuleModel m = new RuleModel();
m.name = "RHS Date";
FactPattern p = new FactPattern("Person");
SingleFieldConstraint con = new SingleFieldConstraint();
con.setFieldType(DataType.TYPE_DATE);
con.setFieldName("dateOfBirth");
con.setOperator("==");
con.setValue("31-Jan-2000");
con.setConstraintValueType(SingleFieldConstraint.TYPE_LITERAL);
p.addConstraint(con);
m.addLhsItem(p);
ActionInsertFact ai = new ActionInsertFact("Birthday");
ai.addFieldValue(new ActionFieldValue("dob", "31-Jan-2000", DataType.TYPE_DATE));
m.addRhsItem(ai);
String result = RuleModelDRLPersistenceImpl.getInstance().marshal(m);
assertTrue("result DRL : " + result, result.indexOf("java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(\"dd-MMM-yyyy\");") != -1);
assertTrue(result.indexOf("fact0.setDob( sdf.parse(\"31-Jan-2000\"") != -1);
checkMarshalling(null, m);
} finally {
if (oldValue == null) {
System.clearProperty("drools.dateformat");
} else {
System.setProperty("drools.dateformat", oldValue);
}
}
}
Aggregations