public NetworkThread(double[][] Matrix, string[] desc, int threadNum) { this.ThreadNum = threadNum; this.Matrix = Matrix; this.NodeDesc = desc; Tree = GPSSNode.BuildTree(this.Matrix, ThreadNum); }
//добавление кода в узлы private void BuildCode(GPSSNode tree, int num = 0) { if (tree.Children.Count == 0 && GPSSNode.Last == tree.Name) { AddNodeCode(networkData.Threads[num].GetNextNodeDesc, tree, num); } else { for (int index = 0; index < tree.Children.Count; ++index) { if ((tree.Name + 1) == tree.Children[index].Name) { AddNodeCode(networkData.Threads[num].GetNextNodeDesc, tree, num); BuildCode(tree.Children[index], num); } else { if (tree.Children.Count > 2) { tree.Transfers[index] = tree.Transfers[index] / (1 - tree.Transfers[0]); } //сократить tree.NodeCode += CodeBuilder.AddTransferCode(tree, tree.Transfers[index], tree.Children[index], num); } } } }
public static GPSSNode BuildTree(double[][] matrix, int threadNum) { var node = new GPSSNode(null, 0, threadNum); node.IsVisited = true; Last = matrix.Length - 1; var visitedNodes = new List <GPSSNode>(); for (int i = 0; i < matrix.Length; i++) { if (!AreAllChildrenVisited(node)) { for (int k = 0; k < node.children.Count; k++) { if (!node.children[k].IsVisited) { node.children[k].IsVisited = true; node = node.children[k]; break; } } } for (int j = 0; j < matrix.Length; j++) { if (visitedNodes.Exists(x => x.Name == node.Name)) { break; } if (matrix[i][j] != 0 && matrix[i][j] < 1) { node.children.Add(new GPSSNode(node, j, threadNum)); node.transfers.Add(matrix[i][j]); } if (matrix[i][j] == 1) { node.children.Add(new GPSSNode(node, j, threadNum)); node.transfers.Add(1); break; } } if (node.children.Count == 0) { node.IsVisited = true; while (AreAllChildrenVisited(node)) { node.IsVisited = true; visitedNodes.Add(node); node = node.Parent; i--; if (node.Parent == null) { return(node); } } } } return(node); }
private static bool AreAllChildrenVisited(GPSSNode node) { if (node.children.Count == 0) { return(true); } for (int i = 0; i < node.children.Count; i++) { if (!node.children[i].IsVisited) { return(false); } } return(true); }
private static void AddNodeCode(string nodeType, GPSSNode node, int threadNum = 0) { string[] param = nodeType.Split(' '); string res = ""; switch (param[0]) { case "GENERATE": res = $"{param[0]} {param[1]}({string.Join(",", param.Skip(2))})\n" + $"queue net\nqueue net_{threadNum+1}\n"; break; case "TERMINATE": res = $"depart net\ndepart net_{threadNum+1}\n" + $"{nodeType}\n\n"; break; case "FACILITY_ONECHANNEL": res = AddQueue(node.Name, threadNum) + $"SEIZE b{node.Name}\n" + AddDepart1(node.Name, threadNum) + $"ADVANCE ({param[1]}({string.Join(",", param.Skip(2))}))\n" + $"RELEASE b{node.Name}\n" + AddDepart2(node.Name, threadNum); break; case "FACILITY_MULTICHANNEL": res = AddQueue(node.Name, threadNum) + $"b_{node.Name} STORAGE {param[1]} \n" + $"ENTER b{node.Name}\n" + AddDepart1(node.Name, threadNum) + $"ADVANCE ({param[2]}({string.Join(",", param.Skip(3))}))\n" + $"LEAVE b{node.Name}\n" + AddDepart2(node.Name, threadNum); break; } //ИЗМЕНИТЬ node.NodeCode = node.NodeCode.Substring(0, node.NodeCode.IndexOf(' ') + 1) + res + node.NodeCode.Substring(node.NodeCode.IndexOf(' ') + 1); }
//обход дерева для записи в строку и запись дерева в массив private void ShowCode(GPSSNode tree) { if (visited.Exists((x => x.Name == tree.Name))) { return; } if (tree.Children.Count == 0 && GPSSNode.Last == tree.Name) { visited.Add(tree); Code += tree.NodeCode; } else { visited.Add(tree); for (int i = 0; i < tree.Children.Count; i++) { if (!visited.Exists(x => x.Name == tree.Children[i].Name)) { Code += tree.NodeCode; ShowCode(tree.Children[i]); } } } }
private static string AddTransferCode(GPSSNode node, double option, GPSSNode childNode, int threadNum) { return(string.Format("TRANSFER {0:N2},,label_{1}_{2}\n", option, childNode.Name, threadNum + 1) .Replace(" 0,", " 0.")); }
public GPSSNode(GPSSNode Parent, int Name, int threadNum = 0) { this.Parent = Parent; this.Name = Name; NodeCode = $"label_{Name}_{threadNum} "; }