public bool RemoveExpert(ExpertInformation expertContainer)
        {
            lock (this)
            {
                if (_expertInfos.Remove(expertContainer) == false)
                {
                    return(false);
                }
            }

            RemovedExpertContainerEvent(expertContainer);
            return(true);
        }
        /// <summary>
        /// Construct.
        /// </summary>
        public static ExpertInformation CreateExternal(string name, Assembly containingAssembly,
                                                       Type containingAssemblyType, Guid guid)
        {
            ExpertInformation result = new ExpertInformation();

            result.Name                          = name;
            result._isExternal                   = true;
            result._isSavedLocally               = true;
            result._containingAssembly           = containingAssembly;
            result._containingAssemblyExpertType = containingAssemblyType;
            result._guid                         = guid;

            return(result);
        }
        void expertManager_AddedExpertContainerEvent(ExpertInformation container)
        {
            WinFormsHelper.BeginFilteredManagedInvoke(this, TimeSpan.FromMilliseconds(250), new GeneralHelper.GenericDelegate<bool>(UpdateUI), true);

            GeneralHelper.DefaultDelegate newDelegate = delegate()
                {
                    // Select the newly created expert.
                    this.listViewExperts.SelectedIndices.Clear();
                    if (listViewExperts.VirtualListSize > 0)
                    {
                        this.listViewExperts.SelectedIndices.Add(listViewExperts.VirtualListSize - 1);
                    }
                };

            WinFormsHelper.BeginManagedInvoke(this, newDelegate);
        }
        public bool AddAssembly(string assemblyPath)
        {
            Assembly assembly;

            try
            {
                assembly = Assembly.LoadFile(assemblyPath);
            }
            catch (Exception ex)
            {
                SystemMonitor.Error("Failed to load assembly [" + assemblyPath + "; " + ex.Message + "]");
                return(false);
            }

            //if (string.IsNullOrEmpty(assemblyGuid))
            //{
            //    SystemMonitor.Error("Assembly has no GUID [" + assembly.FullName + "]");
            //    return false;
            //}

            lock (this)
            {
                if (_expertContainingAssembliesAndPaths.ContainsKey(assembly))
                {
                    return(true);
                }

                _expertContainingAssembliesAndPaths.Add(assembly, assemblyPath);
            }

            // Import Managed experts from this assembly.
            foreach (Type type in assembly.GetExportedTypes())
            {
                if (type.IsSubclassOf(typeof(Expert)))
                {
                    ExpertInformation container = ExpertInformation.CreateExternal(type.Name, assembly, type, Guid.NewGuid());
                    AddExpert(container);
                }
            }

            if (ExpertAssemblyAddedEvent != null)
            {
                ExpertAssemblyAddedEvent(assembly);
            }
            return(true);
        }
        public bool AddExpert(ExpertInformation expert)
        {
            lock (this)
            {
                if (expert.Guid == Guid.Empty || GetContainerByGuid(expert.Guid) != null)
                {
                    SystemMonitor.Error("Failed to add expert container [Already added, or Guid is invalid].");
                    return(false);
                }

                _expertInfos.Add(expert);
            }

            if (AddedExpertContainerEvent != null)
            {
                AddedExpertContainerEvent(expert);
            }
            return(true);
        }
        /// <summary>
        /// Create a new expert based on a local file source and name.
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="expertName"></param>
        /// <param name="operationResultMessage"></param>
        /// <returns></returns>
        public bool CreateExpertFromFile(string filePath, string expertName, out string operationResultMessage)
        {
            foreach (ExpertInformation info in ExpertInfosArray)
            {
                if (info.IsLocal && string.IsNullOrEmpty(info.FilePath) == false &&
                    info.FilePath.ToLower() == filePath.ToLower())
                {
                    operationResultMessage = "Expert already added.";
                    return(false);
                }
            }

            string sourceCode;

            using (StreamReader reader = new StreamReader(filePath))
            {
                sourceCode = reader.ReadToEnd();
            }

            // If this file is coming from the experts folder, we can directly reuse it.
            bool isExpertsFolder         = Platform.Settings.GetMappedPath("ExpertsFolder").ToLower().CompareTo(Path.GetDirectoryName(filePath).ToLower()) == 0;
            ExpertInformation expertInfo = ExpertInformation.CreateLocal(Platform, expertName, isExpertsFolder, sourceCode, Guid.NewGuid(), out operationResultMessage);

            // If creation failed, try with changed name.
            int index = 0;

            while (expertInfo == null && index < 25)
            {
                expertInfo = ExpertInformation.CreateLocal(Platform, expertName + " " + index.ToString(), false, sourceCode, Guid.NewGuid(), out operationResultMessage);
                index++;
            }

            if (expertInfo == null)
            {
                operationResultMessage = "Failed to create new expert since due to file name conflict. Rename source file and try again.";
                return(false);
            }

            operationResultMessage = "";
            AddExpert(expertInfo);

            return(true);
        }
        /// <summary>
        /// Construct.
        /// </summary>
        public static ExpertInformation CreateLocal(Platform platform, string name, bool overrideExisting, string sourceCode, Guid guid, out string operationResultMessage)
        {
            string path = Path.Combine((string)platform.Settings.GetMappedFolder("ExpertsFolder"), name + ".cs");

            if (overrideExisting == false && File.Exists(path))
            {
                operationResultMessage = "Local File with this name already exists.";
                return(null);
            }

            operationResultMessage = "Created";

            ExpertInformation result = new ExpertInformation();

            result.Name        = name;
            result._isExternal = false;
            result._sourceCode = sourceCode;
            result._filePath   = path;
            result._guid       = guid;

            SystemMonitor.CheckError(result.SaveLocal(), "Failed to save expert locally.");

            return(result);
        }
 void expertManager_RemovedExpertContainerEvent(ExpertInformation container)
 {
     WinFormsHelper.BeginFilteredManagedInvoke(this, TimeSpan.FromMilliseconds(250), new GeneralHelper.GenericDelegate<bool>(UpdateUI), true);
 }
        public bool RemoveExpert(ExpertInformation expertContainer)
        {
            lock (this)
            {
                if (_expertInfos.Remove(expertContainer) == false)
                {
                    return false;
                }
            }

            RemovedExpertContainerEvent(expertContainer);
            return true;
        }
        public bool AddExpert(ExpertInformation expert)
        {
            lock (this)
            {
                if (expert.Guid == Guid.Empty || GetContainerByGuid(expert.Guid) != null)
                {
                    SystemMonitor.Error("Failed to add expert container [Already added, or Guid is invalid].");
                    return false;
                }

                _expertInfos.Add(expert);
            }

            if (AddedExpertContainerEvent != null)
            {
                AddedExpertContainerEvent(expert);
            }
            return true;
        }
        /// <summary>
        /// Construct.
        /// </summary>
        public static ExpertInformation CreateLocal(Platform platform, string name, bool overrideExisting, string sourceCode, Guid guid, out string operationResultMessage)
        {
            string path = Path.Combine((string)platform.Settings.GetMappedPath("ExpertsFolder"), name + ".cs");

            if (overrideExisting == false && File.Exists(path))
            {
                operationResultMessage = "Local File with this name already exists.";
                return null;
            }

            operationResultMessage = "Created";

            ExpertInformation result = new ExpertInformation();
            result.Name = name;
            result._isExternal = false;
            result._sourceCode = sourceCode;
            result._filePath = path;
            result._guid = guid;

            SystemMonitor.CheckError(result.SaveLocal(), "Failed to save expert locally.");

            return result;
        }
        /// <summary>
        /// Construct.
        /// </summary>
        public static ExpertInformation CreateExternal(string name, Assembly containingAssembly, 
            Type containingAssemblyType, Guid guid)
        {
            ExpertInformation result = new ExpertInformation();

            result.Name = name;
            result._isExternal = true;
            result._isSavedLocally = true;
            result._containingAssembly = containingAssembly;
            result._containingAssemblyExpertType = containingAssemblyType;
            result._guid = guid;

            return result;
        }