ModuleBase.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Author: Orlys
  2. // Github: https://github.com/Orlys
  3. namespace Yuuna.Contracts.Modules
  4. {
  5. using System;
  6. using System.Collections.Immutable;
  7. using System.Reflection;
  8. using System.Runtime.Loader;
  9. using Yuuna.Contracts.Optimization;
  10. using Yuuna.Contracts.Patterns;
  11. using Yuuna.Contracts.Semantics;
  12. using Yuuna.Contracts.TextSegmention;
  13. /// <summary>
  14. /// 模組狀態。
  15. /// </summary>
  16. public enum ModuleStatus
  17. {
  18. /// <summary>
  19. /// 未初始化。
  20. /// </summary>
  21. Uninitialized,
  22. /// <summary>
  23. /// 初始化失敗。
  24. /// </summary>
  25. FailToInitialize,
  26. /// <summary>
  27. /// 初始化完成。
  28. /// </summary>
  29. Initialized,
  30. }
  31. internal interface IModule
  32. {
  33. IPatternSet Patterns { get; }
  34. }
  35. public abstract class ModuleBase
  36. {
  37. protected virtual string ModuleName { get; }
  38. private readonly PatternFactory _patternfactory;
  39. internal IPatternSet Patterns => this._patternfactory;
  40. public ModuleBase()
  41. {
  42. this._patternfactory = new PatternFactory(this);
  43. this.ModuleName = this.GetType().Name;
  44. this.Status = ModuleStatus.Uninitialized;
  45. }
  46. /// <summary>
  47. /// 模組名稱。
  48. /// </summary>
  49. public string Name
  50. {
  51. get
  52. {
  53. var t = this.GetType();
  54. var getMethod = t.GetProperty(nameof(this.ModuleName), (BindingFlags)52).GetGetMethod(true);
  55. if (!getMethod.GetBaseDefinition().Equals(getMethod))
  56. {
  57. try
  58. {
  59. var test = this.ModuleName;
  60. if (!string.IsNullOrWhiteSpace(test))
  61. {
  62. return test;
  63. }
  64. }
  65. catch
  66. {
  67. }
  68. return t.Name;
  69. }
  70. else
  71. return this.ModuleName;
  72. }
  73. }
  74. /// <summary>
  75. /// 表示模組是否已初始化。
  76. /// </summary>
  77. public ModuleStatus Status { get; private set; }
  78. /// <summary>
  79. /// 初始化模組
  80. /// </summary>
  81. /// <param name="textSegmenter">分詞器</param>
  82. /// <param name="groupManager">群組管理</param>
  83. internal void Initialize(ITextSegmenter textSegmenter, IGroupManager groupManager)
  84. {
  85. if (this.Status.Equals(ModuleStatus.Uninitialized))
  86. {
  87. try
  88. {
  89. this.BuildPatterns(groupManager, this._patternfactory);
  90. textSegmenter.Load(groupManager);
  91. this.Status = ModuleStatus.Initialized;
  92. this.AfterInitialize();
  93. }
  94. catch //(Exception e)
  95. {
  96. this.Status = ModuleStatus.FailToInitialize;
  97. }
  98. }
  99. }
  100. /// <summary>
  101. /// 在初始化後引發。
  102. /// </summary>
  103. protected virtual void AfterInitialize()
  104. {
  105. }
  106. /// <summary>
  107. /// 建立模式規則。
  108. /// </summary>
  109. /// <param name="g"></param>
  110. /// <param name="p"></param>
  111. protected abstract void BuildPatterns(IGroupManager g, IPatternBuilder p);
  112. }
  113. }