Search in sources :

Example 1 with ExpressionField

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

the class RuleModelDRLPersistenceUnmarshallingTest method testNestedFieldConstraintsOnlyLeafOperator.

@Test
public void testNestedFieldConstraintsOnlyLeafOperator() {
    String drl = "rule \"rule1\"\n" + "when\n" + "ParentType( parentChildField.childField == \"hello\" )\n" + "then\n" + "end";
    addModelField("org.test.ParentType", "parentChildField", "org.test.ChildType", "ChildType");
    addModelField("org.test.ChildType", "childField", "java.lang.String", "String");
    RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
    assertNotNull(m);
    assertEquals(1, m.lhs.length);
    assertTrue(m.lhs[0] instanceof FactPattern);
    assertTrue(((FactPattern) m.lhs[0]).getFieldConstraints()[0] instanceof SingleFieldConstraintEBLeftSide);
    SingleFieldConstraintEBLeftSide ebLeftSide = (SingleFieldConstraintEBLeftSide) ((FactPattern) m.lhs[0]).getFieldConstraints()[0];
    assertEquals("childField", ebLeftSide.getFieldName());
    assertEquals("java.lang.String", ebLeftSide.getFieldType());
    assertEquals("==", ebLeftSide.getOperator());
    assertEquals("hello", ebLeftSide.getValue());
    assertEquals(3, ebLeftSide.getExpressionLeftSide().getParts().size());
    assertTrue(ebLeftSide.getExpressionLeftSide().getParts().get(0) instanceof ExpressionUnboundFact);
    ExpressionUnboundFact expressionUnboundFact = ((ExpressionUnboundFact) ebLeftSide.getExpressionLeftSide().getParts().get(0));
    assertEquals("ParentType", expressionUnboundFact.getName());
    assertEquals("ParentType", expressionUnboundFact.getClassType());
    assertEquals("ParentType", expressionUnboundFact.getGenericType());
    assertEquals(((FactPattern) m.lhs[0]).getFactType(), expressionUnboundFact.getFactType());
    assertNull(expressionUnboundFact.getPrevious());
    assertEquals(ebLeftSide.getExpressionLeftSide().getParts().get(1), expressionUnboundFact.getNext());
    assertTrue(ebLeftSide.getExpressionLeftSide().getParts().get(1) instanceof ExpressionField);
    ExpressionField expressionField1 = (ExpressionField) ebLeftSide.getExpressionLeftSide().getParts().get(1);
    assertEquals("parentChildField", expressionField1.getName());
    assertEquals("org.test.ChildType", expressionField1.getClassType());
    assertEquals("ChildType", expressionField1.getGenericType());
    assertEquals(ebLeftSide.getExpressionLeftSide().getParts().get(0), expressionField1.getPrevious());
    assertEquals(ebLeftSide.getExpressionLeftSide().getParts().get(2), expressionField1.getNext());
    assertTrue(ebLeftSide.getExpressionLeftSide().getParts().get(2) instanceof ExpressionField);
    ExpressionField expressionField2 = (ExpressionField) ebLeftSide.getExpressionLeftSide().getParts().get(2);
    assertEquals("childField", expressionField2.getName());
    assertEquals("java.lang.String", expressionField2.getClassType());
    assertEquals("String", expressionField2.getGenericType());
    assertEquals(ebLeftSide.getExpressionLeftSide().getParts().get(1), expressionField2.getPrevious());
    assertNull(expressionField2.getNext());
}
Also used : SingleFieldConstraintEBLeftSide(org.drools.workbench.models.datamodel.rule.SingleFieldConstraintEBLeftSide) 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) ExpressionField(org.drools.workbench.models.datamodel.rule.ExpressionField) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) ExpressionUnboundFact(org.drools.workbench.models.datamodel.rule.ExpressionUnboundFact) Test(org.junit.Test)

Example 2 with ExpressionField

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

the class RuleModelTest method testAllVariableBindings2.

