Search in sources :

Example 6 with ActionRetractFact

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

the class BRLActionColumnTest method setup.

@Before
public void setup() {
    column1 = new BRLActionColumn();
    List<IAction> definition1 = Arrays.asList(new ActionRetractFact("var"));
    column1.setDefinition(definition1);
    List<BRLActionVariableColumn> childColumns1 = Arrays.asList(new BRLActionVariableColumn("var", "FieldType"));
    column1.setChildColumns(childColumns1);
    column1.setHeader("header");
    column1.setHideColumn(false);
    column2 = new BRLActionColumn();
    List<IAction> definition2 = Arrays.asList(new ActionRetractFact("var"));
    column2.setDefinition(definition2);
    List<BRLActionVariableColumn> childColumns2 = Arrays.asList(new BRLActionVariableColumn("var", "FieldType"));
    column2.setChildColumns(childColumns2);
    column2.setHeader("header");
    column2.setHideColumn(false);
}
Also used : ActionRetractFact(org.drools.workbench.models.datamodel.rule.ActionRetractFact) IAction(org.drools.workbench.models.datamodel.rule.IAction) Before(org.junit.Before)

Example 7 with ActionRetractFact

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

the class BRLActionColumnTest method testDiffDefinitions.

@Test
public void testDiffDefinitions() {
    List<IAction> definition1 = Arrays.asList(new ActionRetractFact("var1"));
    column1.setDefinition(definition1);
    List<IAction> definition2 = Arrays.asList(new ActionRetractFact("var2"));
    column2.setDefinition(definition2);
    checkSingleDiff(FIELD_DEFINITION, definition1, definition2, column1, column2);
}
Also used : ActionRetractFact(org.drools.workbench.models.datamodel.rule.ActionRetractFact) IAction(org.drools.workbench.models.datamodel.rule.IAction) Test(org.junit.Test)

Example 8 with ActionRetractFact

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

the class RuleModelCloneVisitor method visitActionRetractFact.

private ActionRetractFact visitActionRetractFact(ActionRetractFact arf) {
    ActionRetractFact clone = new ActionRetractFact();
    clone.setVariableName(arf.getVariableName());
    return clone;
}
Also used : ActionRetractFact(org.drools.workbench.models.datamodel.rule.ActionRetractFact)

Example 9 with ActionRetractFact

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

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

the class GuidedDecisionTablePopulater method addIndirectSourceBuildersColumns.

