Esempio n. 1
0
        private static void PerformPromelaTranslation(string[] args)
        {
            var kplFileName           = string.Empty;
            var kpxFileName           = string.Empty;
            var outputFileName        = string.Empty;
            var translationParameters = PromelaTranslationParams.Default();
            var properties            = typeof(PromelaTranslationConfig).GetProperties();
            var argIndex = 1;

            if (args.Length >= 2)
            {
                kplFileName = args[argIndex++];

                if (!new FileInfo(kplFileName).Exists)
                {
                    throw new Exception(string.Format("File '{0}' does not exist. Please specify a valid input file.", kplFileName));
                }
            }
            else
            {
                throw new Exception("Source file with kP system model not specified");
            }

            for (int i = argIndex; i < args.Length; i++)
            {
                if (args[i] == "-e")
                {
                    if (++i < args.Length)
                    {
                        kpxFileName = args[i];
                    }
                }
                else
                {
                    if (args[i] == "-o")
                    {
                        if (++i < args.Length)
                        {
                            outputFileName = args[i];
                        }
                    }
                    else
                    {
                        string arg     = args[i];
                        int    eqIndex = arg.IndexOf('=');
                        if (eqIndex > 0)
                        {
                            string prop = arg.Substring(0, eqIndex);
                            string val  = arg.Substring(eqIndex + 1);
                            bool   propertyRecognized = false;
                            foreach (PropertyInfo property in properties)
                            {
                                if (property.Name == prop)
                                {
                                    try
                                    {
                                        if (property.PropertyType == typeof(Int32))
                                        {
                                            property.SetValue(translationParameters, Int32.Parse(val));
                                        }
                                        else if (property.PropertyType == typeof(bool))
                                        {
                                            property.SetValue(translationParameters, bool.Parse(val));
                                        }
                                        else
                                        {
                                            property.SetValue(translationParameters, val);
                                        }
                                    }
                                    catch
                                    {
                                        throw new Exception(string.Format("*** Warning. Invalid format for property {0}: value must be of type {1}",
                                                                          property.Name, property.PropertyType.ToString()));
                                    }
                                    propertyRecognized = true;
                                    break;
                                }
                            }

                            if (!propertyRecognized)
                            {
                                throw new Exception(string.Format("*** Warning. Property {0} not recognized.", prop));
                            }
                        }
                    }
                }
            }

            KpModel kpModel = null;

            try
            {
                kpModel = KP.FromKpl(kplFileName);
            }
            catch (KplParseException kplException)
            {
                throw new Exception(string.Format("Failed to parse input file {0}. Reason: {1}", kplFileName, kplException.Message));
            }

            Experiment kpExperiment = null;

            if (!string.IsNullOrEmpty(kpxFileName))
            {
                if (new FileInfo(kpxFileName).Exists)
                {
                    try
                    {
                        kpExperiment = KP.FromKpx(kpxFileName);
                    }
                    catch (Exception exception)
                    {
                        throw new Exception(string.Format("Failed to parse input file {0}. Reason: {1}", kplFileName, exception.Message));
                    }
                }
                else
                {
                    throw new Exception(string.Format("File '{0}' does not exist. Please specify a valid experiment file.", kpxFileName));
                }
            }

            Console.WriteLine("Generating verification model {0} with: ", outputFileName);
            Console.WriteLine();
            foreach (PropertyInfo property in properties)
            {
                Console.WriteLine("{0} = {1}", property.Name, property.GetValue(translationParameters).ToString());
            }
            Console.WriteLine();

            var stream = string.IsNullOrEmpty(outputFileName) ? Console.Out : new StreamWriter(outputFileName);

            KP.WritePromela(kpModel.KPsystem, kpExperiment, translationParameters, stream);
        }