private Boolean evaluateToBoolean(final Expression expr, final ExecutionContext ctx) { final Object l = expr.evaluate(ctx); if (l == null) return null; if (!(l instanceof Boolean)) { final Type t = ctx.getType(l); throw new EvaluationException("Boolean expected but was " + t.getName(), expr, ctx); } final Boolean result = (Boolean) l; return result; }
public NamedFunction createCheckFunction(BackendTypesystem ts, ExtensionFile extensionFile) { final OldExpressionConverter exprConv = new OldExpressionConverter( _emptyExecutionContext.cloneWithResource(extensionFile), _typeConverter, OldHelper.normalizeXtendResourceName(extensionFile.getFullyQualifiedName())); final List<String> paramNames = Arrays.asList(ISSUES_PARAM_NAME, ALL_OBJECTS_PARAM_NAME); final List<BackendType> paramTypes = Arrays.asList(ts.findType(Issues.class), CollectionType.INSTANCE); final List<ExpressionBase> allChecks = new ArrayList<ExpressionBase>(); for (Check chk : extensionFile.getChecks()) { if (!exprConv.hasThis()) { ExecutionContext oldCtx = exprConv.getExecutionContext(); exprConv.setExecutionContext( oldCtx.cloneWithVariable( new Variable( ExecutionContext.IMPLICIT_VARIABLE, oldCtx.getTypeForName(chk.getType().getValue())))); allChecks.add(convertCheck(chk, exprConv)); exprConv.setExecutionContext(oldCtx); } } final ExpressionBase body = new SequenceExpression(allChecks, exprConv.getSourcePos(extensionFile)); return new NamedFunction( ALL_CHECKS_FUNCTION_NAME, new SourceDefinedFunction( ALL_CHECKS_FUNCTION_NAME, paramNames, paramTypes, BooleanType.INSTANCE, body, false, null)); }
@Override public Type analyzeInternal(final ExecutionContext ctx, final Set<AnalysationIssue> issues) { final Type l = left.analyze(ctx, issues); final Type r = right.analyze(ctx, issues); if (l == null || r == null) return null; if (!ctx.getBooleanType().equals(l)) { issues.add( new AnalysationIssue( AnalysationIssue.INCOMPATIBLE_TYPES, "Boolean expected! Found : " + l.getName(), left)); } if (!ctx.getBooleanType().equals(r)) { issues.add( new AnalysationIssue( AnalysationIssue.INCOMPATIBLE_TYPES, "Boolean expected! Found : " + r.getName(), right)); } return ctx.getBooleanType(); }
@Override protected Object evaluateInternal(final ExecutionContext ctx) { final Boolean l = evaluateToBoolean(left, ctx); if (l == null) return ctx.handleNullEvaluation(this); if (operator.getValue().equals(AND)) { if (!l.booleanValue()) return Boolean.FALSE; final Boolean r = evaluateToBoolean(right, ctx); if (r == null) return ctx.handleNullEvaluation(this); return Boolean.valueOf(l.booleanValue() && r.booleanValue()); } else if (operator.getValue().equals(OR)) { if (l.booleanValue()) return Boolean.TRUE; final Boolean r = evaluateToBoolean(right, ctx); if (r == null) return ctx.handleNullEvaluation(this); return Boolean.valueOf(l.booleanValue() || r.booleanValue()); } else if (operator.getValue().equals(IMPLIES)) { if (!l.booleanValue()) return Boolean.TRUE; Boolean b = evaluateToBoolean(right, ctx); if (b == null) return ctx.handleNullEvaluation(this); return b; } else throw new EvaluationException("Unknown Boolean operator " + operator.getValue(), this, ctx); }
public void setExecutionContext(ExecutionContext ctx) { this.monitor = ctx.getMonitor(); }