|
|
@@ -5,14 +5,38 @@ 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;
|
|
|
-
|
|
|
- public abstract class ModuleBase
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 模組狀態。
|
|
|
+ /// </summary>
|
|
|
+ public enum ModuleStatus
|
|
|
+ {
|
|
|
+ /// <summary>
|
|
|
+ /// 未初始化。
|
|
|
+ /// </summary>
|
|
|
+ Uninitialized,
|
|
|
+ /// <summary>
|
|
|
+ /// 初始化失敗。
|
|
|
+ /// </summary>
|
|
|
+ FailToInitialize,
|
|
|
+ /// <summary>
|
|
|
+ /// 初始化完成。
|
|
|
+ /// </summary>
|
|
|
+ Initialized,
|
|
|
+ }
|
|
|
+
|
|
|
+ public abstract class ModuleBase
|
|
|
{
|
|
|
+ private const BindingFlags FLAGS = (BindingFlags)52;
|
|
|
+
|
|
|
+ protected virtual string ModuleName { get; }
|
|
|
+
|
|
|
private readonly PatternFactory _patternfactory;
|
|
|
|
|
|
internal IPatternSet Patterns => this._patternfactory;
|
|
|
@@ -20,23 +44,64 @@ namespace Yuuna.Contracts.Modules
|
|
|
public ModuleBase()
|
|
|
{
|
|
|
this._patternfactory = new PatternFactory(this);
|
|
|
+ this.ModuleName = this.GetType().Name;
|
|
|
+ this.Status = ModuleStatus.Uninitialized;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-
|
|
|
- internal bool Initialize(ITextSegmenter textSegmenter, IGroupManager groupManager)
|
|
|
+ /// <summary>
|
|
|
+ /// 模組名稱。
|
|
|
+ /// </summary>
|
|
|
+ public string Name
|
|
|
{
|
|
|
- try
|
|
|
+ get
|
|
|
{
|
|
|
- this.BuildPatterns(groupManager, this._patternfactory);
|
|
|
- textSegmenter.Load(groupManager);
|
|
|
- this.AfterInitialize();
|
|
|
- return true;
|
|
|
+ var t = this.GetType();
|
|
|
+ var getMethod = t.GetProperty(nameof(this.ModuleName), FLAGS).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;
|
|
|
}
|
|
|
- catch (Exception e)
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <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))
|
|
|
{
|
|
|
- Console.WriteLine(e);
|
|
|
- return false;
|
|
|
+ try
|
|
|
+ {
|
|
|
+ this.BuildPatterns(groupManager, this._patternfactory);
|
|
|
+ textSegmenter.Load(groupManager);
|
|
|
+ this.Status = ModuleStatus.Initialized;
|
|
|
+ this.AfterInitialize();
|
|
|
+ }
|
|
|
+ catch //(Exception e)
|
|
|
+ {
|
|
|
+ this.Status = ModuleStatus.FailToInitialize;
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|