Search in sources :

Example 11 with IAction

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);
}
Also used : TestIActionPersistenceExtension(org.drools.workbench.models.commons.backend.rule.extensions.TestIActionPersistenceExtension) TestIAction(org.drools.workbench.models.commons.backend.rule.actions.TestIAction) IAction(org.drools.workbench.models.datamodel.rule.IAction) TestIAction(org.drools.workbench.models.commons.backend.rule.actions.TestIAction) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) PackageDataModelOracleImpl(org.kie.soup.project.datamodel.commons.oracle.PackageDataModelOracleImpl) Test(org.junit.Test)

Example 12 with IAction

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);
}
Also used : FreeFormLine(org.drools.workbench.models.datamodel.rule.FreeFormLine) TestIAction(org.drools.workbench.models.commons.backend.rule.actions.TestIAction) IAction(org.drools.workbench.models.datamodel.rule.IAction) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) PackageDataModelOracleImpl(org.kie.soup.project.datamodel.commons.oracle.PackageDataModelOracleImpl) Test(org.junit.Test)

Example 13 with IAction

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);
}
Also used : IAction(org.drools.workbench.models.datamodel.rule.IAction) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) DSLSentence(org.drools.workbench.models.datamodel.rule.DSLSentence) Test(org.junit.Test)

Example 14 with IAction

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);
}
Also used : GuidedDecisionTable52(org.drools.workbench.models.guided.dtable.shared.model.GuidedDecisionTable52) IAction(org.drools.workbench.models.datamodel.rule.IAction) ArrayList(java.util.ArrayList) SingleFieldConstraint(org.drools.workbench.models.datamodel.rule.SingleFieldConstraint) BaseSingleFieldConstraint(org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint) BRLActionColumn(org.drools.workbench.models.guided.dtable.shared.model.BRLActionColumn) FreeFormLine(org.drools.workbench.models.datamodel.rule.FreeFormLine) DescriptionCol52(org.drools.workbench.models.guided.dtable.shared.model.DescriptionCol52) BRLActionVariableColumn(org.drools.workbench.models.guided.dtable.shared.model.BRLActionVariableColumn) RowNumberCol52(org.drools.workbench.models.guided.dtable.shared.model.RowNumberCol52) Test(org.junit.Test)

Example 15 with IAction

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);
}
Also used : GuidedDecisionTable52(org.drools.workbench.models.guided.dtable.shared.model.GuidedDecisionTable52) ActionInsertFactCol52(org.drools.workbench.models.guided.dtable.shared.model.ActionInsertFactCol52) LimitedEntryActionInsertFactCol52(org.drools.workbench.models.guided.dtable.shared.model.LimitedEntryActionInsertFactCol52) IAction(org.drools.workbench.models.datamodel.rule.IAction) ArrayList(java.util.ArrayList) SingleFieldConstraint(org.drools.workbench.models.datamodel.rule.SingleFieldConstraint) BaseSingleFieldConstraint(org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint) BRLActionColumn(org.drools.workbench.models.guided.dtable.shared.model.BRLActionColumn) DescriptionCol52(org.drools.workbench.models.guided.dtable.shared.model.DescriptionCol52) ActionFieldValue(org.drools.workbench.models.datamodel.rule.ActionFieldValue) ActionInsertFact(org.drools.workbench.models.datamodel.rule.ActionInsertFact) BRLActionVariableColumn(org.drools.workbench.models.guided.dtable.shared.model.BRLActionVariableColumn) RowNumberCol52(org.drools.workbench.models.guided.dtable.shared.model.RowNumberCol52) Test(org.junit.Test)

Aggregations

IAction (org.drools.workbench.models.datamodel.rule.IAction)63 Test (org.junit.Test)44 IPattern (org.drools.workbench.models.datamodel.rule.IPattern)35 SingleFieldConstraint (org.drools.workbench.models.datamodel.rule.SingleFieldConstraint)32 RuleModel (org.drools.workbench.models.datamodel.rule.RuleModel)31 FactPattern (org.drools.workbench.models.datamodel.rule.FactPattern)30 CompositeFactPattern (org.drools.workbench.models.datamodel.rule.CompositeFactPattern)29 ActionFieldValue (org.drools.workbench.models.datamodel.rule.ActionFieldValue)25 BRLActionColumn (org.drools.workbench.models.guided.dtable.shared.model.BRLActionColumn)21 BaseSingleFieldConstraint (org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint)20 FromAccumulateCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern)20 FromCollectCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern)20 FromCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern)20 ArrayList (java.util.ArrayList)19 BRLActionVariableColumn (org.drools.workbench.models.guided.dtable.shared.model.BRLActionVariableColumn)19 ActionInsertFact (org.drools.workbench.models.datamodel.rule.ActionInsertFact)17 FreeFormLine (org.drools.workbench.models.datamodel.rule.FreeFormLine)17 GuidedDecisionTable52 (org.drools.workbench.models.guided.dtable.shared.model.GuidedDecisionTable52)17 DescriptionCol52 (org.drools.workbench.models.guided.dtable.shared.model.DescriptionCol52)15 RowNumberCol52 (org.drools.workbench.models.guided.dtable.shared.model.RowNumberCol52)15