public static void PostProcess(List <LogAnalyzer> logAnalyzerData, List <SubsystemClass> subList) { int i = 0; int j = 0; int k = 0; List <LogAnalyzer> subOrgData = LogAnalyzer.sortBySubFails(logAnalyzerData); List <LogAnalyzer> conOrgData = LogAnalyzer.sortByConVios(logAnalyzerData); List <string> stateVarList = new List <string>(); // Output Log Data Analyzer to .csv file StreamWriter subOrgDataWrite = new StreamWriter(@"subsystemFailures.csv"); // Creates a new .csv file to write in subOrgDataWrite.Write("Failed Subsystem"); // Headers subOrgDataWrite.Write(",Subsystem Failure Rate"); // Headers subOrgDataWrite.Write(",Asset Name"); // Headers subOrgDataWrite.Write(",Task Names"); // Headers subOrgDataWrite.Write(",Target Names"); // Headers subOrgDataWrite.Write(Environment.NewLine); // Writes new line to the comma-delimited .csv file foreach (LogAnalyzer item in subOrgData) { if (item.ConName == null) { subOrgDataWrite.Write(item.SubName); subOrgDataWrite.Write("," + item.SubFailRate); subOrgDataWrite.Write("," + item.AssetNames[0]); if (item.TaskNames.Count > 0) { foreach (string item2 in item.TaskNames) { if (i >= 1) { subOrgDataWrite.Write(",,"); } subOrgDataWrite.Write("," + item2); subOrgDataWrite.Write("," + item.TarNames[i]); subOrgDataWrite.Write(Environment.NewLine); i++; } i = 0; } else { subOrgDataWrite.Write(Environment.NewLine); } } } subOrgDataWrite.Close(); // Closes the log analyzer data .csv file StreamWriter conOrgDataWrite = new StreamWriter(@"constraintViolations.csv"); // Creates a new .csv file to write in conOrgDataWrite.Write("Violated Constraint"); // Headers conOrgDataWrite.Write(",Constraint Violation Rate"); // Headers conOrgDataWrite.Write(",Asset Names"); // Headers conOrgDataWrite.Write(",Violating Subsystems"); // Headers conOrgDataWrite.Write(",Violating Subsystem Rates"); // Headers conOrgDataWrite.Write(",Violating State Variables"); // Headers conOrgDataWrite.Write(Environment.NewLine); // Writes new line to the comma-delimited .csv file foreach (LogAnalyzer item in conOrgData) { if (item.ConName != null) { conOrgDataWrite.Write(item.ConName); conOrgDataWrite.Write("," + item.ConVioRate); if (item.ConSubNames.Count > 0) { foreach (string conSubName in item.ConSubNames) { if (i == 0) { conOrgDataWrite.Write("," + item.AssetNames[i]); conOrgDataWrite.Write("," + item.ConSubNames[i]); conOrgDataWrite.Write("," + item.ConSubRates[i]); SubsystemClass subsys = subList.Find(x => x.SubName == item.ConSubNames[i]); foreach (string item3 in item.VioNames) { if (subsys.StateVars.Contains(item3)) { if (j > 0 && stateVarList.Contains(item3) == false) { conOrgDataWrite.Write(",,,,," + item3); stateVarList.Add(item3); conOrgDataWrite.Write(Environment.NewLine); } else if (j == 0) { stateVarList.Clear(); conOrgDataWrite.Write("," + item3); stateVarList.Add(item3); conOrgDataWrite.Write(Environment.NewLine); } j++; } } stateVarList.Clear(); j = 0; } else { conOrgDataWrite.Write(",," + item.AssetNames[i]); conOrgDataWrite.Write("," + item.ConSubNames[i]); conOrgDataWrite.Write("," + item.ConSubRates[i]); SubsystemClass subsys = subList.Find(x => x.SubName == item.ConSubNames[i]); foreach (string item3 in item.VioNames) { if (subsys.StateVars.Contains(item3)) { if (k == 0) { conOrgDataWrite.Write("," + item3); stateVarList.Add(item3); conOrgDataWrite.Write(Environment.NewLine); } else if (j > 0 && stateVarList.Contains(item3) == false) { conOrgDataWrite.Write(",,,,," + item3); stateVarList.Add(item3); conOrgDataWrite.Write(Environment.NewLine); } j++; k++; } } stateVarList.Clear(); } k = 0; i++; } i = 0; j = 0; } else { conOrgDataWrite.Write(Environment.NewLine); } } } conOrgDataWrite.Close(); // Closes the logger data .csv file }
// Logger shall have two modes: RunTime and PostProcess // Each mode shall call the LogAnalyzer method // RunTime will update failure Retes. // PostProcess will update failure rates and periodically // write it to a file for the Human user. public static List <LogAnalyzer> Analyze(List <LogAnalyzer> prevAnalytics, Log log, List <Constraint> constraintsList, List <SubsystemClass> subsysList, List <StateVar> stateVarList) { List <LogAnalyzer> dataOutput = new List <LogAnalyzer>(); // New LogAnalyzer list LogAnalyzer logAnalyzer = new LogAnalyzer(null, null, 0, null, null, null, 0, null, null, null, 0); // New LogAnalyzer class string assetName = null; string subName = null; double subFailRate = 0; double oldSubFailRate = 0; string taskName = null; string targetName = null; List <string> taskNames = new List <string>(); List <string> tarNames = new List <string>(); List <string> oldTaskNames = new List <string>(); List <string> oldTarNames = new List <string>(); List <LogData> subFails = new List <LogData>(); // New LogData list List <string> assetNames = new List <string>(); subFails = log.Data.FindAll(x => x.ConName == null); // Find all subsystem failures double prevFailScheds = 0; if (prevAnalytics.Count > 0) { prevFailScheds = prevAnalytics.First().TotalScheds; // Extract the previous number of failed schedules } double currentFailScheds = prevFailScheds + log.Data.Count; // Extract the current total number of failed schedules foreach (SubsystemClass subsystem in subsysList) { assetName = subsystem.AssetName; assetNames.Add(assetName); subName = subsystem.SubName; subFailRate = subFails.FindAll(x => x.SubName == subsystem.SubName).Count / currentFailScheds; if (prevAnalytics.Count > 0) { oldSubFailRate = prevAnalytics.Find(x => x.SubName == subsystem.SubName).SubFailRate *prevFailScheds / currentFailScheds; subFailRate = subFailRate + oldSubFailRate; oldTaskNames = prevAnalytics.Find(x => x.SubName == subsystem.SubName).TaskNames; oldTarNames = prevAnalytics.Find(x => x.SubName == subsystem.SubName).TarNames; } foreach (string oldTask in oldTaskNames) { taskName = oldTask; taskNames.Add(taskName); } foreach (string oldTar in oldTarNames) { targetName = oldTar; tarNames.Add(targetName); } foreach (LogData subFailure in subFails) { if (subFailure.SubName == subsystem.SubName) { taskName = subFailure.TaskName; targetName = subFailure.TarName; taskNames.Add(taskName); tarNames.Add(targetName); } } logAnalyzer = new LogAnalyzer(assetNames, subName, subFailRate, taskNames, tarNames, null, 0, null, null, null, currentFailScheds); dataOutput.Add(logAnalyzer); taskNames = new List <string>(); tarNames = new List <string>(); assetNames = new List <string>(); } List <LogData> conVios = new List <LogData>(); // New LogData list List <string> conSubNames = new List <string>(); // New string List List <string> vioNames = new List <string>(); // New string list List <string> oldVioNames = new List <string>(); // New string list List <double> oldConSubVioRates = new List <double>(); // New double list List <string> oldConSubNames = new List <string>(); // New double list List <double> updatedOldConSubVioRates = new List <double>(); // New double list List <double> conSubRates = new List <double>(); // New double list string ConName = null; string ConSubName; int i = 0; double ConVioRate = 0; double oldConVioRate = 0; double ConSubRate = 0; double oldConSubVioRate = 0; string vioName = null; // For each constraint foreach (Constraint constraint in constraintsList) { // Calculate constraint violation rates ConSubName = null; ConName = constraint.ConName; // Populate constraint name conVios = log.Data.FindAll(x => x.ConName == constraint.ConName); // Find all log data for the constraint violated ConVioRate = conVios.Count / currentFailScheds; // Count all the constraint violations and record violation rate if (prevAnalytics.Count > 0) { oldConVioRate = prevAnalytics.Find(x => x.ConName == constraint.ConName).ConVioRate *prevFailScheds / currentFailScheds; ConVioRate = ConVioRate + oldConVioRate; } // Done calculating constraint violation rates // Update old subsystem failure rates if (prevAnalytics.Count > 0) { oldConSubVioRates = prevAnalytics.Find(x => x.ConName == constraint.ConName).ConSubRates; oldConSubNames = prevAnalytics.Find(x => x.ConName == constraint.ConName).ConSubNames; if (oldConSubVioRates != null) { foreach (double prevConSubVioRate in oldConSubVioRates) { oldConSubVioRate = prevConSubVioRate * prevFailScheds / currentFailScheds; updatedOldConSubVioRates.Add(oldConSubVioRate); } } } // Done updating old subsystem failure rates // Calculate subsystem violation rates foreach (SubsystemClass subsystem in subsysList) { if (oldConSubNames.Count > 0) { foreach (string oldConSubName in oldConSubNames) { if (oldConSubName == subsystem.SubName) { ConSubName = subsystem.SubName; ConSubRate = conVios.FindAll(x => x.SubName == subsystem.SubName).Count / currentFailScheds; ConSubRate = ConSubRate + updatedOldConSubVioRates[i]; conSubNames.Add(ConSubName); conSubRates.Add(ConSubRate); assetName = subsysList.Find(x => x.SubName == ConSubName).AssetName; assetNames.Add(assetName); i++; } } } else if (conVios.FindAll(x => x.SubName == subsystem.SubName).Count > 0) { ConSubName = subsystem.SubName; ConSubRate = conVios.FindAll(x => x.SubName == subsystem.SubName).Count / currentFailScheds; ConSubRate = ConSubRate + oldConSubVioRate; conSubNames.Add(ConSubName); conSubRates.Add(ConSubRate); assetName = subsysList.Find(x => x.SubName == ConSubName).AssetName; assetNames.Add(assetName); } i = 0; } // Done calculating subsystem violation rates if (assetNames.Count == 0) { if (prevAnalytics.Count > 0) { assetNames = prevAnalytics.Find(x => x.ConName == ConName).AssetNames; } else { assetName = "None"; assetNames.Add(assetName); } } // Find violating state variables if (prevAnalytics.Count > 0) { oldVioNames = prevAnalytics.Find(x => x.ConName == ConName).VioNames; foreach (string prevState in oldVioNames) { vioName = prevState; vioNames.Add(vioName); } } foreach (StateVar item in stateVarList) { if (prevAnalytics.Count > 0 && conVios.FindAll(x => x.VioName == item.StateName).Count > 0) { foreach (string prevState in oldVioNames) { if (vioName != prevState) { vioName = item.StateName; vioNames.Add(vioName); } } if (conVios.FindAll(x => x.VioName == item.StateName).Count > 0 && oldVioNames.Count == 0) { vioName = item.StateName; vioNames.Add(vioName); } } else if (conVios.FindAll(x => x.VioName == item.StateName).Count > 0) { vioName = item.StateName; vioNames.Add(vioName); } } // Done finding violating state variables logAnalyzer = new LogAnalyzer(assetNames, null, 0, null, null, ConName, ConVioRate, vioNames, conSubNames, conSubRates, currentFailScheds); conSubNames = new List <string>(); conSubRates = new List <double>(); vioNames = new List <string>(); oldVioNames = new List <string>(); updatedOldConSubVioRates.Clear(); assetNames = new List <string>(); oldConSubNames = new List <string>(); oldConSubVioRate = 0; dataOutput.Add(logAnalyzer); } // Done calculating subsystem failures and constraint violations with relative data return(dataOutput); }
public static void Main(string[] args) { // Subsystem evaluation: // Multiple functions: Logger, RunTime mode, and // PostProcess mode. // Inputs are read from .xml files. // All model information reside within .xml files. // Outputs are comma-delimited .csv files // Read in .xml List <Constraint> constraintsList = new List <Constraint>(); // New lsit of constraints List <SubsystemClass> subsysList = new List <SubsystemClass>(); // New list of subsystems List <StateVar> stateVarList = new List <StateVar>(); // New list of state variables OpenFileDialog ofd = new OpenFileDialog(); // Dialog prompt to user to choose xml file location ofd.Filter = "XML| *.xml"; // Only allows xml files to be inputted if (ofd.ShowDialog() == DialogResult.OK) // If the correct file was selected { constraintsList = readXML.XMLCon(ofd); // List of constraints subsysList = readXML.XMLSub(ofd); // List of subsystems stateVarList = readXML.XMLState(constraintsList, subsysList); // List of state variables } // Done reading in .xml // Read-in Log // Implement checking for passed schedules and failed schedules LogData logData = new LogData(null, null, null, null, null, null, 0, 0); //List<LogData> logDataList = new List<LogData>(); // New List of Log Data List <LogAnalyzer> LogAnalytics = new List <LogAnalyzer>(); // New List of Log Analytics //var logger = new Logger(); // New Log class // Create specified subsystems and constraints for reordering List <SubsystemClass> subsystemList = new List <SubsystemClass>(); SubsystemClass subsystem = new SubsystemClass("SubsystemNode2", null, null); subsystemList.Add(subsystem); subsystem = new SubsystemClass("SubsystemNode3", null, null); subsystemList.Add(subsystem); subsystem = new SubsystemClass("SubsystemNode4", null, null); subsystemList.Add(subsystem); List <Constraint> constraintList = new List <Constraint>(); Constraint constraint = new Constraint("Constraint1", 0, null); constraintList.Add(constraint); constraint = new Constraint("Constraint2", 0, null); constraintList.Add(constraint); List <Constraint> conOrder = new List <Constraint>(); List <SubsystemClass> subOrder = new List <SubsystemClass>(); // Done creating specified subsystems and constraints for reordering // Specify the number of failed schedules // Create data for each failed schedule // Output runTime data for every 25 failed schedules // Output postProcess data for every 100 failed schedules Console.WriteLine("Input number of failed schedules for randomized log data"); // Prompt user to input number of iterations string input = Console.ReadLine(); // Read number of iterations double iters = Convert.ToDouble(input); // Specify number of failed schedules int i = 0; Console.WriteLine("Press 1 and then press enter for RunTime mode, otherwise enter a different value " + "and press enter to begin PostProcess mode."); // Prompt user for mode specification int mode; // Declare mode if (int.TryParse(Console.ReadLine(), out mode)) { for (i = 1; i <= iters; i++) { logData = GenRandLogData.Output(constraintsList, stateVarList, subsysList, i); Logger.Output(logData); // Calculate and record percentage of constraint failure, their respective subsystems, etc. if (mode == 1 && i % 25 == 0) // Check if in RunTime mode && if the iteration is the 25th { LogAnalytics = LogAnalyzer.Analyze(LogAnalytics, Logger.Log, constraintsList, subsysList, stateVarList); conOrder = LogAnalyzer.reorderCons(LogAnalytics, constraintList); subOrder = LogAnalyzer.reorderSubs(LogAnalytics, subsystemList); Logger.Log.Data.Clear(); } else if (i % 10 == 0) // PostProcess mode { LogAnalytics = LogAnalyzer.Analyze(LogAnalytics, Logger.Log, constraintsList, subsysList, stateVarList); LogAnalyzer.PostProcess(LogAnalytics, subsysList); conOrder = LogAnalyzer.reorderCons(LogAnalytics, constraintList); subOrder = LogAnalyzer.reorderSubs(LogAnalytics, subsystemList); Logger.Log.Data.Clear(); } } } // Done logging and calculating subsystem failures and constraint violations with relative data }