Search in sources :

Example 26 with ActionInsertFact

use of org.drools.workbench.models.datamodel.rule.ActionInsertFact in project drools by kiegroup.

the class BRLRuleModelTest method testDecisionTableColumnsWithRHSBoundFacts.

@Test
public void testDecisionTableColumnsWithRHSBoundFacts() {
    GuidedDecisionTable52 dt = new GuidedDecisionTable52();
    Pattern52 p1 = new Pattern52();
    p1.setFactType("Driver");
    p1.setBoundName("$p1");
    ConditionCol52 c1 = new ConditionCol52();
    c1.setFactField("name");
    c1.setConstraintValueType(BaseSingleFieldConstraint.TYPE_LITERAL);
    c1.setBinding("$c1");
    p1.getChildColumns().add(c1);
    dt.getConditions().add(p1);
    ActionInsertFactCol52 ins = new ActionInsertFactCol52();
    ins.setBoundName("$ins");
    ins.setFactField("rating");
    ins.setFactType("Person");
    ins.setType(DataType.TYPE_STRING);
    dt.getActionCols().add(ins);
    ActionInsertFactCol52 ins2 = new ActionInsertFactCol52();
    ins2.setInsertLogical(true);
    ins2.setBoundName("$ins2");
    ins2.setFactField("rating2");
    ins2.setFactType("Person2");
    ins2.setType(DataType.TYPE_STRING);
    dt.getActionCols().add(ins2);
    BRLActionColumn brlAction = new BRLActionColumn();
    ActionInsertFact aif = new ActionInsertFact("Person");
    aif.setBoundName("$aif");
    aif.addFieldValue(new ActionFieldValue("rating", null, DataType.TYPE_STRING));
    aif.getFieldValues()[0].setNature(BaseSingleFieldConstraint.TYPE_LITERAL);
    brlAction.getDefinition().add(aif);
    dt.getActionCols().add(brlAction);
    BRLRuleModel model = new BRLRuleModel(dt);
    assertNotNull(model.getRHSBoundFacts());
    assertEquals(3, model.getRHSBoundFacts().size());
    assertTrue(model.getRHSBoundFacts().contains("$ins"));
    assertTrue(model.getRHSBoundFacts().contains("$ins2"));
    assertTrue(model.getRHSBoundFacts().contains("$aif"));
    ActionInsertFact r1 = model.getRHSBoundFact("$ins");
    assertNotNull(r1);
    assertTrue(r1 instanceof ActionInsertFactCol52ActionInsertFactAdaptor);
    ActionInsertFactCol52ActionInsertFactAdaptor raif1 = (ActionInsertFactCol52ActionInsertFactAdaptor) r1;
    assertEquals("Person", raif1.getFactType());
    assertEquals("rating", raif1.getFieldValues()[0].getField());
    assertEquals(DataType.TYPE_STRING, raif1.getFieldValues()[0].getType());
    assertNull(raif1.getFieldValues()[0].getValue());
    assertEquals(BaseSingleFieldConstraint.TYPE_LITERAL, raif1.getFieldValues()[0].getNature());
    ActionInsertFact r2 = model.getRHSBoundFact("$ins2");
    assertNotNull(r2);
    assertTrue(r2 instanceof ActionInsertFactCol52ActionInsertLogicalFactAdaptor);
    ActionInsertFactCol52ActionInsertLogicalFactAdaptor raif2 = (ActionInsertFactCol52ActionInsertLogicalFactAdaptor) r2;
    assertEquals("Person2", raif2.getFactType());
    assertEquals("rating2", raif2.getFieldValues()[0].getField());
    assertEquals(DataType.TYPE_STRING, raif2.getFieldValues()[0].getType());
    assertNull(raif2.getFieldValues()[0].getValue());
    assertEquals(BaseSingleFieldConstraint.TYPE_LITERAL, raif2.getFieldValues()[0].getNature());
    ActionInsertFact r3 = model.getRHSBoundFact("$aif");
    assertNotNull(r3);
    assertTrue(r3 instanceof ActionInsertFact);
    ActionInsertFact raif3 = (ActionInsertFact) r3;
    assertEquals("Person", raif3.getFactType());
    assertEquals("rating", raif3.getFieldValues()[0].getField());
    assertEquals(DataType.TYPE_STRING, raif3.getFieldValues()[0].getType());
    assertNull(raif3.getFieldValues()[0].getValue());
    assertEquals(BaseSingleFieldConstraint.TYPE_LITERAL, raif3.getFieldValues()[0].getNature());
}
Also used : ConditionCol52(org.drools.workbench.models.guided.dtable.shared.model.ConditionCol52) BRLActionColumn(org.drools.workbench.models.guided.dtable.shared.model.BRLActionColumn) GuidedDecisionTable52(org.drools.workbench.models.guided.dtable.shared.model.GuidedDecisionTable52) Pattern52(org.drools.workbench.models.guided.dtable.shared.model.Pattern52) ActionInsertFactCol52(org.drools.workbench.models.guided.dtable.shared.model.ActionInsertFactCol52) ActionFieldValue(org.drools.workbench.models.datamodel.rule.ActionFieldValue) ActionInsertFact(org.drools.workbench.models.datamodel.rule.ActionInsertFact) ActionInsertFactCol52ActionInsertLogicalFactAdaptor(org.drools.workbench.models.guided.dtable.shared.model.adaptors.ActionInsertFactCol52ActionInsertLogicalFactAdaptor) ActionInsertFactCol52ActionInsertFactAdaptor(org.drools.workbench.models.guided.dtable.shared.model.adaptors.ActionInsertFactCol52ActionInsertFactAdaptor) BRLRuleModel(org.drools.workbench.models.guided.dtable.shared.model.BRLRuleModel) Test(org.junit.Test)

