Search in sources :

Example 6 with MethodInfo

use of org.kie.soup.project.datamodel.oracle.MethodInfo in project drools by kiegroup.

the class RuleModelDRLPersistenceUnmarshallingTest method testMethodCallCheckParameterDataTypes1.

@Test
public void testMethodCallCheckParameterDataTypes1() {
    // BZ-1045423
    String drl = "" + "package org.mortgages;\n" + "import org.mortgages.LoanApplication;\n" + "import java.util.Map;\n" + "rule \"my rule\"\n" + "  dialect \"mvel\"\n" + "  when\n" + "    a : LoanApplication( )\n" + "    m : Map()\n" + "  then\n" + "    m.put(\"key\", a );\n" + "end\n";
    Map<String, List<MethodInfo>> methodInformation = new HashMap<>();
    List<MethodInfo> mapMethodInformation = new ArrayList<>();
    mapMethodInformation.add(new MethodInfo("put", Arrays.asList("java.lang.Object", "java.lang.Object"), "void", "void", "java.util.Map"));
    methodInformation.put("java.util.Map", mapMethodInformation);
    when(dmo.getModuleMethodInformation()).thenReturn(methodInformation);
    RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
    assertTrue(m.rhs[0] instanceof ActionCallMethod);
    ActionCallMethod mc = (ActionCallMethod) m.rhs[0];
    assertEquals("put", mc.getMethodName());
    assertEquals("m", mc.getVariable());
    assertEquals(1, mc.getState());
    assertEquals(2, mc.getFieldValues().length);
    ActionFieldValue f1 = mc.getFieldValue(0);
    assertEquals("\"key\"", f1.getValue());
    assertEquals("java.lang.Object", f1.getType());
    assertEquals(FieldNatureType.TYPE_LITERAL, f1.getNature());
    ActionFieldValue f2 = mc.getFieldValue(1);
    assertEquals("a", f2.getValue());
    assertEquals("java.lang.Object", f2.getType());
    assertEquals(FieldNatureType.TYPE_VARIABLE, f2.getNature());
    String marshalled = RuleModelDRLPersistenceImpl.getInstance().marshal(m);
    logger.debug(marshalled);
    assertEqualsIgnoreWhitespace(drl, marshalled);
}
Also used : HashMap(java.util.HashMap) ActionFieldValue(org.drools.workbench.models.datamodel.rule.ActionFieldValue) ArrayList(java.util.ArrayList) ActionCallMethod(org.drools.workbench.models.datamodel.rule.ActionCallMethod) List(java.util.List) ArrayList(java.util.ArrayList) MethodInfo(org.kie.soup.project.datamodel.oracle.MethodInfo) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) Test(org.junit.Test)

Example 7 with MethodInfo

use of org.kie.soup.project.datamodel.oracle.MethodInfo in project drools by kiegroup.

the class RuleModelDRLPersistenceUnmarshallingTest method testMethodCallCheckParameterDataTypes2.

@Test
public void testMethodCallCheckParameterDataTypes2() {
    // BZ-1045423
    String drl = "" + "package org.mortgages;\n" + "import org.mortgages.MyType;\n" + "rule \"my rule\"\n" + "  dialect \"mvel\"\n" + "  when\n" + "    t : MyType( )\n" + "  then\n" + "    t.doSomething( 1 * 2 );\n" + "end\n";
    Map<String, List<MethodInfo>> methodInformation = new HashMap<>();
    List<MethodInfo> mapMethodInformation = new ArrayList<>();
    mapMethodInformation.add(new MethodInfo("doSomething", Collections.singletonList(DataType.TYPE_NUMERIC_INTEGER), "void", "void", "org.mortgages.MyType"));
    methodInformation.put("org.mortgages.MyType", mapMethodInformation);
    when(dmo.getModuleMethodInformation()).thenReturn(methodInformation);
    RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
    assertTrue(m.rhs[0] instanceof ActionCallMethod);
    ActionCallMethod mc = (ActionCallMethod) m.rhs[0];
    assertEquals("doSomething", mc.getMethodName());
    assertEquals("t", mc.getVariable());
    assertEquals(1, mc.getState());
    assertEquals(1, mc.getFieldValues().length);
    ActionFieldValue f1 = mc.getFieldValue(0);
    assertEquals("1 * 2", f1.getValue());
    assertEquals(DataType.TYPE_NUMERIC_INTEGER, f1.getType());
    assertEquals(FieldNatureType.TYPE_FORMULA, f1.getNature());
    String marshalled = RuleModelDRLPersistenceImpl.getInstance().marshal(m);
    logger.debug(marshalled);
    assertEqualsIgnoreWhitespace(drl, marshalled);
}
Also used : HashMap(java.util.HashMap) ActionFieldValue(org.drools.workbench.models.datamodel.rule.ActionFieldValue) ArrayList(java.util.ArrayList) ActionCallMethod(org.drools.workbench.models.datamodel.rule.ActionCallMethod) List(java.util.List) ArrayList(java.util.ArrayList) MethodInfo(org.kie.soup.project.datamodel.oracle.MethodInfo) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) Test(org.junit.Test)

