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);
}
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);
}
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);
}
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;
}
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);
}
Aggregations