add: DI加入WeatherModule

This commit is contained in:
jackfiled 2023-11-02 13:24:50 +08:00
parent e477c015af
commit 10cf7cd72a
5 changed files with 27 additions and 9 deletions

View File

@ -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));
}

View File

@ -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;
}

View File

@ -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;

View File

@ -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<YamlDeserializerFactory>();
collection.AddSingleton<DefaultChatRobot>();
collection.AddSingleton<IChatRobotFactory, KatheryneChatRobotFactory>();
collection.AddSingleton<WeatherModule>();
collection.AddSingleton<IChatRobotFactory, KatheryneChatRobotFactory>(serviceProvider =>
{
var factory = new KatheryneChatRobotFactory(
serviceProvider.GetRequiredService<YamlDeserializerFactory>(),
serviceProvider.GetRequiredService<ILogger<KatheryneChatRobotFactory>>(),
serviceProvider.GetRequiredService<ILogger<KatheryneChatRobot>>(),
serviceProvider.GetRequiredService<DefaultChatRobot>());
WeatherModule module = serviceProvider.GetRequiredService<WeatherModule>();
factory.Modules.Add(module.ModuleName, module);
return factory;
});
}
}

View File

@ -13,10 +13,10 @@ public class KatheryneChatRobotFactory : IChatRobotFactory
private readonly ILogger<KatheryneChatRobot> _robotLogger;
private readonly DefaultChatRobot _defaultChatRobot;
private readonly Dictionary<string, IParamsModule> _modules = new();
private Grammar? _grammar;
public Dictionary<string, IParamsModule> Modules { get; } = new();
public string GrammarText { get; private set; } = string.Empty;
public KatheryneChatRobotFactory(YamlDeserializerFactory deserializerFactory,
@ -37,7 +37,7 @@ public class KatheryneChatRobotFactory : IChatRobotFactory
/// <exception cref="GrammarException">编译文法失败抛出的异常</exception>
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);
}