Skip to content

Commit

Permalink
exceptions handled and changes to util class
Browse files Browse the repository at this point in the history
  • Loading branch information
bncriju committed Sep 13, 2024
1 parent c78762a commit 10f651b
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 25 deletions.
18 changes: 11 additions & 7 deletions kie-dmn/kie-dmn-feel/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,20 +83,19 @@
</exclusions>
</dependency>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-compiler</artifactId>
<groupId>org.drools</groupId>
<artifactId>drools-compiler</artifactId>
</dependency>

<dependency>
<groupId>com.github.javaparser</groupId>
<artifactId>javaparser-core</artifactId>
</dependency>


<!-- BigMath dependency to obtain BigDecimal advanced math -->
<dependency>
<groupId>ch.obermuhlner</groupId>
<artifactId>big-math</artifactId>
<groupId>ch.obermuhlner</groupId>
<artifactId>big-math</artifactId>
</dependency>

<!-- Logging -->
Expand Down Expand Up @@ -150,16 +149,20 @@
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
</dependency>

</dependencies>

<build>
<testResources>
<testResource> <!-- as I will be including additional testResource directory, I need to re-explicit the default to Maven (standard Maven behaviour) -->
<directory>${project.basedir}/src/test/resources</directory>
<directory>${project.basedir}/src/test/resources</directory>
</testResource>
<testResource>
<directory>${project.basedir}/</directory>
<directory>${project.basedir}/../</directory>
<includes>
<include>ref-dmn-feel-builtin-functions.adoc</include>
</includes>
Expand All @@ -186,4 +189,5 @@

</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,28 @@ private MatchesFunction() {
super( "matches" );
}

public FEELFnResult<Object> FEELFnResult(@ParameterName("input") String input, @ParameterName("pattern") String pattern) {
public FEELFnResult<Boolean> FEELFnResult(@ParameterName("input") String input, @ParameterName("pattern") String pattern) {
return invoke( input, pattern, null );
}

public FEELFnResult<Object> invoke(@ParameterName("input") String input, @ParameterName("pattern") String pattern, @ParameterName("flags") String flags) {
public FEELFnResult<Boolean> invoke(@ParameterName("input") String input, @ParameterName("pattern") String pattern, @ParameterName("flags") String flags) {
try {
return matchFunctionWithFlags(input,pattern,flags);
} catch ( PatternSyntaxException t ) {
return FEELFnResult.ofError( new InvalidParametersEvent( Severity.ERROR, "pattern", "is invalid and can not be compiled", t ) );
} catch (IllegalArgumentException t ) {
} catch (InvalidParameterException t ) {
return FEELFnResult.ofError( new InvalidParametersEvent( Severity.ERROR, t.getMessage(), "cannot be null or is invalid", t ) );
} catch (Throwable t) {
return FEELFnResult.ofError( new InvalidParametersEvent( Severity.ERROR, "pattern", "is invalid and can not be compiled", t ) );
String errorMessage;
if (t.getMessage() != null && !t.getMessage().isEmpty()) {
errorMessage = "Error: " + t.getMessage();
} else {
errorMessage = String.format("Some of the provided parameters might be invalid. Input: '%s', Pattern: '%s', Flags: '%s'",
input, pattern, flags);
}
return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, errorMessage, t));
}
}

static FEELFnResult<Object> matchFunctionWithFlags(String input, String pattern, String flags) {
static FEELFnResult<Boolean> matchFunctionWithFlags(String input, String pattern, String flags) {
log.debug("Input: {} , Pattern: {}, Flags: {}", input, pattern, flags);
if ( input == null ) {
throw new InvalidParameterException("input");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public FEELFnResult<String> invoke(@ParameterName("input") String input, @Parame
return FEELFnResult.ofError( new InvalidParametersEvent( Severity.ERROR, "replacement", "cannot be null" ) );
}

return FEELFnResult.ofResult(XQueryImplUtil.executeReplaceFunction(input,pattern,replacement,flags).toString());
return FEELFnResult.ofResult(String.valueOf(XQueryImplUtil.executeReplaceFunction(input,pattern,replacement,flags)));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,26 @@

public class XQueryImplUtil {

public static Object executeMatchesFunction(String input, String pattern, String flags){
public static Boolean executeMatchesFunction(String input, String pattern, String flags){
flags = flags == null ? "" : flags;
String xQueryExpression = String.format("matches('%s', '%s', '%s')", input, pattern, flags);
return evaluateXQueryExpression(xQueryExpression);
return evaluateXQueryExpression(xQueryExpression, Boolean.class);
}

public static Object executeReplaceFunction(String input, String pattern, String replacement, String flags) {
public static String executeReplaceFunction(String input, String pattern, String replacement, String flags) {
flags = flags == null ? "" : flags;
String xQueryExpression = String.format("replace('%s', '%s', '%s', '%s')", input, pattern, replacement, flags);
return evaluateXQueryExpression(xQueryExpression);
return evaluateXQueryExpression(xQueryExpression, String.class);
}

static Object evaluateXQueryExpression (String expression) {
static <T> T evaluateXQueryExpression (String expression, Class<T> expectedTypeResult) {
try {
Processor processor = new Processor(false);
XQueryCompiler compiler = processor.newXQueryCompiler();
XQueryExecutable executable = compiler.compile(expression);
XQueryEvaluator queryEvaluator = executable.load();
XdmItem resultItem = queryEvaluator.evaluateSingle();
return ((XdmAtomicValue) resultItem).getValue();
return expectedTypeResult.cast((((XdmAtomicValue) resultItem).getValue()));
} catch (ClassCastException | SaxonApiException e) {
throw new IllegalStateException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ void executeMatchesFunctionTest() {
String input = "test";
String pattern = "^test";
String flags = "i";
Object retrieved = XQueryImplUtil.executeMatchesFunction(input, pattern,
boolean retrieved = XQueryImplUtil.executeMatchesFunction(input, pattern,
flags);
boolean expected = true;
assertThat(retrieved).isNotNull().isEqualTo(expected);
Expand Down Expand Up @@ -65,16 +65,16 @@ void executeReplaceFunctionTest() {
String pattern = "^test";
String replacement = "ttt";
String flags = "";
Object retrieved = XQueryImplUtil.executeReplaceFunction(input, pattern, replacement,
flags);
String retrieved = XQueryImplUtil.executeReplaceFunction(input, pattern, replacement,
flags).toString();
String expected = "tttString";
assertThat(retrieved).isNotNull().isEqualTo(expected);

input = "fo\nbar";
pattern = "o.b";
replacement = "ttt";
flags = "s";
retrieved = XQueryImplUtil.executeReplaceFunction(input, pattern, replacement, flags);
retrieved = XQueryImplUtil.executeReplaceFunction(input, pattern, replacement, flags).toString();
expected = "ftttar";
assertThat(retrieved).isNotNull().isEqualTo(expected);
}
Expand Down

0 comments on commit 10f651b

Please sign in to comment.