| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading;
- using System.Xml;
- using IwbZero.ToolCommon.StringModel;
- using WeEngine;
- using WeEngine.ComponentInfo;
- using WeEngine.Enum;
- using WeEngine.Packages;
- using Xunit;
- namespace WePlatform
- {
- public class PackageTest : WeEngineTestBase
- {
- private static string PackageId = "P1";
- private static string CompId1 = "CP1";
- private static string CompId2 = "CP2";
- private readonly string _baseFilePath = $"C:/WisdomExercise/WeEngine/Data/Packages/{PackageId}/";
- [Fact]
- public void PackageCreate()
- {
- CreatePackage();
- }
- #region PACKAGE
- private PackageNode CreatePackage()
- {
- var package = new PackageNode
- {
- Id = PackageId,
- InternalNo = PackageId,
- Name = $"方案包{PackageId}",
- Weights = 100,
- Children = new List<NodeBase>(),
- };
- for (int i = 1; i < 4; i++)
- {
- package.Children.Add(GetRound(i, package));
- }
- SavePackage(package);
- return package;
- }
- 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");
- CreateComponent();
- }
- 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);
- }
- }
- }
- private SceneRoundNode GetRound(int id, NodeBase 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<NodeBase>(),
- };
- 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, NodeBase 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<NodeBase>(),
- };
- 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, NodeBase 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<NodeBase>(),
- };
- 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;
- decimal min = 0, max = 0;
- if (flowDepth > 1)
- {
- var score = 100 / childCount;
- min = Convert.ToDecimal(score * (index - 1)) / Convert.ToDecimal(100); max = childCount == index ? 1 : Convert.ToDecimal(score * index) / Convert.ToDecimal(100);
- }
- var node = new FlowNode()
- {
- Id = $"FN{no}",
- Parent = parent,
- InternalNo = $"FN{id}",
- Name = $"节点{no}",
- PreComponent = parent?.NodeType == NodeType.SceneFlow
- ? ""
- : $"{CompId1}|[[[MinScore]=[{min} * {DefaultVariable.FlowScore}]],[[MaxScore]=[{max} * {DefaultVariable.FlowScore}]]]",
- FlowDepth = flowDepth,
- Children = new List<NodeBase>(),
- SceneInfos = new List<SceneNode>()
- };
-
- if (parent?.NodeType == NodeType.SceneFlow && node.InternalNo == "FN1" && parent.InternalNo=="SF2" && parent.Parent.InternalNo=="SFB1" )
- {
- node.PreComponent = $"{CompId2}|[[[WaitNodePath]=[SFB1.SF1.FN1]]]";
- }
- 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;
- }
- private void CreateComponent()
- {
- var eCp1 = new EngineComponent
- {
- Id = CompId1,
- Parameters = ":MinScore:最小分值:Y,:MaxScore:最大分值:Y",
- Name = "前置条件检验",
- ComponentScript =
- $@"<Content>
- <CpCreateVariables>
- <Variable>
- <Name>@CurrentNodeScore</Name>
- <Type>Number</Type>
- </Variable>
- <Comment></Comment>
- <BreakPoint>False</BreakPoint>
- </CpCreateVariables>
- <CpCreateVariables>
- <Variable>
- <Name>@MinScore</Name>
- <Type>Number</Type>
- </Variable>
- <Comment></Comment>
- <BreakPoint>False</BreakPoint>
- </CpCreateVariables>
- <CpCreateVariables>
- <Variable>
- <Name>@MaxScore</Name>
- <Type>Number</Type>
- </Variable>
- <Comment></Comment>
- <BreakPoint>False</BreakPoint>
- </CpCreateVariables>
- <CpSetVariables>
- <Content>[[@CurrentNodeScore]=[@PrevNodeScore]]</Content>
- <BreakPoint>False</BreakPoint>
- </CpSetVariables>
- <CpSetVariables>
- <Content>[[@MinScore]=[@@MinScore]]</Content>
- <BreakPoint>False</BreakPoint>
- </CpSetVariables>
- <CpSetVariables>
- <Content>[[@MaxScore]=[@@MaxScore]]</Content>
- <BreakPoint>False</BreakPoint>
- </CpSetVariables>
- <CpCheckCondition>
- <Condition>{"@CurrentNodeScore >= @MinScore || @CurrentNodeScore <= @MaxScore".FormatCode2Xml()}</Condition>
- </CpCheckCondition>
- </Content>",
- };
- var eCp2 = new EngineComponent
- {
- Id = CompId2,
- Parameters = ":WaitNodePath:需等待节点:Y",
- Name = "等待节点运行结束",
- ComponentScript =
- @"<Content>
- <CpNodeToWait>
- <WaitNode>@@WaitNodePath</WaitNode>
- </CpNodeToWait>
- </Content>",
- };
- string str = "<ComponentInfos>";
- str += eCp1.ToXmlString();
- str += eCp2.ToXmlString();
- str += "</ComponentInfos>";
- var xDoc = new XmlDocument { InnerXml = str };
- xDoc.Save($"{_baseFilePath}component.iwbx");
- }
- #endregion
- }
- }
|