Search in sources :

Example 81 with ActionFieldValue

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

the class RuleTemplateModelDRLPersistenceTest method testRHSInsert.

@Test
public void testRHSInsert() {
    TemplateModel m = new TemplateModel();
    m.name = "t1";
    FactPattern p = new FactPattern("Person");
    SingleFieldConstraint con = new SingleFieldConstraint();
    con.setFieldType(DataType.TYPE_STRING);
    con.setFieldName("field1");
    con.setOperator("==");
    con.setValue("$f1");
    con.setConstraintValueType(SingleFieldConstraint.TYPE_TEMPLATE);
    p.addConstraint(con);
    m.addLhsItem(p);
    ActionInsertFact actionInsertFact = new ActionInsertFact();
    actionInsertFact.setFactType("Applicant");
    ActionFieldValue actionFieldValue = new ActionFieldValue("age", "123", "Integer");
    actionFieldValue.setNature(SingleFieldConstraint.TYPE_LITERAL);
    actionInsertFact.addFieldValue(actionFieldValue);
    m.addRhsItem(actionInsertFact);
    m.addRow(new String[] { "foo" });
    String expected = "rule \"t1_0\"" + "dialect \"mvel\"\n" + "when \n" + "Person( field1 == \"foo\" )\n" + "then \n" + "  Applicant fact0 = new Applicant(); \n" + "  fact0.setAge(123); \n" + "  insert(fact0); \n" + "end";
    checkMarshall(expected, m);
}
Also used : BaseSingleFieldConstraint(org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint) SingleFieldConstraint(org.drools.workbench.models.datamodel.rule.SingleFieldConstraint) 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)

Example 82 with ActionFieldValue

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

the class RHSClassDependencyVisitorTest method visitActionInsertLogicalFact.

@Test
public void visitActionInsertLogicalFact() {
    ActionInsertLogicalFact iAction = new ActionInsertLogicalFact();
    iAction.setFieldValues(new ActionFieldValue[] { new ActionFieldValue("field", "value", "type") });
    visitor.visit(iAction);
    assertTrue(visitor.getRHSClasses().containsKey("type"));
}
Also used : ActionFieldValue(org.drools.workbench.models.datamodel.rule.ActionFieldValue) ActionInsertLogicalFact(org.drools.workbench.models.datamodel.rule.ActionInsertLogicalFact) Test(org.junit.Test)

Example 83 with ActionFieldValue

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

the class RuleModelDRLPersistenceTest method testActionSetFieldValueWithDSL.

@Test
public void testActionSetFieldValueWithDSL() {
    String oldValue = System.getProperty("drools.dateformat");
    try {
        System.setProperty("drools.dateformat", "dd-MMM-yyyy");
        final String dsl = "There is an Applicant";
        final String dslDefinition = "[when]" + dsl + "=$a : Applicant();";
        final RuleModel m = new RuleModel();
        m.name = "r0";
        final DSLSentence sen = new DSLSentence();
        sen.setDefinition(dsl);
        m.addLhsItem(sen);
        final ActionInsertFact aif = new ActionInsertFact("Applicant");
        aif.addFieldValue(new ActionFieldValue("applicationDate", "13-Mar-2017", DataType.TYPE_DATE));
        m.addRhsItem(aif);
        final String expected = "rule \"r0\"\n" + "dialect \"mvel\"\n" + "when\n" + "There is an Applicant\n" + "then\n" + ">java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(\"dd-MMM-yyyy\");\n" + ">Applicant fact0 = new Applicant();\n" + ">fact0.setApplicationDate( sdf.parse(\"13-Mar-2017\") );\n" + ">insert( fact0 );\n" + "end";
        final String actual = RuleModelDRLPersistenceImpl.getInstance().marshal(m);
        assertEqualsIgnoreWhitespace(expected, actual);
        PackageDataModelOracle dmo = mock(PackageDataModelOracle.class);
        final RuleModel m2 = RuleModelDRLPersistenceImpl.getInstance().unmarshalUsingDSL(actual, Collections.emptyList(), dmo, dslDefinition);
        assertNotNull(m2);
        // LHS
        assertEquals(1, m.lhs.length);
        DSLSentence dslSentence = (DSLSentence) m.lhs[0];
        assertEquals(dsl, dslSentence.getDefinition());
        assertEquals(0, dslSentence.getValues().size());
        // RHS
        assertEquals(1, m2.rhs.length);
        assertTrue(m2.rhs[0] instanceof ActionInsertFact);
        final ActionInsertFact a = (ActionInsertFact) m2.rhs[0];
        assertEquals("Applicant", a.getFactType());
        assertEquals(1, a.getFieldValues().length);
        final ActionFieldValue afv = a.getFieldValues()[0];
        assertEquals("applicationDate", afv.getField());
        assertEquals("13-Mar-2017", afv.getValue());
    } finally {
        if (oldValue == null) {
            System.clearProperty("drools.dateformat");
        } else {
            System.setProperty("drools.dateformat", oldValue);
        }
    }
}
Also used : ActionFieldValue(org.drools.workbench.models.datamodel.rule.ActionFieldValue) ActionInsertFact(org.drools.workbench.models.datamodel.rule.ActionInsertFact) PackageDataModelOracle(org.kie.soup.project.datamodel.oracle.PackageDataModelOracle) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) DSLSentence(org.drools.workbench.models.datamodel.rule.DSLSentence) Test(org.junit.Test)

