Search in sources :

Example 16 with RuleAttribute

use of org.drools.workbench.models.datamodel.rule.RuleAttribute in project drools-wb by kiegroup.

the class RuleModelCloneVisitor method visitRuleModel.

public RuleModel visitRuleModel(RuleModel model) {
    RuleModel clone = new RuleModel();
    clone.modelVersion = model.modelVersion;
    clone.name = model.name;
    clone.parentName = model.parentName;
    clone.setNegated(model.isNegated());
    if (model.attributes != null) {
        clone.attributes = new RuleAttribute[model.attributes.length];
        for (int i = 0; i < model.attributes.length; i++) {
            RuleAttribute attr = model.attributes[i];
            clone.attributes[i] = (RuleAttribute) visit(attr);
        }
    }
    if (model.metadataList != null) {
        clone.metadataList = new RuleMetadata[model.metadataList.length];
        for (int i = 0; i < model.metadataList.length; i++) {
            RuleMetadata md = model.metadataList[i];
            clone.metadataList[i] = (RuleMetadata) visit(md);
        }
    }
    if (model.lhs != null) {
        clone.lhs = new IPattern[model.lhs.length];
        for (int i = 0; i < model.lhs.length; i++) {
            IPattern pattern = model.lhs[i];
            clone.lhs[i] = (IPattern) visit(pattern);
        }
    }
    if (model.rhs != null) {
        clone.rhs = new IAction[model.rhs.length];
        for (int i = 0; i < model.rhs.length; i++) {
            IAction action = model.rhs[i];
            clone.rhs[i] = (IAction) visit(action);
        }
    }
    return clone;
}
Also used : IPattern(org.drools.workbench.models.datamodel.rule.IPattern) IAction(org.drools.workbench.models.datamodel.rule.IAction) RuleAttribute(org.drools.workbench.models.datamodel.rule.RuleAttribute) RuleMetadata(org.drools.workbench.models.datamodel.rule.RuleMetadata) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) ConnectiveConstraint(org.drools.workbench.models.datamodel.rule.ConnectiveConstraint) CompositeFieldConstraint(org.drools.workbench.models.datamodel.rule.CompositeFieldConstraint) SingleFieldConstraint(org.drools.workbench.models.datamodel.rule.SingleFieldConstraint) FieldConstraint(org.drools.workbench.models.datamodel.rule.FieldConstraint)

Example 17 with RuleAttribute

use of org.drools.workbench.models.datamodel.rule.RuleAttribute in project drools-wb by kiegroup.

the class RuleModelCloneVisitorTest method testRuleModelCloning.

