|
@@ -5,110 +5,52 @@ 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;
|
|
|
+ /// <summary>
|
|
|
+ /// 中繼資料。
|
|
|
+ /// </summary>
|
|
|
+ public ModuleMetadataAttribute Metadata { get; }
|
|
|
|
|
|
public ModuleBase()
|
|
|
{
|
|
|
- this._patternfactory = new PatternFactory(this);
|
|
|
- this.ModuleName = this.GetType().Name;
|
|
|
- this.Status = ModuleStatus.Uninitialized;
|
|
|
+ this.Metadata = ModuleMetadataAttribute.GetMetadata(this.GetType());
|
|
|
}
|
|
|
|
|
|
- /// <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; }
|
|
|
+ public bool Initialized { get; private set; }
|
|
|
|
|
|
/// <summary>
|
|
|
/// 初始化模組
|
|
|
/// </summary>
|
|
|
/// <param name="textSegmenter">分詞器</param>
|
|
|
/// <param name="groupManager">群組管理</param>
|
|
|
- internal void Initialize(ITextSegmenter textSegmenter, IGroupManager groupManager)
|
|
|
+ internal void Initialize(ITextSegmenter textSegmenter, IGroupManager groupManager, out IPatternSet patterns)
|
|
|
{
|
|
|
- if (this.Status.Equals(ModuleStatus.Uninitialized))
|
|
|
+ patterns = null;
|
|
|
+ if (!this.Initialized)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
- this.BuildPatterns(groupManager, this._patternfactory);
|
|
|
+ patterns = new PatternFactory(this);
|
|
|
+ this.BuildPatterns(groupManager, patterns as IPatternBuilder);
|
|
|
textSegmenter.Load(groupManager);
|
|
|
- this.Status = ModuleStatus.Initialized;
|
|
|
this.AfterInitialize();
|
|
|
+ this.Initialized = true;
|
|
|
+ this.Patterns = patterns;
|
|
|
}
|
|
|
- catch //(Exception e)
|
|
|
+ catch (Exception e)
|
|
|
{
|
|
|
- this.Status = ModuleStatus.FailToInitialize;
|
|
|
+ throw new TypeInitializationException(this.GetType().FullName, e);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ internal IPatternSet Patterns { get; private set; }
|
|
|
|
|
|
/// <summary>
|
|
|
/// 在初始化後引發。
|
|
@@ -123,7 +65,5 @@ namespace Yuuna.Contracts.Modules
|
|
|
/// <param name="g"></param>
|
|
|
/// <param name="p"></param>
|
|
|
protected abstract void BuildPatterns(IGroupManager g, IPatternBuilder p);
|
|
|
-
|
|
|
-
|
|
|
}
|
|
|
}
|