示例#1
0
        public static void ReadGraphCSV()
        {
            // GLOBALS
            NodeDataContainer nodeDataContainer = new NodeDataContainer();
            string            dirPath           = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            string            filePath          = dirPath + "\\graphData.csv";

            int idCounter = 0;

            using (TextFieldParser parser = new TextFieldParser(filePath))
            {
                parser.TextFieldType = FieldType.Delimited;
                parser.SetDelimiters(",");
                while (!parser.EndOfData)
                {
                    //Process row
                    string[] fields = parser.ReadFields();
                    Console.WriteLine(nodeDataContainer.DataModels.Count);

                    if (nodeDataContainer.DataModels.Count == 0)
                    {
                        NodeDataModel newModel =
                            NodeDataModel.CreateNewDataModel(fields[0], fields[1], fields[3], fields[2]);
                        newModel.UniqueIdCounter = idCounter;
                        nodeDataContainer.AppendToDataContainer(newModel);
                        idCounter++;
                    }
                    else
                    {
                        IEnumerable <NodeDataModel> existingRecords = from record in nodeDataContainer.DataModels
                                                                      where record.NodeAId == fields[2]
                                                                      select record;
                        if (existingRecords.Count() == 0)
                        {
                            NodeDataModel newModel =
                                NodeDataModel.CreateNewDataModel(fields[0], fields[1], fields[3], fields[2]);
                            newModel.UniqueIdCounter = idCounter;
                            nodeDataContainer.AppendToDataContainer(newModel);
                            idCounter++;
                        }
                        else
                        {
                            //Console.WriteLine(existingRecords.Count());
                            NodeDataModel exisingRecord = existingRecords.First();
                            exisingRecord.TotalConnectionsCount += 1;

                            if (exisingRecord.NodeBId != fields[3])
                            {
                                exisingRecord.UniqueConnectionsCount += 1;
                            }
                        }
                    }

                    //if (nodeDataContainer.DataModels.Count > 10)
                    //{
                    //    Console.WriteLine("TEN");
                    //}
                }

                var container = nodeDataContainer.DataModels;
                Console.WriteLine(nodeDataContainer.DataModels.Count);

                Console.ReadKey();
            }

            // NEW CSV SHIT
            StringBuilder csvcontent = new StringBuilder();

            csvcontent.AppendLine("Node A Name,Node B Name,# Connections,# Connections Unique,Type,Node A ID,Node B ID,UniqueID");
            Regex pattern = new Regex("^([^.]+)");

            foreach (NodeDataModel nd in nodeDataContainer.DataModels)
            {
                List <string>   matchStrings = new List <string>(); //Create empty container
                MatchCollection matches      = pattern.Matches(nd.NodeAName);

                string nodeType = "EMPTY";

                // Try to get node types
                foreach (Match match in matches) //Loop over all regex matches found
                {
                    if (match.Success)           //If the match is successful
                    {
                        nodeType = match.Value;
                        matchStrings.Add(nodeType);
                    }
                }

                // Work with raw node types
                if (nodeType.Length > 20)
                {
                    nodeType = "Zero Touch";
                }
                else if (nodeType.Contains("@var"))
                {
                    nodeType = "Code Block??";
                }

                // Create csv row
                string csvLine = string.Format("{0},{1},{2},{3},{4},{5},{6},{7}",
                                               nd.NodeAName, nd.NodeBName, nd.TotalConnectionsCount, nd.UniqueConnectionsCount, nodeType, nd.NodeAId, nd.NodeBId, nd.UniqueIdCounter);

                csvcontent.AppendLine(csvLine);
            }

            File.WriteAllText(dirPath + "\\graphDataFeaturized.csv", csvcontent.ToString());
        }