Example 27 with ActionInsertFact

use of org.drools.workbench.models.datamodel.rule.ActionInsertFact 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)

Example 28 with ActionInsertFact

use of org.drools.workbench.models.datamodel.rule.ActionInsertFact in project drools by kiegroup.

the class GuidedDTDRLPersistenceTest method testRHSWithBRLColumn_ParseToRuleModel.

@Test
public // This test checks a Decision Table involving BRL columns is correctly converted into a RuleModel
void testRHSWithBRLColumn_ParseToRuleModel() {
    GuidedDecisionTable52 dtable = new GuidedDecisionTable52();
    GuidedDTDRLPersistence p = new GuidedDTDRLPersistence();
    // 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);
    // Now to test conversion
    RuleModel rm = new RuleModel();
    List<BaseColumn> allColumns = dtable.getExpandedColumns();
    List<ActionCol52> allActions = dtable.getActionCols();
    // Row 0
    List<DTCellValue52> dtRowData0 = DataUtilities.makeDataRowList(data[0]);
    TemplateDataProvider rowDataProvider0 = new GuidedDTTemplateDataProvider(allColumns, dtRowData0);
    p.doActions(allColumns, allActions, rowDataProvider0, dtRowData0, rm);
    assertEquals(2, rm.rhs.length);
    assertEquals("Baddie", ((ActionInsertFact) rm.rhs[0]).getFactType());
    assertEquals("Smurf", ((ActionInsertFact) rm.rhs[1]).getFactType());
    // examine the first action
    ActionInsertFact result0Action1 = (ActionInsertFact) rm.rhs[0];
    assertEquals(1, result0Action1.getFieldValues().length);
    ActionFieldValue result0Action1FieldValue1 = (ActionFieldValue) result0Action1.getFieldValues()[0];
    assertEquals(DataType.TYPE_STRING, result0Action1FieldValue1.getType());
    assertEquals("name", result0Action1FieldValue1.getField());
    assertEquals("Gargamel", result0Action1FieldValue1.getValue());
    // examine the second action
    ActionInsertFact result0Action2 = (ActionInsertFact) rm.rhs[1];
    assertEquals(2, result0Action2.getFieldValues().length);
    ActionFieldValue result0Action2FieldValue1 = (ActionFieldValue) result0Action2.getFieldValues()[0];
    assertEquals(DataType.TYPE_STRING, result0Action2FieldValue1.getType());
    assertEquals("name", result0Action2FieldValue1.getField());
    assertEquals("$name", result0Action2FieldValue1.getValue());
    ActionFieldValue result0Action2FieldValue2 = (ActionFieldValue) result0Action2.getFieldValues()[1];
    assertEquals(DataType.TYPE_NUMERIC_INTEGER, result0Action2FieldValue2.getType());
    assertEquals("age", result0Action2FieldValue2.getField());
    assertEquals("$age", result0Action2FieldValue2.getValue());
    // Row 1
    List<DTCellValue52> dtRowData1 = DataUtilities.makeDataRowList(data[1]);
    TemplateDataProvider rowDataProvider1 = new GuidedDTTemplateDataProvider(allColumns, dtRowData1);
    p.doActions(allColumns, allActions, rowDataProvider1, dtRowData1, rm);
    assertEquals(2, rm.rhs.length);
    assertEquals("Baddie", ((ActionInsertFact) rm.rhs[0]).getFactType());
    assertEquals("Smurf", ((ActionInsertFact) rm.rhs[1]).getFactType());
    // examine the first action
    ActionInsertFact result1Action1 = (ActionInsertFact) rm.rhs[0];
    assertEquals(1, result1Action1.getFieldValues().length);
    ActionFieldValue result1Action1FieldValue1 = (ActionFieldValue) result1Action1.getFieldValues()[0];
    assertEquals(DataType.TYPE_STRING, result1Action1FieldValue1.getType());
    assertEquals("name", result1Action1FieldValue1.getField());
    assertEquals("Gargamel", result1Action1FieldValue1.getValue());
    // examine the second action
    ActionInsertFact result1Action2 = (ActionInsertFact) rm.rhs[1];
    assertEquals(2, result1Action2.getFieldValues().length);
    ActionFieldValue result1Action2FieldValue1 = (ActionFieldValue) result1Action2.getFieldValues()[0];
    assertEquals(DataType.TYPE_STRING, result1Action2FieldValue1.getType());
    assertEquals("name", result1Action2FieldValue1.getField());
    assertEquals("$name", result1Action2FieldValue1.getValue());
    ActionFieldValue result1Action2FieldValue2 = (ActionFieldValue) result1Action2.getFieldValues()[1];
    assertEquals(DataType.TYPE_NUMERIC_INTEGER, result1Action2FieldValue2.getType());
    assertEquals("age", result1Action2FieldValue2.getField());
    assertEquals("$age", result1Action2FieldValue2.getValue());
    // Row 2
    List<DTCellValue52> dtRowData2 = DataUtilities.makeDataRowList(data[2]);
    TemplateDataProvider rowDataProvider2 = new GuidedDTTemplateDataProvider(allColumns, dtRowData2);
    p.doActions(allColumns, allActions, rowDataProvider2, dtRowData2, rm);
    assertEquals(2, rm.rhs.length);
    assertEquals("Baddie", ((ActionInsertFact) rm.rhs[0]).getFactType());
    assertEquals("Smurf", ((ActionInsertFact) rm.rhs[1]).getFactType());
    // examine the first action
    ActionInsertFact result2Action1 = (ActionInsertFact) rm.rhs[0];
    assertEquals(1, result2Action1.getFieldValues().length);
    ActionFieldValue result2Action1FieldValue1 = (ActionFieldValue) result2Action1.getFieldValues()[0];
    assertEquals(DataType.TYPE_STRING, result2Action1FieldValue1.getType());
    assertEquals("name", result2Action1FieldValue1.getField());
    assertEquals("Gargamel", result2Action1FieldValue1.getValue());
    // examine the second action
    ActionInsertFact result2Action2 = (ActionInsertFact) rm.rhs[1];
    assertEquals(2, result2Action2.getFieldValues().length);
    ActionFieldValue result2Action2FieldValue1 = (ActionFieldValue) result2Action2.getFieldValues()[0];
    assertEquals(DataType.TYPE_STRING, result2Action2FieldValue1.getType());
    assertEquals("name", result2Action2FieldValue1.getField());
    assertEquals("$name", result2Action2FieldValue1.getValue());
    ActionFieldValue result3Action2FieldValue2 = (ActionFieldValue) result2Action2.getFieldValues()[1];
    assertEquals(DataType.TYPE_NUMERIC_INTEGER, result3Action2FieldValue2.getType());
    assertEquals("age", result3Action2FieldValue2.getField());
    assertEquals("$age", result3Action2FieldValue2.getValue());
}
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) ActionCol52(org.drools.workbench.models.guided.dtable.shared.model.ActionCol52) ArrayList(java.util.ArrayList) GuidedDTTemplateDataProvider(org.drools.workbench.models.guided.dtable.backend.util.GuidedDTTemplateDataProvider) DTCellValue52(org.drools.workbench.models.guided.dtable.shared.model.DTCellValue52) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) 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) BaseColumn(org.drools.workbench.models.guided.dtable.shared.model.BaseColumn) BRLActionVariableColumn(org.drools.workbench.models.guided.dtable.shared.model.BRLActionVariableColumn) RowNumberCol52(org.drools.workbench.models.guided.dtable.shared.model.RowNumberCol52) GuidedDTTemplateDataProvider(org.drools.workbench.models.guided.dtable.backend.util.GuidedDTTemplateDataProvider) TemplateDataProvider(org.drools.workbench.models.guided.dtable.backend.util.TemplateDataProvider) Test(org.junit.Test)

