public void Process(CyPhy.CarTestBench testBench)
        {
            GMEConsole.Info.WriteLine("Processing Test Bench: {0}", testBench.Name);
            List <CyPhy.CarModel> carcomps = CollectCarModels(testBench);

            CreateAcarDatabase();
            CopyAndPrepareResources(CollectCarResources(carcomps));
            GenerateAdamsCarCmd(Path.Combine(OutputDir, "adams_car.cmd"), testBench);
            //PrepareResults(testBench);
            GenerateAdamsViewCmd(Path.Combine(OutputDir, "adams_view.cmd"), testBench);
            string outmetrics = Newtonsoft.Json.JsonConvert.SerializeObject(OutputMetrics, Formatting.Indented);

            using (StreamWriter writer = new StreamWriter(Path.Combine(OutputDir, "metrics_tmp.json"), false))
            {
                writer.WriteLine(outmetrics);
            }
            string outcarparammap = Newtonsoft.Json.JsonConvert.SerializeObject(CarParamMap, Formatting.Indented);

            using (StreamWriter writer = new StreamWriter(Path.Combine(OutputDir, "parammap.json"), false))
            {
                writer.WriteLine(outcarparammap);
            }
            runCarSim_bat bat = new runCarSim_bat();

            using (StreamWriter writer = new StreamWriter(Path.Combine(OutputDir, "runCarSim.bat"), false))
            {
                writer.WriteLine(bat.TransformText());
            }
            if (carcomps == null || !carcomps.Any())
            {
                return;
            }
        }
        private void GenerateAdamsCarCmd(string filename, CyPhy.CarTestBench testBench)
        {
            string dbpath = Path.Combine(OutputDir, CarModelDir).Replace("\\", "\\\\");

            using (StreamWriter writer = new StreamWriter(filename, false))
            {
                writer.WriteLine("acar toolkit database add  &");
                writer.WriteLine("database_name = \"" + acardbname + "\"  &");
                writer.WriteLine("database_path = \"" + dbpath + "\"  &");
                writer.WriteLine("alert = yes  &");
                writer.WriteLine("error_variable = .ACAR.variables.errorFlag");
                writer.WriteLine("!");

                writer.WriteLine("acar files assembly open  &");
                writer.WriteLine("assembly_name = \"mdids://" + acardbname + "/assemblies.tbl/DefaultCar.asy\"  &");
                writer.WriteLine("error_variable = .ACAR.variables.errorFlag");
                writer.WriteLine("!");

                foreach (var sim in testBench.Children.CarSimulationCollection)
                {
                    writer.WriteLine(GetSimulationScriptEntry(sim));
                    writer.WriteLine("!");
                }

                writer.WriteLine("acar toolkit database remove  &");
                writer.WriteLine("database_name = \"" + acardbname + "\"  &");
                writer.WriteLine("error_variable = .ACAR.variables.errorFlag");
            }
        }
        private List <CyPhy.CarModel> CollectCarModels(CyPhy.CarTestBench testBench)
        {
            List <CyPhy.CarModel> result = new List <CyPhy.CarModel>();
            var topSUT = testBench.Children.ComponentAssemblyCollection.FirstOrDefault();

            if (topSUT == null)
            {
                GMEConsole.Error.WriteLine("No Top System Under Test found, nothing to be done.");
                return(null);
            }

            /*            if (topSUT.AllReferred == null)
             *          {
             *              GMEConsole.Error.WriteLine("Top System Under Test is not referring anything. Nothing to be done.");
             *              return null;
             *          }*/
            //if (topSUT.AllReferred is CyPhy.ComponentAssembly)
            //{
            foreach (var comp in topSUT.Children.ComponentCollection)
            {
                if (comp.Children.CarModelCollection.Any())
                {
                    foreach (var carc in comp.Children.CarModelCollection)
                    {
                        result.Add(carc);
                    }
                }
            }

            //}
            return(result);
        }
        private void GenerateAdamsViewCmd(string path, CyPhy.CarTestBench testBench)
        {
            using (StreamWriter writer = new StreamWriter(path, false))
            {
                foreach (var sim in testBench.Children.CarSimulationCollection)
                {
                    writer.WriteLine("file analysis read  &");
                    writer.WriteLine("file_name = \"test_" + sim.Kind + "\"");
                    writer.WriteLine("!");

                    HashSet <string> datatoextract = new HashSet <string>();
                    foreach (var metricconn in sim.DstConnections.CarSimulationToMetricCollection)
                    {
                        if (metricconn.DstEnds.Metric != null)
                        {
                            CarMetric m = new CarMetric();
                            m.MetricID = metricconn.DstEnds.Metric.Name;
                            m.SimName  = sim.Name;
                            if (metricconn.DstEnds.Metric.Name == "Acc0_15")
                            {
                                datatoextract.Add("test_" + sim.Name + ".chassis_velocities.longitudinal");
                                m.AdamsResultName = "chassis_velocities.longitudinal";
                                m.LookupValue     = 15;
                                m.ReverseLookup   = true;
                            }
                            else if (metricconn.DstEnds.Metric.Name == "Acc0_50")
                            {
                                datatoextract.Add("test_" + sim.Name + ".chassis_velocities.longitudinal");
                                m.AdamsResultName = "chassis_velocities.longitudinal";
                                m.LookupValue     = 50;
                                m.ReverseLookup   = true;
                            }
                            else if (metricconn.DstEnds.Metric.Name == "Acc0_65")
                            {
                                datatoextract.Add("test_" + sim.Name + ".chassis_velocities.longitudinal");
                                m.AdamsResultName = "chassis_velocities.longitudinal";
                                m.LookupValue     = 65;
                                m.ReverseLookup   = true;
                            }
                            else if (metricconn.DstEnds.Metric.Name == "Braking_Dist")
                            {
                                datatoextract.Add("test_" + sim.Name + ".chassis_displacements.longitudinal");
                                m.AdamsResultName = "chassis_displacements.longitudinal";
                                m.LookupValue     = 300; // Last number
                            }
                            else if (metricconn.DstEnds.Metric.Name == "Braking_Accel")
                            {
                                datatoextract.Add("test_" + sim.Name + ".chassis_accelerations.longitudinal");
                                m.AdamsResultName = "chassis_accelerations.longitudinal";
                                m.Type            = CarMetric.LookupType.Min;
                            }
                            else if (metricconn.DstEnds.Metric.Name == "TopSpeed")
                            {
                                datatoextract.Add("test_" + sim.Name + ".chassis_velocities.longitudinal");
                                m.AdamsResultName = "chassis_velocities.longitudinal";
                                m.Type            = CarMetric.LookupType.Max;
                            }
                            else if (metricconn.DstEnds.Metric.Name == "RideQuality")
                            {
                                datatoextract.Add("test_" + sim.Name + ".absorbed_power.driver_seat_accel_var.Q");
                                m.AdamsResultName = "absorbed_power.driver_seat_accel_var.Q";
                                m.Type            = CarMetric.LookupType.Max;
                            }
                            else if (metricconn.DstEnds.Metric.Name == "TurnCircle")
                            {
                                datatoextract.Add("test_" + sim.Name + ".til_wheel_contact_patch.y_front");
                                m.AdamsResultName = "til_wheel_contact_patch.y_front";
                                m.Type            = CarMetric.LookupType.Range;
                            }
                            else
                            {
                                GMEConsole.Warning.WriteLine("Unknown Metric ID: " + metricconn.DstEnds.Metric.Name + ", ignoring.");
                                continue;
                            }
                            OutputMetrics.Add(m);
                        }
                    }
                    foreach (string data in datatoextract)
                    {
                        var resext = new NumericResultExtract();
                        resext.Name     = data;
                        resext.FileName = data + "_res.txt";
                        writer.WriteLine(resext.TransformText());
                        writer.WriteLine("!");
                    }

                    writer.WriteLine("numeric_results list_info all  &");
                    writer.WriteLine("result_set_component_name = test_" + sim.Kind + ".TIME  &");
                    writer.WriteLine("write_to_terminal = off &");
                    writer.WriteLine("file_name = \"TIME_" + sim.Name + "_res.txt\"");
                    writer.WriteLine("!");
                }
            }
        }