Ejemplo n.º 1
0
 public static DisjunctiveExamplesSpec WitnessK(GrammarRule rule, int parameter,
                                                DisjunctiveExamplesSpec spec)
 {
     var kExamples = new Dictionary<State, IEnumerable<object>>();
     foreach (State input in spec.ProvidedInputs)
     {
         var v = (StringRegion) input[rule.Body[0]];
         var positions = new List<object>();
         foreach (uint pos in spec.DisjunctiveExamples[input])
         {
             positions.Add((int) pos + 1 - (int) v.Start);
             positions.Add((int) pos - (int) v.End - 1);
         }
         kExamples[input] = positions;
     }
     return DisjunctiveExamplesSpec.From(kExamples);
 }
Ejemplo n.º 2
0
 static DisjunctiveExamplesSpec WitnessK(GrammarRule rule, int parameter, DisjunctiveExamplesSpec spec)
 {
     var result = new Dictionary<State, IEnumerable<object>>();
     foreach (var example in spec.DisjunctiveExamples)
     {
         State inputState = example.Key;
         var ks = new HashSet<int?>();
         var inp = (string)inputState[rule.Body[0]];
         foreach (int? pos in example.Value)
         {
             ks.Add(pos);
             ks.Add(pos - inp.Length - 1);
         }
         if (ks.Count == 0) return null;
         result[inputState] = ks.Cast<object>();
     }
     return new DisjunctiveExamplesSpec(result);
 }
Ejemplo n.º 3
0
 static DisjunctiveExamplesSpec WitnessRegexPair(GrammarRule rule, int parameter,  DisjunctiveExamplesSpec spec)
 {
     var result = new Dictionary<State, IEnumerable<object>>();
     foreach (var example in spec.DisjunctiveExamples)
     {
         State inputState = example.Key;
         var inp = (string)inputState[rule.Body[0]];
         List<Tuple<Match, Regex>>[] leftMatches, rightMatches;
         BuildStringMatches(inp, out leftMatches, out rightMatches);
         var regexes = new List<Tuple<Regex, Regex>>();
         foreach (int? pos in example.Value)
         {
             regexes.AddRange(from l in leftMatches[pos.Value]
                              from r in rightMatches[pos.Value]
                              select Tuple.Create(l.Item2, r.Item2));
         }
         if (regexes.Count == 0) return null;
         result[inputState] = regexes;
     }
     return new DisjunctiveExamplesSpec(result);
 }
Ejemplo n.º 4
0
 public static DisjunctiveExamplesSpec WitnessRegexCount(GrammarRule rule, int parameter,
                                                         DisjunctiveExamplesSpec spec,
                                                         ExampleSpec regexBinding)
 {
     var kExamples = new Dictionary<State, IEnumerable<object>>();
     foreach (State input in spec.ProvidedInputs)
     {
         var v = (StringRegion) input[rule.Body[0]];
         var rr = (Tuple<RegularExpression, RegularExpression>) regexBinding.Examples[input];
         var ks = new List<object>();
         foreach (uint pos in spec.DisjunctiveExamples[input])
         {
             var ms = rr.Item1.Run(v).Where(m => rr.Item2.MatchesAt(v, m.Right)).ToArray();
             int index = ms.BinarySearchBy(m => m.Right.CompareTo(pos));
             if (index < 0) return null;
             ks.Add(index + 1);
             ks.Add(index - ms.Length);
         }
         kExamples[input] = ks;
     }
     return DisjunctiveExamplesSpec.From(kExamples);
 }
Ejemplo n.º 5
0
 public static DisjunctiveExamplesSpec WitnessRegexPair(GrammarRule rule, int parameter,
                                                        DisjunctiveExamplesSpec spec)
 {
     var rrExamples = new Dictionary<State, IEnumerable<object>>();
     foreach (State input in spec.ProvidedInputs)
     {
         var v = (StringRegion) input[rule.Body[0]];
         var regexes = new List<Tuple<RegularExpression, RegularExpression>>();
         foreach (uint pos in spec.DisjunctiveExamples[input])
         {
             Dictionary<Token, TokenMatch> rightMatches;
             if (!v.Cache.TryGetAllMatchesStartingAt(pos, out rightMatches)) continue;
             Dictionary<Token, TokenMatch> leftMatches;
             if (!v.Cache.TryGetAllMatchesEndingAt(pos, out leftMatches)) continue;
             var leftRegexes = leftMatches.Keys.Select(RegularExpression.Create).Append(Epsilon);
             var rightRegexes = rightMatches.Keys.Select(RegularExpression.Create).Append(Epsilon);
             var regexPairs = from l in leftRegexes from r in rightRegexes select Tuple.Create(l, r);
             regexes.AddRange(regexPairs);
         }
         rrExamples[input] = regexes;
     }
     return DisjunctiveExamplesSpec.From(rrExamples);
 }
