Beispiel #1
0
        public virtual object                          Execute(ModelParameters parameters)
        {
            object output = null;

            this.Accessed++;

            try
            {
                //Arguments
                object[] args = parameters.Values.ToArray();

                //Invoke the method (with arguments)
                output = _method.Invoke(this.Model, args);
            }
            catch (Exception e)
            {
                this.Model.OnException(this, parameters, e);
                return(output);
            }

            //Should it have thrown, and it didn't?
            if (this.Throws)
            {
                throw new ModelException(this, "Requirements were not met, and was expected to throw Exception: '" + this.Exception + "'" + " ID: '" + this.ExceptionId + "''");
            }
            return(output);
        }
Beispiel #2
0
        protected ModelParameters               DetermineParameters(ModelAction action)
        {
            try
            {
                ModelParameters allparameters = action.Parameters;
                ModelParameters choosen       = new ModelParameters();

                //Loop through the method parameters
                foreach (ParameterInfo info in action.Method.GetParameters())
                {
                    //Find the all parameters assoicated with this param
                    ModelParameters parameters = allparameters.Find(info.Name);
                    //Exclude invalid parameters (if not requested)
                    if (!this.InvalidParameters)
                    {
                        parameters = (ModelParameters)parameters.FindFlag((int)ModelItemFlags.Throws, false);
                    }
                    if (parameters.Count <= 0)
                    {
                        throw new ModelException(this, "Unable to find a ModelParameter for method parameter: '" + info.Name + "'");
                    }

                    //Choose one of the parameters, based upon weight
                    ModelParameter parameter = parameters.Choose(this);
                    parameter.Accessed++;

                    //Note: We cloning the param, since were choosing only one of the values to use this time.
                    parameter = (ModelParameter)parameter.Clone();

                    //Choose (or generate) one of the values
                    ModelValue value = DetermineParameterValue(parameter);
                    value.Accessed++;

                    //Add it to the array
                    parameter.Value = value;
                    choosen.Add(parameter);
                }

                return(choosen);
            }
            catch (Exception e)
            {
                //Make this easier to debug
                throw new ModelException(this, "DetermineParameters", e);
            }
        }
Beispiel #3
0
        public virtual void                 OnException(ModelAction action, ModelParameters parameters, Exception e)
        {
            //Since were using reflection, if an error occurs within the method
            //make this easier to debug (for the method writer) so they see their exception
            //instead of the reflection based one.
            while (e.InnerException != null && e is TargetInvocationException)
            {
                e = e.InnerException;
            }

            //Find what should have thrown the exception, action or parameters
            Type   exception   = action.Exception;
            string exceptionid = action.ExceptionId;

            if (exception == null && exceptionid == null)
            {
                //Otherwise maybe one of the parameters was supposed to throw
                //Find the parameter that expects an error
                ModelParameters found = (ModelParameters)parameters.FindFlag((int)ModelItemFlags.Throws);
                if (found.Count > 0)
                {
                    //Note: We find the 'highest' weighted parameter (ie: order of errors processed)
                    found.SortByWeightDesc();
                    exception   = found.First.Exception;
                    exceptionid = found.First.ExceptionId;
                }
            }

            //Exception not expected
            if (exception == null && exceptionid == null)
            {
                throw new ModelException(action, "Threw an exception: " + e.Message, e);
            }

            //Expected: Simple verification, type based
            if (exception != null && (exception != e.GetType()))
            {
                throw new ModelException(action, "Threw the wrong exception: " + e.Message, e);
            }

            //Expected: Advanced verification, user implemented function
            if (exceptionid != null)
            {
                this.OnException(action, parameters, e, exceptionid);                   //Throws if not verified
            }
        }
Beispiel #4
0
 //Constructor
 public ModelActionInfo(ModelAction action, ModelParameters parameters, object retval)
 {
     _action     = action;
     _parameters = parameters;
     _retval     = retval;
 }
Beispiel #5
0
 public override void                            Reload()
 {
     //Clear, so they will be dynamically setup by reflection again
     _requirements = null;
     _parameters   = null;
 }
Beispiel #6
0
        private void    ExecuteActionInfo(ModelActionInfo actioninfo)
        {
            ModelAction     action     = actioninfo.Action;
            ModelParameters parameters = actioninfo.Parameters;
            Model           model      = action.Model;

            //Pre-Execute, events
            //Note: If CallBefore returns false, we simply don't execute the method
            if (model.OnCallBefore(action, parameters))
            {
                //Adding the selected action (and its param values) to the trace.
                if (this.Options.Tracing)
                {
                    _actionstrace.Add(actioninfo);
                }

                //Execute the method (delegate)
                actioninfo.RetVal = action.Execute(parameters);
                _actionscalled++;

                //Add the returned model to the system
                Model output = actioninfo.RetVal as Model;
                if (output != null)
                {
                    //If it doesn't already exist, and the model type is part of the set
                    if (this.Models.FindInstance(output) == null)
                    {
                        actioninfo.Created = true;

                        //Add returned models, if requested
                        if (this.Options.AddReturnedModels)
                        {
                            //Note: We always obey the maxinstance count
                            Models found = (Models)this.Models.FindType(output.GetType()).FindFlag((int)ModelItemFlags.Disabled, false);
                            if (found.Count < output.MaxInstances)
                            {
                                output.Enabled = true;          //Enabled by default
                                if (output.ParentModel == null) //Hook up the creator, if not already specified
                                {
                                    output.ParentModel = action.Model;
                                }
                                this.Models.Add(output);
                            }
                        }
                    }
                }

                //Trace
                if (this.Options.Tracing)
                {
                    ModelTrace.WriteLine(ModelTrace.FormatMethod(actioninfo));
                }

                //Post-Execute, events
                model.OnCallAfter(action, parameters, actioninfo.RetVal);
            }

            //Reset cached variables
            foreach (ModelVariable variable in this.Models.Variables)
            {
                variable.CachedValue = null;
            }
        }
Beispiel #7
0
 public virtual void                 OnException(ModelAction action, ModelParameters parameters, Exception e, string id)
 {
     //Override this method, and verify the ExceptionId specified in the model
     throw new ModelException(this, "ExceptionId was specified and not verified.  Override Model.VerifyException, and verify the ExceptionId as was specified in the model", e);
 }
Beispiel #8
0
 public virtual void                                 OnCallAfter(ModelAction action, ModelParameters parameters, object result)
 {
     //Note: Override if you want to do after
     if (CallAfter != null)
     {
         CallAfter(action, parameters, result);
     }
 }
Beispiel #9
0
        public virtual bool                                 OnCallBefore(ModelAction action, ModelParameters parameters)
        {
            //Note: Override if you want to do something, or call other methods BEFORE execution of the action
            bool result = true;             //true, indicates continue to call the action

            if (_callbefore != null)
            {
                foreach (CallBeforeHandler handler in _callbefore)
                {
                    result &= handler(action, parameters);
                }
            }

            return(result);
        }