private void addIndirectSourceBuildersColumns() {
    // Extract BRLVariableColumn and SourceBuilders for respective sections of the rule
    final List<BRLVariableColumn> variableColumns = new ArrayList<>();
    final List<GuidedDecisionTableLHSBuilder> lhsBuilders = new ArrayList<>();
    final List<GuidedDecisionTableRHSBuilder> rhsBuilders = new ArrayList<>();
    for (GuidedDecisionTableSourceBuilder sb : sourceBuilders) {
        if (sb instanceof GuidedDecisionTableSourceBuilderIndirect) {
            for (BRLVariableColumn variableColumn : ((GuidedDecisionTableSourceBuilderIndirect) sb).getVariableColumns()) {
                variableColumns.add(variableColumn);
            }
        }
        if (sb instanceof GuidedDecisionTableLHSBuilder) {
            lhsBuilders.add((GuidedDecisionTableLHSBuilder) sb);
        }
        if (sb instanceof GuidedDecisionTableRHSBuilder) {
            rhsBuilders.add((GuidedDecisionTableRHSBuilder) sb);
        }
    }
    // -- PROCESS LHS PATTERNS
    // For each GuidedDecisionTableLHSBuilder gather IPatterns generated
    // from each accumulative iteration and convert to RuleModel
    int previousPatternCount = 0;
    for (int i = 0; i < lhsBuilders.size(); i++) {
        // Construct DRL for this and previous builders
        final String rule = assembleRuleDRL(i, lhsBuilders, Collections.emptyList());
        final RuleModel rm = RuleModelDRLPersistenceImpl.getInstance().unmarshal(rule, Collections.emptyList(), dmo);
        if (Objects.nonNull(rm.lhs)) {
            final Map<IPattern, Map<InterpolationVariable, Integer>> patternTemplateKeys = new HashMap<>();
            final BRLConditionColumn column = new BRLConditionColumn();
            dtable.getConditions().add(column);
            // Extract Template Keys for Patterns in iteration
            boolean allPatternsHaveTemplateKeys = true;
            for (int iPattern = previousPatternCount; iPattern < rm.lhs.length; iPattern++) {
                final IPattern pattern = rm.lhs[iPattern];
                final Map<InterpolationVariable, Integer> templateKeys = new HashMap<>();
                final RuleModelVisitor rmv = new RuleModelVisitor(templateKeys);
                rmv.visit(pattern);
                patternTemplateKeys.put(pattern, templateKeys);
                if (templateKeys.isEmpty()) {
                    allPatternsHaveTemplateKeys = false;
                }
            }
            // If ALL patterns in the iteration have Template keys we can use IPattern otherwise fallback to FreeFormLine
            if (allPatternsHaveTemplateKeys) {
                for (int iPattern = previousPatternCount; iPattern < rm.lhs.length; iPattern++) {
                    final IPattern pattern = rm.lhs[iPattern];
                    column.getDefinition().add(pattern);
                }
            } else {
                final FreeFormLine ffl = new FreeFormLine();
                ffl.setText(lhsBuilders.get(i).getResult());
                column.setDefinition(Collections.singletonList(ffl));
            }
            // Copy variables to column
            for (int iPattern = previousPatternCount; iPattern < rm.lhs.length; iPattern++) {
                final IPattern pattern = rm.lhs[iPattern];
                final Map<InterpolationVariable, Integer> templateKeys = patternTemplateKeys.get(pattern);
                if (Objects.nonNull(templateKeys)) {
                    final List<InterpolationVariable> ivs = new ArrayList<>(templateKeys.keySet());
                    for (BRLVariableColumn variableColumn : variableColumns) {
                        final Iterator<InterpolationVariable> ivsIts = ivs.iterator();
                        while (ivsIts.hasNext()) {
                            final InterpolationVariable iv = ivsIts.next();
                            if (Objects.equals(iv.getVarName(), variableColumn.getVarName())) {
                                final BRLConditionVariableColumn source = (BRLConditionVariableColumn) variableColumn;
                                final BRLConditionVariableColumn target = makeBRLConditionVariableColumn(source, iv);
                                column.getChildColumns().add(target);
                                ivsIts.remove();
                            }
                        }
                    }
                }
            }
            // Give the column a header
            if (column.getChildColumns().isEmpty()) {
                setZeroParameterConditionColumnHeader(column, variableColumns);
            } else {
                setCompositeColumnHeader(column);
            }
        }
        previousPatternCount = rm.lhs.length;
    }
    // -- PROCESS RHS ACTIONS
    final String rule = assembleRuleDRL(lhsBuilders.size() - 1, lhsBuilders, rhsBuilders);
    final RuleModel rm = RuleModelDRLPersistenceImpl.getInstance().unmarshal(rule, Collections.emptyList(), dmo, true);
    if (rm.rhs != null) {
        for (IAction action : rm.rhs) {
            final BRLActionColumn column = new BRLActionColumn();
            column.getDefinition().add(action);
            dtable.getActionCols().add(column);
            if (action instanceof ActionRetractFact) {
                final ActionRetractFact arf = (ActionRetractFact) action;
                createActionRetractFactChildColumns(arf, column, variableColumns);
            } else {
                createRHSChildColumns(rm, action, column, variableColumns);
            }
            if (column.getChildColumns().isEmpty()) {
                setZeroParameterActionColumnHeader(column, variableColumns);
            } else {
                setCompositeColumnHeader(column);
            }
        }
    }
}
Also used : IPattern(org.drools.workbench.models.datamodel.rule.IPattern) BRLConditionColumn(org.drools.workbench.models.guided.dtable.shared.model.BRLConditionColumn) HashMap(java.util.HashMap) InterpolationVariable(org.drools.workbench.models.datamodel.rule.InterpolationVariable) ArrayList(java.util.ArrayList) GuidedDecisionTableSourceBuilderIndirect(org.drools.workbench.screens.dtablexls.backend.server.conversion.builders.GuidedDecisionTableSourceBuilderIndirect) BRLConditionVariableColumn(org.drools.workbench.models.guided.dtable.shared.model.BRLConditionVariableColumn) ActionRetractFact(org.drools.workbench.models.datamodel.rule.ActionRetractFact) BRLActionColumn(org.drools.workbench.models.guided.dtable.shared.model.BRLActionColumn) RuleModelVisitor(org.drools.workbench.models.datamodel.rule.visitors.RuleModelVisitor) GuidedDecisionTableRHSBuilder(org.drools.workbench.screens.dtablexls.backend.server.conversion.builders.GuidedDecisionTableRHSBuilder) IAction(org.drools.workbench.models.datamodel.rule.IAction) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) FreeFormLine(org.drools.workbench.models.datamodel.rule.FreeFormLine) GuidedDecisionTableSourceBuilder(org.drools.workbench.screens.dtablexls.backend.server.conversion.builders.GuidedDecisionTableSourceBuilder) BRLVariableColumn(org.drools.workbench.models.guided.dtable.shared.model.BRLVariableColumn) GuidedDecisionTableLHSBuilder(org.drools.workbench.screens.dtablexls.backend.server.conversion.builders.GuidedDecisionTableLHSBuilder) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

ActionRetractFact (org.drools.workbench.models.datamodel.rule.ActionRetractFact)17 Test (org.junit.Test)8 IAction (org.drools.workbench.models.datamodel.rule.IAction)7 ActionSetField (org.drools.workbench.models.datamodel.rule.ActionSetField)6 CompositeFactPattern (org.drools.workbench.models.datamodel.rule.CompositeFactPattern)6 FactPattern (org.drools.workbench.models.datamodel.rule.FactPattern)6 RuleModel (org.drools.workbench.models.datamodel.rule.RuleModel)6 ArrayList (java.util.ArrayList)5 ActionInsertFact (org.drools.workbench.models.datamodel.rule.ActionInsertFact)5 ActionUpdateField (org.drools.workbench.models.datamodel.rule.ActionUpdateField)5 SingleFieldConstraint (org.drools.workbench.models.datamodel.rule.SingleFieldConstraint)5 BaseSingleFieldConstraint (org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint)4 FreeFormLine (org.drools.workbench.models.datamodel.rule.FreeFormLine)4 FromCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern)4 IPattern (org.drools.workbench.models.datamodel.rule.IPattern)4 ActionGlobalCollectionAdd (org.drools.workbench.models.datamodel.rule.ActionGlobalCollectionAdd)3 DSLSentence (org.drools.workbench.models.datamodel.rule.DSLSentence)3 FromAccumulateCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern)3 FromCollectCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern)3 RuleAttribute (org.drools.workbench.models.datamodel.rule.RuleAttribute)3