/** * {@link #getDefinedValue(Object, String)} method instead. * @deprecated this method cannot distinguish null because: 1. property undefined for current, 2. an error, 3. a properly defined property value valorized to null. * */ public static Object getValue(final Object current, final String property) { return getDefinedValue(current, property).getValueResult().getOrElse(null); }
public boolean isRight() { return ! isLeft(); }
public <E extends Exception> R getOrElseThrow(Function<L, E> exceptionFn) throws E { if (isRight()) { return right.orElse(null); } else { throw exceptionFn.apply(left.get()); } }
@Test public void testLocateInNSAliased() { final Import i = makeImport("nsA", "aliased", "m1"); final List<QName> available = Arrays.asList(new QName("nsA", "m1"), new QName("nsA", "m2"), new QName("nsB", "m3")); final Either<String, QName> result = ImportDMNResolverUtil.resolveImportDMN(i, available, Function.identity()); assertTrue(result.isRight()); assertEquals(new QName("nsA", "m1"), result.getOrElse(null)); }
/** * If valid input values are defined, check that all parameters match the respective valid inputs * @param ctx * @param params * @return */ private Either<FEELEvent, Object> actualInputsMatchInputValues(EvaluationContext ctx, Object[] params) { // check that all the parameters match the input list values if they are defined for( int i = 0; i < params.length; i++ ) { final DTInputClause input = inputs.get( i ); // if a list of values is defined, check the the parameter matches the value if ( input.getInputValues() != null && ! input.getInputValues().isEmpty() ) { final Object parameter = params[i]; boolean satisfies = input.getInputValues().stream().map( ut -> ut.apply( ctx, parameter ) ).filter( Boolean::booleanValue ).findAny().orElse( false ); if ( !satisfies ) { String values = input.getInputValuesText(); return Either.ofLeft(new InvalidInputEvent( FEELEvent.Severity.ERROR, input.getInputExpression()+"='" + parameter + "' does not match any of the valid values " + values + " for decision table '" + getName() + "'.", getName(), null, values ) ); } } } return Either.ofRight(true); }
public R getOrElse(R default_value) { return cata(e -> default_value, Function.identity()); }
public static <L,R> Either<L,R> ofLeft(L value) { return new Either<>(Optional.of(value), Optional.empty()); }
public static PropertyValueResult notDefined() { return new PropertyValueResult(false, Either.ofLeft(new UnsupportedOperationException("Property was not defined."))); }
public static PropertyValueResult ofValue(Object value) { return new PropertyValueResult(true, Either.ofRight(value)); }
@Test public void testNSnoModelNameWithAlias() { final Import i = makeImport("ns1", "mymodel", null); final List<QName> available = Arrays.asList(new QName("ns1", "m1"), new QName("ns2", "m2"), new QName("ns3", "m3")); final Either<String, QName> result = ImportDMNResolverUtil.resolveImportDMN(i, available, Function.identity()); assertTrue(result.isRight()); assertEquals(new QName("ns1", "m1"), result.getOrElse(null)); }
private Object fetchValue(final Object o, final String... names) { Object result = o; for (String nr : names) { result = EvalHelper.getDefinedValue(result, nr) .getValueResult() .cata(err -> { // no need to report error here, eg: [ {x:1, y:2}, {x:2} ].y results in [2] with no errors. return null; }, Function.identity()); } return result; } }
public static <L,R> Either<L,R> ofRight(R value) { return new Either<>(Optional.empty(), Optional.ofNullable(value)); }
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { e.printStackTrace(); return PropertyValueResult.of(Either.ofLeft(e));
@Test public void testNSandModelName() { final Import i = makeImport("ns1", null, "m1"); final List<QName> available = Arrays.asList(new QName("ns1", "m1"), new QName("ns2", "m2"), new QName("ns3", "m3")); final Either<String, QName> result = ImportDMNResolverUtil.resolveImportDMN(i, available, Function.identity()); assertTrue(result.isRight()); assertEquals(new QName("ns1", "m1"), result.getOrElse(null)); }
public <X> X cata(Function<L,X> left, Function<R,X> right) { // warning: left.invoke, because of FEEL specs, could return null. The below is the safest way to implement cata over this Either. return isLeft() ? left.apply( this.left.get() ) : right.apply( this.right.orElse( null ) ); } }
@Override public Object getValue(String symbol) { symbol = EvalHelper.normalizeVariableName( symbol ); if (rootObject != null) { PropertyValueResult dv = EvalHelper.getDefinedValue(rootObject, symbol); if (dv.isDefined()) { return dv.getValueResult().getOrElse(null); } } if ( variables.containsKey( symbol ) ) { return variables.get( symbol ); } if ( parentFrame != null ) { return parentFrame.getValue( symbol ); } return null; }
Either<FEELEvent, Object> either = (Either<FEELEvent, Object>) result; Object eitherResult = either.cata( (left) -> { ctx.notifyEvt( () -> { if ( left instanceof InvalidParametersEvent ) { Object eitherResult = either.cata( (left) -> { ctx.notifyEvt( () -> { if ( left instanceof InvalidParametersEvent ) {
@Test public void testNSonly() { final Import i = makeImport("ns1", null, null); final List<QName> available = Arrays.asList(new QName("ns1", "m1"), new QName("ns2", "m2"), new QName("ns3", "m3")); final Either<String, QName> result = ImportDMNResolverUtil.resolveImportDMN(i, available, Function.identity()); assertTrue(result.isRight()); assertEquals(new QName("ns1", "m1"), result.getOrElse(null)); }
@Test public void testNSandUnexistentModelName() { final Import i = makeImport("ns1", null, "boh"); final List<QName> available = Arrays.asList(new QName("ns1", "m1"), new QName("ns2", "m2"), new QName("ns3", "m3")); final Either<String, QName> result = ImportDMNResolverUtil.resolveImportDMN(i, available, Function.identity()); assertTrue(result.isLeft()); }