@Test
public void testAllVariableBindings2() {
    final RuleModel model = new RuleModel();
    model.lhs = new IPattern[1];
    final FactPattern fp = new FactPattern("Car");
    model.lhs[0] = fp;
    fp.setBoundName("$c");
    SingleFieldConstraint sfc = new SingleFieldConstraintEBLeftSide("make");
    sfc.getExpressionValue().appendPart(new ExpressionField("make", "java.lang.String", "String"));
    sfc.setFieldBinding("$m");
    fp.addConstraint(sfc);
    List<String> vars = model.getAllVariables();
    assertEquals(2, vars.size());
    assertEquals("$c", vars.get(0));
    assertEquals("$m", vars.get(1));
}
Also used : SingleFieldConstraint(org.drools.workbench.models.datamodel.rule.SingleFieldConstraint) SingleFieldConstraintEBLeftSide(org.drools.workbench.models.datamodel.rule.SingleFieldConstraintEBLeftSide) FactPattern(org.drools.workbench.models.datamodel.rule.FactPattern) CompositeFactPattern(org.drools.workbench.models.datamodel.rule.CompositeFactPattern) FromCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern) ExpressionField(org.drools.workbench.models.datamodel.rule.ExpressionField) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) Test(org.junit.Test)

Example 3 with ExpressionField

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

the class RuleModelDRLPersistenceUnmarshallingTest method testFromBoundVariable.

@Test
public void testFromBoundVariable() {
    String drl = "import java.lang.Number;\n" + "import org.drools.workbench.models.commons.backend.rule.Counter;\n" + "rule \"rule1\"\n" + "when\n" + "cc : Counter()\n" + "Number() from cc.number\n" + "then\n" + "end";
    addModelField("org.drools.workbench.models.commons.backend.rule.Counter", "number", "java.lang.Number", DataType.TYPE_NUMERIC);
    RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
    assertNotNull(m);
    assertEquals("rule1", m.name);
    assertEquals(2, m.lhs.length);
    IPattern p0 = m.lhs[0];
    assertTrue(p0 instanceof FactPattern);
    FactPattern fp0 = (FactPattern) p0;
    assertEquals("Counter", fp0.getFactType());
    assertEquals("cc", fp0.getBoundName());
    assertEquals(0, fp0.getNumberOfConstraints());
    IPattern p1 = m.lhs[1];
    assertTrue(p1 instanceof FromCompositeFactPattern);
    FromCompositeFactPattern fcfp1 = (FromCompositeFactPattern) p1;
    FactPattern fp1 = fcfp1.getFactPattern();
    ExpressionFormLine efl1 = fcfp1.getExpression();
    assertNotNull(fp1);
    assertNotNull(efl1);
    assertEquals("Number", fp1.getFactType());
    assertEquals(0, fp1.getNumberOfConstraints());
    assertEquals(2, efl1.getParts().size());
    assertTrue(efl1.getParts().get(0) instanceof ExpressionVariable);
    assertTrue(efl1.getParts().get(1) instanceof ExpressionField);
    ExpressionVariable eflv1 = (ExpressionVariable) efl1.getParts().get(0);
    assertEquals("cc", eflv1.getName());
    assertEquals("Counter", eflv1.getClassType());
    assertEquals(DataType.TYPE_NUMERIC, eflv1.getGenericType());
    ExpressionField eflf1 = (ExpressionField) efl1.getParts().get(1);
    assertEquals("number", eflf1.getName());
    assertEquals("java.lang.Number", eflf1.getClassType());
    assertEquals(DataType.TYPE_NUMERIC, eflf1.getGenericType());
}
Also used : IPattern(org.drools.workbench.models.datamodel.rule.IPattern) ExpressionFormLine(org.drools.workbench.models.datamodel.rule.ExpressionFormLine) FromCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern) 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) ExpressionVariable(org.drools.workbench.models.datamodel.rule.ExpressionVariable) ExpressionField(org.drools.workbench.models.datamodel.rule.ExpressionField) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) Test(org.junit.Test)

Example 4 with ExpressionField

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

the class RuleModelDRLPersistenceUnmarshallingTest method testNestedFieldExpressionsWithAnotherExpression.

