Exemple #1
0
        /// <summary>
        /// Internal method to RunUntil( requirements ) are met
        /// The public version is the delegate method above, much simplier and more powerful for users.
        ///
        /// </summary>
        /// <param name="requirements"></param>
        /// <returns></returns>
        protected bool              RunUntil(ModelRequirements requirements)
        {
            //Run until the following requirements are met (ie: variable = values)
            //Or until the time is elapsed.
            long startingticks  = DateTime.Now.Ticks;
            long remainingticks = DetermineTicks();

            if (this.Options.Tracing)
            {
                ModelTrace.WriteLine("Model Seed: " + this.Options.Seed);
            }

            bool meetsrequirements = Execute(requirements, startingticks, remainingticks);

            //CallLast
            //Because requirements not met and model needs to shutdown correctly.
            if (!meetsrequirements)
            {
                //CallLast, actions
                foreach (Model model in this.Models)
                {
                    if (model.Actions.Accessed > 0)                     //Only if 'first' was called
                    {
                        foreach (ModelAction last in this.GetPossibleActions(model.Actions.FindFlag(ModelActionFlags.CallLast)))
                        {
                            this.ExecuteAction(last);
                        }
                    }
                }
            }

            return(meetsrequirements);
        }
Exemple #2
0
        protected bool                          MeetsRequirements(ModelRequirements requirements)
        {
            //Delegate
            ModelRequirement failedrequirement = null;

            return(this.MeetsRequirements(requirements, out failedrequirement));
        }
Exemple #3
0
        /// <summary>
        /// RunUntil(custom delegate)
        ///     Powerful, where you own the code in the handler, and determine exactly when to stop execution
        ///     ie: RunUntil(delegate() { return _x > 5; }  //Where _x > 5 is any c# code expression
        ///
        /// </summary>
        /// <param name="func"></param>
        /// <returns></returns>
        public bool             RunUntil(ModelFunction func)
        {
            //Build up a ModelRequirement around the delegate
            ModelRequirements criteria = new ModelRequirements(
                new ModelExpression(func, new ModelValue(true))
                );

            return(this.RunUntil(criteria));
        }
Exemple #4
0
 public override void                            Reload()
 {
     //Clear, so they will be dynamically setup by reflection again
     _requirements = null;
     _parameters   = null;
 }
Exemple #5
0
        protected virtual bool Execute(ModelRequirements requirements, long startingticks, long remainingticks)
        {
            bool meetsrequirements = false;

            _actionscalled = 0;

            //Continue until no more actions to execute
            ModelAction action = this.DetermineNextAction();

            while (action != null)
            {
                //Model
                Model model = action.Model;

                //Init
                if (model.Actions.Accessed == 0)
                {
                    model.Init();
                }

                //CallFirst, actions
                //TODO: What happens if this now meets the requirements?
                foreach (ModelAction first in this.GetPossibleActions(model.Actions.FindFlag(ModelActionFlags.CallFirst)))
                {
                    if (first != action)
                    {
                        this.ExecuteAction(first);
                    }
                }

                //Execute (choose the parameters as well)
                //Note: CallFirst might have disabled this model, so we check first
                if (model.Enabled)
                {
                    this.ExecuteAction(action);
                }


                //Determine if all the requirements were met
                if (requirements != null && requirements.Count > 0 &&
                    MeetsRequirements(requirements))
                {
                    meetsrequirements = true;
                    break;
                }

                //Check action count
                if (_actionscalled >= _options.MaxActions)
                {
                    if (this.Options.Tracing)
                    {
                        ModelTrace.WriteLine("MaxActions: '" + _options.MaxActions + "' was reached.");
                    }
                    break;
                }

                //Check Timeout
                long currentticks = DateTime.Now.Ticks;
                if (currentticks - startingticks > remainingticks)
                {
                    if (this.Options.Tracing)
                    {
                        ModelTrace.WriteLine("Timeout: '" + _options.Timeout + "' has elapsed.");
                    }
                    break;
                }

                //Determine the next action
                action = this.DetermineNextAction();
            }
            return(meetsrequirements);
        }
Exemple #6
0
        protected bool                          MeetsRequirements(ModelRequirements requirements, out ModelRequirement failedrequirement)
        {
            if (requirements != null)
            {
                //Loop over all requirements
                foreach (ModelRequirement requirement in requirements)
                {
                    //Ignore disabled requirements
                    if (requirement.Weight == 0 || requirement.Disabled)
                    {
                        continue;
                    }

                    //By default were just looking at this specific variable instance
                    //However if 'Global' is enabled, consider this variable on any models of this type
                    List <Object> currentValues = new List <Object>();
                    if (requirement.Global)
                    {
                        //Note: We do this check everytime, since models could have been added dynamically
                        if (requirement.Variable != null)
                        {
                            foreach (Model model in this.Models.FindType(requirement.Variable.Model.GetType()))
                            {
                                ModelVariables variables = model.Variables.Find(requirement.Variable.Name);
                                foreach (ModelVariable variable in variables)
                                {
                                    currentValues.Add(variable.CachedValue);
                                }
                            }
                        }
                        else
                        {
                            currentValues.Add(true);
                        }
                    }
                    else
                    {
                        if (requirement.Variable == null)
                        {
                            throw new ModelException(this, "Variable is null?");
                        }
                        currentValues.Add(requirement.Variable.CachedValue);
                    }

                    //See if this requirement is met
                    bool matched = false;
                    foreach (Object value in currentValues)
                    {
                        if (requirement.Evaluate(value))
                        {
                            matched = true;
                            break;
                        }
                    }

                    //Stop on first mis-match, (ie: at least one requirement didn't match)
                    if (!matched)
                    {
                        //First failed requirement
                        //Note: This has to be order based, since production checks conditions in-order
                        //and will fail (and throw) according to the first one not meet
                        failedrequirement = requirement;
                        return(false);
                    }
                }
            }

            failedrequirement = null;
            return(true);
        }