2018-10-03 01:44:11 +00:00
|
|
|
|
using BotSharp.Core.Engines;
|
|
|
|
|
|
using BotSharp.Platform.Abstraction;
|
2018-09-28 22:25:55 +00:00
|
|
|
|
using BotSharp.Platform.Models;
|
2018-10-01 17:15:17 +00:00
|
|
|
|
using DotNetToolkit;
|
|
|
|
|
|
using Microsoft.Extensions.Configuration;
|
2018-10-03 01:44:11 +00:00
|
|
|
|
using Newtonsoft.Json;
|
2018-09-28 22:25:55 +00:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
2018-10-03 01:44:11 +00:00
|
|
|
|
using System.IO;
|
2018-10-01 17:15:17 +00:00
|
|
|
|
using System.Linq;
|
2018-09-28 22:25:55 +00:00
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
|
|
|
|
namespace BotSharp.Core
|
|
|
|
|
|
{
|
2018-10-01 17:15:17 +00:00
|
|
|
|
public abstract class PlatformBuilderBase<TAgent> where TAgent : AgentBase
|
2018-09-28 22:25:55 +00:00
|
|
|
|
{
|
2018-10-01 17:15:17 +00:00
|
|
|
|
public IAgentStorage<TAgent> Storage { get; set; }
|
|
|
|
|
|
|
|
|
|
|
|
public IConfiguration PlatformConfig { get; set; }
|
2018-09-28 22:25:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
2018-09-29 18:18:34 +00:00
|
|
|
|
public List<TAgent> GetAllAgents()
|
2018-09-28 22:25:55 +00:00
|
|
|
|
{
|
2018-10-01 17:15:17 +00:00
|
|
|
|
GetStorage();
|
|
|
|
|
|
|
|
|
|
|
|
return Storage.Query();
|
2018-09-28 22:25:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-10-03 01:44:11 +00:00
|
|
|
|
public TAgent LoadAgentFromFile<TImporter>(string dataDir) where TImporter : IAgentImporter<TAgent>, new()
|
|
|
|
|
|
{
|
|
|
|
|
|
var meta = LoadMeta(dataDir);
|
|
|
|
|
|
var importer = new TImporter();
|
|
|
|
|
|
|
|
|
|
|
|
importer.AgentDir = dataDir;
|
|
|
|
|
|
|
|
|
|
|
|
// Load agent summary
|
|
|
|
|
|
var agent = importer.LoadAgent(meta);
|
|
|
|
|
|
|
|
|
|
|
|
// Load user custom entities
|
|
|
|
|
|
importer.LoadCustomEntities(agent);
|
|
|
|
|
|
|
|
|
|
|
|
// Load agent intents
|
|
|
|
|
|
importer.LoadIntents(agent);
|
|
|
|
|
|
|
|
|
|
|
|
// Load system buildin entities
|
|
|
|
|
|
importer.LoadBuildinEntities(agent);
|
|
|
|
|
|
|
2018-10-03 06:00:07 +00:00
|
|
|
|
return agent;
|
2018-10-03 01:44:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private AgentImportHeader LoadMeta(string dataDir)
|
|
|
|
|
|
{
|
|
|
|
|
|
// load meta
|
|
|
|
|
|
string metaJson = File.ReadAllText(Path.Combine(dataDir, "meta.json"));
|
|
|
|
|
|
|
|
|
|
|
|
return JsonConvert.DeserializeObject<AgentImportHeader>(metaJson);
|
|
|
|
|
|
}
|
2018-09-28 22:25:55 +00:00
|
|
|
|
|
2018-09-29 18:18:34 +00:00
|
|
|
|
public TAgent GetAgentById(string agentId)
|
2018-09-28 22:25:55 +00:00
|
|
|
|
{
|
2018-10-01 17:15:17 +00:00
|
|
|
|
GetStorage();
|
|
|
|
|
|
|
|
|
|
|
|
return Storage.FetchById(agentId);
|
2018-09-28 22:25:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-09-29 18:18:34 +00:00
|
|
|
|
public TAgent GetAgentByName(string agentName)
|
2018-09-28 22:25:55 +00:00
|
|
|
|
{
|
2018-10-01 17:15:17 +00:00
|
|
|
|
GetStorage();
|
|
|
|
|
|
|
|
|
|
|
|
return Storage.FetchByName(agentName);
|
2018-09-28 22:25:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-09-29 18:18:34 +00:00
|
|
|
|
public virtual bool SaveAgent(TAgent agent)
|
2018-09-28 22:25:55 +00:00
|
|
|
|
{
|
2018-10-01 17:15:17 +00:00
|
|
|
|
GetStorage();
|
|
|
|
|
|
|
2018-09-28 22:25:55 +00:00
|
|
|
|
// default save agent in FileStorage
|
2018-10-01 17:15:17 +00:00
|
|
|
|
Storage.Persist(agent);
|
2018-09-28 22:25:55 +00:00
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
2018-10-01 17:15:17 +00:00
|
|
|
|
|
|
|
|
|
|
private IAgentStorage<TAgent> GetStorage()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (Storage == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
string storageName = PlatformConfig.GetValue<String>("AgentStorage");
|
|
|
|
|
|
switch (storageName)
|
|
|
|
|
|
{
|
|
|
|
|
|
case "AgentStorageInRedis":
|
|
|
|
|
|
Storage = Activator.CreateInstance<AgentStorageInRedis<TAgent>>();
|
|
|
|
|
|
break;
|
|
|
|
|
|
case "AgentStorageInMemory":
|
|
|
|
|
|
Storage = Activator.CreateInstance<AgentStorageInMemory<TAgent>>();
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return Storage;
|
|
|
|
|
|
}
|
2018-09-28 22:25:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|