Example #1
0
        /// <summary>
        ///     Provides the surface of this function if it has been statically defined
        /// </summary>
        /// <param name="context">the context used to create the surface</param>
        /// <param name="explain"></param>
        /// <returns></returns>
        public override Surface CreateSurface(InterpretationContext context, ExplanationPart explain)
        {
            Surface retVal = null;

            Surface defaultSurface = createSurfaceForValue(context, context.FindOnStack(DefaultFunction).Value, explain);

            if (defaultSurface != null)
            {
                Surface overrideSurface = createSurfaceForValue(context, context.FindOnStack(OverrideFunction).Value,
                                                                explain);
                if (overrideSurface != null)
                {
                    retVal = defaultSurface.Override(overrideSurface);
                }
                else
                {
                    OverrideFunction.AddError("Cannot create graph for OVERRIDE argument");
                }
            }
            else
            {
                DefaultFunction.AddError("Cannot create graph for DEFAULT argument");
            }

            return(retVal);
        }
Example #2
0
        /// <summary>
        ///     Provides the graph of this function if it has been statically defined
        /// </summary>
        /// <param name="context">the context used to create the graph</param>
        /// <param name="parameter"></param>
        /// <param name="explain"></param>
        /// <returns></returns>
        public override Graph CreateGraph(InterpretationContext context, Parameter parameter, ExplanationPart explain)
        {
            Graph retVal = null;

            Graph graph = createGraphForValue(context, context.FindOnStack(Function).Value, explain, parameter);

            if (graph != null)
            {
                double speed     = GetDoubleValue(context.FindOnStack(Speed).Value);
                double solutionX = graph.SolutionX(speed);
                if (solutionX == double.MaxValue)
                {
                    // No value found, return Unknown
                    Range     distanceType    = (Range)EFSSystem.FindByFullName("Default.BaseTypes.Distance");
                    EnumValue unknownDistance = distanceType.findEnumValue("Unknown");
                    retVal = Graph.createGraph(distanceType.getValueAsDouble(unknownDistance));
                }
                else
                {
                    // Create the graph for this solution
                    retVal = Graph.createGraph(solutionX);
                }
            }
            else
            {
                Function.AddError("Cannot create graph for " + Function);
            }

            return(retVal);
        }
Example #3
0
        /// <summary>
        ///     Provides the graph of this function if it has been statically defined
        /// </summary>
        /// <param name="context">the context used to create the graph</param>
        /// <param name="parameter"></param>
        /// <param name="explain"></param>
        /// <returns></returns>
        public override Graph CreateGraph(InterpretationContext context, Parameter parameter, ExplanationPart explain)
        {
            Graph retVal = null;

            Graph firstGraph = createGraphForValue(context, context.FindOnStack(First).Value, explain);

            if (firstGraph != null)
            {
                Graph secondGraph = createGraphForValue(context, context.FindOnStack(Second).Value, explain);
                if (secondGraph != null)
                {
                    retVal = firstGraph.Min(secondGraph) as Graph;
                }
                else
                {
                    Second.AddError("Cannot create graph for " + Second);
                }
            }
            else
            {
                First.AddError("Cannot create graph for " + First);
            }

            return(retVal);
        }
