| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514 |
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using System.Threading;
- using System.Xml;
- using IwbZero.IwbBase;
- using IwbZero.ToolCommon.StringModel;
- using Microsoft.VisualStudio.TestTools.UnitTesting;
- using WeEngine.Enum;
- using WeEngine.Packages;
- namespace WeEngine.Tests.Module
- {
- [TestClass]
- public class ModuleTest
- {
- [TestMethod]
- public void Test()
- {
- for (int i = 0; i < 20; i++)
- {
- var code = GenerateCode();
- Debug.WriteLine($"========================> {code} <========================");
- Thread.Sleep(new Random().Next(6000,10000));
- }
- }
- private string GenerateCode()
- {
- TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1);
- string str = Convert.ToInt64(ts.TotalSeconds).ToString();
- // Debug.WriteLine($"========================> {str} ");
- var arr = str.ToCharArray();
- char temp1 = arr[0], temp2 = arr[1], temp3 = arr[2], temp4 = arr[4];
- arr[0] = arr[arr.Length - 2];
- arr[1] = arr[arr.Length - 4];
- arr[2] = arr[arr.Length - 5];
- arr[4] = arr[arr.Length - 3];
- arr[arr.Length - 3] = temp4;
- arr[arr.Length - 2] = temp3;
- arr[arr.Length - 4] = temp2;
- arr[arr.Length - 5] = temp1;
- //var code = $"{str.Substring(5)}{str.Substring(0, 5)}";
- var code = new string(arr);
- return code;
- }
- [TestMethod]
- public void Test1()
- {
- //ScoreEvalModuleContextBase scoreEvalModule = new ScoreEvalModuleContextBase("EvolutionScoreEvalModule");
- //ScoreEvalModuleContextBase scoreEvalModule2 = new ScoreEvalModuleContextBase("QuestionScoreEvalModule");
- //scoreEvalModule.MatchKeyWord("11",);
- //scoreEvalModule2.MatchKeyWord("22");
- }
- private static string PackageNo = "P1";
- private readonly string _baseFilePath = $"C:/WisdomExercise/WeEngine/Data/Packages/{PackageNo}/";
-
- [TestMethod]
- public void Test2()
- {
- var package = new PackageNode
- {
- InternalNo = PackageNo,
- Name = $"方案包{PackageNo}",
- Weights = 100,
-
- Children = new List<NodeBase>(),
- };
- for (int i = 1; i < 4; i++)
- {
- package.Children.Add(GetRound(i , package));
- }
- SavePackage(package);
- }
- [TestMethod]
- public void Test3()
- {
- var xmlDoc = new XmlDocument();
- xmlDoc.Load(GetFilePath());
- var xmlNode = IwbXmlNode.ParseGenericXml(xmlDoc.LastChild.InnerXml);
- var package = new PackageNode().BuildNodeByXmlNode(xmlNode);
- var str = package.ToXmlString();
- var xmlDoc2 = new XmlDocument { InnerXml = str };
- xmlDoc2.Save($"{_baseFilePath}package.iwbx");
- //xmlDoc2.Save(GetFilePath("package"));
- //xmlDoc2.Save(GetFilePath("package2"));
- }
- [TestMethod]
- public void Test4()
- {
- var xmlDoc = new XmlDocument();
- xmlDoc.Load(GetFilePath());
- var xmlNode = IwbXmlNode.ParseGenericXml(xmlDoc.LastChild.InnerXml);
- var package = new PackageNode().BuildNodeByXmlNode(xmlNode);
- var package2 = (PackageNode)package.Clone();
- package2.RunningId = "G101";
- //PackageCache.Instance.SetCache(package2);
- //package2.RunnerManager.Instance.Run(package2, OperationType.Start);
- //string word = "lmtp73dj";
- //package2.RunnerModule.Run($"{package2.Id}|{word}|民政局", OperationType.MatchKey);
- //package2.RunnerModule.Run($"{package2.Id}|{word}|消防局", OperationType.MatchKey);
- //word = "pqja5";
- //var round = package2.CurRoundInfo.ActualScore;
- //package2.RunnerModule.Run($"{package2.Id}|{word}|消防局", OperationType.MatchKey);
- //word = "6";
- //package2.RunnerModule.Run($"{package2.Id}|{word}|消防局", OperationType.MatchKey);
- //word = "8";
- //package2.RunnerModule.Run($"{package2.Id}|{word}|消防局", OperationType.MatchKey);
- var xmlDoc2 = new XmlDocument { InnerXml = package2.ToXmlString() };
- xmlDoc2.Save(GetFilePath("package3"));
- }
-
- public static string GetFilePath(string fileName = "package")
- {
- var path = AppDomain.CurrentDomain.BaseDirectory;
- path = path.Substring(0, path.IndexOf("WeEngine.Tests", StringComparison.Ordinal));
- string filePath = $"{path}WeEngine.Tests/Module/{fileName}.xml";
- return filePath;
- }
- private Random _random = new Random(unchecked((int)DateTime.Now.Ticks));
- private void SavePackage(PackageNode package)
- {
- var guides = new List<GuideNode>();
- var flows= new List<SceneFlowNode>();
- var scenes = new List<SceneNode>();
- var behaviors = new List<BehaviorNode>();
- foreach (var round in package.Children)
- {
- AddGuide(round, ref guides);
- foreach (var block in round.Children)
- {
- AddGuide(block, ref guides);
- flows.AddRange(block.Children.Select(a => (SceneFlowNode)a));
- }
- }
- flows = flows.IwbDistinct(a => a.Id).ToList();
- if (flows.Any())
- {
- var str = "";
- foreach (var flow in flows)
- {
- AddGuide(flow, ref guides);
- if (flow.HasChild)
- {
- foreach (var nodeBase in flow.Children)
- {
- var child = (FlowNode) nodeBase;
- AddScene(child, ref scenes, ref guides);
- }
- }
- str += flow.ToXmlString();
- }
- if (str.IsNotEmpty())
- {
- str = $"<SceneFlows>{str}</SceneFlows>";
- var xDoc = new XmlDocument {InnerXml = str};
- xDoc.Save($"{_baseFilePath}flow.iwbx");
- }
- }
- scenes = scenes.IwbDistinct(a => a.Id).ToList();
- if (scenes.Any())
- {
- var str = "";
- foreach (var scene in scenes)
- {
- AddGuide(scene, ref guides);
- if (scene.HasChild)
- {
- foreach (var nodeBase in scene.Children)
- {
- var child = (BehaviorNode)nodeBase;
- AddGuide(child, ref guides);
- behaviors.Add(child);
- }
- }
- str += scene.ToXmlString();
- }
- if (str.IsNotEmpty())
- {
- str = $"<SceneInfos>{str}</SceneInfos>";
- var xDoc = new XmlDocument { InnerXml = str };
- xDoc.Save($"{_baseFilePath}scene.iwbx");
- }
- }
- behaviors = behaviors.IwbDistinct(a => a.Id).ToList();
- if (behaviors.Any())
- {
- var str = "";
- foreach (var behavior in behaviors)
- {
- str += behavior.ToXmlString();
- }
- if (str.IsNotEmpty())
- {
- str = $"<Behaviors>{str}</Behaviors>";
- var xDoc = new XmlDocument { InnerXml = str };
- xDoc.Save($"{_baseFilePath}behavior.iwbx");
- }
- }
- guides = guides.IwbDistinct(a => a.Id).ToList();
- if (guides.Any())
- {
- var str = "";
- foreach (var guide in guides)
- {
- str += guide.ToXmlString();
- }
- if (str.IsNotEmpty())
- {
- str = $"<GuideInfos>{str}</GuideInfos>";
- var xDoc = new XmlDocument { InnerXml = str };
- xDoc.Save($"{_baseFilePath}guide.iwbx");
- }
- }
- var result = package.ToXmlString();
- var xmlDoc2 = new XmlDocument { InnerXml = result };
- xmlDoc2.Save($"{_baseFilePath}package.iwbx");
- }
- private void AddGuide(NodeBase node, ref List<GuideNode> guides)
- {
- if (node.GuideInfos != null && node.GuideInfos.Any())
- {
- guides.AddRange(node.GuideInfos);
- }
- }
- private void AddScene(FlowNode node, ref List<SceneNode> scenes, ref List<GuideNode> guides)
- {
- AddGuide(node, ref guides);
- if (node.SceneInfos != null && node.SceneInfos.Any())
- {
- scenes.AddRange(node.SceneInfos);
- }
- if (node.HasChild)
- {
- foreach (var nodeBase in node.Children)
- {
- var child = (FlowNode) nodeBase;
- AddScene(child,ref scenes,ref guides);
- }
- }
- }
- #region www
- private SceneRoundNode GetRound(int id, PackageNode parent = null)
- {
- _random = new Random(unchecked((int) DateTime.Now.Ticks) + id);
- var node = new SceneRoundNode()
- {
- InternalNo = $"R{id}",
- Parent = parent,
- Id = $"R{id}",
- Name = $"轮次{id}",
- SceneCount = 2,
- ControlRate = 80,
- RoundFullScore = 500,
- RoundIndex = id,
- Children = new List<SceneFlowBlockNode>(),
- };
- node.Children.Add(GetBlock(1, node));
- for (int i = 2; i <= _random.Next(3, 4); i++)
- {
- node.Children.Add(GetBlock(i, node, 1));
- }
- return node;
- }
- private SceneFlowBlockNode GetBlock(int id, SceneRoundNode parent = null, int t = 0)
- {
- var node = new SceneFlowBlockNode()
- {
- Id = $"SFB{id}",
- Parent = parent,
- InternalNo = $"SFB{id}",
- Name = $"情景流块{id}",
- BlockType = t == 0 ? SceneFlowBlockType.Objective : SceneFlowBlockType.Evolutionary,
- Weights = _random.Next(0, 1) == 0 ? 50 : 100,
- Children = new List<SceneFlowNode>(),
- };
- for (int i = 1; i <= (t == 0 ? 2 : _random.Next(2, 3)); i++)
- {
- Thread.Sleep(50);
- node.Children.Add(GetFlow(i, node));
- }
- return node;
- }
- private SceneFlowNode GetFlow(int id, SceneFlowBlockNode parent = null)
- {
- _random = new Random(unchecked((int) DateTime.Now.Ticks) + id);
- var no = $"{DateTime.Now:mmssffff}";
- var node = new SceneFlowNode()
- {
- Id = $"SF{no}",
- Parent = parent,
- InternalNo = $"SF{id}",
- Name = $"情景流_{_random.Next(2, 5).GetRandomChinese()}",
- Weights = _random.Next(0, 1) == 0 ? 50 : 100,
- Children = new List<FlowNode>(),
- };
- var count = _random.Next(2, 3);
- node.Children.Add(GetFlowNode(1, ref count, node));
- //for (int i = 1; i <= _random.Next(1, 3); i++)
- //{
- // var depth = _random.Next(2, 3);
- // node.Children.Add(GetFlowNode($"{no}_{i}", ref depth, node));
- //}
- return node;
- }
- private FlowNode GetFlowNode(int id, ref int depth, NodeBase parent = null,int childCount=1,int index=0)
- {
- _random = new Random(depth * unchecked((int) DateTime.Now.Ticks) + id);
- var no = $"{DateTime.Now:mmssffff}";
- var flowDepth = parent != null && parent.NodeType != NodeType.SceneFlow
- ? (((FlowNode) parent).FlowDepth + 1)
- : 1;
- int min = 0, max = 0;
- if (flowDepth>1)
- {
- var score = 100 / childCount;
- min = score * (index - 1); max = childCount == index ? 100 : score * index;
- }
- var node = new FlowNode()
- {
- Id = $"FN{no}",
- Parent = parent,
- InternalNo = $"FN{id}",
- Name = $"节点{no}",
- PreComponent = parent?.NodeType == NodeType.SceneFlow
- ? ""
- : $"@FlowScore >{min} && @FlowScore<={max}",
- FlowDepth = flowDepth,
- Children = new List<NodeBase>(),
- SceneInfos = new List<SceneNode>()
- };
- if (depth == 3&&node.InternalNo == "FN1")
- {
- node.PostComponent = "RunFunSetVariableWithRp('FnScore','@NodeScore','N44')";
- }
- if (depth == 3&&node.InternalNo == "FN2")
- {
- node.PreComponent = "RunFunWaitNodeWithRp('N5','@FnScore > 20')";
- }
- for (int i = 1; i <= _random.Next(2, 3); i++)
- {
- Thread.Sleep(20);
- node.SceneInfos.Add(GetSceneInfo(i, node));
- }
- if (depth > 0)
- {
- depth--;
- var count = _random.Next(2, 3);
- for (int i = 1; i <= count; i++)
- {
- Thread.Sleep(25);
- node.Children.Add(GetFlowNode(i, ref depth, node, count,i));
- }
- }
- return node;
- }
- private SceneNode GetSceneInfo(int id, NodeBase parent = null)
- {
- _random = new Random(unchecked((int) DateTime.Now.Ticks) + id);
- var no = $"{DateTime.Now:mmssffff}";
- var scene = new SceneNode()
- {
- Id = $"SN{no}",
- Parent = parent,
- InternalNo = $"SN{id}",
- Name = $"情景_{_random.Next(2, 5).GetRandomChinese()}",
- Description = _random.Next(50, 150).GetRandomChinese(),
- Weights = _random.Next(0, 1) == 0 ? 50 : 100,
- Children = new List<NodeBase>()
- };
- for (int i = 1; i <= _random.Next(2, 4); i++)
- {
- Thread.Sleep(15);
- scene.Children.Add(GetBehavior(i, scene));
- }
- return scene;
- }
- private BehaviorNode GetBehavior(int id, NodeBase parent = null)
- {
- _random = new Random(unchecked((int) DateTime.Now.Ticks) + id);
- var no = $"{DateTime.Now:mmssffff}";
- string[] allRoles = "消防局,公安局,民政局,交通局,应急指挥中心,防台防汛办,水利局,电力公司".StrToArray();
- string[] allTag1 = "对上,对下,对相关,对媒体".StrToArray();
- string[] allTag2 = "掌控力,研判力,决策力,协调力,舆论引导力".StrToArray();
- var str = "";
- {
- var role = allRoles[_random.Next(0, allRoles.Length - 1)];
- str += role;
- allRoles.RemoveFromArray(role);
- }
- if (_random.Next(0, 1)>0)
- {
- var role1 = allRoles[_random.Next(0, allRoles.Length - 1)];
- str += $",{role1}";
- allRoles.RemoveFromArray(role1);
- if (_random.Next(0, 2) > 1)
- {
- var role2 = allRoles[_random.Next(0, allRoles.Length - 1)];
- str += $",{role2}";
- allRoles.RemoveFromArray(role2);
- if (_random.Next(0, 3) > 2)
- {
- var role3 = allRoles[_random.Next(0, allRoles.Length - 1)];
- str += $",{role3}";
- allRoles.RemoveFromArray(role3);
- }
- }
- }
- _random = new Random(unchecked((int)DateTime.Now.Ticks) * allTag1.Length +id);
- var tag = $"{allTag1[_random.Next(0, allTag1.Length - 1)]},{allTag2[_random.Next(0, allTag2.Length - 1)]}";
- _random = new Random(unchecked((int) DateTime.Now.Ticks) * allTag2.Length + id);
- var node = new BehaviorNode(GetKeyWords())
- {
- Id = $"B{no}",
- Parent = parent,
- InternalNo = $"B{id}",
- Name = $"行为_{_random.Next(2, 5).GetRandomChinese()}",
- Description = _random.Next(50, 150).GetRandomChinese(),
- Weights = _random.Next(0, 1) == 0 ? 50 : 100,
- BehaviorTag = tag,
- BehaviorRole = str.StrToArray(),
- BehaviorRoleLogic = BehaviorRoleLogicType.Or,
- BehaviorScoreType = BehaviorScoreType.Normal,
- };
- var ran = _random.Next(0, 10);
- if (ran > 9)
- {
- node.BehaviorScoreType = BehaviorScoreType.ImportantNegative;
- }
- else if (ran > 8)
- {
- node.BehaviorScoreType = BehaviorScoreType.Negative;
- }
- return node;
- }
- private string GetKeyWords()
- {
- _random = new Random(unchecked((int) DateTime.Now.Ticks));
- var ran = _random.Next(0, 10);
- string str;
- if (ran >= 8)
- {
- str = $"[{RandomKeyWord(_random.Next(4, 9))}:30],[{RandomKeyWord(_random.Next(4, 9))}:70]";
- }
- else if (ran >= 6)
- {
- str = $"[{RandomKeyWord(_random.Next(4, 9))}:40],[{RandomKeyWord(_random.Next(4, 9))}:60]";
- }
- else if (ran >= 4)
- {
- str = $"[{RandomKeyWord(_random.Next(4, 9))}:60],[[{RandomKeyWord(_random.Next(4, 9))},{RandomKeyWord(_random.Next(4, 9))}]:40]";
- }
- else if (ran >= 2)
- {
- str = $"[{RandomKeyWord(_random.Next(4, 9))}:70],[[{RandomKeyWord(_random.Next(4, 9))},{RandomKeyWord(_random.Next(4, 9))},{RandomKeyWord(_random.Next(4, 9))}]:30]";
- }
- else
- {
- str = $"[{RandomKeyWord(_random.Next(4, 9))}:100]";
- }
- return $"[{str}]";
- }
- private string RandomKeyWord(int len)
- {
- _random = new Random(unchecked((int) DateTime.Now.Ticks)+len);
- char[] arr = "qwertyuiopasdfghjklzxcvbnm1234567890".ToCharArray();
- var str = "";
- while (str.Length < len)
- {
- str += arr[_random.Next(0, arr.Length)];
- }
- return str;
- }
-
- #endregion
- }
- }
|