Exemplo n.º 1
0
        static string GetHash(PairwiseSettings settings, string input)
        {
            if (!settings.CanCache)
            {
                return(Guid.NewGuid().ToString());
            }

            string both = settings.GetPictArgs() + input;

            return(Convert.ToBase64String(HashAlg.ComputeHash(EncodingForHashAlgorithm.GetBytes(both))));
        }
Exemplo n.º 2
0
        public string[][] MaybeExecutePict(PairwiseSettings settings, string inputContent)
        {
            string key = GetHash(settings, inputContent);

            if (cache != null && settings.CanCache && cache.Contains(key))
            {
                PictConstants.Trace("Found key, using cached value!");
                this.last = cache[key];
                return(last.ParsedOutput);
            }

            string randomFileName = Guid.NewGuid().ToString() + PictConstants.TempFileSuffix;

            using (StreamWriter sw = new StreamWriter(randomFileName))
            {
                sw.WriteLine("# " + randomFileName + " " + settings.GetPictArgs());
                sw.WriteLine(inputContent);
            }

            try
            {
                string[][] ret = AlwaysExecutePictOnFileName(randomFileName, settings);

                if (cache != null && settings.CanCache)
                {
                    // always execute pict set last correctly
                    cache[key] = this.LastExecutionInformation;
                }

                return(ret);
            }
            finally
            {
                File.Delete(randomFileName);
            }
        }
Exemplo n.º 3
0
 internal PairwiseModelLookupContext CreateLookupContext(PairwiseSettings settings)
 {
     return(new PairwiseModelLookupContext(this, settings));
 }
Exemplo n.º 4
0
 public PairwiseModelLookupContext(PairwiseModel model, PairwiseSettings settings)
 {
     this.parameters = new PairwiseParameterCollection(model.Parameters);
     this.settings   = settings;
 }
Exemplo n.º 5
0
 public PairwiseTuple[] GenerateTuples(PairwiseSettings settings)
 {
     return(new PictPairwiseVisitor(this, settings).Generate());
 }
Exemplo n.º 6
0
 public PictPairwiseVisitor(PairwiseModel model, PairwiseSettings settings)
 {
     this.model    = model;
     this.settings = settings;
     this.context  = model.CreateLookupContext(settings);
 }
Exemplo n.º 7
0
        string[][] AlwaysExecutePictOnFileNameCore(string modelFileName, string argsForPict, PairwiseSettings settings)
        {
            if (modelFileName == null)
            {
                throw new ArgumentNullException("modelFileName");
            }

            if (argsForPict == null)
            {
                argsForPict = "";
            }
            else
            {
                argsForPict = argsForPict.Trim();
            }

            if (!File.Exists(modelFileName))
            {
                throw new FileNotFoundException("Pict model not found: " + modelFileName);
            }

            ProcessStartInfo psi = new ProcessStartInfo();

            // this will throw if not found
            psi.FileName = PairwiseSettings.GetPictExecutableFullPath();
            PictConstants.Trace("Attempting to use {0}", psi.FileName);

            psi.Arguments              = (MaybeQuote(modelFileName) + " " + argsForPict).Trim();
            psi.UseShellExecute        = false;
            psi.RedirectStandardError  = true;
            psi.RedirectStandardOutput = true;

            DateTime started = DateTime.Now;

            PictConstants.Trace("Starting " + psi.Arguments);

            using (Process process = Process.Start(psi))
            {
                PictConstants.Trace("Started successfully");

                string[][] parsed = Split(process.StandardOutput, PictConstants.OutputValueSeparator);

                process.StandardOutput.Close();

                int  seed      = 0;
                bool wasRandom = false;

                process.WaitForExit();
                PictConstants.Trace("Done waiting for exit");

                TimeSpan elapsed = DateTime.Now - started;

                PictConstants.Trace("Elapsed time: {0}", elapsed);

                // note: there's some better way to do this??
                string allStdErr = process.StandardError.ReadToEnd();

                process.StandardError.Close();
                if (allStdErr.Length != 0)
                {
                    PictConstants.Trace("Begin stderr/{0}/end stderr*", allStdErr);

                    ArrayList msgs = new ArrayList();
                    foreach (string s in allStdErr.Split('\r', '\n'))
                    {
                        if (s.Trim().Length != 0)
                        {
                            msgs.Add(s.TrimEnd());
                        }
                    }

                    for (int i = 0; i < msgs.Count; ++i)
                    {
                        string message = (string)msgs[i];

                        if (message.Trim().Length == 0)
                        {
                            continue;
                        }

                        if (message.StartsWith(PictConstants.UsedSeedMessage))
                        {
                            message   = message.Replace(PictConstants.UsedSeedMessage, "").Trim();
                            wasRandom = true;
                            seed      = int.Parse(message, PictConstants.Culture);
                            PictConstants.Trace("{0} {1}", wasRandom, seed);
                        }
                        else if (message.StartsWith(PictConstants.WarningMessage) || (message.IndexOf("arning") != -1))
                        {
                            while (i < msgs.Count - 1 && ((string)msgs[i + 1]).StartsWith(" "))
                            {
                                message += Environment.NewLine + ((string)msgs[++i]);
                            }

                            if (settings != null)
                            {
                                settings.OnWarning(message.Trim());
                            }
                        }
                        else
                        {
                            PictConstants.Trace("throwing /{0}/", message.Trim());
                            throw new PairwiseException(message.Trim());
                        }
                    }
                }

                // string stdout = sb.ToString();
                PictExecutionInformation pei = new PictExecutionInformation();

                pei.FileName            = modelFileName;
                pei.Generated           = DateTime.Now;
                pei.Options             = argsForPict;
                pei.RandomSeedSpecified = wasRandom;

                // pei.RawOutput = stdout;
                pei.ParsedOutput = parsed;
                if (wasRandom)
                {
                    pei.RandomSeed = seed;
                }

                this.last = pei;
                return(parsed);
            } // end using(Process)
        }