Example #4
0
        /// <summary>
        ///     Provides the value of the function
        /// </summary>
        /// <param name="context"></param>
        /// <param name="actuals">the actual parameters values</param>
        /// <param name="explain"></param>
        /// <returns>The value for the function application</returns>
        public override IValue Evaluate(InterpretationContext context, Dictionary <Actual, IValue> actuals,
                                        ExplanationPart explain)
        {
            IValue retVal = null;

            int token = context.LocalScope.PushContext();

            AssignParameters(context, actuals);

            StructureValue startDate = context.FindOnStack(StartDate).Value as StructureValue;

            if (startDate != null)
            {
                int year   = GetIntValue(startDate, "Year");
                int month  = GetIntValue(startDate, "Month");
                int day    = GetIntValue(startDate, "Day");
                int hour   = GetIntValue(startDate, "Hour");
                int minute = GetIntValue(startDate, "Minute");
                int second = GetIntValue(startDate, "Second");
                int tts    = GetIntValue(startDate, "TTS");

                DoubleValue addedTime = context.FindOnStack(Increment).Value as DoubleValue;
                if (addedTime != null)
                {
                    DateTime start       = new DateTime(year, month, day, hour, minute, second, tts);
                    DateTime currentTime = start.AddSeconds(addedTime.Val);

                    retVal = GetEfsDate(currentTime, startDate.Type as Structure);
                }
            }

            context.LocalScope.PopContext(token);

            return(retVal);
        }
        /// <summary>
        ///     Provides the value of the function
        /// </summary>
        /// <param name="context"></param>
        /// <param name="actuals">the actual parameters values</param>
        /// <param name="explain"></param>
        /// <returns>The value for the function application</returns>
        public override IValue Evaluate(InterpretationContext context, Dictionary <Actual, IValue> actuals,
                                        ExplanationPart explain)
        {
            IValue retVal = null;

            int token = context.LocalScope.PushContext();

            AssignParameters(context, actuals);

            DoubleValue value    = context.FindOnStack(Value).Value as DoubleValue;
            DoubleValue multiple = context.FindOnStack(Multiple).Value as DoubleValue;

            if (value != null && multiple != null)
            {
                double res = Math.Floor(value.Val);
                while (res > 0 && res % multiple.Val != 0)
                {
                    res--;
                }
                retVal = new DoubleValue(ReturnType, res);
            }

            context.LocalScope.PopContext(token);

            return(retVal);
        }
Example #6
0
        /// <summary>
        ///     Provides the surface of this function if it has been statically defined
        /// </summary>
        /// <param name="context">the context used to create the surface</param>
        /// <param name="explain"></param>
        /// <returns></returns>
        public override Surface CreateSurface(InterpretationContext context, ExplanationPart explain)
        {
            Surface retVal = null;

            IValue  firstValue   = context.FindOnStack(First).Value;
            IValue  secondValue  = context.FindOnStack(Second).Value;
            Surface firstSurface = createSurfaceForValue(context, firstValue, explain);

            if (firstSurface != null)
            {
                Surface secondSurface = createSurfaceForValue(context, secondValue, explain);
                if (secondSurface != null)
                {
                    retVal = firstSurface.Min(secondSurface);
                }
                else
                {
                    Second.AddError("Cannot create surface for " + Second);
                }
            }
            else
            {
                First.AddError("Cannot create surface for " + First);
            }

            return(retVal);
        }
Example #7
0
        /// <summary>
        ///     Provides the graph of this function if it has been statically defined
        /// </summary>
        /// <param name="context">the context used to create the graph</param>
        /// <param name="parameter"></param>
        /// <param name="explain"></param>
        /// <returns></returns>
        public override Graph CreateGraph(InterpretationContext context, Parameter parameter, ExplanationPart explain)
        {
            Graph retVal = null;

            Graph graph = null;

            Function function = context.FindOnStack(Function).Value as Function;

            if (function != null)
            {
                int       token = context.LocalScope.PushContext();
                Parameter p     = (Parameter)function.FormalParameters[0];
                context.LocalScope.SetGraphParameter(p);
                graph = createGraphForValue(context, function, explain, p);
                context.LocalScope.PopContext(token);
            }

            if (graph != null)
            {
                Function increment = context.FindOnStack(Increment).Value as Function;
                retVal = graph.AddIncrement(context, increment, explain);
            }
            else
            {
                Function.AddError("Cannot create graph for " + Function);
            }

            return(retVal);
        }
