private DirectedSparseGraph <DependantType> BuildTypeDependencyGraph() { var graph = new DirectedSparseGraph <DependantType>(); var processedTypes = new List <DependantType>(); foreach (var syntaxTree in _context.Compilation.SyntaxTrees) { var root = syntaxTree.GetRoot(); var semanticModel = _context.Compilation.GetSemanticModel(syntaxTree); var classNodes = root.DescendantNodes().OfType <ClassDeclarationSyntax>().ToList(); foreach (var classNode in classNodes) { DependantType type = null; var classSymbol = semanticModel.GetDeclaredSymbol(classNode) as INamedTypeSymbol; var processedType = processedTypes.FirstOrDefault(t => t.Name == classSymbol.Name); if (processedType != null) { // we already added this to the graph as a dependency earlier type = processedType; type.SemanticModel = semanticModel; type.NamedTypeSymbol = classSymbol; } else { type = new DependantType() { Name = classSymbol.Name, Namespace = classSymbol.ContainingNamespace.ToString(), ContainingAssembly = classSymbol.ContainingAssembly.Name, IsExternal = false, SemanticModel = semanticModel, NamedTypeSymbol = classSymbol, TypeKind = classSymbol.TypeKind }; processedTypes.Add(type); graph.AddVertex(type); } if (_context.Solution != null) { var referencesToClass = SymbolFinder.FindReferencesAsync(classSymbol, _context.Solution).Result; } var classDependencies = classNode.DescendantNodes() .Select(n => semanticModel.GetTypeInfo(n).Type) .Where(n => n != null) .Distinct() .ToList(); foreach (var dependency in classDependencies) { DependantType dep = null; var processedDep = processedTypes.FirstOrDefault(t => t.Name == dependency.Name); if (processedDep != null) { dep = processedDep; } else { dep = new DependantType() { Name = dependency.Name, Namespace = dependency.ContainingNamespace.ToString(), ContainingAssembly = dependency.ContainingAssembly.Name, NamedTypeSymbol = dependency as INamedTypeSymbol, TypeKind = dependency.TypeKind, IsExternal = dependency.ContainingAssembly.Name == "mscorlib" }; processedTypes.Add(dep); } if (!graph.HasVertex(dep)) { graph.AddVertex(dep); } graph.AddEdge(type, dep); } } var enumNodes = root.DescendantNodes().OfType <EnumDeclarationSyntax>().ToList(); foreach (var enumNode in enumNodes) { DependantType type = null; var enumSymbol = semanticModel.GetDeclaredSymbol(enumNode) as INamedTypeSymbol; var processedType = processedTypes.FirstOrDefault(t => t.Name == enumSymbol.Name); if (processedType != null) { // we already added this to the graph as a dependency earlier type = processedType; type.SemanticModel = semanticModel; type.NamedTypeSymbol = enumSymbol; } else { type = new DependantType() { Name = enumSymbol.Name, Namespace = enumSymbol.ContainingNamespace.ToString(), ContainingAssembly = enumSymbol.ContainingAssembly.Name, IsExternal = false, SemanticModel = semanticModel, NamedTypeSymbol = enumSymbol, TypeKind = enumSymbol.TypeKind }; processedTypes.Add(type); graph.AddVertex(type); } } } return(graph); }
public float GetDiesel(Entity entity) { return(network.HasVertex(entity) ? entity.GetState <SubstanceNetworkState>().GetSubstance(SubstanceType.Diesel) : 0.0f); }
/// <summary> /// Employees constructor takes an array of strings from readallline -System.IO /// Iterates through each record in the array using the GetEnumerator() and moving the cursor. /// </summary> /// <param name="lines"></param> public Employees(string[] lines) { nodeGraph = new DirectedSparseGraph <Worker>(); workers = new Dictionary <string, Worker>(); IEnumerable <string[]> rows = lines .Select(r => r.Split('\t')); IEnumerable <IEnumerable <string> > records = from row in rows select(from item in row select item); foreach (var n in records) { var p = n.GetEnumerator(); while (p.MoveNext()) { try { var data = p.Current.Split(','); if (string.IsNullOrEmpty(data[0])) { Debug.WriteLine("Invalid employee id- please resolve this!", debug); continue; } if (string.IsNullOrEmpty(data[1]) && Owner < 1) { Owner++; } else if (string.IsNullOrEmpty(data[1]) && Owner == 1) { Debug.WriteLine($"A company can have only one C.E.O {data[1]} Adding error", debug); continue; } // Salary check using a discard if (Int32.TryParse(data[2], out _)) { var empl = new Worker(data[0], data[1], int.Parse(data[2])); try { workers.Add(empl.Id, empl); } catch { //Employee appers twice in he dictionary<string,Worker> Debug.WriteLine($"{data[1]} Has alrady been added to the list", debug); } if (!nodeGraph.HasVertex(empl))//false { nodeGraph.AddVertex(empl); } } else { Debug.WriteLine($"Invalid salary value... for user {data[1]}", debug); } } catch (Exception e) { Debug.WriteLine(e.Message, debug); } } p.Dispose(); } ///Check for d-Linking foreach (KeyValuePair <string, Worker> keyValuePair in workers) { if (!string.IsNullOrEmpty(keyValuePair.Value.Manager)) { // check for double linking bool doubleLinked = false; foreach (Worker worker in nodeGraph.D_F_S(keyValuePair.Value).ToArray()) { if (worker.Equals(keyValuePair.Value.Manager)) { doubleLinked = true; break; } } // ensure that each employee has only one manager if (nodeGraph.IncomingEdges(keyValuePair.Value).ToArray().Length < 1 && !doubleLinked) { nodeGraph.AddEdge(workers[keyValuePair.Value.Manager], keyValuePair.Value); } else { if (nodeGraph.IncomingEdges(keyValuePair.Value).ToArray().Length >= 1) { Debug.WriteLine($"Employee {keyValuePair.Value.Id} have more than one manager", debug); //resume not returning } Debug.WriteLine("Double linking not allowed", debug); } } } }