123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- // Author: Orlys
- // Github: https://github.com/Orlys
- namespace Yuuna.Contracts.Modules
- {
- using System;
- using System.Collections.Immutable;
- using System.Reflection;
- using System.Runtime.Loader;
- using Yuuna.Contracts.Optimization;
- using Yuuna.Contracts.Patterns;
- using Yuuna.Contracts.Semantics;
- using Yuuna.Contracts.TextSegmention;
- /// <summary>
- /// 模組狀態。
- /// </summary>
- public enum ModuleStatus
- {
- /// <summary>
- /// 未初始化。
- /// </summary>
- Uninitialized,
- /// <summary>
- /// 初始化失敗。
- /// </summary>
- FailToInitialize,
- /// <summary>
- /// 初始化完成。
- /// </summary>
- Initialized,
- }
- internal interface IModule
- {
- IPatternSet Patterns { get; }
- }
- public abstract class ModuleBase
- {
- protected virtual string ModuleName { get; }
- private readonly PatternFactory _patternfactory;
-
- internal IPatternSet Patterns => this._patternfactory;
- public ModuleBase()
- {
- this._patternfactory = new PatternFactory(this);
- this.ModuleName = this.GetType().Name;
- this.Status = ModuleStatus.Uninitialized;
- }
- /// <summary>
- /// 模組名稱。
- /// </summary>
- public string Name
- {
- get
- {
- var t = this.GetType();
- var getMethod = t.GetProperty(nameof(this.ModuleName), (BindingFlags)52).GetGetMethod(true);
- if (!getMethod.GetBaseDefinition().Equals(getMethod))
- {
- try
- {
- var test = this.ModuleName;
- if (!string.IsNullOrWhiteSpace(test))
- {
- return test;
- }
- }
- catch
- {
- }
- return t.Name;
- }
- else
- return this.ModuleName;
- }
- }
- /// <summary>
- /// 表示模組是否已初始化。
- /// </summary>
- public ModuleStatus Status { get; private set; }
- /// <summary>
- /// 初始化模組
- /// </summary>
- /// <param name="textSegmenter">分詞器</param>
- /// <param name="groupManager">群組管理</param>
- internal void Initialize(ITextSegmenter textSegmenter, IGroupManager groupManager)
- {
- if (this.Status.Equals(ModuleStatus.Uninitialized))
- {
- try
- {
- this.BuildPatterns(groupManager, this._patternfactory);
- textSegmenter.Load(groupManager);
- this.Status = ModuleStatus.Initialized;
- this.AfterInitialize();
- }
- catch //(Exception e)
- {
- this.Status = ModuleStatus.FailToInitialize;
- }
- }
- }
- /// <summary>
- /// 在初始化後引發。
- /// </summary>
- protected virtual void AfterInitialize()
- {
- }
- /// <summary>
- /// 建立模式規則。
- /// </summary>
- /// <param name="g"></param>
- /// <param name="p"></param>
- protected abstract void BuildPatterns(IGroupManager g, IPatternBuilder p);
- }
- }
|