Example #8
0
        /// <summary>
        ///     Provides the value of the function
        /// </summary>
        /// <param name="context"></param>
        /// <param name="actuals">the actual parameters values</param>
        /// <param name="explain"></param>
        /// <returns>The value for the function application</returns>
        public override IValue Evaluate(InterpretationContext context, Dictionary <Actual, IValue> actuals,
                                        ExplanationPart explain)
        {
            IValue retVal = null;

            AssignParameters(context, actuals);
            Graph graph = createGraphForValue(context, context.FindOnStack(FunctionA).Value, explain);

            if (graph != null)
            {
                foreach (Graph.Segment segment in graph.Segments)
                {
                    if (segment.Expression.A == 0.0)
                    {
                        double speed = segment.Expression.V0;

                        Function function = context.FindOnStack(FunctionB).Value as Function;
                        if (function.FormalParameters.Count > 0)
                        {
                            Parameter functionParameter = (Parameter)function.FormalParameters[0];
                            Actual    actual            = functionParameter.CreateActual();
                            actual.Value = new DoubleValue(EFSSystem.DoubleType, speed);
                            Dictionary <Actual, IValue> values = new Dictionary <Actual, IValue>();
                            values[actual] = new DoubleValue(EFSSystem.DoubleType, speed);
                            IValue solution    = function.Evaluate(context, values, explain);
                            double doubleValue = GetDoubleValue(solution);

                            if (doubleValue >= segment.Start && doubleValue <= segment.End)
                            {
                                retVal = new DoubleValue(EFSSystem.DoubleType, doubleValue);
                                break;
                            }
                        }
                        else
                        {
                            FunctionB.AddError("The FunctionB doesn't have any parameter");
                            break;
                        }
                    }
                    else
                    {
                        FunctionA.AddError("The FunctionA is not a step function");
                        break;
                    }
                }
            }
            else
            {
                FunctionA.AddError("Cannot create graph for " + FunctionA);
            }

            if (retVal == null)
            {
                FunctionA.AddError("Cannot compute the intersection of " + FunctionA + " and " + FunctionB);
                FunctionB.AddError("Cannot compute the intersection of " + FunctionA + " and " + FunctionB);
            }

            return(retVal);
        }
Example #9
0
        /// <summary>
        ///     Provides the value of the function
        /// </summary>
        /// <param name="context"></param>
        /// <param name="actuals">the actual parameters values</param>
        /// <param name="explain"></param>
        /// <returns>The value for the function application</returns>
        public override IValue Evaluate(InterpretationContext context, Dictionary <Actual, IValue> actuals,
                                        ExplanationPart explain)
        {
            IValue retVal = EFSSystem.BoolType.False;

            int token = context.LocalScope.PushContext();

            AssignParameters(context, actuals);

            ListValue collection = context.FindOnStack(Collection).Value as ListValue;

            if (collection != null)
            {
                IValue expectedFirst = context.FindOnStack(ExpectedFirst).Value;
                if (expectedFirst != null)
                {
                    int firstIndex = collection.Val.IndexOf(expectedFirst);
                    if (firstIndex >= 0)
                    {
                        IValue expectedSecond = context.FindOnStack(ExpectedSecond).Value;
                        if (expectedSecond != null)
                        {
                            int secondIndex = collection.Val.IndexOf(expectedSecond);

                            if (secondIndex >= 0)
                            {
                                if (firstIndex < secondIndex)
                                {
                                    retVal = EFSSystem.BoolType.True;
                                }
                            }
                            else
                            {
                                Collection.AddError("Cannot find " + expectedSecond.FullName + " in " +
                                                    collection.ToString() + " to evaluate " + Name);
                            }
                        }
                        else
                        {
                            Collection.AddError("Cannot evaluate second element to evaluate " + Name);
                        }
                    }
                    else
                    {
                        Collection.AddError("Cannot find " + expectedFirst.FullName + " in " + collection.ToString() +
                                            " to evaluate " + Name);
                    }
                }
                else
                {
                    Collection.AddError("Cannot evaluate first element to evaluate " + Name);
                }
            }
            context.LocalScope.PopContext(token);

            return(retVal);
        }