Ejemplo n.º 6
0
 static DisjunctiveExamplesSpec WitnessKForRegexPair(GrammarRule rule, int parameter,  DisjunctiveExamplesSpec spec, ExampleSpec rrSpec)
 {
     var result = new Dictionary<State, IEnumerable<object>>();
     foreach (var example in spec.DisjunctiveExamples)
     {
         State inputState = example.Key;
         var inp = (string)inputState[rule.Body[0]];
         var regexPair = (Tuple<Regex, Regex>)rrSpec.Examples[inputState];
         Regex left = regexPair.Item1;
         Regex right = regexPair.Item2;
         var rightMatches = right.Matches(inp).Cast<Match>().ToDictionary(m => m.Index);
         var matchPositions = new List<int>();
         foreach (Match m in left.Matches(inp))
         {
             if (rightMatches.ContainsKey(m.Index + m.Length))
                 matchPositions.Add(m.Index + m.Length);
         }
         var ks = new HashSet<int?>();
         foreach (int? pos in example.Value)
         {
             int occurrence = matchPositions.BinarySearch(pos.Value);
             if (occurrence < 0) continue;
             ks.Add(occurrence);
             ks.Add(occurrence - matchPositions.Count);
         }
         if (ks.Count == 0) return null;
         result[inputState] = ks.Cast<object>();
     }
     return new DisjunctiveExamplesSpec(result);
 }
Ejemplo n.º 7
0
        internal DisjunctiveExamplesSpec WitnessSelectWithWhereColumnArray(GrammarRule rule, DisjunctiveExamplesSpec spec, ExampleSpec conditionSpec)
        {
            var ppExamples     = new Dictionary <State, IEnumerable <object> >();
            var conditionTable = (DataTable)conditionSpec.Examples.Values.First();

            foreach (State input in spec.ProvidedInputs)
            {
                DataTable inputTable           = ((DataTable[])input[rule.Grammar.InputSymbol])[0];
                var       desiredOutput        = (DataTable)spec.DisjunctiveExamples[input].First(); // conditionTable;
                var       allPossibleSolutions = new List <object>();
                allPossibleSolutions.Add(desiredOutput.Columns.Cast <DataColumn>().ToArray());

                ppExamples[input] = allPossibleSolutions;
            }
            //Complete the rows
            return(DisjunctiveExamplesSpec.From(ppExamples));
        }