@Test
public void testNestedFieldExpressionsWithAnotherExpression() {
    String drl = "rule rule1\n" + "when\n" + "p : Person( address.postalCode == p.address.postalCode )\n" + "then\n" + "end";
    addModelField("org.test.Person", "address", "org.test.Address", "Address");
    addModelField("org.test.Person", "this", "org.test.Person", DataType.TYPE_THIS);
    addModelField("org.test.Address", "postalCode", "java.lang.Integer", "Integer");
    RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
    assertNotNull(m);
    assertEquals(1, m.lhs.length);
    assertTrue(m.lhs[0] instanceof FactPattern);
    assertTrue(((FactPattern) m.lhs[0]).getFieldConstraints()[0] instanceof SingleFieldConstraintEBLeftSide);
    SingleFieldConstraintEBLeftSide ebLeftSide = (SingleFieldConstraintEBLeftSide) ((FactPattern) m.lhs[0]).getFieldConstraints()[0];
    assertEquals("postalCode", ebLeftSide.getFieldName());
    assertEquals("java.lang.Integer", ebLeftSide.getFieldType());
    assertEquals("==", ebLeftSide.getOperator());
    assertEquals("", ebLeftSide.getValue());
    assertEquals(3, ebLeftSide.getExpressionValue().getParts().size());
    assertTrue(ebLeftSide.getExpressionValue().getParts().get(0) instanceof ExpressionVariable);
    ExpressionVariable expressionVariable = (ExpressionVariable) ebLeftSide.getExpressionValue().getParts().get(0);
    assertEquals("p", expressionVariable.getName());
    assertEquals("org.test.Person", expressionVariable.getClassType());
    assertEquals(DataType.TYPE_THIS, expressionVariable.getGenericType());
    assertTrue(ebLeftSide.getExpressionValue().getParts().get(1) instanceof ExpressionField);
    ExpressionField ef1 = (ExpressionField) ebLeftSide.getExpressionValue().getParts().get(1);
    assertEquals("address", ef1.getName());
    assertEquals("org.test.Address", ef1.getClassType());
    assertEquals("Address", ef1.getGenericType());
    assertTrue(ebLeftSide.getExpressionValue().getParts().get(2) instanceof ExpressionField);
    ExpressionField ef2 = (ExpressionField) ebLeftSide.getExpressionValue().getParts().get(2);
    assertEquals("postalCode", ef2.getName());
    assertEquals("java.lang.Integer", ef2.getClassType());
    assertEquals(DataType.TYPE_NUMERIC_INTEGER, ef2.getGenericType());
    assertEquals(3, ebLeftSide.getExpressionLeftSide().getParts().size());
    assertTrue(ebLeftSide.getExpressionLeftSide().getParts().get(0) instanceof ExpressionUnboundFact);
    ExpressionUnboundFact expressionUnboundFact = ((ExpressionUnboundFact) ebLeftSide.getExpressionLeftSide().getParts().get(0));
    assertEquals("Person", expressionUnboundFact.getName());
    assertEquals("Person", expressionUnboundFact.getClassType());
    assertEquals("Person", expressionUnboundFact.getGenericType());
    assertEquals(((FactPattern) m.lhs[0]).getFactType(), expressionUnboundFact.getFactType());
    assertNull(expressionUnboundFact.getPrevious());
    assertEquals(ebLeftSide.getExpressionLeftSide().getParts().get(1), expressionUnboundFact.getNext());
    assertTrue(ebLeftSide.getExpressionLeftSide().getParts().get(1) instanceof ExpressionField);
    ExpressionField expressionField1 = (ExpressionField) ebLeftSide.getExpressionLeftSide().getParts().get(1);
    assertEquals("address", expressionField1.getName());
    assertEquals("org.test.Address", expressionField1.getClassType());
    assertEquals("Address", expressionField1.getGenericType());
    assertEquals(ebLeftSide.getExpressionLeftSide().getParts().get(0), expressionField1.getPrevious());
    assertEquals(ebLeftSide.getExpressionLeftSide().getParts().get(2), expressionField1.getNext());
    assertTrue(ebLeftSide.getExpressionLeftSide().getParts().get(2) instanceof ExpressionField);
    ExpressionField expressionField2 = (ExpressionField) ebLeftSide.getExpressionLeftSide().getParts().get(2);
    assertEquals("postalCode", expressionField2.getName());
    assertEquals("java.lang.Integer", expressionField2.getClassType());
    assertEquals(DataType.TYPE_NUMERIC_INTEGER, expressionField2.getGenericType());
    assertEquals(ebLeftSide.getExpressionLeftSide().getParts().get(1), expressionField2.getPrevious());
    assertNull(expressionField2.getNext());
}
Also used : SingleFieldConstraintEBLeftSide(org.drools.workbench.models.datamodel.rule.SingleFieldConstraintEBLeftSide) 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) ExpressionVariable(org.drools.workbench.models.datamodel.rule.ExpressionVariable) ExpressionField(org.drools.workbench.models.datamodel.rule.ExpressionField) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) ExpressionUnboundFact(org.drools.workbench.models.datamodel.rule.ExpressionUnboundFact) Test(org.junit.Test)