Example #10
0
        /// <summary>
        ///     Provides the value of the function
        /// </summary>
        /// <param name="context"></param>
        /// <param name="actuals">the actual parameters values</param>
        /// <param name="explain"></param>
        /// <returns>The value for the function application</returns>
        public override IValue Evaluate(InterpretationContext context, Dictionary <Actual, IValue> actuals,
                                        ExplanationPart explain)
        {
            IValue retVal = null;

            int token = context.LocalScope.PushContext();

            AssignParameters(context, actuals);

            Collection collectionType =
                (Collection)
                EFSSystem.FindType(
                    OverallNameSpaceFinder.INSTANCE.findByName(EFSSystem.Dictionaries[0],
                                                               "Default"),
                    "TargetsCol");
            ListValue collection = new ListValue(collectionType, new List <IValue>());

            Function function = context.FindOnStack(Targets).Value as Function;

            if (function != null && !function.Name.Equals("EMPTY"))
            {
                Graph graph1 = createGraphForValue(context, function, explain);
                ComputeTargets(graph1.Function, collection);
            }

            context.LocalScope.PopContext(token);

            retVal = collection;
            return(retVal);
        }
Example #11
0
        /// <summary>
        ///     Provides the value of the function
        /// </summary>
        /// <param name="context"></param>
        /// <param name="actuals">the actual parameters values</param>
        /// <param name="explain"></param>
        /// <returns>The value for the function application</returns>
        public override IValue Evaluate(InterpretationContext context, Dictionary <Actual, IValue> actuals,
                                        ExplanationPart explain)
        {
            IValue retVal = null;

            int token = context.LocalScope.PushContext();

            AssignParameters(context, actuals);
            BoolValue val = context.FindOnStack(Value).Value as BoolValue;

            if (val != null)
            {
                if (val.Val)
                {
                    retVal = EFSSystem.BoolType.False;
                }
                else
                {
                    retVal = EFSSystem.BoolType.True;
                }
            }
            context.LocalScope.PopContext(token);

            return(retVal);
        }
Example #12
0
        /// <summary>
        /// Provides the value of the function
        /// </summary>
        /// <param name="context"></param>
        /// <param name="actuals">the actual parameters values</param>
        /// <param name="explain"></param>
        /// <returns>The value for the function application</returns>
        public override IValue Evaluate(InterpretationContext context, Dictionary <Actual, IValue> actuals,
                                        ExplanationPart explain)
        {
            ListValue retVal = null;

            int token = context.LocalScope.PushContext();

            AssignParameters(context, actuals);

            StringValue value = context.FindOnStack(Value).Value as StringValue;

            if (value != null)
            {
                retVal = new ListValue(EfsSystem.Instance.StringCollection, new List <IValue>());

                foreach (char c in value.Val)
                {
                    StringValue character = new StringValue(EfsSystem.Instance.StringType, c.ToString(CultureInfo.InvariantCulture));
                    retVal.Val.Add(character);
                }
            }

            context.LocalScope.PopContext(token);

            return(retVal);
        }
Example #13
0
        /// <summary>
        ///     Provides the value of the function
        /// </summary>
        /// <param name="context"></param>
        /// <param name="actuals">the actual parameters values</param>
        /// <param name="explain"></param>
        /// <returns>The value for the function application</returns>
        public override IValue Evaluate(InterpretationContext context, Dictionary <Actual, IValue> actuals,
                                        ExplanationPart explain)
        {
            IValue retVal = null;

            int token = context.LocalScope.PushContext();

            AssignParameters(context, actuals);

            ListValue value = context.FindOnStack(Collection) as ListValue;

            if (value != null)
            {
                Collection collectionType = value.Type as Collection;
                if (collectionType != null && collectionType.Type != null)
                {
                    Type elementType = collectionType.Type;

                    if (value.Val.Count >= collectionType.getMaxSize())
                    {
                        AddError("Cannot allocate element in list : list full");
                    }
                    else
                    {
                        retVal = elementType.DefaultValue;
                        value.Val.Add(retVal);
                    }
                }
            }
            context.LocalScope.PopContext(token);

            return(retVal);
        }
