Search in sources :

Example 56 with IPattern

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

the class GuidedDecisionTreeModelUnmarshallingVisitor method visit.

private GuidedDecisionTree visit(final String drl, final String baseFileName, final List<String> globals, final PackageDataModelOracle dmo) {
    PortablePreconditions.checkNotNull("drl", drl);
    PortablePreconditions.checkNotNull("baseFileName", baseFileName);
    PortablePreconditions.checkNotNull("globals", globals);
    PortablePreconditions.checkNotNull("dmo", dmo);
    final GuidedDecisionTree model = new GuidedDecisionTree();
    model.setTreeName(baseFileName);
    // De-serialize Package name
    final String packageName = PackageNameParser.parsePackageName(drl);
    model.setPackageName(packageName);
    // De-serialize imports
    final Imports imports = ImportsParser.parseImports(drl);
    for (Import item : imports.getImports()) {
        model.getImports().addImport(item);
    }
    // Split DRL into separate rules
    final List<String> rules = new ArrayList<String>();
    final Pattern pattern = Pattern.compile("\\s?rule\\s(.+?)\\send\\s?", Pattern.DOTALL);
    final Matcher matcher = pattern.matcher(drl);
    while (matcher.find()) {
        rules.add(matcher.group());
    }
    // Build a linear Path of Nodes for each rule
    final List<GuidedDecisionTreeParserExtendedError> rulesParserContent = new ArrayList<GuidedDecisionTreeParserExtendedError>();
    for (String rule : rules) {
        final GuidedDecisionTreeParserExtendedError ruleParserContent = new GuidedDecisionTreeParserExtendedError();
        rulesParserContent.add(ruleParserContent);
        try {
            final RuleModel rm = RuleModelDRLPersistenceImpl.getInstance().unmarshal(rule, globals, dmo);
            ruleParserContent.setOriginalRuleName(rm.name);
            ruleParserContent.setOriginalDrl(rule);
            for (IPattern p : rm.lhs) {
                ruleParserContent.getNodes().addAll(visit(p, model, dmo, ruleParserContent.getMessages()));
            }
            for (IAction a : rm.rhs) {
                ruleParserContent.getNodes().addAll(visit(a, getTypesOnPath(ruleParserContent.getNodes()), model, dmo, ruleParserContent.getMessages()));
            }
        } catch (Exception e) {
            ruleParserContent.getMessages().add(new DefaultParserMessage(e.getMessage()));
        }
    }
    // Combine Paths into a single tree.
    for (GuidedDecisionTreeParserExtendedError ruleParserContent : rulesParserContent) {
        Node activeModelNode = null;
        boolean error = !ruleParserContent.getMessages().isEmpty();
        for (int index = 0; !error && index < ruleParserContent.getNodes().size(); index++) {
            final Node node = ruleParserContent.getNodes().get(index);
            switch(index) {
                case 0:
                    if (!(node instanceof TypeNode)) {
                        ruleParserContent.getMessages().add(new InvalidRootParserMessage());
                        error = true;
                        break;
                    }
                    final TypeNode root = (TypeNode) node;
                    if (model.getRoot() == null) {
                        model.setRoot(root);
                    } else if (!root.equals(model.getRoot())) {
                        ruleParserContent.getMessages().add(new AmbiguousRootParserMessage(root.getClassName()));
                        error = true;
                        break;
                    }
                    activeModelNode = model.getRoot();
                    break;
                default:
                    if (!activeModelNode.getChildren().contains(node)) {
                        // If the active node in the Model doesn't contain the child add it as a new child
                        activeModelNode.addChild(node);
                        activeModelNode = node;
                    } else {
                        // Otherwise swap out the Node in the Path for the existing one in the Model
                        activeModelNode = activeModelNode.getChildren().get(activeModelNode.getChildren().indexOf(node));
                    }
            }
        }
        if (!ruleParserContent.getMessages().isEmpty()) {
            model.getParserErrors().add(new GuidedDecisionTreeParserError(ruleParserContent.getOriginalRuleName(), ruleParserContent.getOriginalDrl(), ruleParserContent.getMessages()));
        }
    }
    return model;
}
Also used : IPattern(org.drools.workbench.models.datamodel.rule.IPattern) Pattern(java.util.regex.Pattern) FactPattern(org.drools.workbench.models.datamodel.rule.FactPattern) IPattern(org.drools.workbench.models.datamodel.rule.IPattern) Import(org.kie.soup.project.datamodel.imports.Import) IAction(org.drools.workbench.models.datamodel.rule.IAction) Matcher(java.util.regex.Matcher) InvalidRootParserMessage(org.drools.workbench.models.guided.dtree.shared.model.parser.messages.InvalidRootParserMessage) GuidedDecisionTreeParserError(org.drools.workbench.models.guided.dtree.shared.model.parser.GuidedDecisionTreeParserError) 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) GuidedDecisionTree(org.drools.workbench.models.guided.dtree.shared.model.GuidedDecisionTree) DefaultParserMessage(org.drools.workbench.models.guided.dtree.shared.model.parser.messages.DefaultParserMessage) Imports(org.kie.soup.project.datamodel.imports.Imports) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) CompositeFieldConstraint(org.drools.workbench.models.datamodel.rule.CompositeFieldConstraint) FieldConstraint(org.drools.workbench.models.datamodel.rule.FieldConstraint) SingleFieldConstraint(org.drools.workbench.models.datamodel.rule.SingleFieldConstraint) BaseSingleFieldConstraint(org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint) TypeNode(org.drools.workbench.models.guided.dtree.shared.model.nodes.TypeNode) AmbiguousRootParserMessage(org.drools.workbench.models.guided.dtree.shared.model.parser.messages.AmbiguousRootParserMessage)