Example 29 with ActionInsertFact

use of org.drools.workbench.models.datamodel.rule.ActionInsertFact in project drools by kiegroup.

the class GuidedDecisionTreeModelUnmarshallingVisitor method visit.

private List<Node> visit(final IAction a, final List<TypeNode> types, final GuidedDecisionTree model, final PackageDataModelOracle dmo, final List<ParserMessage> messages) {
    final List<Node> nodes = new ArrayList<Node>();
    if (a instanceof ActionRetractFact) {
        final ActionRetractFact arf = (ActionRetractFact) a;
        final String binding = arf.getVariableName();
        for (TypeNode tn : types) {
            if (tn.isBound()) {
                if (tn.getBinding().equals(binding)) {
                    final ActionRetractNode arn = new ActionRetractNodeImpl(tn);
                    nodes.add(arn);
                    return nodes;
                }
            }
        }
        messages.add(new BindingNotFoundParserMessage(binding));
        return nodes;
    } else if (a instanceof ActionInsertLogicalFact) {
        final ActionInsertLogicalFact aif = (ActionInsertLogicalFact) a;
        final ActionInsertNode aun = new ActionInsertNodeImpl(aif.getFactType());
        aun.setLogicalInsertion(true);
        for (org.drools.workbench.models.datamodel.rule.ActionFieldValue afv : aif.getFieldValues()) {
            if (afv.getNature() != FieldNatureType.TYPE_LITERAL) {
                messages.add(new UnsupportedFieldNatureTypeParserMessage());
                return nodes;
            }
            final String fieldName = afv.getField();
            final Value value = getValue(aif.getFactType(), afv.getField(), model, dmo, messages, afv.getValue());
            if (value != null) {
                final ActionFieldValue _afv = new ActionFieldValueImpl(fieldName, value);
                aun.getFieldValues().add(_afv);
            }
        }
        nodes.add(aun);
        return nodes;
    } else if (a instanceof ActionInsertFact) {
        final ActionInsertFact aif = (ActionInsertFact) a;
        final ActionInsertNode aun = new ActionInsertNodeImpl(aif.getFactType());
        aun.setLogicalInsertion(false);
        for (org.drools.workbench.models.datamodel.rule.ActionFieldValue afv : aif.getFieldValues()) {
            if (afv.getNature() != FieldNatureType.TYPE_LITERAL) {
                messages.add(new UnsupportedFieldNatureTypeParserMessage());
                return nodes;
            }
            final String fieldName = afv.getField();
            final Value value = getValue(aif.getFactType(), afv.getField(), model, dmo, messages, afv.getValue());
            if (value != null) {
                final ActionFieldValue _afv = new ActionFieldValueImpl(fieldName, value);
                aun.getFieldValues().add(_afv);
            }
        }
        nodes.add(aun);
        return nodes;
    } else if (a instanceof ActionUpdateField) {
        final ActionUpdateField auf = (ActionUpdateField) a;
        final String binding = auf.getVariable();
        for (TypeNode tn : types) {
            if (tn.isBound()) {
                if (tn.getBinding().equals(binding)) {
                    final ActionUpdateNode aun = new ActionUpdateNodeImpl(tn);
                    aun.setModify(true);
                    for (org.drools.workbench.models.datamodel.rule.ActionFieldValue afv : auf.getFieldValues()) {
                        if (afv.getNature() != FieldNatureType.TYPE_LITERAL) {
                            messages.add(new UnsupportedFieldNatureTypeParserMessage());
                            return nodes;
                        }
                        final String fieldName = afv.getField();
                        final Value value = getValue(tn.getClassName(), afv.getField(), model, dmo, messages, afv.getValue());
                        if (value != null) {
                            final ActionFieldValue _afv = new ActionFieldValueImpl(fieldName, value);
                            aun.getFieldValues().add(_afv);
                        }
                    }
                    nodes.add(aun);
                    return nodes;
                }
            }
        }
        messages.add(new BindingNotFoundParserMessage(binding));
        return nodes;
    } else if (a instanceof ActionSetField) {
        final ActionSetField asf = (ActionSetField) a;
        final String binding = asf.getVariable();
        for (TypeNode tn : types) {
            if (tn.isBound()) {
                if (tn.getBinding().equals(binding)) {
                    final ActionUpdateNode aun = new ActionUpdateNodeImpl(tn);
                    for (org.drools.workbench.models.datamodel.rule.ActionFieldValue afv : asf.getFieldValues()) {
                        if (afv.getNature() != FieldNatureType.TYPE_LITERAL) {
                            messages.add(new UnsupportedFieldNatureTypeParserMessage());
                            return nodes;
                        }
                        final String fieldName = afv.getField();
                        final Value value = getValue(tn.getClassName(), afv.getField(), model, dmo, messages, afv.getValue());
                        if (value != null) {
                            final ActionFieldValue _afv = new ActionFieldValueImpl(fieldName, value);
                            aun.getFieldValues().add(_afv);
                        }
                    }
                    nodes.add(aun);
                    return nodes;
                }
            }
        }
        messages.add(new BindingNotFoundParserMessage(binding));
        return nodes;
    } else {
        messages.add(new UnsupportedIActionParserMessage());
        return nodes;
    }
}
Also used : ActionInsertNode(org.drools.workbench.models.guided.dtree.shared.model.nodes.ActionInsertNode) ActionRetractNode(org.drools.workbench.models.guided.dtree.shared.model.nodes.ActionRetractNode) TypeNode(org.drools.workbench.models.guided.dtree.shared.model.nodes.TypeNode) Node(org.drools.workbench.models.guided.dtree.shared.model.nodes.Node) ActionUpdateNode(org.drools.workbench.models.guided.dtree.shared.model.nodes.ActionUpdateNode) ActionInsertNode(org.drools.workbench.models.guided.dtree.shared.model.nodes.ActionInsertNode) ConstraintNode(org.drools.workbench.models.guided.dtree.shared.model.nodes.ConstraintNode) ArrayList(java.util.ArrayList) ActionRetractNodeImpl(org.drools.workbench.models.guided.dtree.shared.model.nodes.impl.ActionRetractNodeImpl) ActionInsertNodeImpl(org.drools.workbench.models.guided.dtree.shared.model.nodes.impl.ActionInsertNodeImpl) ActionRetractFact(org.drools.workbench.models.datamodel.rule.ActionRetractFact) ActionUpdateField(org.drools.workbench.models.datamodel.rule.ActionUpdateField) BindingNotFoundParserMessage(org.drools.workbench.models.guided.dtree.shared.model.parser.messages.BindingNotFoundParserMessage) ActionFieldValue(org.drools.workbench.models.guided.dtree.shared.model.nodes.ActionFieldValue) ActionRetractNode(org.drools.workbench.models.guided.dtree.shared.model.nodes.ActionRetractNode) ActionSetField(org.drools.workbench.models.datamodel.rule.ActionSetField) UnsupportedFieldNatureTypeParserMessage(org.drools.workbench.models.guided.dtree.shared.model.parser.messages.UnsupportedFieldNatureTypeParserMessage) ActionInsertFact(org.drools.workbench.models.datamodel.rule.ActionInsertFact) ActionUpdateNode(org.drools.workbench.models.guided.dtree.shared.model.nodes.ActionUpdateNode) ShortValue(org.drools.workbench.models.guided.dtree.shared.model.values.impl.ShortValue) StringValue(org.drools.workbench.models.guided.dtree.shared.model.values.impl.StringValue) FloatValue(org.drools.workbench.models.guided.dtree.shared.model.values.impl.FloatValue) BigDecimalValue(org.drools.workbench.models.guided.dtree.shared.model.values.impl.BigDecimalValue) ByteValue(org.drools.workbench.models.guided.dtree.shared.model.values.impl.ByteValue) Value(org.drools.workbench.models.guided.dtree.shared.model.values.Value) DoubleValue(org.drools.workbench.models.guided.dtree.shared.model.values.impl.DoubleValue) IntegerValue(org.drools.workbench.models.guided.dtree.shared.model.values.impl.IntegerValue) DateValue(org.drools.workbench.models.guided.dtree.shared.model.values.impl.DateValue) BooleanValue(org.drools.workbench.models.guided.dtree.shared.model.values.impl.BooleanValue) BigIntegerValue(org.drools.workbench.models.guided.dtree.shared.model.values.impl.BigIntegerValue) LongValue(org.drools.workbench.models.guided.dtree.shared.model.values.impl.LongValue) ActionFieldValue(org.drools.workbench.models.guided.dtree.shared.model.nodes.ActionFieldValue) EnumValue(org.drools.workbench.models.guided.dtree.shared.model.values.impl.EnumValue) ActionUpdateNodeImpl(org.drools.workbench.models.guided.dtree.shared.model.nodes.impl.ActionUpdateNodeImpl) TypeNode(org.drools.workbench.models.guided.dtree.shared.model.nodes.TypeNode) ActionFieldValueImpl(org.drools.workbench.models.guided.dtree.shared.model.nodes.impl.ActionFieldValueImpl) UnsupportedIActionParserMessage(org.drools.workbench.models.guided.dtree.shared.model.parser.messages.UnsupportedIActionParserMessage) ActionInsertLogicalFact(org.drools.workbench.models.datamodel.rule.ActionInsertLogicalFact)

