use of org.drools.workbench.models.datamodel.rule.IAction in project drools by kiegroup.
the class RuleModelDRLPersistenceExtensionsTest method unmarshalWithExtensions.
@Test
public void unmarshalWithExtensions() {
RuleModel ruleModel = RuleModelDRLPersistenceImpl.getInstance().unmarshal(DRL_RULE, Collections.emptyList(), new PackageDataModelOracleImpl(), Arrays.asList(new TestIActionPersistenceExtension()));
assertEquals(1, ruleModel.rhs.length);
IAction iAction = ruleModel.rhs[0];
assertTrue(iAction instanceof TestIAction);
}
use of org.drools.workbench.models.datamodel.rule.IAction in project drools by kiegroup.
the class RuleModelDRLPersistenceExtensionsTest method unmarshalDSLWithoutExtensions.
@Test
public void unmarshalDSLWithoutExtensions() {
RuleModel ruleModel = RuleModelDRLPersistenceImpl.getInstance().unmarshalUsingDSL(DSL_RULE, Collections.emptyList(), new PackageDataModelOracleImpl());
assertEquals(1, ruleModel.rhs.length);
IAction iAction = ruleModel.rhs[0];
assertTrue(iAction instanceof FreeFormLine);
}
use of org.drools.workbench.models.datamodel.rule.IAction in project drools by kiegroup.
the class RuleModelDRLPersistenceTest method testMoreComplexRenderingWithDsl.
@Test
public void testMoreComplexRenderingWithDsl() {
final RuleModel m = getComplexModel(true);
String expected = "package org.test;\n" + "rule \"Complex Rule\"\n" + "no-loop true\n" + "salience -10\n" + "agenda-group \"aGroup\"\n" + "dialect \"mvel\"\n" + "when\n" + " >p1 : Person( f1 : age < 42 )\n" + " >not (Cancel( )) \n" + "then\n" + " >modify( p1 ) {\n" + " >setStatus( \"rejected\" ),\n" + " >setName( \"Fred\" )\n" + " >}\n" + " >retract( p1 );\n" + "Send an email to administrator\n" + "end\n";
checkMarshallingUsingDsl(expected, m);
String drl = ruleModelPersistence.marshal(m);
assertEqualsIgnoreWhitespace(expected, drl);
String dslFile = "[then]Send an email to {administrator}=sendMailTo({administrator});";
RuleModel unmarshalledModel = ruleModelPersistence.unmarshalUsingDSL(drl, null, dmo, dslFile);
IAction[] actions = unmarshalledModel.rhs;
DSLSentence dslSentence = (DSLSentence) actions[actions.length - 1];
assertEquals("Send an email to {administrator}", dslSentence.getDefinition());
checkMarshallingUsingDsl(expected, unmarshalledModel);
}
use of org.drools.workbench.models.datamodel.rule.IAction in project drools by kiegroup.
the class GuidedDTDRLPersistenceTest method testRHSWithBRLColumn_ParseToDRL_FreeFormLine.
@Test
public // This test checks a Decision Table involving BRL columns is correctly converted into DRL
void testRHSWithBRLColumn_ParseToDRL_FreeFormLine() {
GuidedDecisionTable52 dtable = new GuidedDecisionTable52();
// Row 0 should become an IAction in the resulting RuleModel as it contains values for all Template fields in the BRL Column
// Row 1 should *NOT* become an IAction in the resulting RuleModel as it does *NOT* contain values for all Template fields in the BRL Column
// Row 2 should *NOT* become an IAction in the resulting RuleModel as it does *NOT* contain values for all Template fields in the BRL Column
// Row 3 should *NOT* become an IAction in the resulting RuleModel as it does *NOT* contain values for all Template fields in the BRL Column
String[][] data = new String[][] { new String[] { "1", "desc", "Pupa", "50" }, new String[] { "2", "desc", "", "50" }, new String[] { "3", "desc", "Pupa", "" }, new String[] { "4", "desc", "", "" } };
// Simple (mandatory) columns
dtable.setRowNumberCol(new RowNumberCol52());
dtable.setDescriptionCol(new DescriptionCol52());
// BRL Action
BRLActionColumn brl1 = new BRLActionColumn();
// BRL Action definition
List<IAction> brl1Definition = new ArrayList<IAction>();
FreeFormLine brl1DefinitionFreeFormLine = new FreeFormLine();
brl1DefinitionFreeFormLine.setText("System.out.println( \"name == @{name}, age == @{age}\" );");
brl1Definition.add(brl1DefinitionFreeFormLine);
brl1.setDefinition(brl1Definition);
// Setup BRL column bindings
BRLActionVariableColumn brl1Variable1 = new BRLActionVariableColumn("name", DataType.TYPE_STRING);
BRLActionVariableColumn brl1Variable2 = new BRLActionVariableColumn("age", DataType.TYPE_NUMERIC_INTEGER);
brl1.getChildColumns().add(brl1Variable1);
brl1.getChildColumns().add(brl1Variable2);
dtable.getActionCols().add(brl1);
dtable.setData(DataUtilities.makeDataLists(data));
// Now to test conversion
int ruleStartIndex;
int pattern1StartIndex;
GuidedDTDRLPersistence p = GuidedDTDRLPersistence.getInstance();
String drl = p.marshal(dtable);
// Row 0
ruleStartIndex = drl.indexOf("//from row number: 1");
assertFalse(ruleStartIndex == -1);
pattern1StartIndex = drl.indexOf("System.out.println( \"name == Pupa, age == 50\" );", ruleStartIndex);
assertFalse(pattern1StartIndex == -1);
// Row 1
ruleStartIndex = drl.indexOf("//from row number: 2");
assertFalse(ruleStartIndex == -1);
pattern1StartIndex = drl.indexOf("System.out.println(", ruleStartIndex);
assertTrue(pattern1StartIndex == -1);
// Row 2
ruleStartIndex = drl.indexOf("//from row number: 3");
assertFalse(ruleStartIndex == -1);
pattern1StartIndex = drl.indexOf("System.out.println(", ruleStartIndex);
assertTrue(pattern1StartIndex == -1);
// Row 3
ruleStartIndex = drl.indexOf("//from row number: 4");
assertFalse(ruleStartIndex == -1);
pattern1StartIndex = drl.indexOf("System.out.println(", ruleStartIndex);
assertTrue(pattern1StartIndex == -1);
}
use of org.drools.workbench.models.datamodel.rule.IAction in project drools by kiegroup.
the class GuidedDTDRLPersistenceTest method testRHSWithBRLColumn_ParseToDRL.
@Test
public // This test checks a Decision Table involving BRL columns is correctly converted into DRL
void testRHSWithBRLColumn_ParseToDRL() {
GuidedDecisionTable52 dtable = new GuidedDecisionTable52();
// All three rows are entered, some columns with optional data
String[][] data = new String[][] { new String[] { "1", "desc", "Gargamel", "Pupa", "50" }, new String[] { "2", "desc", "Gargamel", "", "50" }, new String[] { "3", "desc", "Gargamel", "Pupa", "" } };
// Simple (mandatory) columns
dtable.setRowNumberCol(new RowNumberCol52());
dtable.setDescriptionCol(new DescriptionCol52());
// Simple Action
ActionInsertFactCol52 a1 = new ActionInsertFactCol52();
a1.setBoundName("$b");
a1.setFactType("Baddie");
a1.setFactField("name");
a1.setType(DataType.TYPE_STRING);
dtable.getActionCols().add(a1);
// BRL Column
BRLActionColumn brl1 = new BRLActionColumn();
// BRL Column definition
List<IAction> brl1Definition = new ArrayList<IAction>();
ActionInsertFact brl1DefinitionAction1 = new ActionInsertFact("Smurf");
ActionFieldValue brl1DefinitionAction1FieldValue1 = new ActionFieldValue("name", "$name", DataType.TYPE_STRING);
brl1DefinitionAction1FieldValue1.setNature(BaseSingleFieldConstraint.TYPE_TEMPLATE);
brl1DefinitionAction1.addFieldValue(brl1DefinitionAction1FieldValue1);
ActionFieldValue brl1DefinitionAction1FieldValue2 = new ActionFieldValue("age", "$age", DataType.TYPE_NUMERIC_INTEGER);
brl1DefinitionAction1FieldValue2.setNature(BaseSingleFieldConstraint.TYPE_TEMPLATE);
brl1DefinitionAction1.addFieldValue(brl1DefinitionAction1FieldValue2);
brl1Definition.add(brl1DefinitionAction1);
brl1.setDefinition(brl1Definition);
// Setup BRL column bindings
BRLActionVariableColumn brl1Variable1 = new BRLActionVariableColumn("$name", DataType.TYPE_STRING, "Person", "name");
brl1.getChildColumns().add(brl1Variable1);
BRLActionVariableColumn brl1Variable2 = new BRLActionVariableColumn("$age", DataType.TYPE_NUMERIC_INTEGER, "Person", "age");
brl1.getChildColumns().add(brl1Variable2);
dtable.getActionCols().add(brl1);
dtable.setData(DataUtilities.makeDataLists(data));
// Now to test conversion
int ruleStartIndex;
int action1StartIndex;
int action2StartIndex;
GuidedDTDRLPersistence p = GuidedDTDRLPersistence.getInstance();
String drl = p.marshal(dtable);
// Row 0
ruleStartIndex = drl.indexOf("//from row number: 1");
assertFalse(ruleStartIndex == -1);
action1StartIndex = drl.indexOf("Baddie $b = new Baddie();", ruleStartIndex);
assertFalse(action1StartIndex == -1);
action1StartIndex = drl.indexOf("$b.setName( \"Gargamel\" );", action1StartIndex);
assertFalse(action1StartIndex == -1);
action1StartIndex = drl.indexOf("insert( $b );", action1StartIndex);
assertFalse(action1StartIndex == -1);
action2StartIndex = drl.indexOf("Smurf fact0 = new Smurf();", ruleStartIndex);
assertFalse(action2StartIndex == -1);
action2StartIndex = drl.indexOf("fact0.setName( \"Pupa\" );", action2StartIndex);
assertFalse(action2StartIndex == -1);
action2StartIndex = drl.indexOf("fact0.setAge( 50 );", action2StartIndex);
assertFalse(action2StartIndex == -1);
action2StartIndex = drl.indexOf("insert( fact0 );", action2StartIndex);
assertFalse(action2StartIndex == -1);
// Row 1
ruleStartIndex = drl.indexOf("//from row number: 2");
int ruleEndIndex = drl.indexOf("//from row number: 3");
assertFalse(ruleStartIndex == -1);
action1StartIndex = drl.indexOf("Baddie $b = new Baddie();", ruleStartIndex);
assertFalse(action1StartIndex == -1);
action1StartIndex = drl.indexOf("$b.setName( \"Gargamel\" );", action1StartIndex);
assertFalse(action1StartIndex == -1);
action1StartIndex = drl.indexOf("insert( $b );", action1StartIndex);
assertFalse(action1StartIndex == -1);
action2StartIndex = drl.indexOf("Smurf fact0 = new Smurf();", ruleStartIndex);
assertFalse(action2StartIndex == -1);
action2StartIndex = drl.indexOf("fact0.setName(", ruleStartIndex);
assertFalse(action2StartIndex < ruleEndIndex);
action2StartIndex = drl.indexOf("fact0.setAge( 50 );", ruleStartIndex);
assertFalse(action2StartIndex == -1);
action2StartIndex = drl.indexOf("insert( fact0 );", ruleStartIndex);
assertFalse(action2StartIndex == -1);
// Row 2
ruleStartIndex = drl.indexOf("//from row number: 3");
assertFalse(ruleStartIndex == -1);
action1StartIndex = drl.indexOf("Baddie $b = new Baddie();", ruleStartIndex);
assertFalse(action1StartIndex == -1);
action1StartIndex = drl.indexOf("$b.setName( \"Gargamel\" );", action1StartIndex);
assertFalse(action1StartIndex == -1);
action1StartIndex = drl.indexOf("insert( $b );", action1StartIndex);
assertFalse(action1StartIndex == -1);
action2StartIndex = drl.indexOf("Smurf fact0 = new Smurf();", ruleStartIndex);
assertFalse(action2StartIndex == -1);
action2StartIndex = drl.indexOf("fact0.setName( \"Pupa\" );", ruleStartIndex);
assertFalse(action2StartIndex == -1);
action2StartIndex = drl.indexOf("fact0.setAge( 50 );", ruleStartIndex);
assertTrue(action2StartIndex == -1);
action2StartIndex = drl.indexOf("insert( fact0 );", ruleStartIndex);
assertFalse(action2StartIndex == -1);
}
Aggregations