Example #14
0
        /// <summary>
        ///     Provides the value of the function
        /// </summary>
        /// <param name="context"></param>
        /// <param name="actuals">the actual parameters values</param>
        /// <param name="explain"></param>
        /// <returns>The value for the function application</returns>
        public override IValue Evaluate(InterpretationContext context, Dictionary <Actual, IValue> actuals,
                                        ExplanationPart explain)
        {
            IValue retVal = null;

            int token = context.LocalScope.PushContext();

            AssignParameters(context, actuals);
            Function function = context.FindOnStack(Function).Value as Function;

            if (function != null)
            {
                double speed = GetDoubleValue(context.FindOnStack(Speed).Value);

                Parameter parameter = (Parameter)function.FormalParameters[0];
                int       token2    = context.LocalScope.PushContext();
                context.LocalScope.SetGraphParameter(parameter);
                Graph graph = function.CreateGraph(context, (Parameter)function.FormalParameters[0], explain);
                context.LocalScope.PopContext(token2);
                if (graph != null)
                {
                    double solutionX = graph.SolutionX(speed);
                    if (solutionX == double.MaxValue)
                    {
                        Range distanceType = (Range)EFSSystem.FindByFullName("Default.BaseTypes.Distance");
                        retVal = distanceType.findEnumValue("Unknown");
                    }
                    else
                    {
                        retVal = new DoubleValue(EFSSystem.DoubleType, solutionX);
                    }
                }
                else
                {
                    Function.AddError("Cannot evaluate graph for function while computing the distance for the given speed");
                }
            }
            else
            {
                Function.AddError("Cannot get function for " + Function);
            }
            context.LocalScope.PopContext(token);

            return(retVal);
        }
Example #15
0
        /// <summary>
        /// Provides the value of the function
        /// </summary>
        /// <param name="context"></param>
        /// <param name="actuals">the actual parameters values</param>
        /// <param name="explain"></param>
        /// <returns>The value for the function application</returns>
        public override IValue Evaluate(InterpretationContext context, Dictionary <Actual, IValue> actuals,
                                        ExplanationPart explain)
        {
            StringValue retVal = null;

            int token = context.LocalScope.PushContext();

            AssignParameters(context, actuals);

            StringValue string1 = context.FindOnStack(String1).Value as StringValue;
            StringValue string2 = context.FindOnStack(String2).Value as StringValue;

            if (string1 != null && string2 != null)
            {
                retVal = new StringValue(EfsSystem.Instance.StringType, string1.Val + string2.Val);
            }

            context.LocalScope.PopContext(token);

            return(retVal);
        }
