forked from apache/incubator-kie-drools
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
191 additions
and
12 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
90 changes: 90 additions & 0 deletions
90
kie-dmn/kie-dmn-feel/src/test/java/org/kie/dmn/feel/lang/examples/ExamplesTest.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,90 @@ | ||
/* | ||
* Copyright 2016 Red Hat, Inc. and/or its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.kie.dmn.feel.lang.examples; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.kie.dmn.feel.lang.runtime.FEEL; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
import java.util.Arrays; | ||
import java.util.Map; | ||
|
||
public class ExamplesTest { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger( ExamplesTest.class ); | ||
public static final String DEFAULT_IDENT = " "; | ||
|
||
@Test | ||
public void testLoadApplicantContext() { | ||
String expression = loadExpression( "applicant.feel" ); | ||
Map applicant = (Map) FEEL.evaluate( expression ); | ||
System.out.println( printContext( applicant ) ); | ||
} | ||
|
||
@Test | ||
public void testLoadExample_10_6_1() { | ||
String expression = loadExpression( "example_10_6_1.feel" ); | ||
Map context = (Map) FEEL.evaluate( expression ); | ||
System.out.println( printContext( context ) ); | ||
} | ||
|
||
@Test | ||
public void testLoadExample_10_6_2() { | ||
String expression = loadExpression( "example_10_6_1.feel" ); | ||
Map context = (Map) FEEL.evaluate( expression ); | ||
Number yearlyIncome = (Number) FEEL.evaluate( "monthly income * 12", context ); | ||
System.out.println( "Yearly income = " + yearlyIncome ); | ||
} | ||
|
||
private String loadExpression(String fileName) { | ||
try { | ||
return new String( Files.readAllBytes( Paths.get( getClass().getResource( fileName ).toURI() ) ) ); | ||
} catch ( Exception e ) { | ||
logger.error( "Error reading file " + fileName, e ); | ||
Assert.fail("Error reading file "+fileName); | ||
} | ||
return null; | ||
} | ||
|
||
private String printContext( Map context ) { | ||
return printContext( context, "" ); | ||
} | ||
|
||
private String printContext( Map<String, Object> context, String ident ) { | ||
StringBuilder builder = new StringBuilder( ); | ||
builder.append( "{\n" ); | ||
for( Map.Entry e : context.entrySet() ) { | ||
builder.append( ident ) | ||
.append( DEFAULT_IDENT ) | ||
.append( e.getKey() ) | ||
.append( ": " ); | ||
if( e.getValue() instanceof Map ) { | ||
builder.append( printContext( (Map<String, Object>) e.getValue(), ident + DEFAULT_IDENT ) ); | ||
} else { | ||
builder.append( e.getValue() ) | ||
.append( "\n" ); | ||
} | ||
} | ||
builder.append( ident+"}\n" ); | ||
return builder.toString(); | ||
} | ||
|
||
} |
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
16 changes: 16 additions & 0 deletions
16
kie-dmn/kie-dmn-feel/src/test/resources/org/kie/dmn/feel/lang/examples/applicant.feel
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,16 @@ | ||
/* | ||
* Example context from DMN Spec page 100 | ||
* | ||
* Defines the data of an Applicant | ||
*/ | ||
{ | ||
Age : 51, | ||
Marital Status : "M", | ||
Employment Status : "EMPLOYED", | ||
Existing Customer : false, | ||
Monthly : { | ||
Income: 10000.00, | ||
Repayments: 2500.00, | ||
Expenses: 3000.00 | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
kie-dmn/kie-dmn-feel/src/test/resources/org/kie/dmn/feel/lang/examples/eligibility.feel
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,15 @@ | ||
{ | ||
Age : Applicant.Age, | ||
Monthly Income : Applicant.Monthly.Income, | ||
Pre-Bureau Risk Category : Affordability.Pre-Bureau Risk Category | ||
Installment Affordable : Affordability.Installment Affordable | ||
Eligibility : | ||
if Pre-Bureau Risk Category = "DECLINE" or | ||
Installment Affordable = false or | ||
Age < 18 or | ||
Monthly Income < 100 | ||
then | ||
"INELIGIBLE" | ||
else | ||
"ELIGIBLE" | ||
}.Eligibility |
40 changes: 40 additions & 0 deletions
40
kie-dmn/kie-dmn-feel/src/test/resources/org/kie/dmn/feel/lang/examples/example_10_6_1.feel
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,40 @@ | ||
/* | ||
* Example context from DMN Spec page 142 | ||
* | ||
* Defines a complex context | ||
*/ | ||
{ | ||
applicant : { | ||
age : 51, | ||
marital status : "M", | ||
employment status : "EMPLOYED", | ||
existing customer : false, | ||
monthly : { | ||
income: 10000.00, | ||
repayments: 2500.00, | ||
expenses: 3000.00 | ||
} | ||
}, | ||
requested product : { | ||
product type : "STANDARD LOAN", | ||
rate : 0.25, | ||
term : 36, | ||
amount : 100000.00 | ||
}, | ||
monthly income : applicant.monthly.income, | ||
monthly outgoings : [ applicant.monthly.repayments, applicant.monthly.expenses ], | ||
credit history : [ | ||
{ | ||
record date : date( "2008-03-12" ), | ||
event : "home mortgage", | ||
weight : 100 | ||
}, | ||
{ | ||
record date : date( "2011-04-01" ), | ||
event : "foreclosure warning", | ||
weight : 150 | ||
} | ||
], | ||
PMT : function( rate, term, amount ) | ||
(amount * rate / 12) / ( 1 - ( 1 + rate / 12 ) ** -term ) | ||
} |
17 changes: 17 additions & 0 deletions
17
kie-dmn/kie-dmn-feel/src/test/resources/org/kie/dmn/feel/lang/runtime/examples.feel
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,17 @@ | ||
{ a : 10, | ||
b : 10, | ||
c : a + b, | ||
a + b : 5, | ||
x : a + b + c | ||
} | ||
|
||
|
||
if Pre-Bureau Risk Category = "DECLINE" or | ||
Installment Affordable = false or | ||
Age < 18 or | ||
Monthly Income < 100 | ||
then | ||
"INELIGIBLE" | ||
else | ||
"ELIGIBLE" | ||
|