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.
Basics FEEL Decision Table support (#8)
* Very basics working but still missing a lot of tests and cases. * Decision Table, Named parameter support.
- Loading branch information
Showing
5 changed files
with
256 additions
and
17 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
80 changes: 80 additions & 0 deletions
80
kie-dmn-feel/src/test/java/org/kie/dmn/feel/lang/examples/SimpleDecisionTablesTest.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,80 @@ | ||
package org.kie.dmn.feel.lang.examples; | ||
|
||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
import java.util.Map; | ||
|
||
import org.junit.Assert; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
import org.kie.dmn.feel.FEEL; | ||
import org.kie.dmn.feel.lang.CompiledExpression; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class SimpleDecisionTablesTest { | ||
private static final Logger logger = LoggerFactory.getLogger( ExamplesTest.class ); | ||
public static final String DEFAULT_IDENT = " "; | ||
private static FEEL feel; | ||
|
||
@BeforeClass | ||
public static void setupTest() { | ||
feel = FEEL.newInstance(); | ||
} | ||
|
||
@Test | ||
public void testMain() { | ||
String expression = loadExpression( "simple_decision_tables.feel" ); | ||
|
||
// String expression2 = "decision table ( \n" + | ||
// " rule list: [\n" + | ||
// " [ <18, \"Young\"],\n" + | ||
// " [ >18, \"Adult\"]\n" + | ||
// " ]\n" + | ||
// " )"; | ||
String expression2 = "1 = 1"; | ||
System.out.println(expression2); | ||
CompiledExpression compile = feel.compile(expression2, feel.newCompilerContext()); | ||
|
||
System.out.println(compile); | ||
|
||
System.out.println("---"); | ||
|
||
Map context = (Map) feel.evaluate( expression ); | ||
|
||
System.out.println( printContext( context ) ); | ||
} | ||
|
||
private static String loadExpression(String fileName) { | ||
try { | ||
return new String( Files.readAllBytes( Paths.get( ExamplesTest.class.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(); | ||
} | ||
} |
16 changes: 0 additions & 16 deletions
16
kie-dmn-feel/src/test/resources/org/kie/dmn/feel/lang/examples/decision_table.feel
This file was deleted.
Oops, something went wrong.
57 changes: 57 additions & 0 deletions
57
kie-dmn-feel/src/test/resources/org/kie/dmn/feel/lang/examples/simple_decision_tables.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,57 @@ | ||
{ | ||
myDecisionTable : decision table ( | ||
outputs: "Adult", | ||
input expression list: [Age], | ||
rule list: [ | ||
[ <18, "Young"], | ||
[ >18, "Adult"] | ||
], | ||
hit policy: "U" | ||
), | ||
|
||
/* TODO Please note errors: | ||
line 15:66 no viable alternative at input 'Age' | ||
line 15:79 no viable alternative at input 'History' | ||
*/ | ||
biggerDecisionTable : decision table ( | ||
outputs: "Applicant Risk Rating", | ||
input expression list: [Applicant Age, Medical History], | ||
rule list: [ | ||
[ >60, "good", "Medium" ], | ||
[ >60, "bad", "High" ], | ||
[ [25..60], -, "Medium" ], | ||
[ <25, "good", "Low" ], | ||
[ <25, "bad", "Medium" ] | ||
], | ||
hit policy: "Unique" | ||
), | ||
|
||
multipleOutputs : decision table ( | ||
outputs: [Out1, Out2], | ||
input expression list: [In1], | ||
rule list: [ | ||
[ >1, "out1a", "out2a" ], | ||
[ >2, "out1b", "out2b" ] | ||
], | ||
hit policy: "Unique" | ||
), | ||
|
||
// not sure if Input Expression List is expecting a list of Strings or should be some tokens? | ||
multipleInOut : decision table ( | ||
outputs: ["Out1", "Out2"], | ||
input expression list: ["In1", "In2"], | ||
rule list: [ | ||
[ <47, >1, "io1a", "io2a" ], | ||
[ >0, >0, "io1b", "io2b" ] | ||
], | ||
hit policy: "Unique" | ||
), | ||
|
||
|
||
result : myDecisionTable( 34 ), | ||
result2: biggerDecisionTable( 35, "good" ), | ||
result3: multipleOutputs(2), | ||
result4: multipleInOut(In2: 47, In1:2), | ||
|
||
basic : 1 = 1 | ||
} |