@Test
public void testRuleModelCloning() {
    RuleModel model = new RuleModel();
    // --------------------------------------------------------------------------------------------------------------
    // LHS
    // --------------------------------------------------------------------------------------------------------------
    // 
    RuleAttribute attribute = new RuleAttribute("att1_name", "att1_val");
    model.addAttribute(attribute);
    RuleMetadata metadata = new RuleMetadata("meta1_name", "meta1_val");
    model.addMetadata(metadata);
    FactPattern f1 = buildFactPattern();
    FactPattern f2 = new FactPattern();
    f2.setBoundName("$f2");
    f2.setFactType("Fact2");
    f2.addConstraint(buildCompositeFieldConstraint());
    CompositeFactPattern composite = new CompositeFactPattern();
    composite.setType(CompositeFactPattern.COMPOSITE_TYPE_EXISTS);
    composite.addFactPattern(f1);
    composite.addFactPattern(f2);
    model.addLhsItem(composite);
    model.addLhsItem(buildFromAccumulateCompositeFactPattern());
    model.addLhsItem(buildFromCollectCompositeFactPattern());
    model.addLhsItem(buildFromCompositeFactPattern());
    model.addLhsItem(buildFreeFormLine());
    model.addLhsItem(buildDslSentence());
    ActionGlobalCollectionAdd addGlobal = new ActionGlobalCollectionAdd();
    addGlobal.setFactName("MyFact");
    addGlobal.setGlobalName("glist");
    model.addRhsItem(addGlobal);
    // --------------------------------------------------------------------------------------------------------------
    // RHS
    // --------------------------------------------------------------------------------------------------------------
    // 
    ActionInsertFact aif = new ActionInsertFact();
    aif.setBoundName("$f");
    aif.setFactType("FactType");
    aif.addFieldValue(buildActionFieldValue());
    aif.addFieldValue(buildActionFieldValue());
    model.addRhsItem(aif);
    ActionUpdateField auf = new ActionUpdateField();
    auf.setVariable("$var");
    auf.addFieldValue(buildActionFieldValue());
    auf.addFieldValue(buildActionFieldValue());
    model.addRhsItem(auf);
    ActionSetField asf = new ActionSetField();
    asf.setVariable("$var");
    asf.addFieldValue(buildActionFieldValue());
    asf.addFieldValue(buildActionFieldValue());
    model.addRhsItem(asf);
    ActionRetractFact arf = new ActionRetractFact();
    arf.setVariableName("$f");
    model.addRhsItem(arf);
    ActionCallMethod callMethod1 = new ActionCallMethod();
    callMethod1.setVariable("var1");
    callMethod1.setMethodName("testMethod1 ");
    callMethod1.setState(ActionCallMethod.TYPE_DEFINED);
    callMethod1.addFieldValue(new ActionFieldFunction("field1", "value1", "type1"));
    model.addRhsItem(callMethod1);
    ActionCallMethod callMethod2 = new ActionCallMethod();
    callMethod2.setVariable("var2");
    callMethod2.setMethodName("testMethod2 ");
    callMethod2.setState(ActionCallMethod.TYPE_UNDEFINED);
    callMethod2.addFieldValue(new ActionFieldFunction("field2", "value2", "type2"));
    model.addRhsItem(callMethod2);
    model.addRhsItem(buildFreeFormLine());
    // --------------------------------------------------------------------------------------------------------------
    // Clone and verify
    // --------------------------------------------------------------------------------------------------------------
    // 
    RuleModel clone = new RuleModelCloneVisitor().visitRuleModel(model);
    assertArrayEquals(model.attributes, clone.attributes);
    int attIndex = 0;
    for (RuleAttribute att : model.attributes) {
        assertNotSame(att, clone.attributes[attIndex++]);
    }
    assertArrayEquals(model.metadataList, clone.metadataList);
    int metIndex = 0;
    for (RuleMetadata met : model.metadataList) {
        assertNotSame(met, clone.metadataList[metIndex++]);
    }
    assertArrayEquals(model.lhs, clone.lhs);
    int lhsIndex = 0;
    for (IPattern pattern : model.lhs) {
        assertNotSame(pattern, clone.lhs[lhsIndex++]);
    }
    assertArrayEquals(model.rhs, clone.rhs);
    int rhsIndex = 0;
    for (IAction action : model.rhs) {
        assertNotSame(action, clone.rhs[rhsIndex++]);
    }
}
Also used : IPattern(org.drools.workbench.models.datamodel.rule.IPattern) IAction(org.drools.workbench.models.datamodel.rule.IAction) ActionCallMethod(org.drools.workbench.models.datamodel.rule.ActionCallMethod) IFactPattern(org.drools.workbench.models.datamodel.rule.IFactPattern) FactPattern(org.drools.workbench.models.datamodel.rule.FactPattern) FromAccumulateCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern) CompositeFactPattern(org.drools.workbench.models.datamodel.rule.CompositeFactPattern) FromCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern) FromCollectCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern) FromAccumulateCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern) CompositeFactPattern(org.drools.workbench.models.datamodel.rule.CompositeFactPattern) FromCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern) FromCollectCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern) ActionFieldFunction(org.drools.workbench.models.datamodel.rule.ActionFieldFunction) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) CompositeFieldConstraint(org.drools.workbench.models.datamodel.rule.CompositeFieldConstraint) SingleFieldConstraint(org.drools.workbench.models.datamodel.rule.SingleFieldConstraint) BaseSingleFieldConstraint(org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint) ActionRetractFact(org.drools.workbench.models.datamodel.rule.ActionRetractFact) ActionUpdateField(org.drools.workbench.models.datamodel.rule.ActionUpdateField) ActionSetField(org.drools.workbench.models.datamodel.rule.ActionSetField) ActionInsertFact(org.drools.workbench.models.datamodel.rule.ActionInsertFact) RuleAttribute(org.drools.workbench.models.datamodel.rule.RuleAttribute) RuleMetadata(org.drools.workbench.models.datamodel.rule.RuleMetadata) ActionGlobalCollectionAdd(org.drools.workbench.models.datamodel.rule.ActionGlobalCollectionAdd) Test(org.junit.Test)

Example 18 with RuleAttribute

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

the class RuleModelDRLPersistenceImpl method marshalAttributes.

/**
 * Marshal model attributes
 * @param buf
 * @param model
 */
