public static boolean isInstanceOf( Object o, String name ) { return determineTypeFromInstance( o ) == determineTypeFromName( name ); }
@Override public boolean isAssignableValue(Object value) { return wrapped.isAssignableValue(value); }
@Test public void testYearsAndMonthsDuration() { final DMNRuntime runtime = DMNRuntimeUtil.createRuntime("yearMonthDuration.dmn", this.getClass() ); final DMNModel dmnModel = runtime.getModel("http://www.trisotech.com/dmn/definitions/_6eda1490-21ca-441e-8a26-ab3ca800e43c", "Drawing 1" ); assertThat( dmnModel, notNullValue() ); assertThat( DMNRuntimeUtil.formatMessages( dmnModel.getMessages() ), dmnModel.hasErrors(), is( false ) ); final BuiltInType feelType = (BuiltInType) BuiltInType.determineTypeFromName("yearMonthDuration" ); final Period period = (Period) feelType.fromString("P2Y1M" ); final DMNContext context = runtime.newContext(); context.set( "iDuration", period ); final DMNResult dmnResult = runtime.evaluateAll(dmnModel, context ); assertThat( DMNRuntimeUtil.formatMessages( dmnResult.getMessages() ), dmnResult.hasErrors(), is( false ) ); final DMNContext result = dmnResult.getContext(); assertThat( result.get( "How long" ), is( "Longer than a year" ) ); }
@Override public Type evaluate(EvaluationContext ctx) { return BuiltInType.determineTypeFromName( getText() ); }
private void init() { // the following automatically adds the GLOBAL scope as a child to the built-in scope new ScopeImpl( Scope.GLOBAL, builtInScope ); // pre-loads all the built in functions and types Stream.of( BuiltInFunctions.getFunctions() ).forEach( f -> builtInScope.define( f.getSymbol() ) ); Stream.of(BuiltInType.values()).flatMap(b -> b.getSymbols().stream()).forEach(t -> builtInScope.define(t)); }
@Override public boolean isAssignableValue(Object value) { if ( value == null ) { return true; // a null-value can be assigned to any type. } return isInstanceOf(value, this); } }
/** * Marshalls the given value into FEEL code that can be executed to * reconstruct the value. For instance, here are some examples of the marshalling process: * * * number 10 marshalls as: 10 * * string foo marshalls as: "foo" * * duration P1D marshalls as: duration( "P1D" ) * * context { x : 10, y : foo } marshalls as: { x : 10, y : "foo" } * * @param value the FEEL value to be marshalled * * @return a string representing the FEEL code that needs to be executed to reconstruct the value */ @Override public String marshall(Object value) { if( value == null ) { return "null"; } return KieExtendedDMNFunctions.getFunction(CodeFunction.class).invoke(value).cata(justNull(), Function.identity()); }
public static boolean isInstanceOf( Object o, Type t ) { if ( o == null ) { return false; // See FEEL specifications Table 49. } if ( t == UNKNOWN ) { return true; } return determineTypeFromInstance( o ) == t; }
@Test public void testDateAllowedValues() { final DMNRuntime runtime = DMNRuntimeUtil.createRuntime("date_allowed_values.dmn", this.getClass() ); final DMNModel dmnModel = runtime.getModel( "http://www.trisotech.com/definitions/_fbf002a3-615b-4f02-98e4-c28d4676225a", "Error with constraints verification" ); assertThat( dmnModel, notNullValue() ); assertThat( DMNRuntimeUtil.formatMessages( dmnModel.getMessages() ), dmnModel.hasErrors(), is( false ) ); final DMNContext ctx = runtime.newContext(); final Object duration = BuiltInType.DURATION.fromString("P20Y" ); ctx.set( "yearsMonth", duration ); final Object dateTime = BuiltInType.DATE_TIME.fromString("2017-05-16T17:58:00.000" ); ctx.set( "dateTime", dateTime ); final DMNResult dmnResult = runtime.evaluateAll(dmnModel, ctx ); final DMNContext result = dmnResult.getContext(); assertThat( DMNRuntimeUtil.formatMessages( dmnResult.getMessages() ), dmnResult.hasErrors(), is( false ) ); assertThat( (Map<String,Object>) result.get( "Decision Logic 1" ), hasEntry( "years and months", duration ) ); assertThat( (Map<String,Object>) result.get( "Decision Logic 1" ), hasEntry( "Date Time", dateTime ) ); }
@Override public boolean isInstanceOf(Object o) { return isInstanceOf(o, this); }
/** * Marshalls the give FEEL value into a String. The result is similar to * calling the string() function in a FEEL expression, with the difference * that a null value is returned as the "null" string instead of the null * value itself. * * @param value the FEEL value to be marshalled * * @return the string representation of the value */ @Override public String marshall(Object value) { if( value == null ) { return "null"; } return BuiltInFunctions.getFunction( StringFunction.class ).invoke( value ).cata( justNull(), Function.identity()); }
@Override public Range evaluate(EvaluationContext ctx) { Object s = start.evaluate( ctx ); Object e = end.evaluate( ctx ); boolean problem = false; if ( s == null ) { ctx.notifyEvt( astEvent(Severity.ERROR, Msg.createMessage(Msg.IS_NULL, "Start"))); problem = true; } if ( e == null ) { ctx.notifyEvt( astEvent(Severity.ERROR, Msg.createMessage(Msg.IS_NULL, "End"))); problem = true; } if (problem) { return null; } if ( BuiltInType.determineTypeFromInstance( s ) != BuiltInType.determineTypeFromInstance( e ) && !s.getClass().isAssignableFrom( e.getClass() ) ) { ctx.notifyEvt( astEvent(Severity.ERROR, Msg.createMessage(Msg.X_TYPE_INCOMPATIBLE_WITH_Y_TYPE, "Start", "End"))); return null; } Comparable start = convertToComparable( ctx, s ); Comparable end = convertToComparable( ctx, e ); return new RangeImpl( lowerBound==IntervalBoundary.OPEN ? Range.RangeBoundary.OPEN : Range.RangeBoundary.CLOSED, start, end, upperBound==IntervalBoundary.OPEN ? Range.RangeBoundary.OPEN : Range.RangeBoundary.CLOSED ); }
@Override public boolean isInstanceOf(Object o) { return wrapped.isInstanceOf(o); }
return null; } else if ( feelType.equals( org.kie.dmn.feel.lang.types.BuiltInType.NUMBER ) ) { return BuiltInFunctions.getFunction( NumberFunction.class ).invoke( value, null, null ).cata( justNull(), Function.identity() ); } else if ( feelType.equals( org.kie.dmn.feel.lang.types.BuiltInType.STRING ) ) { return value; } else if ( feelType.equals( org.kie.dmn.feel.lang.types.BuiltInType.DATE ) ) { return BuiltInFunctions.getFunction( DateFunction.class ).invoke( value ).cata( justNull(), Function.identity() ); } else if ( feelType.equals( org.kie.dmn.feel.lang.types.BuiltInType.TIME ) ) { return BuiltInFunctions.getFunction( TimeFunction.class ).invoke( value ).cata( justNull(), Function.identity() ); } else if ( feelType.equals( org.kie.dmn.feel.lang.types.BuiltInType.DATE_TIME ) ) { return BuiltInFunctions.getFunction( DateAndTimeFunction.class ).invoke( value ).cata( justNull(), Function.identity() ); } else if ( feelType.equals( org.kie.dmn.feel.lang.types.BuiltInType.DURATION ) ) { return BuiltInFunctions.getFunction( DurationFunction.class ).invoke( value ).cata( justNull(), Function.identity() ); } else if ( feelType.equals( org.kie.dmn.feel.lang.types.BuiltInType.BOOLEAN ) ) { return Boolean.parseBoolean( value );