Example #16
0
        /// <summary>
        ///     Provides the value of the function.
        ///     The function returns true if the string passes the check.
        /// </summary>
        /// <param name="context"></param>
        /// <param name="actuals">the actual parameters values</param>
        /// <param name="explain"></param>
        /// <returns>The value for the function application</returns>
        public override IValue Evaluate(InterpretationContext context, Dictionary <Actual, IValue> actuals,
                                        ExplanationPart explain)
        {
            IValue retVal = EFSSystem.BoolType.False;

            int token = context.LocalScope.PushContext();

            AssignParameters(context, actuals);

            StringValue number = context.FindOnStack(Number).Value as StringValue;

            if (number != null && number.Val != "")
            {
                string       textValue = number.Val;
                NumberStyles style     = NumberStyles.Number;
                if (textValue.StartsWith("0x", StringComparison.CurrentCultureIgnoreCase))
                {
                    textValue = textValue.Substring(2);
                    style     = NumberStyles.HexNumber;
                }
                if (textValue.EndsWith(" h", StringComparison.CurrentCultureIgnoreCase))
                {
                    textValue = textValue.Substring(0, textValue.Length - 2);
                    style     = NumberStyles.HexNumber;
                }
                while (textValue[textValue.Length - 1] == 'F')
                {
                    textValue = textValue.Substring(0, textValue.Length - 1);
                    style     = NumberStyles.HexNumber;
                }

                if (style == NumberStyles.HexNumber)
                {
                    ulong val;
                    if (ulong.TryParse(textValue, style, CultureInfo.InvariantCulture, out val))
                    {
                        retVal = EFSSystem.BoolType.True;
                    }
                }
                else
                {
                    Decimal val;
                    if (Decimal.TryParse(textValue, style, CultureInfo.InvariantCulture, out val))
                    {
                        retVal = EFSSystem.BoolType.True;
                    }
                }
            }
            context.LocalScope.PopContext(token);

            return(retVal);
        }
Example #17
0
        /// <summary>
        ///     Provides the graph of this function if it has been statically defined
        /// </summary>
        /// <param name="context">the context used to create the graph</param>
        /// <param name="parameter"></param>
        /// <param name="explain"></param>
        /// <returns></returns>
        public override Graph CreateGraph(InterpretationContext context, Parameter parameter, ExplanationPart explain)
        {
            Graph retVal = null;

            IVariable variable = context.FindOnStack(Value);

            if (variable != null)
            {
                retVal = Graph.createGraph(GetDoubleValue(variable.Value), parameter);
            }
            else
            {
                AddError("Cannot find variable " + Value + " on the stack");
            }

            return(retVal);
        }
Example #18
0
        /// <summary>
        ///     Provides the value of the function
        /// </summary>
        /// <param name="context"></param>
        /// <param name="actuals">the actual parameters values</param>
        /// <param name="explain"></param>
        /// <returns>The value for the function application</returns>
        public override IValue Evaluate(InterpretationContext context, Dictionary <Actual, IValue> actuals,
                                        ExplanationPart explain)
        {
            IValue retVal = EFSSystem.BoolType.False;

            int token = context.LocalScope.PushContext();

            AssignParameters(context, actuals);

            if (context.FindOnStack(Element).Value != EFSSystem.EmptyValue)
            {
                retVal = EFSSystem.BoolType.True;
            }

            context.LocalScope.PopContext(token);

            return(retVal);
        }
        /// <summary>
        ///     Provides the value of the function
        /// </summary>
        /// <param name="context"></param>
        /// <param name="actuals">the actual parameters values</param>
        /// <param name="explain"></param>
        /// <returns>The value for the function application</returns>
        public override IValue Evaluate(InterpretationContext context, Dictionary <Actual, IValue> actuals,
                                        ExplanationPart explain)
        {
            IValue retVal = null;

            int token = context.LocalScope.PushContext();

            AssignParameters(context, actuals);

            DoubleValue value = context.FindOnStack(Value).Value as DoubleValue;

            if (value != null)
            {
                int res = (int)Math.Round(value.Val);
                retVal = new IntValue(ReturnType, res);
            }

            context.LocalScope.PopContext(token);

            return(retVal);
        }
Example #20
0
        /// <summary>
        ///     Provides the value of the function
        /// </summary>
        /// <param name="context"></param>
        /// <param name="actuals">the actual parameters values</param>
        /// <param name="explain"></param>
        /// <returns>The value for the function application</returns>
        public override IValue Evaluate(InterpretationContext context, Dictionary <Actual, IValue> actuals,
                                        ExplanationPart explain)
        {
            IValue retVal = null;

            int token = context.LocalScope.PushContext();

            AssignParameters(context, actuals);
            IValue value = context.FindOnStack(Value).Value;

            if (value is Function)
            {
                retVal = value;
            }
            else
            {
                retVal = TargetType.convert(value);
            }
            context.LocalScope.PopContext(token);

            return(retVal);
        }
