public static async Task <GraphData> ConvertToGraphData(List <CsvConcept> concepts, List <CsvElement> elements, List <CsvCountry> countries, List <CsvPeople> peoples, List <CsvProject> projects, List <CsvPublication> publications, List <CsvRegion> regions) { List <NodeElement> nodeElements = new List <NodeElement>(); List <NodeCountry> nodeCountries = new List <NodeCountry>(); List <NodeProject> nodeProjects = new List <NodeProject>(); List <NodeVt> nodeConcepts = new List <NodeVt>(); List <NodePublication> nodePublications = new List <NodePublication>(); List <NodeCaseStudy> nodePeople = new List <NodeCaseStudy>(); foreach (var item in elements) { nodeElements.Add(CsvElement.ToNodeElement(item)); } foreach (var item in countries) { nodeCountries.Add(CsvCountry.ToNodeElement(item)); } foreach (var item in regions) { nodeCountries.Add(CsvRegion.ToNodeElement(item)); } foreach (var item in projects) { nodeProjects.Add(CsvProject.ToNodeElement(item)); } foreach (var item in concepts) { nodeConcepts.Add(CsvConcept.ToNodeElement(item)); } foreach (var item in publications) { nodePublications.Add(CsvPublication.ToNodeElement(item)); } foreach (var item in peoples) { nodePeople.Add(CsvPeople.ToNodeElement(item)); } List <EdgeItem> edgeItems = new List <EdgeItem>(); foreach (var concept in concepts) { if (concept.LinkedElements.Contains(",")) { string[] items = concept.LinkedElements.Split(','); foreach (var s in items) { edgeItems.Add(new EdgeItem() { Object = concept.Name, predicate = "related", subject = s, weight = 2 }); } } else { if (!string.IsNullOrEmpty(concept.LinkedElements)) { edgeItems.Add(new EdgeItem() { Object = concept.Name, predicate = "related", subject = concept.LinkedElements, weight = 2 }); } } } foreach (var concept in elements) { if (concept.LinkedElements.Contains(",")) { string[] items = concept.LinkedElements.Split(','); foreach (var s in items) { edgeItems.Add(new EdgeItem() { Object = concept.Name, predicate = "related", subject = s, weight = 2 }); } } else { if (!string.IsNullOrEmpty(concept.LinkedElements)) { edgeItems.Add(new EdgeItem() { Object = concept.Name, predicate = "related", subject = concept.LinkedElements, weight = 2 }); } } } foreach (var concept in countries) { if (concept.LinkedElements.Contains(",")) { string[] items = concept.LinkedElements.Split(','); foreach (var s in items) { edgeItems.Add(new EdgeItem() { Object = concept.Name, predicate = "related", subject = s, weight = 2 }); } } else { if (!string.IsNullOrEmpty(concept.LinkedElements)) { edgeItems.Add(new EdgeItem() { Object = concept.Name, predicate = "related", subject = concept.LinkedElements, weight = 2 }); } } } foreach (var concept in regions) { if (concept.LinkedElements.Contains(",")) { string[] items = concept.LinkedElements.Split(','); foreach (var s in items) { edgeItems.Add(new EdgeItem() { Object = concept.Name, predicate = "related", subject = s, weight = 2 }); } } else { if (!string.IsNullOrEmpty(concept.LinkedElements)) { edgeItems.Add(new EdgeItem() { Object = concept.Name, predicate = "related", subject = concept.LinkedElements, weight = 2 }); } } } foreach (var concept in peoples) { if (concept.LinkedElements.Contains(",")) { string[] items = concept.LinkedElements.Split(','); foreach (var s in items) { edgeItems.Add(new EdgeItem() { Object = concept.Name, predicate = "related", subject = s, weight = 2 }); } } else { if (!string.IsNullOrEmpty(concept.LinkedElements)) { edgeItems.Add(new EdgeItem() { Object = concept.Name, predicate = "related", subject = concept.LinkedElements, weight = 2 }); } } } foreach (var concept in projects) { if (concept.LinkedElements.Contains(",")) { string[] items = concept.LinkedElements.Split(','); foreach (var s in items) { edgeItems.Add(new EdgeItem() { Object = concept.Name, predicate = "related", subject = s, weight = 2 }); } } else { if (!string.IsNullOrEmpty(concept.LinkedElements)) { edgeItems.Add(new EdgeItem() { Object = concept.Name, predicate = "related", subject = concept.LinkedElements, weight = 2 }); } } } foreach (var concept in publications) { if (concept.LinkedElements.Contains(",")) { string[] items = concept.LinkedElements.Split(','); foreach (var s in items) { edgeItems.Add(new EdgeItem() { Object = concept.Name, predicate = "related", subject = s, weight = 2 }); } } else { if (!string.IsNullOrEmpty(concept.LinkedElements)) { edgeItems.Add(new EdgeItem() { Object = concept.Name, predicate = "related", subject = concept.LinkedElements, weight = 2 }); } } } GraphData graphData = new GraphData() { Meta = new Meta() { generated = "Generated with ontoUcmd", language = "en" }, NodeElements = nodeElements, NodeCountries = nodeCountries, NodeProjects = nodeProjects, NodePublications = nodePublications, NodeVts = nodeConcepts, NodeCaseStudies = nodePeople, EdgeItems = edgeItems }; return(graphData); }
static async void Build() { bool locationBuild = false; Console.WriteLine(); Console.WriteLine("Build JSON from CSV."); IniFile ini = new IniFile(); if (!File.Exists(iniPath)) { File.WriteAllText(iniPath, ""); } ini.Load(iniPath); Console.WriteLine("Infos :"); string path = ini.GetKeyValue("Config", "OutputPath"); string lpath = ini.GetKeyValue("Config", "OutputLocationPath"); Console.WriteLine($" Output path : {path}"); Console.WriteLine($" Location output path : {lpath}"); if (File.Exists(path)) { Console.WriteLine($" The file {path} already exists and will be updated."); } if (File.Exists(lpath)) { Console.WriteLine($" The file {lpath} already exists and will be updated."); } Console.WriteLine(); Console.WriteLine("Converting Excel files to Csv..."); List <CsvElement> csvElements = new List <CsvElement>(); List <CsvConcept> csvConcepts = new List <CsvConcept>(); List <CsvCountry> csvCountries = new List <CsvCountry>(); List <CsvRegion> csvRegions = new List <CsvRegion>(); List <CsvPeople> csvPeoples = new List <CsvPeople>(); List <CsvProject> csvProjects = new List <CsvProject>(); List <CsvPublication> csvPublications = new List <CsvPublication>(); Console.WriteLine("Reading data from csv documents..."); string str = ""; string name = ""; Dictionary <string, string> dic = new Dictionary <string, string>(); // Converts all excel files found in config.ini to csv files, in a temporary dir. name = "Elements"; str = ini.GetKeyValue("ExcelFiles", name); if (!File.Exists(str)) { Console.WriteLine($"error: {name} csv file could not be found."); } else { ExcelService.SaveAsCsv(str, Environment.GetFolderPath(Environment.SpecialFolder.InternetCache) + $@"\{name}.csv"); dic.Add(name, Environment.GetFolderPath(Environment.SpecialFolder.InternetCache) + $@"\{name}.csv"); } name = "Concepts"; str = ini.GetKeyValue("ExcelFiles", name); if (!File.Exists(str)) { Console.WriteLine($"error: {name} csv file could not be found."); } else { ExcelService.SaveAsCsv(str, Environment.GetFolderPath(Environment.SpecialFolder.InternetCache) + $@"\{name}.csv"); dic.Add(name, Environment.GetFolderPath(Environment.SpecialFolder.InternetCache) + $@"\{name}.csv"); } name = "Countries"; str = ini.GetKeyValue("ExcelFiles", name); if (!File.Exists(str)) { Console.WriteLine($"error: {name} csv file could not be found."); } else { ExcelService.SaveAsCsv(str, Environment.GetFolderPath(Environment.SpecialFolder.InternetCache) + $@"\{name}.csv"); dic.Add(name, Environment.GetFolderPath(Environment.SpecialFolder.InternetCache) + $@"\{name}.csv"); } name = "Regions"; str = ini.GetKeyValue("ExcelFiles", name); if (!File.Exists(str)) { Console.WriteLine($"error: {name} csv file could not be found."); } else { ExcelService.SaveAsCsv(str, Environment.GetFolderPath(Environment.SpecialFolder.InternetCache) + $@"\{name}.csv"); dic.Add(name, Environment.GetFolderPath(Environment.SpecialFolder.InternetCache) + $@"\{name}.csv"); } name = "Projects"; str = ini.GetKeyValue("ExcelFiles", name); if (!File.Exists(str)) { Console.WriteLine($"error: {name} csv file could not be found."); } else { ExcelService.SaveAsCsv(str, Environment.GetFolderPath(Environment.SpecialFolder.InternetCache) + $@"\{name}.csv"); dic.Add(name, Environment.GetFolderPath(Environment.SpecialFolder.InternetCache) + $@"\{name}.csv"); } name = "Publications"; str = ini.GetKeyValue("ExcelFiles", name); if (!File.Exists(str)) { Console.WriteLine($"error: {name} csv file could not be found."); } else { ExcelService.SaveAsCsv(str, Environment.GetFolderPath(Environment.SpecialFolder.InternetCache) + $@"\{name}.csv"); dic.Add(name, Environment.GetFolderPath(Environment.SpecialFolder.InternetCache) + $@"\{name}.csv"); } name = "People"; str = ini.GetKeyValue("ExcelFiles", name); if (!File.Exists(str)) { Console.WriteLine($"error: {name} csv file could not be found."); } else { ExcelService.SaveAsCsv(str, Environment.GetFolderPath(Environment.SpecialFolder.InternetCache) + $@"\{name}.csv"); dic.Add(name, Environment.GetFolderPath(Environment.SpecialFolder.InternetCache) + $@"\{name}.csv"); } str = dic["Elements"]; if (!File.Exists(str)) { Console.WriteLine("error: Elements csv file could not be found."); } else { csvElements = CsvElement.FromFile(str); } str = dic["Concepts"]; if (!File.Exists(str)) { Console.WriteLine("error: Concepts csv file could not be found."); } else { csvConcepts = CsvConcept.FromFile(str); } str = dic["Countries"]; if (!File.Exists(str)) { Console.WriteLine("error: Countries csv file could not be found."); } else { csvCountries = CsvCountry.FromFile(str); } str = dic["Regions"]; if (!File.Exists(str)) { Console.WriteLine("error: Regions csv file could not be found."); } else { csvRegions = CsvRegion.FromFile(str); } str = dic["People"]; if (!File.Exists(str)) { Console.WriteLine("error: People csv file could not be found."); } else { csvPeoples = CsvPeople.FromFile(str); } str = dic["Projects"]; if (!File.Exists(str)) { Console.WriteLine("error: Projects csv file could not be found."); } else { csvProjects = CsvProject.FromFile(str); } str = dic["Publications"]; if (!File.Exists(str)) { Console.WriteLine("error: Publications csv file could not be found."); } else { csvPublications = CsvPublication.FromFile(str); } Console.WriteLine("CSV files has been read. Building graph_xx.json..."); Console.WriteLine("Converting..."); var graphData = await SerializationService.ConvertToGraphData(csvConcepts, csvElements, csvCountries, csvPeoples, csvProjects, csvPublications, csvRegions); Console.WriteLine("Building..."); SerializationService.BuildGraphData(path, graphData); Console.WriteLine("Graph file has been build."); Console.ForegroundColor = ConsoleColor.Yellow; Console.Write("[Info] Do you want to build locations files (txt/json) ? Y/N : "); string resp = Console.ReadLine(); Console.ForegroundColor = ConsoleColor.Gray; if (!string.IsNullOrEmpty(resp) && resp.ToLower() == "y") { locationBuild = true; Console.WriteLine("Building location text file..."); SerializationService.BuildLocationFile(lpath, graphData, csvConcepts); Console.WriteLine("Location text file has been build."); Console.WriteLine("Building location json file..."); ConstellationLocationGenerator.Build(lpath, $"{Path.GetDirectoryName(lpath)}\\constellation-node-locations_.json"); Console.WriteLine("Location json file has been build."); Console.WriteLine($" Location txt output path : {lpath}"); Console.WriteLine($" Location txt output path : {Path.GetDirectoryName(lpath)}\\constellation-node-locations_.json"); } Console.WriteLine($" Output path : {path}"); Console.WriteLine(""); }