Ejemplo n.º 8
0
        internal DisjunctiveExamplesSpec WitnessComparatorConstValue(GrammarRule rule, DisjunctiveExamplesSpec spec, ExampleSpec columnSpec, ExampleSpec cmpSymbolSpec)
        {
            /* Inverse for the constValue field */
            var ppExamples = new Dictionary <State, IEnumerable <object> >();


            foreach (State input in spec.ProvidedInputs)
            {
                // Add a for loop here & Iterate over it to add everything to all possible solutions
                var allPossibleSolutions = new List <object>();
                foreach (DataTable outputTable in spec.DisjunctiveExamples[input])
                {
                    DataTable inputTable = ((DataTable[])input[rule.Grammar.InputSymbol])[0];
                    if (outputTable.Rows.Count == 0)
                    {
                        ppExamples[input] = allPossibleSolutions;
                        continue;
                    }
                    string     cmpSymbol = (string)cmpSymbolSpec.Examples[cmpSymbolSpec.ProvidedInputs.First()]; // Get the comparison symbol
                    DataColumn column    = (DataColumn)columnSpec.Examples[columnSpec.ProvidedInputs.First()];   // Column

                    bool flag = true;

                    if (column.DataType == typeof(string))

                    {
                        var    mappedCmpSymbol = "";
                        string valueToCompare  = "";
                        switch (cmpSymbol)
                        {
                        case "==":
                            mappedCmpSymbol = "=";
                            var valuesInColumn =
                                outputTable.Rows.Cast <DataRow>().Select(t => t[column.ColumnName]).Distinct();
                            // TODO: Get a linq guy to look this up
                            if (valuesInColumn.Count() != 1)
                            {
                                flag = false;
                            }
                            else
                            {
                                valueToCompare = (string)valuesInColumn.First();
                            }
                            // else, Keep it as empty

                            break;

                        default:
                            // TODO: Unsupported datatype
                            flag = false;
                            break;
                        }
                        if (flag)
                        {
                            var countRowsInput = inputTable.Select(column.ColumnName + mappedCmpSymbol + "'" + valueToCompare + "'").Count();
                            // var countRowsOutput = outputTable.Select(column.ColumnName + mappedCmpSymbol + "'" + valueToCompare + "'").Count();
                            var countRowsOutput = outputTable.Rows.Count;     // This seems more correct
                            if (flag && countRowsInput == countRowsOutput)
                            {
                                allPossibleSolutions.Add((object)valueToCompare);
                            }
                        }
                    }
                    else if (column.DataType == typeof(double))
                    {
                        double valueToCompare  = 0;
                        var    mappedCmpSymbol = "";

                        switch (cmpSymbol)
                        {
                        case "==":
                            mappedCmpSymbol = "=";
                            var valuesInColumn =
                                outputTable.Rows.Cast <DataRow>().Select(t => t[column.ColumnName]).Distinct();
                            // TODO: Get a linq guy to look this up
                            if (valuesInColumn.Count() != 1)
                            {
                                flag = false;
                            }
                            else
                            {
                                valueToCompare = (double)valuesInColumn.First();
                            }
                            // else, Keep it as empty
                            break;

                        case ">=":
                        {
                            mappedCmpSymbol = ">=";
                            valueToCompare  = (double)outputTable.Rows.Cast <DataRow>().Select(t => t[column.ColumnName]).Min();
                            break;
                        }

                        case "<=":
                        {
                            mappedCmpSymbol = "<=";

                            valueToCompare = (double)outputTable.Rows.Cast <DataRow>().Select(t => t[column.ColumnName]).Max();
                            break;
                        }

                        case "<":
                        {
                            mappedCmpSymbol = "<";
                            var excludedRow1 = inputTable.AsEnumerable().Where(r =>
                                                                               !outputTable.AsEnumerable()
                                                                               .Select(x =>
                                                                                       x[column.ColumnName])
                                                                               .ToList()
                                                                               .Contains(r[column.ColumnName])
                                                                               );
                            if (excludedRow1 != null && excludedRow1.Count() != 0)
                            {
                                valueToCompare = (double)
                                                 ((excludedRow1).ToList()).Cast <DataRow>().Select(t => t[column.ColumnName]).Min();
                                if (valueToCompare <=
                                    (double)outputTable
                                    .Rows.Cast <DataRow>()
                                    .Select(t => t[column.ColumnName]).Max()
                                    )
                                {
                                    flag = false;
                                }
                            }
                            else
                            {
                                flag = false;
                            }

                            break;
                        }

                        case ">":
                            mappedCmpSymbol = ">";
                            var excludedRow = inputTable.AsEnumerable().Where(r => !outputTable.AsEnumerable().Select(x
                                                                                                                      => x[column.ColumnName]).ToList().Contains(r[column.ColumnName]));
                            if (excludedRow != null && excludedRow.Count() != 0)
                            {
                                valueToCompare = (double)
                                                 (excludedRow.ToList()).Cast <DataRow>().Select(t => t[column.ColumnName]).Max();
                                if (valueToCompare >=
                                    (double)outputTable
                                    .Rows.Cast <DataRow>()
                                    .Select(t => t[column.ColumnName]).Min()
                                    )
                                {
                                    flag = false;
                                }
                            }
                            else
                            {
                                flag = false;
                            }
                            break;

                        case "!=":
                            mappedCmpSymbol = "<>";
                            var excluded2 = inputTable.AsEnumerable().Where(r =>
                                                                            !outputTable.AsEnumerable()
                                                                            .Select(x =>
                                                                                    x[column.ColumnName]).ToList()
                                                                            .Contains(r[column.ColumnName])
                                                                            );
                            var valuesExcludedInColumn =
                                ((excluded2).ToList()).Cast <DataRow>().Select(t => t[column.ColumnName]).Distinct();
                            // TODO: Get a linq guy to look this up
                            if (valuesExcludedInColumn.Count() != 1)
                            {
                                flag = false;
                            }
                            else
                            {
                                valueToCompare = (double)valuesExcludedInColumn.First();
                            }

                            // else, Keep it as empty
                            break;

                        default:
                            // TODO: Unsupported datatype
                            flag = false;
                            break;
                        }

                        if (flag)
                        {
                            var countRowsInput  = inputTable.Select(column.ColumnName + mappedCmpSymbol + valueToCompare).Count();
                            var countRowsOutput = outputTable.Select(column.ColumnName + mappedCmpSymbol + valueToCompare).Count();
                            // This seems more correct // var countRowsOutput = outputTable.Rows.Count;
                            if (flag && countRowsInput == countRowsOutput && (countRowsInput != 0 || outputTable.Rows.Count == 0))
                            {
                                allPossibleSolutions.Add((object)valueToCompare);
                            }
                        }
                    }
                }
                ppExamples[input] = allPossibleSolutions;
            }
            return(DisjunctiveExamplesSpec.From(ppExamples));
        }
