//计算虚工作:查找存在多个紧前工作的节点,循环每个节点的紧前工作是否存在实工作,不存在则是虚工作 private void virtualWork(Dictionary <string, NetWork> table) { foreach (KeyValuePair <string, NetWork> virtualWork in table.Where(c => c.Value.Precedence.Split(',').Length > 1)) { Double time = 0; NetWork work = virtualWork.Value; string[] pres = work.Precedence.Split(','); foreach (string pre in pres) { foreach (KeyValuePair <string, NetWork> item in table) { if (item.Value.Code.Trim() == pre.Trim()) { MainWork main = list.Where(c => c.Code == item.Value.Code).SingleOrDefault(); if (item.Value.Last != work.First) { Boolean isExistWork = false; foreach (KeyValuePair <string, NetWork> vir in table) { if (item.Value.Last == vir.Value.First && work.First == vir.Value.Last) { isExistWork = true; break; } } if (!isExistWork) { string virt = item.Value.Last.ToString() + ":" + work.First.ToString(); work.VirtualWork = !string.IsNullOrEmpty(work.VirtualWork) ? work.VirtualWork + "," + virt : virt; if (main != null && !string.IsNullOrEmpty(main.Code)) { if (main.WorkTime > time) { time = main.WorkTime; string path = string.IsNullOrEmpty(main.Path) ? work.First.ToString() : main.Path + "-" + work.First.ToString(); setList(work.Code, path, main.WorkTime + work.Time); } } } } else { //算实工作 if (main != null && !string.IsNullOrEmpty(main.Code)) { if (main.WorkTime + work.Time > time) { string path = string.IsNullOrEmpty(main.Path) ? work.Last.ToString() : main.Path + "-" + work.Last.ToString(); time = main.WorkTime + work.Time; setList(work.Code, path, time); } } } } } } //再次处理唯一紧前(紧前工作紧前多个) var single = table.Where(c => c.Value.Precedence.Split(',').Length == 1 && c.Value.Precedence != "").OrderBy(o => o.Value.Time).ToArray(); var multi = table.Where(c => c.Value.Precedence.Split(',').Length > 1).ToArray(); if (multi.Length > 0) { foreach (KeyValuePair <string, NetWork> mul in multi) { if (single.Length > 0) { foreach (KeyValuePair <string, NetWork> sing in single.OrderBy(o => o.Value.BeginTime)) { NetWork works = sing.Value; foreach (KeyValuePair <string, NetWork> item in table.OrderBy(o => o.Value.BeginTime)) { if (item.Value.Code == works.Precedence && item.Value.Precedence.Split(',').Length > 1 && mul.Value.Code == item.Value.Code) { MainWork main = list.Where(c => c.Code == item.Value.Code).SingleOrDefault(); if (main != null && !string.IsNullOrEmpty(main.Code)) { string path = string.IsNullOrEmpty(main.Path) ? works.Last.ToString() : main.Path + "-" + works.Last.ToString(); setList(works.Code, path, main.WorkTime + works.Time); } } } } } } } //处理没有线路的路径 foreach (KeyValuePair <string, NetWork> tb in table) { NetWork works = tb.Value; if (list.Where(c => c.Code == tb.Value.Code).Count() == 0) { foreach (KeyValuePair <string, NetWork> item in table.OrderBy(o => o.Value.BeginTime)) { if (item.Value.Code == works.Precedence) { if (item.Value.Precedence.Split(',').Length == 1) { MainWork main = list.Where(c => c.Code == item.Value.Code).SingleOrDefault(); if (main != null && !string.IsNullOrEmpty(main.Code)) { string path = string.IsNullOrEmpty(main.Path) ? works.Last.ToString() : main.Path + "-" + works.Last.ToString(); setList(works.Code, path, main.WorkTime + works.Time); } } else { double max = 0; MainWork main = null; string[] strs = item.Value.Precedence.Split(','); foreach (string pre in strs) { var ls = list.SingleOrDefault(c => c.Code == pre); if (ls != null) { if (max < ls.WorkTime) { max = ls.WorkTime; main = ls; } } } if (main != null && !string.IsNullOrEmpty(main.Code)) { string path = string.IsNullOrEmpty(main.Path) ? works.Last.ToString() : main.Path + "-" + works.Last.ToString(); setList(works.Code, path, main.WorkTime + works.Time); } } } } } } } int maxNode = Convert.ToInt32(nodeList.Max(o => o.Number)); foreach (KeyValuePair <string, NetWork> item in table) { NetWork work = item.Value; if (item.Value.Code.Contains("_@VIR")) { string vir = work.First + ":" + work.Last; work.VirtualWork = string.IsNullOrEmpty(work.VirtualWork) ? vir : work.VirtualWork + "," + vir; work.IsVir = true; } if (item.Value.Precedence.Contains("_@VIR") && work.Last != maxNode) { bool isVirExist = false; var precedence = table.Where(o => o.Value.Precedence.Contains(item.Value.Code)).ToArray(); foreach (var pre in precedence) { foreach (string str in pre.Value.Precedence.Split(',')) { if (str == item.Value.Code && !str.Contains("_@VIR")) { isVirExist = true; } } } if (!isVirExist) { work.VirtualWork = work.Last + ":" + maxNode; } } if (table.Where(o => o.Value.Precedence.Contains(work.Code)).ToArray().Length <= 0 && work.Last != maxNode) { work.VirtualWork = work.Last + ":" + maxNode; } } }
/*前编号算法:无紧前则编号为1,有唯一紧前则后编号为前编号,否则取最大小时对应的编号 * 后编号算法:无紧前则编号为MAX+1,有两个以上则按工时编号,工时小的编号小。有唯一紧前则以紧前 * 的后编号为前编号,按时间MAX+1。*/ private void calcNode(Dictionary <string, NetWork> table, DateTime beginTime) { table = table.OrderBy(c => c.Value.EndTime).ToDictionary(c => c.Key, o => o.Value); int maxNum = 2; foreach (KeyValuePair <string, NetWork> work in table) { work.Value.Last = maxNum; maxNum = maxNum + 1; } //无紧前算法 var netWork = table.Where(c => c.Value.Precedence == "" || c.Value.Precedence == null).ToArray(); var single = table.Where(c => c.Value.Precedence.Split(',').Length == 1 && c.Value.Precedence != "").OrderBy(o => o.Value.Time).ToArray(); var multi = table.Where(c => c.Value.Precedence.Split(',').Length > 1).ToArray(); if (netWork.Length > 0) { foreach (KeyValuePair <string, NetWork> item in netWork) { NetWork work = item.Value; work.First = 1; setList(work.Code, work.First + "-" + work.Last, work.Time); } } //唯一紧前(前工作的紧前没有多个) if (single.Length > 0) { foreach (KeyValuePair <string, NetWork> sing in single.OrderBy(o => o.Value.BeginTime)) { NetWork work = sing.Value; foreach (KeyValuePair <string, NetWork> item in table.OrderBy(o => o.Value.BeginTime)) { if (item.Value.Code == work.Precedence && item.Value.Precedence.Split(',').Length <= 1) { work.First = item.Value.Last; MainWork main = list.Where(c => c.Code == item.Value.Code).SingleOrDefault(); if (main != null && !string.IsNullOrEmpty(main.Code)) { string path = string.IsNullOrEmpty(main.Path) ? work.Last.ToString() : main.Path + "-" + work.Last.ToString(); setList(work.Code, path, main.WorkTime + work.Time); } } } } } //多个紧前 if (multi.Length > 0) { foreach (KeyValuePair <string, NetWork> mul in multi) { int nultiMax = 0; DateTime maxTime = beginTime; NetWork work = mul.Value; string[] pres = work.Precedence.Split(','); foreach (string pre in pres) { foreach (KeyValuePair <string, NetWork> item in table) { if (item.Value.Code == pre) { if (maxTime < item.Value.EndTime) { maxTime = item.Value.EndTime; nultiMax = item.Value.Last; MainWork main = list.Where(c => c.Code == item.Value.Code).SingleOrDefault(); if (main != null && !string.IsNullOrEmpty(main.Code)) { string path = string.IsNullOrEmpty(main.Path) ? work.Last.ToString() : main.Path + "-" + work.Last.ToString(); setList(work.Code, path, main.WorkTime + work.Time); } } } } } work.First = nultiMax; //再次处理唯一紧前(紧前工作紧前多个) if (single.Length > 0) { foreach (KeyValuePair <string, NetWork> sing in single) { NetWork works = sing.Value; foreach (KeyValuePair <string, NetWork> item in table) { if (item.Value.Code == works.Precedence && item.Value.Precedence.Split(',').Length > 1 && mul.Value.Code == item.Value.Code) { works.First = item.Value.Last; } } } } } } }