From 10cf7cd72aca02eff680957a9ebe12f6ce0a1b7f Mon Sep 17 00:00:00 2001 From: jackfiled Date: Thu, 2 Nov 2023 13:24:50 +0800 Subject: [PATCH] =?UTF-8?q?add:=20DI=E5=8A=A0=E5=85=A5WeatherModule?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Katheryne.Tests/StringFormatterTests.cs | 1 + Katheryne/KatheryneChatRobot.cs | 6 +++--- Katheryne/Models/StringFormatter.cs | 2 +- Katheryne/ServiceCollectionExtensions.cs | 19 ++++++++++++++++++- .../Services/KatheryneChatRobotFactory.cs | 8 ++++---- 5 files changed, 27 insertions(+), 9 deletions(-) diff --git a/Katheryne.Tests/StringFormatterTests.cs b/Katheryne.Tests/StringFormatterTests.cs index 9f2703c..51a1440 100644 --- a/Katheryne.Tests/StringFormatterTests.cs +++ b/Katheryne.Tests/StringFormatterTests.cs @@ -54,6 +54,7 @@ public class StringFormatterTests Regex regex = new(".*?"); Match match = regex.Match("Test Input"); + Assert.True(formatter.IsFormat); Assert.Equal("Test Hello, Katheryne", formatter.Format(match.Groups)); } diff --git a/Katheryne/KatheryneChatRobot.cs b/Katheryne/KatheryneChatRobot.cs index 14b93e8..f441609 100644 --- a/Katheryne/KatheryneChatRobot.cs +++ b/Katheryne/KatheryneChatRobot.cs @@ -59,7 +59,7 @@ public class KatheryneChatRobot : IChatRobot if (answer.IsFormat) { string temp = answer.Format(match.Groups); - _logger.LogInformation("Format answer {} to {}.", + _logger.LogDebug("Format answer {} to {}.", answer.RowString, temp); result.Add(temp); } @@ -67,7 +67,7 @@ public class KatheryneChatRobot : IChatRobot { result.Add(answer.RowString); } - _logger.LogInformation("Moving to stage {} on input {}.", + _logger.LogDebug("Moving to stage {} on input {}.", _currentStage, input); break; } @@ -95,7 +95,7 @@ public class KatheryneChatRobot : IChatRobot _currentStage = transformer.NextStage; result.Add(_grammarTree[_currentStage].Answer.RowString); - _logger.LogInformation("Moving to stage {} with empty transform.", + _logger.LogDebug("Moving to stage {} with empty transform.", _currentStage); break; } diff --git a/Katheryne/Models/StringFormatter.cs b/Katheryne/Models/StringFormatter.cs index 9b37481..514618a 100644 --- a/Katheryne/Models/StringFormatter.cs +++ b/Katheryne/Models/StringFormatter.cs @@ -27,7 +27,7 @@ public class StringFormatter GetFormatTags(); } - public bool IsFormat => _formatTags.Count != 0; + public bool IsFormat => _formatTags.Count != 0 || _params.Count != 0; public string RowString => _originString; diff --git a/Katheryne/ServiceCollectionExtensions.cs b/Katheryne/ServiceCollectionExtensions.cs index a2a2b55..52883cb 100644 --- a/Katheryne/ServiceCollectionExtensions.cs +++ b/Katheryne/ServiceCollectionExtensions.cs @@ -1,6 +1,8 @@ using Katheryne.Abstractions; +using Katheryne.Modules; using Katheryne.Services; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; namespace Katheryne; @@ -10,6 +12,21 @@ public static class ServiceCollectionExtensions { collection.AddSingleton(); collection.AddSingleton(); - collection.AddSingleton(); + collection.AddSingleton(); + + collection.AddSingleton(serviceProvider => + { + var factory = new KatheryneChatRobotFactory( + serviceProvider.GetRequiredService(), + serviceProvider.GetRequiredService>(), + serviceProvider.GetRequiredService>(), + serviceProvider.GetRequiredService()); + + WeatherModule module = serviceProvider.GetRequiredService(); + + factory.Modules.Add(module.ModuleName, module); + + return factory; + }); } } \ No newline at end of file diff --git a/Katheryne/Services/KatheryneChatRobotFactory.cs b/Katheryne/Services/KatheryneChatRobotFactory.cs index 1b4bff4..7483ab2 100644 --- a/Katheryne/Services/KatheryneChatRobotFactory.cs +++ b/Katheryne/Services/KatheryneChatRobotFactory.cs @@ -13,10 +13,10 @@ public class KatheryneChatRobotFactory : IChatRobotFactory private readonly ILogger _robotLogger; private readonly DefaultChatRobot _defaultChatRobot; - private readonly Dictionary _modules = new(); - private Grammar? _grammar; + public Dictionary Modules { get; } = new(); + public string GrammarText { get; private set; } = string.Empty; public KatheryneChatRobotFactory(YamlDeserializerFactory deserializerFactory, @@ -37,7 +37,7 @@ public class KatheryneChatRobotFactory : IChatRobotFactory /// 编译文法失败抛出的异常 public void SetGrammar(string grammarText) { - _factoryLogger.LogInformation("Receive new grammar: {}.", grammarText); + _factoryLogger.LogDebug("Receive new grammar: {}.", grammarText); GrammarText = grammarText; IDeserializer deserializer = _deserializerFactory.GetDeserializer(); @@ -50,7 +50,7 @@ public class KatheryneChatRobotFactory : IChatRobotFactory { throw new GrammarException("Failed to parse lexical model.", ex); } - _grammar = new Grammar(new GrammarTree(model, _modules), + _grammar = new Grammar(new GrammarTree(model, Modules), model.RobotName, model.BeginStageName); }