Example 5 with ExpressionField

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

the class RuleModelDRLPersistenceImpl method parsePatternSource.

private IFactPattern parsePatternSource(final RuleModel m, final PatternDescr pattern, final PatternSourceDescr patternSource, final boolean isJavaDialect, final Map<String, String> boundParams, final PackageDataModelOracle dmo) {
    if (pattern.getIdentifier() != null) {
        boundParams.put(pattern.getIdentifier(), pattern.getObjectType());
    }
    if (patternSource instanceof AccumulateDescr) {
        AccumulateDescr accumulate = (AccumulateDescr) patternSource;
        FromAccumulateCompositeFactPattern fac = new FromAccumulateCompositeFactPattern();
        fac.setSourcePattern(parseBaseDescr(m, accumulate.getInput(), isJavaDialect, boundParams, dmo));
        fac.setInitCode(accumulate.getInitCode());
        fac.setActionCode(accumulate.getActionCode());
        fac.setReverseCode(accumulate.getReverseCode());
        fac.setResultCode(accumulate.getResultCode());
        FactPattern factPattern = new FactPattern(pattern.getObjectType());
        factPattern.setBoundName(pattern.getIdentifier());
        parseConstraint(m, factPattern, pattern.getConstraint(), isJavaDialect, boundParams, dmo);
        fac.setFactPattern(factPattern);
        for (AccumulateDescr.AccumulateFunctionCallDescr func : accumulate.getFunctions()) {
            String funcName = func.getFunction();
            boolean first = true;
            StringBuilder sb = new StringBuilder();
            for (String param : func.getParams()) {
                if (first) {
                    first = false;
                } else {
                    sb.append(", ");
                }
                sb.append(param);
            }
            fac.setFunction(funcName + "(" + sb + ")");
            break;
        }
        return fac;
    } else if (patternSource instanceof CollectDescr) {
        CollectDescr collect = (CollectDescr) patternSource;
        FromCollectCompositeFactPattern fac = new FromCollectCompositeFactPattern();
        fac.setRightPattern(parseBaseDescr(m, collect.getInputPattern(), isJavaDialect, boundParams, dmo));
        fac.setFactPattern(getFactPattern(m, pattern, isJavaDialect, boundParams, dmo));
        return fac;
    } else if (patternSource instanceof EntryPointDescr) {
        EntryPointDescr entryPoint = (EntryPointDescr) patternSource;
        FromEntryPointFactPattern fep = new FromEntryPointFactPattern();
        fep.setEntryPointName(entryPoint.getText());
        fep.setFactPattern(getFactPattern(m, pattern, isJavaDialect, boundParams, dmo));
        return fep;
    } else if (patternSource instanceof FromDescr) {
        FromDescr from = (FromDescr) patternSource;
        FromCompositeFactPattern fcfp = new FromCompositeFactPattern();
        FactPattern factPattern = new FactPattern(pattern.getObjectType());
        factPattern.setBoundName(pattern.getIdentifier());
        parseConstraint(m, factPattern, pattern.getConstraint(), isJavaDialect, boundParams, dmo);
        fcfp.setFactPattern(factPattern);
        ExpressionFormLine expression = new ExpressionFormLine();
        fcfp.setExpression(expression);
        String dataSource = from.getDataSource().toString();
        String[] splitSource = dataSource.split("\\.");
        ModelField[] fields = null;
        for (int i = 0; i < splitSource.length; i++) {
            String sourcePart = splitSource[i];
            if (i == 0) {
                String type = boundParams.get(sourcePart);
                expression.appendPart(new ExpressionVariable(sourcePart, type, DataType.TYPE_NUMERIC));
                fields = findFields(m, dmo, type);
            } else {
                ModelField modelField = null;
                if (fields != null) {
                    for (ModelField field : fields) {
                        if (field.getName().equals(sourcePart)) {
                            modelField = field;
                            break;
                        }
                    }
                }
                if (modelField == null) {
                    final String previousClassName = expression.getClassType();
                    final List<MethodInfo> mis = dmo.getModuleMethodInformation().get(previousClassName);
                    boolean isMethod = false;
                    if (mis != null) {
                        for (MethodInfo mi : mis) {
                            if (mi.getName().equals(sourcePart)) {
                                expression.appendPart(new ExpressionMethod(mi.getName(), mi.getReturnClassType(), mi.getGenericType(), mi.getParametricReturnType()));
                                isMethod = true;
                                break;
                            }
                        }
                    }
                    if (isMethod == false) {
                        expression.appendPart(new ExpressionText(sourcePart));
                    }
                } else {
                    expression.appendPart(new ExpressionField(sourcePart, modelField.getClassName(), modelField.getType()));
                    fields = findFields(m, dmo, modelField.getClassName());
                }
            }
        }
        return fcfp;
    }
    throw new RuntimeException("Unknown pattern source " + patternSource);
}
Also used : FromEntryPointFactPattern(org.drools.workbench.models.datamodel.rule.FromEntryPointFactPattern) EntryPointDescr(org.drools.compiler.lang.descr.EntryPointDescr) FromEntryPointFactPattern(org.drools.workbench.models.datamodel.rule.FromEntryPointFactPattern) CompositeFactPattern(org.drools.workbench.models.datamodel.rule.CompositeFactPattern) FromCollectCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern) FromAccumulateCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern) FromCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern) IFactPattern(org.drools.workbench.models.datamodel.rule.IFactPattern) FactPattern(org.drools.workbench.models.datamodel.rule.FactPattern) FromDescr(org.drools.compiler.lang.descr.FromDescr) ExpressionText(org.drools.workbench.models.datamodel.rule.ExpressionText) FromCollectCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern) ExpressionField(org.drools.workbench.models.datamodel.rule.ExpressionField) AccumulateDescr(org.drools.compiler.lang.descr.AccumulateDescr) ExpressionFormLine(org.drools.workbench.models.datamodel.rule.ExpressionFormLine) ExpressionMethod(org.drools.workbench.models.datamodel.rule.ExpressionMethod) FromCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern) ModelField(org.kie.soup.project.datamodel.oracle.ModelField) CollectDescr(org.drools.compiler.lang.descr.CollectDescr) FromAccumulateCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern) ExpressionVariable(org.drools.workbench.models.datamodel.rule.ExpressionVariable) ArrayList(java.util.ArrayList) StringUtils.splitArgumentsList(org.drools.core.util.StringUtils.splitArgumentsList) List(java.util.List) ActionFieldList(org.drools.workbench.models.datamodel.rule.ActionFieldList) MethodInfo(org.kie.soup.project.datamodel.oracle.MethodInfo) RuleModelPersistenceHelper.findMethodInfo(org.drools.workbench.models.commons.backend.rule.RuleModelPersistenceHelper.findMethodInfo)

