/// <summary> /// Constructor /// </summary> /// <param name="dictionary"></param> public BoolType(EFSSystem efsSystem) : base(efsSystem, "Boolean") { True = new Values.BoolValue(this, true); False = new Values.BoolValue(this, false); InitDeclaredElements(); }
/// <summary> /// Indicates whether all preconditions are satisfied for a given case, ignoring expressions like parameter <= xxx or parameter >= xxx /// </summary> /// <param name="context">The interpretation context</param> /// <param name="cas">The case to evaluate</param> /// <param name="parameter"></param> /// <returns></returns> private bool PreconditionSatisfied(Interpreter.InterpretationContext context, Case cas, Parameter parameter) { bool retVal = true; foreach (Rules.PreCondition preCondition in cas.PreConditions) { if (!ExpressionBasedOnParameter(parameter, preCondition.ExpressionTree)) { Values.BoolValue boolValue = preCondition.ExpressionTree.GetValue(context) as Values.BoolValue; if (boolValue == null) { throw new Exception("Cannot evaluate precondition " + preCondition.Name); } else if (!boolValue.Val) { retVal = false; break; } } } return(retVal); }
/// <summary> /// Provides the value of the function /// </summary> /// <param name="instance">the instance on which the function is evaluated</param> /// <param name="actuals">the actual parameters values</param> /// <param name="localScope">the values of local variables</param> /// <returns>The value for the function application</returns> public override Values.IValue Evaluate(Interpreter.InterpretationContext context, Dictionary <Variables.Actual, Values.IValue> actuals) { Values.IValue retVal = null; int token = context.LocalScope.PushContext(); AssignParameters(context, actuals); Values.BoolValue val = context.findOnStack(Value).Value as Values.BoolValue; if (val != null) { if (val.Val) { retVal = EFSSystem.BoolType.False; } else { retVal = EFSSystem.BoolType.True; } } context.LocalScope.PopContext(token); return(retVal); }
/// <summary> /// Provides the value associated to this Expression /// </summary> /// <param name="context">The context on which the value must be found</param> /// <returns></returns> public override Values.IValue GetValue(InterpretationContext context) { Values.IValue retVal = null; Values.ListValue value = ListExpression.GetValue(context) as Values.ListValue; if (value != null) { int token = PrepareIteration(context); retVal = EFSSystem.BoolType.False; foreach (Values.IValue v in value.Val) { if (v != EFSSystem.EmptyValue) { IteratorVariable.Value = v; if (Condition != null) { Values.BoolValue b = Condition.GetValue(context) as Values.BoolValue; if (b != null && b.Val) { retVal = EFSSystem.BoolType.True; break; } } else { retVal = EFSSystem.BoolType.True; break; } } NextIteration(); } EndIteration(context, token); } return(retVal); }
/// <summary> /// Evaluates the boundaries associated to a specific preCondition /// </summary> /// <param name="context">The context used to evaluate the precondition and segment value</param> /// <param name="preCondition">The precondition to evaluate the range</param> /// <param name="parameter"></param> /// <returns></returns> private List <Graph.Segment> EvaluateBoundaries(Interpreter.InterpretationContext context, Rules.PreCondition preCondition, Parameter parameter) { List <Graph.Segment> retVal = new List <Graph.Segment>(); if (parameter != null) { Interpreter.BinaryExpression expression = preCondition.ExpressionTree as Interpreter.BinaryExpression; if (ExpressionBasedOnParameter(parameter, expression)) { Values.IValue val; if (expression.Right.Ref == parameter) { // Expression like xxx <= Parameter val = expression.Left.GetValue(context); switch (expression.Operation) { case Interpreter.BinaryExpression.OPERATOR.LESS: case Interpreter.BinaryExpression.OPERATOR.LESS_OR_EQUAL: retVal.Add(new Graph.Segment(getDoubleValue(val), double.MaxValue, new Graph.Segment.Curve())); break; case Interpreter.BinaryExpression.OPERATOR.GREATER: case Interpreter.BinaryExpression.OPERATOR.GREATER_OR_EQUAL: retVal.Add(new Graph.Segment(0, getDoubleValue(val), new Graph.Segment.Curve())); break; default: throw new Exception("Invalid comparison operator while evaluating Graph of function"); } } else { if (expression.Left.Ref == parameter) { // Expression like Parameter <= xxx val = expression.Right.GetValue(context); switch (expression.Operation) { case Interpreter.BinaryExpression.OPERATOR.LESS: case Interpreter.BinaryExpression.OPERATOR.LESS_OR_EQUAL: retVal.Add(new Graph.Segment(0, getDoubleValue(val), new Graph.Segment.Curve())); break; case Interpreter.BinaryExpression.OPERATOR.GREATER: case Interpreter.BinaryExpression.OPERATOR.GREATER_OR_EQUAL: retVal.Add(new Graph.Segment(getDoubleValue(val), double.MaxValue, new Graph.Segment.Curve())); break; default: throw new Exception("Invalid comparison operator while evaluating Graph of function"); } } else { if (FunctionCallOnParameter(expression.Right, parameter)) { Graph graph = expression.Right.createGraph(context, parameter); if (graph != null) { // Expression like xxx <= f(Parameter) val = expression.Left.GetValue(context); retVal = graph.GetSegments(Interpreter.BinaryExpression.Inverse(expression.Operation), getDoubleValue(val)); } else { AddError("Cannot create graph for " + expression.Right); } } else { Graph graph = expression.Left.createGraph(context, parameter); if (graph != null) { // Expression like f(Parameter) <= xxx val = expression.Right.GetValue(context); retVal = graph.GetSegments(expression.Operation, getDoubleValue(val)); } else { throw new Exception("Cannot evaluate bounds of segment"); } } } } } else { if (!ExpressionBasedOnPlaceHolder(context, expression)) { Values.BoolValue value = preCondition.ExpressionTree.GetValue(context) as Values.BoolValue; if (value != null && value.Val) { retVal.Add(new Graph.Segment(0, double.MaxValue, new Graph.Segment.Curve())); } } } } else { AddError("Parameter is null"); } return(retVal); }
/// <summary> /// Provides the value associated to this Term /// </summary> /// <param name="instance">The instance on which the value is computed</param> /// <param name="globalFind">Indicates that the search should be performed globally</param> /// <returns></returns> public override Values.IValue GetValue(InterpretationContext context) { Values.IValue retVal = null; ExplanationPart previous = SetupExplanation(); Values.IValue leftValue = null; Values.IValue rightValue = null; try { leftValue = Left.GetValue(context); if (leftValue != null) { switch (Operation) { case OPERATOR.EXP: case OPERATOR.MULT: case OPERATOR.ADD: case OPERATOR.SUB: case OPERATOR.DIV: { rightValue = Right.GetValue(context); if (rightValue != null) { retVal = leftValue.Type.PerformArithmericOperation(context, leftValue, Operation, rightValue); } else { AddError("Error while computing value for " + Right.ToString()); } } break; case OPERATOR.AND: { if (leftValue.Type == EFSSystem.BoolType) { Values.BoolValue lb = leftValue as Values.BoolValue; if (lb.Val) { rightValue = Right.GetValue(context); if (rightValue != null) { if (rightValue.Type == EFSSystem.BoolType) { retVal = rightValue as Values.BoolValue; } else { AddError("Cannot apply an operator " + Operation.ToString() + " on a variable of type " + rightValue.GetType()); } } else { AddError("Error while computing value for " + Right.ToString()); } } else { retVal = lb; } } else { AddError("Cannot apply an operator " + Operation.ToString() + " on a variable of type " + leftValue.GetType()); } } break; case OPERATOR.OR: { if (leftValue.Type == EFSSystem.BoolType) { Values.BoolValue lb = leftValue as Values.BoolValue; if (!lb.Val) { rightValue = Right.GetValue(context); if (rightValue != null) { if (rightValue.Type == EFSSystem.BoolType) { retVal = rightValue as Values.BoolValue; } else { AddError("Cannot apply an operator " + Operation.ToString() + " on a variable of type " + rightValue.GetType()); } } else { AddError("Error while computing value for " + Right.ToString()); } } else { retVal = lb; } } else { AddError("Cannot apply an operator " + Operation.ToString() + " on a variable of type " + leftValue.GetType()); } } break; case OPERATOR.LESS: { rightValue = Right.GetValue(context); if (rightValue != null) { retVal = EFSSystem.GetBoolean(leftValue.Type.Less(leftValue, rightValue)); } else { AddError("Error while computing value for " + Right.ToString()); } } break; case OPERATOR.LESS_OR_EQUAL: { rightValue = Right.GetValue(context); if (rightValue != null) { retVal = EFSSystem.GetBoolean(leftValue.Type.CompareForEquality(leftValue, rightValue) || leftValue.Type.Less(leftValue, rightValue)); } else { AddError("Error while computing value for " + Right.ToString()); } } break; case OPERATOR.GREATER: { rightValue = Right.GetValue(context); if (rightValue != null) { retVal = EFSSystem.GetBoolean(leftValue.Type.Greater(leftValue, rightValue)); } else { AddError("Error while computing value for " + Right.ToString()); } } break; case OPERATOR.GREATER_OR_EQUAL: { rightValue = Right.GetValue(context); if (rightValue != null) { retVal = EFSSystem.GetBoolean(leftValue.Type.CompareForEquality(leftValue, rightValue) || leftValue.Type.Greater(leftValue, rightValue)); } else { AddError("Error while computing value for " + Right.ToString()); } } break; case OPERATOR.EQUAL: { rightValue = Right.GetValue(context); if (rightValue != null) { retVal = EFSSystem.GetBoolean(leftValue.Type.CompareForEquality(leftValue, rightValue)); } else { AddError("Error while computing value for " + Right.ToString()); } } break; case OPERATOR.NOT_EQUAL: { rightValue = Right.GetValue(context); if (rightValue != null) { retVal = EFSSystem.GetBoolean(!leftValue.Type.CompareForEquality(leftValue, rightValue)); } else { AddError("Error while computing value for " + Right.ToString()); } } break; case OPERATOR.IN: { rightValue = Right.GetValue(context); if (rightValue != null) { retVal = EFSSystem.GetBoolean(rightValue.Type.Contains(rightValue, leftValue)); } else { AddError("Error while computing value for " + Right.ToString()); } } break; case OPERATOR.NOT_IN: { rightValue = Right.GetValue(context); if (rightValue != null) { retVal = EFSSystem.GetBoolean(!rightValue.Type.Contains(rightValue, leftValue)); } else { AddError("Error while computing value for " + Right.ToString()); } } break; } } else { AddError("Error while computing value for " + Left.ToString()); } } catch (Exception e) { AddError(e.Message); } if (explain) { CompleteExplanation(previous, Name + " = " + explainNamable(retVal)); } return(retVal); }