Exemplo n.º 8
0
 public string[][] AlwaysExecutePictOnFileName(string modelFileName, PairwiseSettings settings)
 {
     return(AlwaysExecutePictOnFileNameCore(modelFileName, settings.GetPictArgs(), settings));
 }
Exemplo n.º 9
0
        public static PairwiseSettings Parse(string argsForPict)
        {
            if (argsForPict == null)
            {
                throw new ArgumentNullException("argsForPict");
            }
            PairwiseSettings settings = new PairwiseSettings(2, false);

            foreach (string raw in argsForPict.Split())
            {
                string s = raw.Trim();

                if (s.Length == 0)
                {
                    continue;
                }

                if (s[0] != '/')
                {
                    throw new ArgumentOutOfRangeException("Unrecognized prefix: " + s);
                }

                char cmd = Char.ToLower(s[1], PictConstants.Culture);

                if (s.Length > 3 && s[2] != ':')
                {
                    throw new ArgumentOutOfRangeException("Unrecognized delimiter: " + s);
                }

                PictConstants.Trace(s + " " + s.Length);

                string extra = s.Length > 2 ? s.Substring(3) : "";

                switch (cmd)
                {
                case 'o':
                    settings.Order = Int32.Parse(extra, PictConstants.Culture);
                    break;

                case 'd':
                    settings.delimParameter    = GetChar(extra);
                    settings.setValueSeparator = settings.delimParameter + " ";
                    break;

                case 'a':
                    settings.aliasDelimiter = GetChar(extra);
                    break;

                case 'n':
                    settings.negativePrefix       = GetChar(extra);
                    settings.negativePrefixString = settings.negativePrefix.ToString(PictConstants.Culture);
                    break;

                case 'c':
                    settings.IsCaseSensitive = true;
                    break;

                case 'r':
                    settings.RandomizeGeneration = true;
                    if (extra.Length != 0)
                    {
                        settings.RandomSeedSpecified = true;
                        settings.RandomSeed          = Int32.Parse(extra);
                    }

                    break;

                case 'e':
                    settings.seedFile = extra;
                    break;

                default:
                    throw new ArgumentOutOfRangeException("Unrecognized: " + raw);
                }
            }

            PictConstants.Trace("Parse: {0} -> {1}", argsForPict, settings.GetPictArgs());
            return(settings);
        }