示例#2
0
        public void ParseJSONs(ParseDYF dyfObj)
        {
            // DEFINE GLOBALS
            GraphDataParse           csvParser           = new GraphDataParse();
            NodeDataContainer        nodeDataContainer   = new NodeDataContainer();
            GraphDataParseFeaturized csvParserFeaturized = new GraphDataParseFeaturized();

            onsole.WriteLine("Enter Directory Path"); //Prompt user to enter directory to search for files
            string dirPath = Console.ReadLine();      //Read user input

            //string dirPath = "C:\\Users\\pmitev\\Desktop\\graphs";
            string[] fileEntries = Directory.GetFiles(dirPath); //Get all the files of the input directory

            foreach (var filepath in fileEntries)
            {
                Console.WriteLine(filepath);

                using (StreamReader reader = File.OpenText(filepath))
                {
                    // Read JSON file, and get a JToken from it for iterating
                    try
                    {
                        JObject jObject         = (JObject)JToken.ReadFrom(new JsonTextReader(reader));
                        JToken  nodeObject      = jObject["Nodes"];
                        JToken  connectorObject = jObject["Connectors"];

                        //Define Dictionaries for the node and the In / Out
                        Dictionary <string, string> NodeDictionary = new Dictionary <string, string>();
                        Dictionary <string, string> IODictionary   = new Dictionary <string, string>();

                        //Create dictionary for ID and node name
                        foreach (var node in nodeObject)
                        {
                            string stringnodeID   = node["Id"].ToString();
                            string stringnodename = "NONE";

                            try
                            {
                                stringnodename = node["FunctionSignature"].ToString();
                                Console.WriteLine(stringnodename);
                            }
                            catch
                            {
                                Console.WriteLine("MISSING FUNCTION SIGNATURE");
                            }

                            if (stringnodename != "NONE")
                            {
                                NodeDictionary.Add(stringnodeID, stringnodename);


                                JToken outputObject = node["Outputs"];

                                foreach (var output in outputObject)
                                {
                                    string outputID = output["Id"].ToString();
                                    IODictionary.Add(outputID, stringnodeID);
                                }

                                JToken inputObject = node["Inputs"];

                                foreach (var inputs in inputObject)
                                {
                                    string inputID = inputs["Id"].ToString();
                                    IODictionary.Add(inputID, stringnodeID);
                                }

                                //Console.WriteLine(IODictionary);
                            }
                        }

                        foreach (var connector in connectorObject)
                        {
                            string inputID  = connector["Start"].ToString();
                            string outputID = connector["End"].ToString();

                            try
                            {
                                string NodeAID = IODictionary[inputID];
                                string NodeBID = IODictionary[outputID];

                                string NodeASig = NodeDictionary[NodeAID];
                                string NodeBSig = NodeDictionary[NodeBID];


                                if (dyfObj.customPackageMappings.Keys.Contains(NodeASig))
                                {
                                    var result = dyfObj.customPackageMappings[NodeASig];
                                    NodeASig = result;
                                }

                                if (dyfObj.customPackageMappings.Keys.Contains(NodeBSig))
                                {
                                    var result = dyfObj.customPackageMappings[NodeBSig];
                                    NodeBSig = result;
                                }

                                if (nodeDataContainer.DataModels.Count > 0)
                                {
                                    IEnumerable <NodeDataModel> existingRecords = from record in nodeDataContainer.DataModels
                                                                                  where record.NodeAId == NodeAID
                                                                                  select record;

                                    // CHECK IF NODE HAS BEEN ADDED TO RECORD
                                    if (existingRecords.Any())
                                    {
                                        NodeDataModel exisingRecord = existingRecords.First();
                                        exisingRecord.TotalConnectionsCount = +1;

                                        if (exisingRecord.NodeBId != NodeBID)
                                        {
                                            exisingRecord.UniqueConnectionsCount = +1;
                                        }
                                    }
                                    else
                                    {
                                    }
                                }
                                else
                                {
                                    NodeDataModel newModel = NodeDataModel.CreateNewDataModel(NodeASig, NodeBSig, NodeBID, NodeAID);
                                    nodeDataContainer.AppendToDataContainer(newModel);
                                }

                                // ADD TO CSV
                                NodeDataModel.ParseNodeDataModels(DataModels);
                                csvParser.AppendToCsv(NodeASig, NodeBSig, NodeAID, NodeBID);
                            }
                            catch (Exception) {}
                        }
                    }
                    catch
                    {
                        Console.WriteLine("NOT JSON");
                    }

                    // WRIE TO CSV
                    csvParser.ExportCSV();
                    Console.WriteLine("EXPORT COMPLETE");
                }
            }
        }