备份:制造多种切片的分析方式

代码:

  1 using System;
  2 using AnalyseSLN;
  3 using Test_Tools.分析SLN内容;
  4 using TestModel;
  5 using System.Collections.Generic;
  6 using System.Linq;
  7 using System.IO;
  8 using Newtonsoft.Json;
  9 using Newtonsoft.Json.Linq;
 10 using System.Text.RegularExpressions;
 11 using Test_Tools.分析SLN内容.SLNModel;
 12 namespace Test_Tools
 13 {
 14 
 15     class Program 
 16     {
 17         public static void Mu()
 18         {
 19             var Folder_path = @"E:\ATEST\Folder\data_updated_data_2021";
 20             var Save_Folder_path = @"D:\VS2017\WorkSpace\Static_Analyse\Analyse_WorkSpace";
 21             string[] Json_paths =
 22                       Directory.GetFiles(Save_Folder_path, "*.json", SearchOption.TopDirectoryOnly);
 23             List<SolutionModel> SolutionModels = new List<SolutionModel>();
 24             //string JsonPath= @"D:\VS2017\WorkSpace\Static_Analyse\Analyse_WorkSpace\ElasticApmAgent.json";
 25             foreach (string JsonPath in Json_paths)
 26             {
 27                 Console.WriteLine(JsonPath);
 28                 try
 29                 {
 30                     OldSolutionModel OldsolutionModel = JsonConvert.DeserializeObject<OldSolutionModel>(File.ReadAllText(JsonPath));
 31                     var ProjectCricps = CricpAnalyseAsync.ExtractCricps(OldsolutionModel.Projects);
 32                     List<CricpModel> ProjectInterSectCricps = CricpAnalyseAsync.ExtractInterSectCricps(ProjectCricps);
 33                     SolutionModel solutionModel = new SolutionModel();
 34                     solutionModel.Name = OldsolutionModel.Name;
 35                     solutionModel.PeojectNum = OldsolutionModel.PeojectNum;
 36                     solutionModel.Multi_deps = OldsolutionModel.Multi_deps;
 37                     solutionModel.Conflict_deps = OldsolutionModel.Conflict_deps;
 38                     solutionModel.ConflictWarn_deps = OldsolutionModel.ConflictWarn_deps;
 39                     solutionModel.Projects = OldsolutionModel.Projects;
 40                     solutionModel.ProjectCricps = OldsolutionModel.ProjectCricps;
 41                     solutionModel.ProjectInterSectCricps = ProjectInterSectCricps;
 42                     string Json_sln = JsonConvert.SerializeObject(solutionModel);
 43                     JsonUtil.Save_json(JsonPath, Json_sln);
 44                     SolutionModels.Add(solutionModel);
 45                 }
 46                 catch (Exception ex)
 47                 {
 48                     continue;
 49                 }
 50             }
 51             string Json_slns = JsonConvert.SerializeObject(SolutionModels);
 52             JsonUtil.Save_json(Save_Folder_path + @"\WholeAnalyse.json", Json_slns);
 53         }
 54         static public List<List<string>> DistinctList(List<List<string>> OriginalListCrpcps)
 55         {
 56             List<string> WholeFrm = new List<string>();
 57             List<List<string>> DistinctListCrpcps = new List<List<string>>();
 58             foreach (List<string> Original in OriginalListCrpcps)
 59             {
 60                 foreach (string ori in Original)
 61                 {
 62                     if (!WholeFrm.Contains(ori))
 63                     {
 64                         WholeFrm.Add(ori);
 65                     }
 66                 }
 67                 if (DistinctListCrpcps.Count() == 0)
 68                 {
 69                     DistinctListCrpcps.Add(Original);
 70                     continue;
 71                 }
 72                 bool cur = false;
 73                 foreach (List<string> Distinct in DistinctListCrpcps)
 74                 {
 75                     foreach (var Ori in Original)
 76                     {
 77                         if (!Distinct.Contains(Ori))
 78                         {
 79                             cur = true;
 80                         }
 81                     }
 82                 }
 83                 if (cur)
 84                 {
 85                     DistinctListCrpcps.Add(Original);
 86                 }
 87             }
 88             //所有的切片,如果这个切片被其他切片包含,那么删除这个切片,
 89             //这个切片里面所有的东西都在另一个切片里面
 90             List<List<string>> SimpleDistinctListCrpcps = new List<List<string>>();
 91             foreach (var Dis in DistinctListCrpcps)
 92             {
 93                 foreach (var DisOther in DistinctListCrpcps)
 94                 {
 95                     //除了本身,如果这个的所有东西都在另一个切片里面,那么这个就要删除
 96                     if ((Dis != DisOther) && (Dis.All(x => DisOther.Contains(x))))
 97                     {
 98                         SimpleDistinctListCrpcps.Add((Dis));
 99                     }
100                 }
101             }
102             foreach (var sim in SimpleDistinctListCrpcps)
103             {
104                 DistinctListCrpcps.Remove(sim);
105             }
106             return DistinctListCrpcps;
107         }
108         static public List<SimpleCountCricp> BigDistinctCricps(List<SimpleCountCricp> Result)
109         {
110             //自定义其他一个大切片,只要这里面的平台在其他切片里面的平台出现过,那么就认为这些是兼容的
111             //针对每一个文件进行分析
112             foreach (var Project in Result)
113             {
114                 var CricpList = Project.Cricps;
115                 List<List<string>> BigCompatibleCricpList = new List<List<string>>();
116                 foreach (var Cric in CricpList)
117                 {
118                     //根绝每一个切片,制造一个大的兼容切片
119                     List<string> BigList = Cric;
120                     foreach (var CricCheck in CricpList)
121                     {
122                         //这个切片里面有一个在这个里面
123                         if (Cric != CricCheck && (Cric.Any(x => CricCheck.Contains(x))))
124                         {
125                             BigList = BigList.Union(CricCheck).ToList();
126                         }
127                     }
128                     BigCompatibleCricpList.Add(BigList);
129                 }
130                 BigCompatibleCricpList = DistinctList(BigCompatibleCricpList);
131                 Project.Cricps = BigCompatibleCricpList;
132                 Project.CricpNum = BigCompatibleCricpList.Count();
133             }
134             return Result;
135         }
136         static public List<SimpleCountCricp> CoreFrameworkCompatibleCricps(List<SimpleCountCricp> Result)
137         {
138             foreach (var Project in Result)
139             {
140                 var CricpList = Project.Cricps;
141                 List<List<string>> CompatibleCricpList = new List<List<string>>();
142                 bool CompatibleCore = true;
143                 bool CompatibleFramework = true;
144                 List<string> CoreCompatible = new List<string>();
145                 List<string> FrameworkCompatible = new List<string>();
146                 foreach (var Cric in CricpList)
147                 {
148                     if (Cric.Any(x => x.Contains(".NETCoreApp")))
149                     {
150                         CompatibleFramework = false;
151                         CoreCompatible = CoreCompatible.Union(Cric).ToList();
152                         CoreCompatible = CoreCompatible.Distinct().ToList();
153                     }
154                     else if (Cric.Any(x => x.Contains(".NETFramework")))
155                     {
156                         CompatibleCore = false;
157                         FrameworkCompatible = FrameworkCompatible.Union(Cric).ToList();
158                         FrameworkCompatible = FrameworkCompatible.Distinct().ToList();
159                     }
160                 }
161                 foreach (var Cric in CricpList)
162                 {
163                     if (Cric.Any(x => x.Contains(".NETCoreApp")))
164                     {
165                         continue;
166                     }
167                     else if (Cric.Any(x => x.Contains(".NETFramework")))
168                     {
169                         continue;
170                     }
171                     if (FrameworkCompatible.Count() > 0)
172                     {
173                         FrameworkCompatible = FrameworkCompatible.Union(Cric).ToList();
174                         FrameworkCompatible = FrameworkCompatible.Distinct().ToList();
175                     }
176                     if (CoreCompatible.Count() > 0)
177                     {
178                         CoreCompatible = CoreCompatible.Union(Cric).ToList();
179                         CoreCompatible = CoreCompatible.Distinct().ToList();
180                     }
181                 }
182                 if (!CompatibleCore && !CompatibleFramework)
183                 {
184                     CompatibleCricpList.Add(FrameworkCompatible);
185                     CompatibleCricpList.Add(CoreCompatible);
186                 }
187                 else
188                 {
189                     //合并所有的列表为一个大切片
190                     List<string> BIG = new List<string>();
191                     foreach (var Cric in CricpList)
192                     {
193                         BIG = BIG.Union(Cric).ToList();
194                     }
195                     CompatibleCricpList.Add(BIG);
196                 }
197                 Project.Cricps = CompatibleCricpList;
198                 Project.CricpNum = Project.Cricps.Count();
199             }
200             return Result;
201         }
202         static public void SelfTest()
203         {
204             //AnalyseSLNAsync.AnalyseAsync();
205             List<ProjectModel> projectmodels = new List<ProjectModel>();
206             //A(fram)-》B-》C   D(fram)-》B-》C  E(core)-》B-》C
207             //F->M->N
208             ProjectModel A = new ProjectModel();
209             A.Name = "A";
210             A.Framework = "Fram";
211             ProjectModel B = new ProjectModel();
212             B.Name = "B";
213             B.Framework = "Stand";
214             ProjectModel C = new ProjectModel();
215             C.Name = "C";
216             B.Framework = "Stand";
217             ProjectModel D = new ProjectModel();
218             D.Name = "D";
219             D.Framework = "Fram";
220             ProjectModel E = new ProjectModel();
221             E.Name = "E";
222             E.Framework = "Core";
223 
224             ProjectModel F = new ProjectModel();
225             F.Name = "F";
226             F.Framework = "Fram";
227             ProjectModel M = new ProjectModel();
228             M.Name = "M";
229             M.Framework = "Fram";
230             ProjectModel N = new ProjectModel();
231             N.Name = "N";
232             N.Framework = "Fram";
233             //构建依赖图谱
234             A.Projectrefs.Add(B);
235             B.Projectrefs.Add(C);
236             D.Projectrefs.Add(B);
237             E.Projectrefs.Add(B);
238 
239             F.Projectrefs.Add(M);
240             M.Projectrefs.Add(N);
241             //制作列表
242             projectmodels.Add(A);
243             projectmodels.Add(B);
244             projectmodels.Add(C);
245             projectmodels.Add(D);
246             projectmodels.Add(E);
247 
248             projectmodels.Add(F);
249             projectmodels.Add(M);
250             projectmodels.Add(N);
251             var ProjectCricps = CricpAnalyseAsync.ExtractCricps(projectmodels);
252             List<CricpModel> ProjectInterSectCricps = new List<CricpModel>();
253             foreach (var ProjectCricp in ProjectCricps)
254             {
255                 if (ProjectInterSectCricps.Contains(ProjectCricp))
256                 {
257                     continue;
258                 }
259                 foreach (var CheckCricp in ProjectCricps)
260                 {
261                     //如果切片里面任意一个节点在其他切片里面出现过,那么就认为他们存在交集
262                     if ((ProjectCricp != CheckCricp) && (ProjectCricp.ComposeCricp.Any(x => CheckCricp.ComposeCricp.Contains(x))))
263                     {
264                         ProjectInterSectCricps.Add(ProjectCricp);
265                         ProjectInterSectCricps.Add(CheckCricp);
266                         ProjectInterSectCricps = ProjectInterSectCricps.Distinct().ToList();
267                     }
268                 }
269             }
270         }
271         static public void Main(string[] args)
272         {
273             List<int>ResultNum=new List<int>();
274             //Mu();
275             //直接存储所有的切片
276             var Folder_path = @"D:\VS2017\WorkSpace\Static_Analyse\Analyse_WorkSpace\Old";
277             var Save_Folder_path = @"D:\VS2017\WorkSpace\Static_Analyse\Analyse_WorkSpace\New";
278             string[] Json_paths =
279                       Directory.GetFiles(Folder_path, "*.json", SearchOption.TopDirectoryOnly);
280             List<SimpleCountCricp> Result =new List<SimpleCountCricp>();
281             foreach (string JsonPath in Json_paths)
282             {
283                 //string JsonPath = @"D:\VS2017\WorkSpace\Static_Analyse\Analyse_WorkSpace\Old\Aggregates.NET.json";
284                 Console.WriteLine(JsonPath);
285                 try
286                 {
287                     SolutionModel solutionModel = JsonConvert.DeserializeObject<SolutionModel>(File.ReadAllText(JsonPath));
288                     var Crips = solutionModel.ProjectCricps;
289                     //存储每一个切片里面的平台类型
290                     List<List<string>> OriginalListCrpcps = new List<List<string>>();
291                     foreach (var cricp in Crips)
292                     {
293                         //统计切片里面有多少类framweork
294                         List<string> CripsFrameworkType = new List<string>();
295                         var ComposeCricp = cricp.ComposeCricp;
296                         foreach (var Topproject in ComposeCricp)
297                         {
298                             //如果字典里没有这个框架,那么新增
299                             if (!CripsFrameworkType.Contains(Topproject.Framework))
300                             {
301                                 CripsFrameworkType.Add(Topproject.Framework);
302                             }
303                         }
304                         OriginalListCrpcps.Add(CripsFrameworkType);
305                     }
306                     //去重,一摸一样的只算一个
307                     //SimpleDistinctListCrpcps——》指的是包含其他切片的切片,也就是把多颗连通树,放到一个切片里面,只要这里面是相同的
308                     var SimpleDistinctListCrpcps = DistinctList(OriginalListCrpcps);
309                     if(SimpleDistinctListCrpcps.Count!=0)
310                     {
311                         SimpleCountCricp SimpleDistinct = new SimpleCountCricp();
312                         var FileName = Regex.Replace(JsonPath, @"D:\\VS2017\\WorkSpace\\Static_Analyse\\Analyse_WorkSpace\\Old\\", "");
313                         FileName = Regex.Replace(FileName, @".json", "");
314                         SimpleDistinct.Filename = FileName;
315                         SimpleDistinct.Cricps = SimpleDistinctListCrpcps;
316                         SimpleDistinct.CricpNum = SimpleDistinctListCrpcps.Count;
317                         Result.Add(SimpleDistinct);
318                         ResultNum.Add(SimpleDistinct.CricpNum);
319                     }
320                 }
321                 catch (Exception ex)
322                 {
323                     Console.WriteLine(ex);
324                     continue;
325                 }
326             }
327             string Json_save = JsonConvert.SerializeObject(Result);
328             var Json_Path = @"D:\VS2017\WorkSpace\Static_Analyse\Analyse_WorkSpace\Final\Final_SimpleDistinctListCrpcps.json";
329             JsonUtil.Save_json(Json_Path, Json_save);
330 
331             //制作兼容的列表_>制作大的去重切片
332             Result = BigDistinctCricps(Result);
333             Json_save = JsonConvert.SerializeObject(Result);
334             Json_Path = @"D:\VS2017\WorkSpace\Static_Analyse\Analyse_WorkSpace\Final\BigDistinctListCrpcps.json";
335             JsonUtil.Save_json(Json_Path, Json_save);
336             //制作兼容的列表_>制作兼容的切片
337             Result = CoreFrameworkCompatibleCricps(Result);
338             Json_save = JsonConvert.SerializeObject(Result);
339             Json_Path = @"D:\VS2017\WorkSpace\Static_Analyse\Analyse_WorkSpace\Final\CoreFrameworkCompatibleListCrpcps.json";
340             JsonUtil.Save_json(Json_Path, Json_save);
341             ResultNum = new List<int>();
342             //调查一个切片里面究竟有多少个子项目,在这里根据切片的框架,直接分类所有的子项目,划分成切片
343             foreach (string JsonPath in Json_paths)
344             {
345                 Console.WriteLine(JsonPath);
346                 try
347                 {
348                     SolutionModel solutionModel = JsonConvert.DeserializeObject<SolutionModel>(File.ReadAllText(JsonPath));
349                     var FileName = Regex.Replace(JsonPath, @"D:\\VS2017\\WorkSpace\\Static_Analyse\\Analyse_WorkSpace\\Old\\", "");
350                     FileName = Regex.Replace(FileName, @".json", "");
351                     var Crips = solutionModel.ProjectCricps;
352                     //切片分类的基准
353                     var BaseCricpSolution=Result.Where(x=>x.Filename==FileName).ToList().First();
354                     if (BaseCricpSolution == null)
355                         continue;
356                     //需要被存储的新切片
357                     List<List<ProjectModel>> NewCompatibleCricps = new List<List<ProjectModel>>();
358                     //对于每一个基准的切片,只需要找出来,在普通切片里面的所有符合基准切片要求的切片
359                     foreach (var Base in BaseCricpSolution.Cricps)
360                     {
361                         List<ProjectModel> NewCompatibleProjects = new List<ProjectModel>();
362                         foreach (var ProjectCricp in Crips)
363                         {
364                             //进到每一个切片里面
365                             //如果这个切片满足这个基准,那么就把这个切片所有的信息加载到这个基准里
366                             if(ProjectCricp.ComposeCricp.All(x=> Base.Contains(x.Framework)))
367                             {
368                                 NewCompatibleProjects = NewCompatibleProjects.Union(ProjectCricp.ComposeCricp).ToList();
369                                 NewCompatibleProjects = NewCompatibleProjects.Distinct().ToList();
370                             }
371                         }
372                         ResultNum.Add(NewCompatibleProjects.Count);
373                         NewCompatibleCricps.Add(NewCompatibleProjects);
374                     }
375                     SolutionCricpModel ResultsolutionCricpModel = new SolutionCricpModel();
376                     ResultsolutionCricpModel.Name = solutionModel.Name;
377                     ResultsolutionCricpModel.PeojectNum = solutionModel.PeojectNum;
378                     ResultsolutionCricpModel.Multi_deps = solutionModel.Multi_deps;
379                     ResultsolutionCricpModel.Conflict_deps = solutionModel.Conflict_deps;
380                     ResultsolutionCricpModel.ConflictWarn_deps = solutionModel.ConflictWarn_deps;
381                     ResultsolutionCricpModel.Projects = solutionModel.Projects;
382                     ResultsolutionCricpModel.ProjectCricps = NewCompatibleCricps;
383                     Json_save = JsonConvert.SerializeObject(ResultsolutionCricpModel);
384                     Json_Path = @"D:\VS2017\WorkSpace\Static_Analyse\Analyse_WorkSpace\Final\Projects\"+ FileName + ".json";
385                     JsonUtil.Save_json(Json_Path, Json_save);
386                 }
387                 catch (Exception ex)
388                 {
389                     Console.WriteLine(ex);
390                     continue;
391                 }
392             }
393             Console.WriteLine(ResultNum.Average());
394             Console.WriteLine(ResultNum.Max());
395             Console.WriteLine(ResultNum.Count());
396             Console.ReadKey();
397         }
398 
399     }
400 }

 

posted @ 2022-05-31 21:55  博二爷  阅读(20)  评论(0编辑  收藏  举报