Example #1
0
        /// <summary>
        /// Initializes the tool factory.
        /// </summary>
        /// <exception cref="InvalidFormatException">The specified factory is invalid or not supported.</exception>
        /// <exception cref="System.InvalidOperationException">Unable to initialize the tool factory.</exception>
        private void InitializeFactory()
        {
            var factoryName = Manifest[FactoryName];

            if (string.IsNullOrEmpty(factoryName))
            {
                if (DefaultFactory != null)
                {
                    ToolFactory = (BaseToolFactory)Activator.CreateInstance(DefaultFactory);
                    ToolFactory.Initialize(this);
                }
            }
            else
            {
                if (Library.TypeResolver.IsRegistered(factoryName))
                {
                    try {
                        var type = Library.TypeResolver.ResolveType(factoryName);

                        ToolFactory = Library.GetInstance <BaseToolFactory>(type);
                        ToolFactory.Initialize(this);
                    } catch (Exception ex) {
                        throw new InvalidOperationException("Unable to initialize the tool factory.", ex);
                    }
                }
                else
                {
                    throw new InvalidOperationException(string.Format("The tool factory '{0}' is not registered in the type resolver.", factoryName));
                }
            }
        }
Example #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="BaseModel"/> class.
        /// </summary>
        /// <param name="componentName">Name of the component.</param>
        /// <param name="languageCode">The language code.</param>
        /// <param name="manifestInfoEntries">The manifest information entries.</param>
        /// <param name="toolFactory">The tool factory.</param>
        /// <exception cref="System.ArgumentNullException">
        /// The <paramref name="componentName"/> cannot be null.
        /// or
        /// The <paramref name="languageCode"/> cannot be empty or null.
        /// </exception>
        /// <exception cref="System.InvalidOperationException">Unable to initialize the factory.</exception>
        protected BaseModel(
            string componentName,
            string languageCode,
            Dictionary <string, string> manifestInfoEntries,
            BaseToolFactory toolFactory) : this(componentName) {
            if (string.IsNullOrEmpty(languageCode))
            {
                throw new ArgumentNullException("languageCode", @"The language cannot be empty or null.");
            }

            Manifest[ManifestVersionProperty] = "1.0";
            Manifest[LanguageEntry]           = languageCode;
            Manifest[VersionProperty]         = Library.OpenNLPVersion.ToString();
            Manifest[TimestampEntry]          = Library.CurrentTimeMillis().ToString(CultureInfo.InvariantCulture);
            Manifest[ComponentNameEntry]      = componentName;

            if (manifestInfoEntries != null)
            {
                foreach (var entry in manifestInfoEntries)
                {
                    Manifest[entry.Key] = entry.Value;
                }
            }

            if (toolFactory != null)
            {
                ToolFactory = toolFactory;

                if (!Library.TypeResolver.IsRegistered(toolFactory.GetType()))
                {
                    throw new InvalidOperationException("The tool factory type " + toolFactory.GetType().Name + " is not registered in the type resolver.");
                }

                Manifest[FactoryName] = Library.TypeResolver.ResolveName(toolFactory.GetType());

                var map = toolFactory.CreateArtifactMap();
                foreach (var item in map)
                {
                    artifactMap.Add(item.Key, item.Value);
                }

                var entries = toolFactory.CreateManifestEntries();
                foreach (var item in entries)
                {
                    Manifest[item.Key] = item.Value;
                }
            }

            if (ToolFactory == null)
            {
                try {
                    InitializeFactory();
                } catch (Exception ex) {
                    throw new InvalidOperationException("Unable to initialize the factory.", ex);
                }
            }

            CreateBaseArtifactSerializers();
        }
Example #3
0
        protected BaseModel(
            string componentName,
            string languageCode,
            Dictionary<string, string> manifestInfoEntries,
            BaseToolFactory toolFactory) : this(componentName) {

            if (string.IsNullOrEmpty(languageCode)) {
                throw new ArgumentNullException("languageCode", @"The language cannot be empty or null.");
            }

            Manifest[ManifestVersionProperty] = "1.0";
            Manifest[LanguageEntry] = languageCode;
            Manifest[VersionProperty] = Library.MaxOpenNLPVersion.ToString();
            Manifest[TimestampEntry] = Library.CurrentTimeMillis().ToString(CultureInfo.InvariantCulture);
            Manifest[ComponentNameEntry] = componentName;

            if (manifestInfoEntries != null) {
                foreach (var entry in manifestInfoEntries) {
                    Manifest[entry.Key] = entry.Value;
                }
            }

            if (toolFactory != null) {
                ToolFactory = toolFactory;

                if (!Library.TypeResolver.IsRegistered(toolFactory.GetType()))
                    throw new InvalidOperationException("The tool factory type " + toolFactory.GetType().Name + " is not registered in the type resolver.");

                Manifest[FactoryName] = Library.TypeResolver.ResolveName(toolFactory.GetType());

                // Assign the model as the artifact provider.
                toolFactory.Initialize(this);

                var map = toolFactory.CreateArtifactMap();
                foreach (var item in map) {
                    artifactMap.Add(item.Key, item.Value);
                }

                var entries = toolFactory.CreateManifestEntries();
                foreach (var item in entries) {
                    Manifest[item.Key] = item.Value;
                }
            }

            if (ToolFactory == null) {
                try {
                    InitializeFactory();
                } catch (Exception ex) {
                    throw new InvalidOperationException("Unable to initialize the factory.", ex);
                }
            }

            // ReSharper disable once DoNotCallOverridableMethodsInConstructor
            CreateArtifactSerializers();
        }
Example #4
0
 public SentenceModel(string languageCode, Dictionary <string, string> manifestInfoEntries,
                      BaseToolFactory toolFactory)
     : base(ComponentName, languageCode, manifestInfoEntries, toolFactory)
 {
 }
Example #5
0
        /// <summary>
        /// Initializes the tool factory.
        /// </summary>
        /// <exception cref="InvalidFormatException">The specified factory is invalid or not supported.</exception>
        /// <exception cref="System.InvalidOperationException">Unable to initialize the tool factory.</exception>
        private void InitializeFactory() {
            var factoryName = Manifest[FactoryName];

            if (string.IsNullOrEmpty(factoryName)) {
                if (DefaultFactory != null) {
                    ToolFactory = (BaseToolFactory)Activator.CreateInstance(DefaultFactory);
                    ToolFactory.Initialize(this);
                }
            } else {
                if (Library.TypeResolver.IsRegistered(factoryName)) {
                    try {
                        var type = Library.TypeResolver.ResolveType(factoryName);

                        ToolFactory = Library.GetInstance<BaseToolFactory>(type);
                        ToolFactory.Initialize(this);
                    } catch (Exception ex) {
                        throw new InvalidOperationException("Unable to initialize the tool factory.", ex);
                    }
                } else {
                    throw new InvalidOperationException(string.Format("The tool factory '{0}' is not registered in the type resolver.", factoryName));
                }
            }
        }