Example #21
0
        /// <summary>
        ///     Provides the graph of this function if it has been statically defined
        /// </summary>
        /// <param name="context">the context used to create the graph</param>
        /// <param name="parameter"></param>
        /// <param name="explain"></param>
        /// <returns></returns>
        public override Graph CreateGraph(InterpretationContext context, Parameter parameter, ExplanationPart explain)
        {
            Graph retVal = null;

            Graph    mrspGraph        = null;
            Function speedRestriction = context.FindOnStack(SpeedRestrictions).Value as Function;

            if (speedRestriction != null)
            {
                Parameter p = (Parameter)speedRestriction.FormalParameters[0];

                int token = context.LocalScope.PushContext();
                context.LocalScope.SetGraphParameter(p);
                mrspGraph = createGraphForValue(context, context.FindOnStack(SpeedRestrictions).Value, explain, p);
                context.LocalScope.PopContext(token);
            }

            if (mrspGraph != null)
            {
                Function deceleratorFactor = context.FindOnStack(DecelerationFactor).Value as Function;
                if (deceleratorFactor != null)
                {
                    Surface decelerationSurface = deceleratorFactor.CreateSurface(context, explain);
                    if (decelerationSurface != null)
                    {
                        FlatSpeedDistanceCurve           mrspCurve           = mrspGraph.FlatSpeedDistanceCurve(mrspGraph.ExpectedEndX());
                        AccelerationSpeedDistanceSurface accelerationSurface =
                            decelerationSurface.createAccelerationSpeedDistanceSurface(double.MaxValue, double.MaxValue);
                        QuadraticSpeedDistanceCurve brakingCurve = null;
                        try
                        {
                            brakingCurve = EtcsBrakingCurveBuilder.Build_A_Safe_Backward(accelerationSurface, mrspCurve);
                        }
                        catch (Exception)
                        {
                            retVal = new Graph();
                            retVal.AddSegment(new Graph.Segment(0, double.MaxValue, new Graph.Segment.Curve(0, 0, 0)));
                        }

                        if (brakingCurve != null)
                        {
                            retVal = new Graph();

                            // TODO : Remove the distinction between linear curves and quadratic curves
                            bool isLinear = true;
                            for (int i = 0; i < brakingCurve.SegmentCount; i++)
                            {
                                QuadraticCurveSegment segment = brakingCurve[i];
                                if (segment.A.ToUnits() != 0.0 || segment.V0.ToUnits() != 0.0)
                                {
                                    isLinear = false;
                                    break;
                                }
                            }

                            for (int i = 0; i < brakingCurve.SegmentCount; i++)
                            {
                                QuadraticCurveSegment segment = brakingCurve[i];

                                Graph.Segment newSegment;
                                if (isLinear)
                                {
                                    newSegment = new Graph.Segment(
                                        segment.X.X0.ToUnits(),
                                        segment.X.X1.ToUnits(),
                                        new Graph.Segment.Curve(0.0,
                                                                segment.V0.ToSubUnits(SiSpeed_SubUnits.KiloMeter_per_Hour), 0.0));
                                }
                                else
                                {
                                    newSegment = new Graph.Segment(
                                        segment.X.X0.ToUnits(),
                                        segment.X.X1.ToUnits(),
                                        new Graph.Segment.Curve(
                                            segment.A.ToSubUnits(SiAcceleration_SubUnits.Meter_per_SecondSquare),
                                            segment.V0.ToSubUnits(SiSpeed_SubUnits.KiloMeter_per_Hour),
                                            segment.D0.ToSubUnits(SiDistance_SubUnits.Meter)
                                            )
                                        );
                                }
                                retVal.AddSegment(newSegment);
                            }
                        }
                    }
                    else
                    {
                        DecelerationFactor.AddError("Cannot create surface for " + DecelerationFactor);
                    }
                }
                else
                {
                    DecelerationFactor.AddError("Cannot evaluate " + DecelerationFactor + " as a function");
                }
            }
            else
            {
                SpeedRestrictions.AddError("Cannot create graph for " + SpeedRestrictions);
            }

            return(retVal);
        }
        /// <summary>
        ///     Creates a graph for the function
        /// </summary>
        /// <param name="context"></param>
        /// <param name="parameter"></param>
        /// <param name="explain"></param>
        /// <returns></returns>
        public override Graph CreateGraph(InterpretationContext context, Parameter parameter, ExplanationPart explain)
        {
            Graph retVal = new Graph();

            StructureValue LocationStruct = context.FindOnStack(Target).Value as StructureValue;

            SiDistance location;
            SiSpeed    speed;

            if (LocationStruct != null)
            {
                IVariable locationField = LocationStruct.Val["Location"] as IVariable;
                location = new SiDistance((locationField.Value as DoubleValue).Val);
                IVariable speedField = LocationStruct.Val["Speed"] as IVariable;
                speed = new SiSpeed((speedField.Value as DoubleValue).Val, SiSpeed_SubUnits.KiloMeter_per_Hour);

                Function decelerationFactor = context.FindOnStack(DecelerationFactor).Value as Function;
                if (decelerationFactor != null)
                {
                    Surface DecelerationSurface = decelerationFactor.CreateSurface(context, explain);
                    if (DecelerationSurface != null)
                    {
                        AccelerationSpeedDistanceSurface accelerationSurface =
                            DecelerationSurface.createAccelerationSpeedDistanceSurface(double.MaxValue, double.MaxValue);

                        QuadraticSpeedDistanceCurve BrakingCurve = null;

                        try
                        {
                            BrakingCurve = EtcsBrakingCurveBuilder.Build_Deceleration_Curve(accelerationSurface, speed,
                                                                                            location);
                        }
                        catch (Exception e)
                        {
                            retVal.AddSegment(new Graph.Segment(0, double.MaxValue, new Graph.Segment.Curve(0, 0, 0)));
                        }

                        SiSpeed finalSpeed = new SiSpeed(GetDoubleValue(context.FindOnStack(EndSpeed).Value),
                                                         SiSpeed_SubUnits.KiloMeter_per_Hour);
                        for (int i = 0; i < BrakingCurve.SegmentCount; i++)
                        {
                            QuadraticCurveSegment segment = BrakingCurve[i];

                            SiSpeed endSpeed = Max(finalSpeed, segment.Get(segment.X.X1));

                            SiDistance endDistance;
                            if (endSpeed == finalSpeed)
                            {
                                // Ensures that a braking curve is calculated until the finalSpeed
                                // but not further than the end of the curve segment
                                SiSpeed tmp = Max(segment.Get(segment.X.X1),
                                                  endSpeed - new SiSpeed(0.001));
                                endDistance = segment.IntersectAt(tmp);
                            }
                            else
                            {
                                endDistance = segment.X.X1;
                            }

                            Graph.Segment newSegment = new Graph.Segment(
                                segment.X.X0.ToUnits(),
                                endDistance.ToUnits(),
                                new Graph.Segment.Curve(
                                    segment.A.ToSubUnits(SiAcceleration_SubUnits.Meter_per_SecondSquare),
                                    segment.V0.ToSubUnits(SiSpeed_SubUnits.KiloMeter_per_Hour),
                                    segment.D0.ToSubUnits(SiDistance_SubUnits.Meter)
                                    )
                                );
                            retVal.AddSegment(newSegment);

                            if (endSpeed == finalSpeed)
                            {
                                break;
                            }
                        }
                    }
                }
            }
            return(retVal);
        }