Example 30 with ActionInsertFact

use of org.drools.workbench.models.datamodel.rule.ActionInsertFact in project drools by kiegroup.

the class RuleTemplateModelDRLPersistenceTest method testActionInsertFactZeroValues.

@Test
public void testActionInsertFactZeroValues() {
    TemplateModel m = new TemplateModel();
    m.name = "r1";
    FactPattern fp = new FactPattern("Person");
    fp.setBoundName("$p");
    m.addLhsItem(fp);
    ActionInsertFact aif = new ActionInsertFact("Present");
    aif.setBoundName("f0");
    ActionFieldValue afv0 = new ActionFieldValue();
    afv0.setNature(FieldNatureType.TYPE_TEMPLATE);
    afv0.setField("field1");
    afv0.setValue("$f1");
    aif.addFieldValue(afv0);
    ActionFieldValue afv1 = new ActionFieldValue();
    afv1.setNature(FieldNatureType.TYPE_TEMPLATE);
    afv1.setField("field2");
    afv1.setValue("$f2");
    aif.addFieldValue(afv1);
    m.addRhsItem(aif);
    String expected = "rule \"r1_0\"\n" + "dialect \"mvel\"\n" + "when\n" + "$p : Person()\n" + "then\n" + "Present f0 = new Present();\n" + "insert(f0);\n" + "end";
    m.addRow(new String[] { null, null });
    checkMarshall(expected, m);
}
Also used : ActionFieldValue(org.drools.workbench.models.datamodel.rule.ActionFieldValue) ActionInsertFact(org.drools.workbench.models.datamodel.rule.ActionInsertFact) CompositeFactPattern(org.drools.workbench.models.datamodel.rule.CompositeFactPattern) FactPattern(org.drools.workbench.models.datamodel.rule.FactPattern) FromCollectCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern) TemplateModel(org.drools.workbench.models.guided.template.shared.TemplateModel) Test(org.junit.Test)