Example 84 with ActionFieldValue

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

the class RuleModelDRLPersistenceTest method testAssertWithDSL.

@Test
public void testAssertWithDSL() throws Exception {
    RuleModel m = new RuleModel();
    DSLSentence sen = new DSLSentence();
    sen.setDefinition("I CAN HAS DSL");
    m.addRhsItem(sen);
    ActionInsertFact ins = new ActionInsertFact("Shizzle");
    ActionFieldValue val = new ActionFieldValue("goo", "42", "Numeric");
    ins.setFieldValues(new ActionFieldValue[1]);
    ins.getFieldValues()[0] = val;
    m.addRhsItem(ins);
    ActionInsertLogicalFact insL = new ActionInsertLogicalFact("Shizzle");
    ActionFieldValue valL = new ActionFieldValue("goo", "42", "Numeric");
    insL.setFieldValues(new ActionFieldValue[1]);
    insL.getFieldValues()[0] = valL;
    m.addRhsItem(insL);
    String result = RuleModelDRLPersistenceImpl.getInstance().marshal(m);
    assertTrue(result.indexOf(">insert") > -1);
    assertTrue(result.indexOf(">insertLogical") > -1);
}
Also used : ActionFieldValue(org.drools.workbench.models.datamodel.rule.ActionFieldValue) ActionInsertFact(org.drools.workbench.models.datamodel.rule.ActionInsertFact) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) DSLSentence(org.drools.workbench.models.datamodel.rule.DSLSentence) ActionInsertLogicalFact(org.drools.workbench.models.datamodel.rule.ActionInsertLogicalFact) Test(org.junit.Test)

Example 85 with ActionFieldValue

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

the class RuleModelDRLPersistenceTest method testLiteralBigDecimalJava.

@Test
public void testLiteralBigDecimalJava() {
    RuleModel m = new RuleModel();
    m.name = "test literal bigdecimal";
    m.addAttribute(new RuleAttribute("dialect", "java"));
    FactPattern p = new FactPattern("Person");
    SingleFieldConstraint con = new SingleFieldConstraint();
    con.setFieldType(DataType.TYPE_NUMERIC_BIGDECIMAL);
    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_BIGDECIMAL));
    m.addRhsItem(ai);
    String expected = "rule \"test literal bigdecimal\" \n" + "\tdialect \"java\"\n when \n" + "     Person(field1 == 44B) \n" + " then \n" + "Person fact0 = new Person(); \n" + "fact0.setField1( new java.math.BigDecimal( \"55\" ) ); \n" + "insert( fact0 ); \n" + "end";
    checkMarshalling(expected, m);
}
Also used : SingleFieldConstraint(org.drools.workbench.models.datamodel.rule.SingleFieldConstraint) BaseSingleFieldConstraint(org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint) ActionFieldValue(org.drools.workbench.models.datamodel.rule.ActionFieldValue) ActionInsertFact(org.drools.workbench.models.datamodel.rule.ActionInsertFact) RuleAttribute(org.drools.workbench.models.datamodel.rule.RuleAttribute) FromEntryPointFactPattern(org.drools.workbench.models.datamodel.rule.FromEntryPointFactPattern) CompositeFactPattern(org.drools.workbench.models.datamodel.rule.CompositeFactPattern) FromCollectCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern) FactPattern(org.drools.workbench.models.datamodel.rule.FactPattern) FromAccumulateCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern) FromCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) Test(org.junit.Test)

Aggregations

ActionFieldValue (org.drools.workbench.models.datamodel.rule.ActionFieldValue)136 Test (org.junit.Test)113 CompositeFactPattern (org.drools.workbench.models.datamodel.rule.CompositeFactPattern)71 FactPattern (org.drools.workbench.models.datamodel.rule.FactPattern)71 FromCollectCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern)65 RuleModel (org.drools.workbench.models.datamodel.rule.RuleModel)63 ActionUpdateField (org.drools.workbench.models.datamodel.rule.ActionUpdateField)53 ActionInsertFact (org.drools.workbench.models.datamodel.rule.ActionInsertFact)48 FromAccumulateCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern)41 FromCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern)41 SingleFieldConstraint (org.drools.workbench.models.datamodel.rule.SingleFieldConstraint)33 IPattern (org.drools.workbench.models.datamodel.rule.IPattern)30 BaseSingleFieldConstraint (org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint)28 TemplateModel (org.drools.workbench.models.guided.template.shared.TemplateModel)26 IAction (org.drools.workbench.models.datamodel.rule.IAction)25 BRLActionColumn (org.drools.workbench.models.guided.dtable.shared.model.BRLActionColumn)24 ActionSetField (org.drools.workbench.models.datamodel.rule.ActionSetField)23 GuidedDecisionTable52 (org.drools.workbench.models.guided.dtable.shared.model.GuidedDecisionTable52)23 BRLActionVariableColumn (org.drools.workbench.models.guided.dtable.shared.model.BRLActionVariableColumn)21 ArrayList (java.util.ArrayList)19