Ejemplo n.º 9
0
        internal DisjunctiveExamplesSpec WitnessComparatorCmpSymbol(GrammarRule rule, DisjunctiveExamplesSpec spec)
        {
            /* Inverse for the constValue field */
            var ppExamples = new Dictionary <State, IEnumerable <object> >();

            foreach (State input in spec.ProvidedInputs)
            {
                var allPossibleSolutions = Semantics.CmpGen;
                ppExamples[input] = allPossibleSolutions;
            }
            return(DisjunctiveExamplesSpec.From(ppExamples));
        }
Ejemplo n.º 10
0
        internal DisjunctiveExamplesSpec WitnessConcatTree2(GrammarRule rule, DisjunctiveExamplesSpec spec, DisjunctiveExamplesSpec tree1Spec)
        {
            var result = new Dictionary <State, IEnumerable <object> >();

            foreach (KeyValuePair <State, IEnumerable <object> > example in spec.DisjunctiveExamples)
            {
                State inputState = example.Key;
                List <IReadOnlyList <Node> > possibleCombinations = new List <IReadOnlyList <Node> >();
                foreach (IReadOnlyList <Node> tree1NodeList in tree1Spec.DisjunctiveExamples[inputState])
                {
                    IEnumerable <Node> temp = from output in example.Value
                                              from outNode in (IReadOnlyList <Node>)output
                                              where tree1NodeList.All(
                        tree1Node => Semantics.NodeValue(tree1Node, Path) != Semantics.NodeValue(outNode, Path))
                                              select outNode;
                    possibleCombinations.Add(temp.ToList());
                }

                result[inputState] = possibleCombinations;
            }

            return(DisjunctiveExamplesSpec.From(result));
        }
Ejemplo n.º 11
0
 internal DisjunctiveExamplesSpec WitnessApplyAction(GrammarRule rule, DisjunctiveExamplesSpec spec)
 {
     return(spec);
 }
Ejemplo n.º 12
0
        internal DisjunctiveExamplesSpec WitnessSelectDownstreamPath(GrammarRule rule, DisjunctiveExamplesSpec spec)
        {
            var result = new Dictionary <State, IEnumerable <object> >();

            foreach (KeyValuePair <State, IEnumerable <object> > example in spec.DisjunctiveExamples)
            {
                State inputState = example.Key;
                var   input      = example.Key[Grammar.InputSymbol] as MergeConflict;
                result[inputState] = SelectPath(example.Value, input.Downstream);
            }
            return(DisjunctiveExamplesSpec.From(result));
        }
Ejemplo n.º 13
0
        internal DisjunctiveExamplesSpec WitnessRemoveTree2(GrammarRule rule, DisjunctiveExamplesSpec spec, DisjunctiveExamplesSpec tree1Spec)
        {
            var result = new Dictionary <State, IEnumerable <object> >();

            foreach (KeyValuePair <State, IEnumerable <object> > example in spec.DisjunctiveExamples)
            {
                State inputState = example.Key;
                List <IReadOnlyList <Node> > possibleCombinations = new List <IReadOnlyList <Node> >();

                foreach (IReadOnlyList <Node> output in example.Value)
                {
                    foreach (IReadOnlyList <Node> tree1NodeList in tree1Spec.DisjunctiveExamples[inputState])
                    {
                        possibleCombinations.Add(
                            tree1NodeList.Where(n => !output.Select(Utils.GetPath).Contains(n.GetPath())).ToList());
                    }
                }

                result[inputState] = possibleCombinations;
            }

            return(DisjunctiveExamplesSpec.From(result));
        }