public virtual void Publish(string message, IExperimentState state)
 {
     if (Publisher != null)
     {
         Publisher.Publish(message, state);
     }
 }
 public virtual void Publish(string message, IExperimentState state)
 {
     if (!string.IsNullOrEmpty(message))
     {
         messages.AppendFormat("  {3} - Message from Experiment '{0}' in Step '{1}': {2}\n",
                               state.Name, state.CurrentStep, message, state.Timestamp.ToLongTimeString());
     }
 }
 public Experiment(string name, ISciencePublisher publisher, IExperimentState state,
                   IExperimentSteps <T, TPublish> steps, bool throwOnInternalExceptions)
 {
     _name      = name;
     _publisher = publisher; //if this is null, nothing gets published! should we throw?
     _throwOnInternalExceptions = throwOnInternalExceptions;
     _currentState = state;
     _steps        = steps; //should be overwriten by public Set
 }
        public T Run() //Should this be virtual? overriding this breaks the class' logic, so no
        {
            if (CurrentState == null)
            {
                throw new InvalidOperationException(
                          "The Experiment State is null! Can't run Experiment.");
            }
            CurrentState.ExperimentName = Name;
            IExperimentResult <TPublish> results = new ExperimentResult <TPublish>
            {
                LastState = CurrentState,
                Name      = Name
            };
            T         controlValue     = default(T);
            Exception controlException = null;

            try
            {
                controlValue = RunControl(results, out controlException); //If this throws, the control itself failed
            }
            catch (Exception)
            {
                //Fatal error occurred!
                if (ThrowOnInternalExceptions)
                {
                    throw;
                }
                //continue on with the candidates
            }
            try
            {
                if (RunCandidates(results, controlValue, controlException))
                {
                    TryPublish(results); //Don't publish if we didn't run any candidates
                }
            }
            catch (Exception)
            {
                //A fatal error occured in one of the candidates or Publisher, throw for debug purposes if flag is set
                if (ThrowOnInternalExceptions)
                {
                    throw;
                }
            }
            _currentState = null;
            if (controlException != null)
            {
                throw controlException;
            }
            else
            {
                return(controlValue);
            }
        }
        //public StatsDPublisher(string hostName, int port, string prefix, IStopWatchFactory swFactory)
        //{

        #region Public Methods

        //}
        /// <summary>
        /// Sends properly formatted StatsD messages prefixed by "[Experiment Name].[Current Name].[Current Step]."
        /// </summary>
        /// <example>
        /// state = new ExperimentState { Name = "Candidate 1", ExperimentName = "Science!",
        /// CurrentStep = Operations.OnMismatch }; Publish("gaugor:333|g", state) --&gt; Sends
        /// StatsD with: Name: "Science!.Candidate_1.OnMismatch.gaugor", Value: 333, Type: Gauge
        /// </example>
        /// <remarks>
        /// see https://github.com/etsy/statsd/blob/master/docs/metric_types.md for formatting
        /// </remarks>
        /// <param name="message"></param>
        /// <param name="state"></param>
        public void Publish(string message, IExperimentState state)
        {
            // Do nothing for now gaugor:333|g
            if (!string.IsNullOrEmpty(message))
            {
                using (var udp = new StatsdUDP(hostName, port))
                {
                    var statsd = new Statsd(udp, prefix);
                    TrySendMessage(message, string.Format("{0}.{1}.{2}.",
                                                          ReplaceWhitespace(state.ExperimentName),
                                                          ReplaceWhitespace(state.Name), state.CurrentStep),
                                   statsd);
                }
            }
        }
 public Experiment(string name, ISciencePublisher publisher, IExperimentState state,
                   bool throwOnInternalExceptions)
     : this(name, publisher, state, new ExperimentSteps <T, TPublish>(), throwOnInternalExceptions)
 {
 }