Example 57 with IPattern

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

the class BRLConditionColumnTest method testDiffAll.

@Test
public void testDiffAll() {
    List<IPattern> definition1 = Arrays.asList(new FactPattern("Mock1"));
    column1.setDefinition(definition1);
    List<BRLConditionVariableColumn> childColumns1 = Arrays.asList(new BRLConditionVariableColumn("var1", "FieldType1"));
    column1.setChildColumns(childColumns1);
    column1.setHeader("header1");
    column1.setHideColumn(false);
    List<IPattern> definition2 = Arrays.asList(new FactPattern("Mock2"));
    column2.setDefinition(definition2);
    List<BRLConditionVariableColumn> childColumns2 = Arrays.asList(new BRLConditionVariableColumn("var2", "FieldType2"));
    column2.setChildColumns(childColumns2);
    column2.setHeader("header2");
    column2.setHideColumn(true);
    List<BaseColumnFieldDiff> diff = column1.diff(column2);
    assertNotNull(diff);
    assertEquals(FIELD_HIDE_COLUMN, diff.get(0).getFieldName());
    assertEquals(false, diff.get(0).getOldValue());
    assertEquals(true, diff.get(0).getValue());
    assertEquals(FIELD_HEADER, diff.get(1).getFieldName());
    assertEquals("header1", diff.get(1).getOldValue());
    assertEquals("header2", diff.get(1).getValue());
    assertEquals(FIELD_DEFINITION, diff.get(2).getFieldName());
    assertEquals(definition1, diff.get(2).getOldValue());
    assertEquals(definition2, diff.get(2).getValue());
    assertEquals(FIELD_FIELD_TYPE, diff.get(3).getFieldName());
    assertEquals("FieldType1", diff.get(3).getOldValue());
    assertEquals("FieldType2", diff.get(3).getValue());
    assertEquals(FIELD_VAR_NAME, diff.get(4).getFieldName());
    assertEquals("var1", diff.get(4).getOldValue());
    assertEquals("var2", diff.get(4).getValue());
}
Also used : IPattern(org.drools.workbench.models.datamodel.rule.IPattern) FactPattern(org.drools.workbench.models.datamodel.rule.FactPattern) Test(org.junit.Test)

Example 58 with IPattern

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

the class BRLConditionColumnTest method setup.

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

Example 59 with IPattern

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

the class RuleModelDRLPersistenceUnmarshallingTest method testRHSInsertFactWithFieldAsLiteralSamePackage.

@Test
public void testRHSInsertFactWithFieldAsLiteralSamePackage() {
    String drl = "package org.test\n" + "rule \"variable\"\n" + "dialect \"mvel\"\n" + "when\n" + "Person( field1 == 44 )\n" + "then\n" + "Person fact0 = new Person();\n" + "fact0.setField1( 55 );\n" + "insert( fact0 );\n" + "end";
    addModelField("org.test.Person", "field1", "java.lang.Integer", DataType.TYPE_NUMERIC_INTEGER);
    when(dmo.getPackageName()).thenReturn("org.test");
    final RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
    assertNotNull(m);
    assertEquals(1, m.lhs.length);
    IPattern p = m.lhs[0];
    assertTrue(p instanceof FactPattern);
    FactPattern fp = (FactPattern) p;
    assertEquals("Person", fp.getFactType());
    assertEquals(1, fp.getConstraintList().getConstraints().length);
    assertTrue(fp.getConstraint(0) instanceof SingleFieldConstraint);
    SingleFieldConstraint sfp = (SingleFieldConstraint) fp.getConstraint(0);
    assertEquals("Person", sfp.getFactType());
    assertEquals("field1", sfp.getFieldName());
    assertEquals("==", sfp.getOperator());
    assertEquals("44", sfp.getValue());
    assertEquals(BaseSingleFieldConstraint.TYPE_LITERAL, sfp.getConstraintValueType());
    assertEquals(1, m.rhs.length);
    IAction a = m.rhs[0];
    assertTrue(a instanceof ActionInsertFact);
    ActionInsertFact ap = (ActionInsertFact) a;
    assertEquals("Person", ap.getFactType());
    assertEquals("fact0", ap.getBoundName());
    assertEquals(1, ap.getFieldValues().length);
    ActionFieldValue afv = ap.getFieldValues()[0];
    assertEquals("field1", afv.getField());
    assertEquals(FieldNatureType.TYPE_LITERAL, afv.getNature());
    assertEquals("55", afv.getValue());
    assertEquals(DataType.TYPE_NUMERIC_INTEGER, afv.getType());
}
Also used : IPattern(org.drools.workbench.models.datamodel.rule.IPattern) SingleFieldConstraint(org.drools.workbench.models.datamodel.rule.SingleFieldConstraint) BaseSingleFieldConstraint(org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint) IAction(org.drools.workbench.models.datamodel.rule.IAction) ActionFieldValue(org.drools.workbench.models.datamodel.rule.ActionFieldValue) ActionInsertFact(org.drools.workbench.models.datamodel.rule.ActionInsertFact) 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)