protected void marshalAttributes(final StringBuilder buf, final RuleModel model) {
    boolean hasDialect = false;
    for (int i = 0; i < model.attributes.length; i++) {
        RuleAttribute attr = model.attributes[i];
        buf.append("\t");
        buf.append(attr);
        buf.append("\n");
        if (attr.getAttributeName().equals("dialect")) {
            constraintValueBuilder = DRLConstraintValueBuilder.getBuilder(attr.getValue());
            hasDialect = true;
        }
    }
    // Un comment below for mvel
    if (!hasDialect) {
        RuleAttribute attr = new RuleAttribute("dialect", DRLConstraintValueBuilder.DEFAULT_DIALECT);
        buf.append("\t");
        buf.append(attr);
        buf.append("\n");
    }
}
Also used : RuleAttribute(org.drools.workbench.models.datamodel.rule.RuleAttribute) FieldConstraint(org.drools.workbench.models.datamodel.rule.FieldConstraint) ConnectiveConstraint(org.drools.workbench.models.datamodel.rule.ConnectiveConstraint) CompositeFieldConstraint(org.drools.workbench.models.datamodel.rule.CompositeFieldConstraint) SingleFieldConstraint(org.drools.workbench.models.datamodel.rule.SingleFieldConstraint) BaseSingleFieldConstraint(org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint)

Example 19 with RuleAttribute

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

the class RuleTemplateModelXMLPersistenceTest method testBasics.

@Test
public void testBasics() {
    final RuleTemplateModelPersistence p = RuleTemplateModelXMLPersistenceImpl.getInstance();
    final TemplateModel m = new TemplateModel();
    m.addLhsItem(new FactPattern("Person"));
    m.addLhsItem(new FactPattern("Accident"));
    m.addAttribute(new RuleAttribute("no-loop", "true"));
    m.addRhsItem(new ActionInsertFact("Report"));
    ActionGlobalCollectionAdd ag = new ActionGlobalCollectionAdd();
    ag.setFactName("x");
    ag.setGlobalName("g");
    m.addRhsItem(ag);
    m.name = "my rule";
    final String xml = p.marshal(m);
    System.out.println(xml);
    assertTrue(xml.indexOf("Person") > -1);
    assertTrue(xml.indexOf("Accident") > -1);
    assertTrue(xml.indexOf("no-loop") > -1);
    assertTrue(xml.indexOf("org.kie") == -1);
    assertTrue(xml.indexOf("addToGlobal") > -1);
    RuleModel rm_ = RuleTemplateModelXMLPersistenceImpl.getInstance().unmarshal(xml);
    assertEquals(2, rm_.rhs.length);
}
Also used : ActionInsertFact(org.drools.workbench.models.datamodel.rule.ActionInsertFact) RuleAttribute(org.drools.workbench.models.datamodel.rule.RuleAttribute) CompositeFactPattern(org.drools.workbench.models.datamodel.rule.CompositeFactPattern) FactPattern(org.drools.workbench.models.datamodel.rule.FactPattern) TemplateModel(org.drools.workbench.models.guided.template.shared.TemplateModel) ActionGlobalCollectionAdd(org.drools.workbench.models.datamodel.rule.ActionGlobalCollectionAdd) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) Test(org.junit.Test)

Example 20 with RuleAttribute

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

RuleAttribute (org.drools.workbench.models.datamodel.rule.RuleAttribute)33 RuleModel (org.drools.workbench.models.datamodel.rule.RuleModel)25 Test (org.junit.Test)24 CompositeFactPattern (org.drools.workbench.models.datamodel.rule.CompositeFactPattern)14 FactPattern (org.drools.workbench.models.datamodel.rule.FactPattern)14 FromAccumulateCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern)12 FromCollectCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern)12 FromCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern)12 FromEntryPointFactPattern (org.drools.workbench.models.datamodel.rule.FromEntryPointFactPattern)11 SingleFieldConstraint (org.drools.workbench.models.datamodel.rule.SingleFieldConstraint)11 ActionInsertFact (org.drools.workbench.models.datamodel.rule.ActionInsertFact)10 BaseSingleFieldConstraint (org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint)9 ActionFieldValue (org.drools.workbench.models.datamodel.rule.ActionFieldValue)7 ActionGlobalCollectionAdd (org.drools.workbench.models.datamodel.rule.ActionGlobalCollectionAdd)4 CompositeFieldConstraint (org.drools.workbench.models.datamodel.rule.CompositeFieldConstraint)4 FieldConstraint (org.drools.workbench.models.datamodel.rule.FieldConstraint)4 TemplateModel (org.drools.workbench.models.guided.template.shared.TemplateModel)4 ActionCallMethod (org.drools.workbench.models.datamodel.rule.ActionCallMethod)3 ActionFieldFunction (org.drools.workbench.models.datamodel.rule.ActionFieldFunction)3 ActionRetractFact (org.drools.workbench.models.datamodel.rule.ActionRetractFact)3