Example 8 with MethodInfo

use of org.kie.soup.project.datamodel.oracle.MethodInfo 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)

Example 9 with MethodInfo

use of org.kie.soup.project.datamodel.oracle.MethodInfo in project drools by kiegroup.

the class RuleModelPersistenceHelper method findMethodInfo.

static MethodInfo findMethodInfo(final List<MethodInfo> methodInfos, final String expressionPart) {
    if (methodInfos != null && expressionPart != null) {
        // Find a MethodInfo that matches name and parameter count
        final int expressionParameterCount = parseExpressionParameters(expressionPart).size();
        final String normalizedExpressionPart = normalizeExpressionPart(expressionPart);
        for (MethodInfo methodInfo : methodInfos) {
            if (methodInfo.getName().equals(normalizedExpressionPart) && methodInfo.getParams().size() == expressionParameterCount) {
                return methodInfo;
            }
        }
    }
    return null;
}
Also used : MethodInfo(org.kie.soup.project.datamodel.oracle.MethodInfo) ConnectiveConstraint(org.drools.workbench.models.datamodel.rule.ConnectiveConstraint) SingleFieldConstraint(org.drools.workbench.models.datamodel.rule.SingleFieldConstraint) BaseSingleFieldConstraint(org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint)

Example 10 with MethodInfo

use of org.kie.soup.project.datamodel.oracle.MethodInfo in project drools by kiegroup.

the class BaseRuleModelTest method addMethodInformation.

protected void addMethodInformation(final String factName, final String name, final List<String> params, final String returnType, final String parametricReturnType, final String genericType) {
    MethodInfo mi = new MethodInfo(name, params, returnType, parametricReturnType, genericType);
    List<MethodInfo> existingMethodInfo = projectMethodInformation.get(factName);
    if (existingMethodInfo == null) {
        existingMethodInfo = new ArrayList<>();
        projectMethodInformation.put(factName, existingMethodInfo);
    }
    existingMethodInfo.add(mi);
}
Also used : MethodInfo(org.kie.soup.project.datamodel.oracle.MethodInfo)

Aggregations

MethodInfo (org.kie.soup.project.datamodel.oracle.MethodInfo)16 ArrayList (java.util.ArrayList)12 List (java.util.List)12 HashMap (java.util.HashMap)11 RuleModel (org.drools.workbench.models.datamodel.rule.RuleModel)11 Test (org.junit.Test)11 ActionCallMethod (org.drools.workbench.models.datamodel.rule.ActionCallMethod)9 ActionFieldValue (org.drools.workbench.models.datamodel.rule.ActionFieldValue)3 CompositeFactPattern (org.drools.workbench.models.datamodel.rule.CompositeFactPattern)3 ExpressionMethod (org.drools.workbench.models.datamodel.rule.ExpressionMethod)3 FactPattern (org.drools.workbench.models.datamodel.rule.FactPattern)3 FromAccumulateCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern)3 FromCollectCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern)3 FromCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern)3 ExpressionField (org.drools.workbench.models.datamodel.rule.ExpressionField)2 ExpressionUnboundFact (org.drools.workbench.models.datamodel.rule.ExpressionUnboundFact)2 SingleFieldConstraint (org.drools.workbench.models.datamodel.rule.SingleFieldConstraint)2 SingleFieldConstraintEBLeftSide (org.drools.workbench.models.datamodel.rule.SingleFieldConstraintEBLeftSide)2 AccumulateDescr (org.drools.compiler.lang.descr.AccumulateDescr)1 CollectDescr (org.drools.compiler.lang.descr.CollectDescr)1