forked from apache/incubator-kie-drools
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DROOLS-1701 Fix closure context handling, misc fn issues (apache#8)
- Loading branch information
Showing
9 changed files
with
231 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
kie-dmn/kie-dmn-feel/src/main/java/org/kie/dmn/feel/lang/FunctionDefs.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package org.kie.dmn.feel.lang; | ||
|
||
import java.math.BigDecimal; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
import org.drools.javaparser.JavaParser; | ||
import org.drools.javaparser.ast.NodeList; | ||
import org.drools.javaparser.ast.expr.CastExpr; | ||
import org.drools.javaparser.ast.expr.ClassExpr; | ||
import org.drools.javaparser.ast.expr.Expression; | ||
import org.drools.javaparser.ast.expr.MethodCallExpr; | ||
import org.drools.javaparser.ast.expr.NameExpr; | ||
import org.drools.javaparser.ast.expr.NullLiteralExpr; | ||
import org.drools.javaparser.ast.expr.StringLiteralExpr; | ||
import org.drools.javaparser.ast.type.Type; | ||
import org.kie.dmn.feel.lang.ast.FunctionDefNode; | ||
|
||
public class FunctionDefs { | ||
|
||
public static Expression asMethodCall( | ||
String className, | ||
String methodSignature, | ||
List<String> params) { | ||
// creating a simple algorithm to find the method in java | ||
// without using any external libraries in this initial implementation | ||
// might need to explicitly use a classloader here | ||
String[] mp = FunctionDefNode.parseMethod(methodSignature); | ||
try { | ||
String methodName = mp[0]; | ||
String[] paramTypeNames = FunctionDefNode.parseParams(mp[1]); | ||
ArrayList<Expression> paramExprs = new ArrayList<>(); | ||
if (paramTypeNames.length == params.size()) { | ||
for (int i = 0; i < params.size(); i++) { | ||
String paramName = params.get(i); | ||
String paramTypeName = paramTypeNames[i]; | ||
Type paramTypeCanonicalName = | ||
JavaParser.parseType( | ||
FunctionDefNode.getType(paramTypeName).getCanonicalName()); | ||
|
||
Expression param = | ||
new CastExpr(paramTypeCanonicalName, | ||
new MethodCallExpr( | ||
null, | ||
"coerceTo", | ||
new NodeList<>( | ||
new ClassExpr(paramTypeCanonicalName), | ||
new MethodCallExpr( | ||
new NameExpr("feelExprCtx"), | ||
"getValue", | ||
new NodeList<>(new StringLiteralExpr(paramName)) | ||
)))); | ||
|
||
paramExprs.add(param); | ||
} | ||
|
||
return new MethodCallExpr( | ||
new NameExpr(className), | ||
methodName, | ||
new NodeList<>(paramExprs)); | ||
} else { | ||
return new NullLiteralExpr(); | ||
} | ||
} catch (Exception e) { | ||
throw new IllegalArgumentException(e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.