Aggregations

ActionInsertFact (org.drools.workbench.models.datamodel.rule.ActionInsertFact)78 Test (org.junit.Test)64 ActionFieldValue (org.drools.workbench.models.datamodel.rule.ActionFieldValue)48 RuleModel (org.drools.workbench.models.datamodel.rule.RuleModel)47 FactPattern (org.drools.workbench.models.datamodel.rule.FactPattern)43 CompositeFactPattern (org.drools.workbench.models.datamodel.rule.CompositeFactPattern)42 FromCollectCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern)39 FromAccumulateCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern)34 FromCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern)34 SingleFieldConstraint (org.drools.workbench.models.datamodel.rule.SingleFieldConstraint)34 BaseSingleFieldConstraint (org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint)32 FromEntryPointFactPattern (org.drools.workbench.models.datamodel.rule.FromEntryPointFactPattern)24 IAction (org.drools.workbench.models.datamodel.rule.IAction)17 BRLActionColumn (org.drools.workbench.models.guided.dtable.shared.model.BRLActionColumn)13 IPattern (org.drools.workbench.models.datamodel.rule.IPattern)12 GuidedDecisionTable52 (org.drools.workbench.models.guided.dtable.shared.model.GuidedDecisionTable52)12 ArrayList (java.util.ArrayList)11 CompositeFieldConstraint (org.drools.workbench.models.datamodel.rule.CompositeFieldConstraint)10 RuleAttribute (org.drools.workbench.models.datamodel.rule.RuleAttribute)10 BRLActionVariableColumn (org.drools.workbench.models.guided.dtable.shared.model.BRLActionVariableColumn)10