Пример #1
0
        /// <summary>
        ///     Applies a rule defined in a procedure
        /// </summary>
        /// <param name="rule"></param>
        /// <param name="changes"></param>
        /// <param name="ctxt"></param>
        /// <param name="explanation"></param>
        /// <param name="runner"></param>
        private void ApplyRule(Rule rule, ChangeList changes, InterpretationContext ctxt, ExplanationPart explanation,
                               Runner runner)
        {
            foreach (RuleCondition condition in rule.RuleConditions)
            {
                ExplanationPart conditionExplanation = ExplanationPart.CreateSubExplanation(explanation, condition);

                if (condition.EvaluatePreConditions(ctxt, conditionExplanation, runner))
                {
                    ExplanationPart.SetNamable(conditionExplanation, EfsSystem.Instance.BoolType.True);
                    foreach (Action action in condition.Actions)
                    {
                        action.GetChanges(ctxt, changes, conditionExplanation, true, runner);
                    }

                    foreach (Rule subRule in condition.SubRules)
                    {
                        ApplyRule(subRule, changes, ctxt, conditionExplanation, runner);
                    }
                    break;
                }
                else
                {
                    ExplanationPart.SetNamable(conditionExplanation, EfsSystem.Instance.BoolType.False);
                }
            }
        }
Пример #2
0
        /// <summary>
        ///     Provides the actual value for the preconditions
        /// </summary>
        /// <param name="context">The context on which the precondition must be evaluated</param>
        /// <param name="explanation">The explanation part to fill, if any</param>
        /// <param name="log">indicates that this should be logged</param>
        /// <param name="runner"></param>
        /// <returns></returns>
        public bool EvaluatePreConditions(InterpretationContext context, ExplanationPart explanation, Runner runner)
        {
            bool retVal = true;

            foreach (PreCondition preCondition in PreConditions)
            {
                try
                {
                    ExplanationPart subExplanation = ExplanationPart.CreateNamedSubExplanation(explanation,
                                                                                               "PreCondition ", preCondition);
                    BoolValue value = preCondition.Expression.GetExpressionValue(context, subExplanation) as BoolValue;
                    ExplanationPart.SetNamable(subExplanation, value);
                    if (value != null)
                    {
                        retVal = retVal && value.Val;
                    }
                    else
                    {
                        retVal = false;
                        // TODO : Handle Error
                    }

                    if (!retVal)
                    {
                        break;
                    }
                }
                catch (Exception e)
                {
                    preCondition.Expression.AddErrorAndExplain(e.Message, explanation);
                    retVal = false;
                    break;
                }
            }

            return(retVal);
        }
Пример #3
0
        /// <summary>
        ///     Provides the changes performed by this statement
        /// </summary>
        /// <param name="context">The context on which the changes should be computed</param>
        /// <param name="changes">The list to fill with the changes</param>
        /// <param name="explanation">The explanatino to fill, if any</param>
        /// <param name="apply">Indicates that the changes should be applied immediately</param>
        /// <param name="runner"></param>
        public override void GetChanges(InterpretationContext context, ChangeList changes, ExplanationPart explanation,
                                        bool apply, Runner runner)
        {
            if (Call != null)
            {
                // Explain what happens in this statement
                explanation = ExplanationPart.CreateSubExplanation(explanation, this);

                InterpretationContext ctxt      = GetContext(context, explanation);
                Procedure             procedure = Call.GetProcedure(ctxt, explanation);
                if (procedure != null)
                {
                    ctxt.HasSideEffects = true;

                    // If the procedure has been defined in a structure,
                    // ensure that it is applied to an instance of that structure
                    Structure structure = procedure.Enclosing as Structure;
                    if (structure != null)
                    {
                        ITypedElement current = ctxt.Instance as ITypedElement;
                        while (current != null)
                        {
                            if (current.Type != structure)
                            {
                                current = current.Enclosing as ITypedElement;
                            }
                            else
                            {
                                ctxt.Instance = current;
                                ExplanationPart.CreateSubExplanation(explanation, "Instance", current);

                                current = null;
                            }
                        }
                    }

                    ExplanationPart part = ExplanationPart.CreateSubExplanation(explanation, procedure);
                    if (ctxt.Instance is IVariable)
                    {
                        ExplanationPart.SetNamable(part, ctxt.Instance);
                        ExplanationPart instanceExplanation = ExplanationPart.CreateSubExplanation(part, "instance = ");
                        ExplanationPart.SetNamable(instanceExplanation, ctxt.Instance);
                    }

                    int token = ctxt.LocalScope.PushContext();
                    foreach (
                        KeyValuePair <Actual, IValue> pair in Call.AssignParameterValues(context, procedure, true, part))
                    {
                        ctxt.LocalScope.SetVariable(pair.Key, pair.Value);
                    }

                    foreach (Rule rule in procedure.Rules)
                    {
                        ApplyRule(rule, changes, ctxt, part, runner);
                    }

                    ctxt.LocalScope.PopContext(token);
                }
                else
                {
                    AddError("Cannot determine the called procedure for " + ToString(), RuleChecksEnum.ExecutionFailed);
                }
            }
            else
            {
                AddError("Expression " + ToString() + " is not a valid procedure call", RuleChecksEnum.ExecutionFailed);
            }
        }