Example 60 with IPattern

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

the class RuleModelDRLPersistenceUnmarshallingTest method newRHSFactsCanBeUsedInRHSBinding.

@Test
public void newRHSFactsCanBeUsedInRHSBinding() throws Exception {
    String drl = "package org.mortgages;\n" + "rule \"r1\"\n" + "dialect \"mvel\"\n" + "when\n" + "$l : LoanApplication()\n" + "then\n" + "Applicant $a = new Applicant();\n" + "insert( $a );\n" + "modify( $l ) {\n" + "  setApplicant( $a )" + "}\n" + "end";
    addModelField("org.mortgages.Applicant", "this", "org.mortgages.Applicant", DataType.TYPE_THIS);
    addModelField("org.mortgages.LoanApplication", "this", "org.mortgages.LoanApplication", DataType.TYPE_THIS);
    addModelField("org.mortgages.LoanApplication", "applicant", "org.mortgages.Applicant", "org.mortgages.Applicant");
    when(dmo.getPackageName()).thenReturn("org.mortgages");
    RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
    assertNotNull(m);
    assertEquals("r1", m.name);
    // LHS Pattern
    assertEquals(1, m.lhs.length);
    IPattern p = m.lhs[0];
    assertTrue(p instanceof FactPattern);
    FactPattern fp = (FactPattern) p;
    assertEquals("LoanApplication", fp.getFactType());
    assertEquals(0, fp.getNumberOfConstraints());
    assertEquals(2, m.rhs.length);
    assertTrue(m.rhs[0] instanceof ActionInsertFact);
    ActionInsertFact aif = (ActionInsertFact) m.rhs[0];
    assertEquals("$a", aif.getBoundName());
    assertEquals("Applicant", aif.getFactType());
    assertEquals(0, aif.getFieldValues().length);
    assertTrue(m.rhs[1] instanceof ActionUpdateField);
    ActionUpdateField auf = (ActionUpdateField) m.rhs[1];
    assertEquals("$l", auf.getVariable());
    assertEquals(1, auf.getFieldValues().length);
    ActionFieldValue afv0 = auf.getFieldValues()[0];
    assertEquals("applicant", afv0.getField());
    assertEquals("=$a", afv0.getValue());
    assertEquals(FieldNatureType.TYPE_VARIABLE, afv0.getNature());
    assertEquals("Applicant", afv0.getType());
}
Also used : IPattern(org.drools.workbench.models.datamodel.rule.IPattern) ActionUpdateField(org.drools.workbench.models.datamodel.rule.ActionUpdateField) ActionFieldValue(org.drools.workbench.models.datamodel.rule.ActionFieldValue) ActionInsertFact(org.drools.workbench.models.datamodel.rule.ActionInsertFact) 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

IPattern (org.drools.workbench.models.datamodel.rule.IPattern)118 FactPattern (org.drools.workbench.models.datamodel.rule.FactPattern)95 Test (org.junit.Test)92 RuleModel (org.drools.workbench.models.datamodel.rule.RuleModel)84 CompositeFactPattern (org.drools.workbench.models.datamodel.rule.CompositeFactPattern)83 FromAccumulateCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern)75 FromCollectCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern)75 FromCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern)75 SingleFieldConstraint (org.drools.workbench.models.datamodel.rule.SingleFieldConstraint)71 BaseSingleFieldConstraint (org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint)58 IAction (org.drools.workbench.models.datamodel.rule.IAction)34 ActionFieldValue (org.drools.workbench.models.datamodel.rule.ActionFieldValue)30 ArrayList (java.util.ArrayList)23 BRLConditionColumn (org.drools.workbench.models.guided.dtable.shared.model.BRLConditionColumn)23 BRLConditionVariableColumn (org.drools.workbench.models.guided.dtable.shared.model.BRLConditionVariableColumn)22 FreeFormLine (org.drools.workbench.models.datamodel.rule.FreeFormLine)20 ActionUpdateField (org.drools.workbench.models.datamodel.rule.ActionUpdateField)16 DescriptionCol52 (org.drools.workbench.models.guided.dtable.shared.model.DescriptionCol52)16 GuidedDecisionTable52 (org.drools.workbench.models.guided.dtable.shared.model.GuidedDecisionTable52)16 RowNumberCol52 (org.drools.workbench.models.guided.dtable.shared.model.RowNumberCol52)16