Aggregations

ExpressionField (org.drools.workbench.models.datamodel.rule.ExpressionField)26 CompositeFactPattern (org.drools.workbench.models.datamodel.rule.CompositeFactPattern)24 FactPattern (org.drools.workbench.models.datamodel.rule.FactPattern)24 FromCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern)24 Test (org.junit.Test)24 FromAccumulateCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern)23 FromCollectCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern)23 RuleModel (org.drools.workbench.models.datamodel.rule.RuleModel)23 ExpressionUnboundFact (org.drools.workbench.models.datamodel.rule.ExpressionUnboundFact)21 SingleFieldConstraintEBLeftSide (org.drools.workbench.models.datamodel.rule.SingleFieldConstraintEBLeftSide)21 FromEntryPointFactPattern (org.drools.workbench.models.datamodel.rule.FromEntryPointFactPattern)11 ExpressionMethod (org.drools.workbench.models.datamodel.rule.ExpressionMethod)5 ExpressionVariable (org.drools.workbench.models.datamodel.rule.ExpressionVariable)5 ExpressionFormLine (org.drools.workbench.models.datamodel.rule.ExpressionFormLine)4 IPattern (org.drools.workbench.models.datamodel.rule.IPattern)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 ExpressionText (org.drools.workbench.models.datamodel.rule.ExpressionText)2 SingleFieldConstraint (org.drools.workbench.models.datamodel.rule.SingleFieldConstraint)2 MethodInfo (org.kie.soup.project.datamodel.oracle.MethodInfo)2