public ValidationError ValidateField(PropertyValue propVal)
 {
     return this.SetProperty(null, propVal, true);
 }
        /// <summary>
        /// The set property.
        /// </summary>
        /// <param name="journeyUuid">
        /// The journey uuid.
        /// </param>
        /// <param name="propVal">
        /// The prop val.
        /// </param>
        /// <param name="testOnly">
        /// The test only.
        /// </param>
        /// <returns>
        /// </returns>
        /// <exception cref="Exception">
        /// </exception>
        /// <exception cref="JsonException">
        /// </exception>
        /// <exception cref="JsonException">
        /// </exception>
        /// <exception cref="Exception">
        /// </exception>
        /// <exception cref="Exception">
        /// </exception>
        /// <exception cref="Exception">
        /// </exception>
        private ValidationError SetProperty(string journeyUuid, PropertyValue propVal, bool testOnly)
        {
            if (propVal.Name == null)
            {
                throw new Exception(
                    "Property value name is null. Cannot proceed with validation. Please check your JSON syntax.");
            }

            if (journeyUuid == null && !testOnly)
            {
                throw new JsonException("The journey UUID can only be null if the testOnly attribute is true.");
            }

            // var valErrors = new List<ValidationError>();
            var jp = ObjectCache.GetObject<JourneyManager>();

            PropertyInfo[] propertyInfos = typeof(EvolutionaryProperties).GetProperties();
            EvolutionaryProperties properties;
            try
            {
                properties = journeyUuid == null ? new EvolutionaryProperties() : jp.GetJourney(journeyUuid).Properties;
            }
            catch (Exception)
            {
                throw new JsonException("The supplied journey UUID was incorrect.");
            }
            {
                // foreach (var propVal in propVals)
                var propertyInfo = propertyInfos.FirstOrDefault(s => s.Name == propVal.Name);

                if (propertyInfo == null)
                {
                    return new ValidationError { Target = propVal.Name, Message = Strings.ERR_PROP_NOT_FOUND };

                    // continue;
                }

                object castVal = null;
                string errMess = string.Empty;
                bool isList = false;
                bool isArray = false;

                if (propertyInfo.PropertyType.IsValueType)
                {
                    try
                    {
                        if (propertyInfo.PropertyType.IsEnum)
                        {
                            castVal = Enum.Parse(propertyInfo.PropertyType, propVal.Value);
                        }
                        else
                        {
                            castVal = Convert.ChangeType(propVal.Value, propertyInfo.PropertyType);
                        }
                    }
                    catch (Exception e)
                    {
                        errMess = e.Message;
                    }
                }
                else
                {
                    try
                    {
                        Type pType = propertyInfo.PropertyType;

                        switch (pType.Name)
                        {
                            case "INetworkNode":

                                if (propVal.Value == null)
                                {
                                    throw new Exception("This field cannot be blank.");
                                }

                                PtvDataProvider provider = ObjectCache.GetObjects<PtvDataProvider>()[0];
                                PtvNode node = null;
                                try
                                {
                                    node = (PtvNode)provider.GetNodeFromId(Convert.ToInt32(propVal.Value));
                                }
                                catch (KeyNotFoundException)
                                {
                                    throw new Exception("There is no such node with the given ID or name.");
                                }

                                    // provider.GetNodeFromName(propVal.Value);
                                if (node == null)
                                {
                                    throw new Exception("No matching node for supplied name.");
                                }

                                castVal = node;
                                break;

                            case "FitnessParameter[]":

                                var objs = propVal.Value.Split(new[] { ',' });
                                var objectives = new FitnessParameter[objs.Length];
                                int i = 0;
                                foreach (var obj in objs)
                                {
                                    FitnessParameter e;
                                    if (!Enum.TryParse(obj, out e))
                                    {
                                        throw new Exception("Cannot parse string into enum.");
                                    }

                                    objectives[i++] = e;
                                }

                                castVal = objectives;

                                break;
                            default:

                                // var pType = new;
                                if (pType.IsGenericType)
                                {
                                    if (pType.Name.Contains("List"))
                                    {
                                        pType = pType.GetGenericArguments()[0];
                                        isList = true;
                                    }
                                }

                                if (pType.IsArray)
                                {
                                    pType = pType.GetElementType();
                                    isArray = true;
                                }

                                try
                                {
                                    var types = Assembly.GetAssembly(pType).GetTypes();
                                    pType = types.Where(t => t.Name == propVal.Value).First();
                                }
                                catch (Exception)
                                {
                                    throw new Exception("Cannot find type in assembly. (" + propVal.Value + ")");
                                }

                                // var pType = Assembly.GetAssembly (propertyInfo.PropertyType).GetType(propVal.Value,true,true);
                                var objects = ObjectCache.GetObjects(pType);
                                if (objects.Length == 0)
                                {
                                    object newObj = null;
                                    try
                                    {
                                        // newObj = pType.InvokeMember("",BindingFlags.CreateInstance,null,null,new[] {properties});
                                        newObj = Activator.CreateInstance(pType, new[] { properties });
                                    }
                                    catch (Exception)
                                    {
                                        // newObj = pType.InvokeMember("",BindingFlags.CreateInstance,null,null,null);
                                        newObj = Activator.CreateInstance(pType);
                                    }

                                    ObjectCache.RegisterObject(newObj);
                                    castVal = newObj;
                                }
                                else
                                {
                                    castVal = objects.First();
                                }

                                if (isArray)
                                {
                                    var array = Array.CreateInstance(pType, 1);
                                    array.SetValue(castVal, 0);
                                    castVal = array;
                                }

                                break;

                                // throw new Exception("No handler for type: " + pType.Name);
                        }
                    }
                    catch (Exception e)
                    {
                        return new ValidationError
                            {
                                Target = propVal.Name,
                                Message = string.Format("{0}: {1}", Strings.ERR_UNSUPP_REFTYPE, e.Message)
                            };
                    }
                }

                if (castVal == null)
                {
                    // ERR_INVALID_CAST
                    return new ValidationError
                        {
                            Target = propVal.Name,
                            Message = string.Format("{0}: {1}", Strings.ERR_INVALID_CAST, errMess)
                        };

                    // continue;
                }

                if (!testOnly)
                {
                    if (isList)
                    {
                        IList a = (IList)propertyInfo.GetValue(properties, null);

                        a.Add(castVal);
                    }
                    else
                    {
                        propertyInfo.SetValue(properties, castVal, null);
                    }
                }
            }

            return new ValidationError { Target = propVal.Name, Message = Strings.VALIDATION_SUCCESS };
        }
 public ValidationError SetProperty(string journeyUuid, PropertyValue propVal)
 {
     return this.SetProperty(journeyUuid, propVal, false);
 }
        public ValidationError[] SetProperties(string journeyUuid, PropertyValue[] propVals)
        {
            /*
             FORMAT:
             { "propVals" : [{ "propVal" : {"name":"CrossoverRate","value":"0.7"} }] }
             */

            if (propVals == null)
            {
                throw new Exception(
                    "Property value collection is null. Cannot proceed with validation. Please check your JSON syntax.");

            }
            List<ValidationError> valErrors = new List<ValidationError>();
            foreach (var propVal in propVals)
            {
                var valError = this.SetProperty(journeyUuid, propVal);
                if (valError != null)
                {
                    valErrors.Add(valError);
                }
            }

            this.SaveJourneys();
            return valErrors.ToArray();
        }
 public ValidationError SetProperty(Journey journey, PropertyValue propVal)
 {
     return this.SetProperty(journey, propVal, false);
 }
        public ValidationError[] Search(string userKey, PropertyValue[] propVals)
        {
            var journeyManager = ObjectCache.GetObject<JourneyManager>();
            if (journeyManager == null)
            {
                journeyManager = new JourneyManager();
                ObjectCache.RegisterObject(journeyManager);
            }

            bool validationError = false;

            //Load the default journey
            Journey journey;
            try
            {
                if (journeyManager.Contains(userKey))
                {
                    journey = journeyManager.GetJourney(userKey);
                }
                else
                {
                    journey = (Journey)journeyManager.GetJourney("default").Clone();
                    journey.Uuid = userKey;
                    journeyManager.Add(journey);
                    journeyManager.Save();
                }

            }
            catch (KeyNotFoundException)
            {
                throw new Exception(
                   "No default journey to base this journey off. Please specify one using the control panel.");

            }

            journeyManager.Clean(journey);
            journeyManager.Save();

            /*
             FORMAT:
             { "propVals" : [{ "propVal" : {"name":"CrossoverRate","value":"0.7"} }] }
             */

            if (propVals == null)
            {
                throw new Exception(
                    "Property value collection is null. Cannot proceed with validation. Please check your JSON syntax.");

            }

            var valErrors = new List<ValidationError>();
            foreach (var propVal in propVals)
            {
                var valError = this.SetProperty(journey, propVal);
                if (valError != null)
                {
                    valErrors.Add(valError);
                    if (valError.Message != Strings.VALIDATION_SUCCESS)
                    {
                        validationError = true;
                    }
                }
            }

            // Only perform search if there are no validation errors.
            if (!validationError)
            {
                var optimiser = ObjectCache.GetObject<JourneyOptimiser>();
                optimiser.Remove(journey);
                optimiser.EnqueueJourney(journey,1);
                optimiser.Resume();

            }

            return valErrors.ToArray();
        }
        /// <summary>
        /// The set properties.
        /// </summary>
        /// <param name="propVals">
        /// The prop vals.
        /// </param>
        /// <returns>
        /// </returns>
        private ValidationError[] SetProperties(PropertyValue[] propVals)
        {
            /*
             FORMAT:
             { "propVals" : [{ "propVal" : {"name":"CrossoverRate","value":"0.7"} }] }
             */
            List<ValidationError> valErrors = new List<ValidationError>();
            foreach (var propVal in propVals)
            {
                var valError = this.SetProperty(propVal);
                if (valError != null)
                {
                    valErrors.Add(valError);
                }
            }

            return valErrors.ToArray();
        }