Beispiel #1
0
        internal DisjunctiveExamplesSpec WitnessSelectWithWhereCondition(GrammarRule rule, ExampleSpec spec)
        {
            var ppExamples = new Dictionary <State, IEnumerable <object> >();

            foreach (State input in spec.ProvidedInputs)
            {
                var        inputTable           = ((DataTable[])input[rule.Grammar.InputSymbol])[0];
                var        outputTable          = (DataTable)spec.Examples[input];
                var        allPossibleSolutions = new List <object>();
                var        selectQuery          = outputTable.PrimaryKey.Select(c => c.ColumnName).ToArray();
                var        completeRowsList     = new List <DataRow[]>();
                DataView   view            = new DataView(outputTable);
                List <int> outputCountList = new List <int>();
                DataTable  distinctValues  = view.ToTable(true, selectQuery);
                foreach (DataRow row in distinctValues.Rows)
                {
                    var query = string.Join(" AND ", selectQuery.Select(c => (inputTable.Columns[c].DataType == typeof(string))? $"{c}='{row[c]}'":$"{c}={row[c]}"));
                    completeRowsList.Add(inputTable.Select(query));
                    outputCountList.Add(outputTable.Select(query).Count());
                }
                var conditionTables = Utils.Utils.GetAllPossibleTables(completeRowsList, outputCountList, inputTable);
                allPossibleSolutions.AddRange(conditionTables);
                ppExamples[input] = allPossibleSolutions;
            }
            //Complete the rows
            return(DisjunctiveExamplesSpec.From(ppExamples));
        }
        public DisjunctiveExamplesSpec WitnessRegexPair(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      = inputState[rule.Body[0]] as string;

                var regexes = new List <Tuple <Regex, Regex> >();
                foreach (int output in example.Value)
                {
                    List <Regex>[] leftMatches, rightMatches;
                    BuildStringMatches(input, out leftMatches, out rightMatches);


                    List <Regex> leftRegex  = leftMatches[output];
                    List <Regex> rightRegex = rightMatches[output];
                    if (leftRegex.Count == 0 || rightRegex.Count == 0)
                    {
                        return(null);
                    }
                    regexes.AddRange(from l in leftRegex
                                     from r in rightRegex
                                     select Tuple.Create(l, r));
                }
                if (regexes.Count == 0)
                {
                    return(null);
                }
                result[inputState] = regexes;
            }
            return(DisjunctiveExamplesSpec.From(result));
        }
Beispiel #3
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));
        }
        private static DisjunctiveExamplesSpec deduceSubstrBasedKey(FunctionalDisjunctiveOutputSpec spec, ComparatorCreator cmpCreator)
        {
            var strExamples = new Dictionary <State, IEnumerable <object> >();

            foreach (State input in spec.ProvidedInputs)
            {
                var validStrings = new List <string>();
                var relation     = spec.Relation[input];

                var superString = "";

                // To find a super-string, find a mapping that contains exactly
                // one value (i.e. a strict ordering)
                foreach (var mapping in relation)
                {
                    var key             = (Tuple <string, string>)mapping.Key;
                    var possibleResults = mapping.Value;
                    if (possibleResults.Count == 1)
                    {
                        var resultsEnumerator = possibleResults.GetEnumerator();
                        resultsEnumerator.MoveNext();
                        var onlyResult = (int)resultsEnumerator.Current;
                        if (onlyResult == 0)
                        {
                            throw new Exception("strict comparisons cannot be zero");
                        }

                        // If we have found a strict ordering, the super-string is
                        // the item corresponding to the larger value
                        superString = (string)((onlyResult > 0) ? key.Item1 : key.Item2);
                        break;
                    }
                }

                if (superString.Length == 0)
                {
                    throw new Exception("No strict ordering found");
                }

                // Generate all possible sub-strings from superString
                for (var i = 0; i < superString.Length - 1; i++)
                {
                    for (var len = 1; i + len <= superString.Length; len++)
                    {
                        // Extract sub-string
                        var subStr = superString.Substring(i, len);
                        // Create comparator from provided lambda
                        var cmp = cmpCreator(subStr);
                        // Check if this subStr is consistent for all mappings
                        if (validateComparator(relation, cmp))
                        {
                            validStrings.Add(subStr);
                        }
                    }
                }
                strExamples[input] = validStrings;
            }

            return(DisjunctiveExamplesSpec.From(strExamples));
        }
Beispiel #5
0
        public static DisjunctiveExamplesSpec WitnessRelativeToken(GrammarRule rule, int parameter, ExampleSpec spec)
        {
            var result = new Dictionary <State, IEnumerable <object> >();

            foreach (var input in spec.ProvidedInputs)
            {
                var example = spec.DisjunctiveExamples[input].First() as Tuple <PythonNode, PythonNode>;
                if (example != null)
                {
                    //if the anchor node is the target node, we don't generate relative paths
                    //just the root path "0"
                    if (AnchorAndTargetAreEqual(example))
                    {
                        return(null);
                    }
                    var node         = example.Item1;
                    var treeTemplate = new TreeTemplate(node.GetType().Name);
                    if (node.Value != null)
                    {
                        treeTemplate.Value = node.Value;
                    }

                    result[input] = new List <TreeTemplate>()
                    {
                        treeTemplate, new Wildcard(node.GetType().Name)
                    };
                }
                else
                {
                    return(null);
                }
            }
            return(DisjunctiveExamplesSpec.From(result));
        }
Beispiel #6
0
        public DisjunctiveExamplesSpec WitnessK(GrammarRule rule, DisjunctiveExamplesSpec spec)
        {
            var kExamples = new Dictionary <State, IEnumerable <object> >();

            foreach (var example in spec.DisjunctiveExamples)
            {
                State inputState = example.Key;
                var   v          = inputState[rule.Body[0]] as string;

                var positions = new List <int>();
                foreach (int pos in example.Value)
                {
                    //the positive spec for k
                    positions.Add(pos + 1);
                    //TODO add the negative spec for k
                    //uncomment the next statement and replace X by the expression that should return the negative spec
                    //positions.Add(X);
                }
                if (positions.Count == 0)
                {
                    return(null);
                }
                kExamples[inputState] = positions.Cast <object>();
            }
            return(DisjunctiveExamplesSpec.From(kExamples));
        }
        public static DisjunctiveExamplesSpec WitnessRegexPair(GrammarRule rule, int parameter, ExampleSpec spec)
        {
            var result = new Dictionary <State, IEnumerable <object> >();

            foreach (var example in spec.Examples)
            {
                State inputState = example.Key;
                var   input      = inputState[rule.Body[0]] as string;
                var   output     = (int)example.Value;

                List <Tuple <Match, Regex> >[] leftMatches, rightMatches;
                BuildStringMatches(input, out leftMatches, out rightMatches);

                var leftRegex  = leftMatches[output];
                var rightRegex = rightMatches[output];
                if (leftRegex.Count == 0 || rightRegex.Count == 0)
                {
                    return(null);
                }
                var regexes = new List <Tuple <Regex, Regex> >();
                regexes.AddRange(from l in leftRegex
                                 from r in rightRegex
                                 select Tuple.Create(l.Item2, r.Item2));
                result[inputState] = regexes;
            }
            return(DisjunctiveExamplesSpec.From(result));
        }
        internal DisjunctiveExamplesSpec WitnessCheck(GrammarRule rule, DisjunctiveExamplesSpec spec)
        {
            var result = new Dictionary <State, IEnumerable <object> >();

            foreach (KeyValuePair <State, IEnumerable <object> > example in spec.DisjunctiveExamples)
            {
                State        inputState   = example.Key;
                List <int[]> predicateInt = new List <int[]>();
                var          duplicate    = example.Key[rule.Body[0]] as List <IReadOnlyList <Node> >;
                List <int>   temp         = new List <int>();
                for (int i = 0; i < duplicate.Count; i++)
                {
                    if (duplicate[i].Count > 0)
                    {
                        temp.Add(i);
                    }
                }
                int[] tempArr  = new int[temp.Count];
                int   countInt = 0;
                foreach (int a in temp)
                {
                    tempArr[countInt] = a;
                    countInt++;
                }
                predicateInt.Add(tempArr);
                result[inputState] = predicateInt;
            }
            return(DisjunctiveExamplesSpec.From(result));
        }
Beispiel #9
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));
        }
Beispiel #10
0
        internal DisjunctiveExamplesSpec WitnessRegexPair(GrammarRule rule, 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 <object>();
                foreach (uint pos in spec.DisjunctiveExamples[input])
                {
                    UnboundedMultiValueCache <Token, TokenMatch> rightMatches;
                    if (!v.Cache.TryGetAllMatchesStartingAt(pos, out rightMatches))
                    {
                        continue;
                    }
                    UnboundedMultiValueCache <Token, TokenMatch> leftMatches;
                    if (!v.Cache.TryGetAllMatchesEndingAt(pos, out leftMatches))
                    {
                        continue;
                    }
                    var leftRegexes  = RegularExpression.LearnLeftMatches(v, pos, RegularExpression.DefaultTokenCount);
                    var rightRegexes = RegularExpression.LearnRightMatches(v, pos, RegularExpression.DefaultTokenCount);
                    var regexPairs   =
                        from l in leftRegexes from r in rightRegexes select(object) ValueTuple.Create(l, r);

                    regexes.AddRange(regexPairs);
                }
                rrExamples[input] = regexes;
            }
            return(DisjunctiveExamplesSpec.From(rrExamples));
        }
Beispiel #11
0
        internal DisjunctiveExamplesSpec WitnessLogicalSymbol(GrammarRule rule, ExampleSpec spec)
        {
            var ppExamples = new Dictionary <State, IEnumerable <object> >();

            foreach (State input in spec.ProvidedInputs)
            {
                var allPossibleSolutions = Semantics.LogicGen;
                ppExamples[input] = allPossibleSolutions;
            }
            return(DisjunctiveExamplesSpec.From(ppExamples));
        }
Beispiel #12
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));
        }
        internal DisjunctiveExamplesSpec WitnessSelectUpstreamPath(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.Upstream);
            }
            return(DisjunctiveExamplesSpec.From(result));
        }
Beispiel #14
0
        internal DisjunctiveExamplesSpec WitnessComparatorColumn(GrammarRule rule, DisjunctiveExamplesSpec spec)
        {
            /* Inverse for the column field */
            var ppExamples = new Dictionary <State, IEnumerable <object> >();

            foreach (State input in spec.ProvidedInputs)
            {
                DataTable[] tableList            = (DataTable[])input[rule.Grammar.InputSymbol]; // Single table hack
                var         dataColumnArrayArray = tableList.SelectMany(t => t.Columns.Cast <DataColumn>().ToArray()).ToArray();
                ppExamples[input] = dataColumnArrayArray;
            }

            return(DisjunctiveExamplesSpec.From(ppExamples));
        }
Beispiel #15
0
        internal DisjunctiveExamplesSpec WitnessSelectWithoutWhereTableNames(GrammarRule rule, ExampleSpec spec)
        {
            var ppExamples = new Dictionary <State, IEnumerable <object> >();

            foreach (State input in spec.ProvidedInputs)
            {
                var tableList            = (DataTable[])input[rule.Grammar.InputSymbol];
                var inputTable           = tableList[0];
                var allPossibleSolutions = new List <object>();
                allPossibleSolutions.Add(new[] { inputTable.TableName });
                ppExamples[input] = allPossibleSolutions;
            }

            return(DisjunctiveExamplesSpec.From(ppExamples));
        }
Beispiel #16
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));
        }
Beispiel #17
0
        internal DisjunctiveExamplesSpec WitnessK(GrammarRule rule, 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));
        }
Beispiel #18
0
        internal DisjunctiveExamplesSpec WitnessSelectWithoutWhere(GrammarRule rule, ExampleSpec spec)
        {
            var ppExamples = new Dictionary <State, IEnumerable <object> >();

            foreach (State input in spec.ProvidedInputs)
            {
                var tableList            = (DataTable[])input[rule.Body[1]];
                var inputTable           = tableList[0];
                var desiredOutput        = (DataTable)spec.Examples[input];
                var allPossibleSolutions = new List <object>();
                if (desiredOutput.Rows.Count == inputTable.Rows.Count)
                {
                    allPossibleSolutions.Add(desiredOutput.Columns.Cast <DataColumn>().ToArray());
                }
                ppExamples[input] = allPossibleSolutions;
            }

            return(DisjunctiveExamplesSpec.From(ppExamples));
        }
        internal DisjunctiveExamplesSpec WitnessApplyPattern(GrammarRule rule, DisjunctiveExamplesSpec spec)
        {
            var result = new Dictionary <State, IEnumerable <object> >();

            foreach (KeyValuePair <State, IEnumerable <object> > example in spec.DisjunctiveExamples)
            {
                State         inputState = example.Key;
                MergeConflict input      = (MergeConflict)inputState[Grammar.InputSymbol];
                List <object> ret        =
                    example.Value.OfType <IReadOnlyList <Node> >()
                    .Select(output => Semantics.Concat(input.Upstream, input.Downstream).Count != output.Count)
                    .Distinct()
                    .Cast <object>()
                    .ToList();
                result[inputState] = ret;
            }

            return(DisjunctiveExamplesSpec.From(result));
        }
Beispiel #20
0
        internal DisjunctiveExamplesSpec WitnessPositionPair(GrammarRule rule, ExampleSpec spec)
        {
            var ppExamples = new Dictionary <State, IEnumerable <object> >();

            foreach (State input in spec.ProvidedInputs)
            {
                var v             = (StringRegion)input[rule.Body[0]];
                var desiredOutput = (StringRegion)spec.Examples[input];
                var occurrences   = new List <object>();
                for (int i = v.Value.IndexOf(desiredOutput.Value, StringComparison.Ordinal);
                     i >= 0;
                     i = v.Value.IndexOf(desiredOutput.Value, i + 1, StringComparison.Ordinal))
                {
                    occurrences.Add(ValueTuple.Create(v.Start + (uint?)i, v.Start + (uint?)i + desiredOutput.Length));
                }
                ppExamples[input] = occurrences;
            }
            return(DisjunctiveExamplesSpec.From(ppExamples));
        }
Beispiel #21
0
        public DisjunctiveExamplesSpec WitnessRegexPair(GrammarRule rule, DisjunctiveExamplesSpec spec)
        {
            var result = new Dictionary <State, IEnumerable <object> >();

            foreach (var example in spec.DisjunctiveExamples)
            {
                State inputState = example.Key;
                var   input      = inputState[rule.Body[0]] as string;

                var regexes = new List <Tuple <Regex, Regex> >();
                foreach (int output in example.Value)
                {
                    //TODO, complete the witness function for the rr parameter.
                    //Given the position in the output variable, you need to generate
                    //all pairs of regular expressions that match this position.

                    //You can use the auxiliary function bellow to get the regular expressions
                    //that match each position in the input string
                    //Uncomment the code about to do so.
                    //List<Regex>[] leftMatches, rightMatches;
                    //BuildStringMatches(input, out leftMatches, out rightMatches);

                    //Get the list of regexes that match the position 'output' from the leftMatches and rightMatches
                    //by completing the next two lines.
                    //var leftRegex = ...
                    //var rightRegex = ...

                    //if leftRegex or rightRegex is empty, we could not find a spec for this parameter in this input state
                    //if (leftRegex.Count == 0 || rightRegex.Count == 0)
                    //    return null;

                    //generate the cross product of the left and right regexes and for each pair, add it to the regexes list.
                }

                if (regexes.Count == 0)
                {
                    return(null);
                }
                result[inputState] = regexes;
            }
            return(DisjunctiveExamplesSpec.From(result));
        }
        internal DisjunctiveExamplesSpec WitnessRemoveTree1(GrammarRule rule, DisjunctiveExamplesSpec spec)
        {
            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> >();
                var input = example.Key[Grammar.InputSymbol] as MergeConflict;
                foreach (IReadOnlyList <Node> _ in example.Value)
                {
                    possibleCombinations.Add(input.Upstream);
                    possibleCombinations.Add(input.Downstream);
                }

                result[inputState] = possibleCombinations;
            }

            return(DisjunctiveExamplesSpec.From(result));
        }
Beispiel #23
0
        internal DisjunctiveExamplesSpec WitnessInputTable2(GrammarRule rule, DisjunctiveExamplesSpec spec, ExampleSpec inputTable1Spec, ExampleSpec logicalOperatorSpec)
        {
            var ppExamples = new Dictionary <State, IEnumerable <object> >();


            foreach (State input in spec.ProvidedInputs)
            {
                DataTable outputTable                 = (DataTable)spec.DisjunctiveExamples[input].First();
                DataTable inputTable                  = ((DataTable[])input[rule.Grammar.InputSymbol])[0];
                DataTable inputTable1                 = (DataTable)inputTable1Spec.Examples[inputTable1Spec.ProvidedInputs.First()];
                var       allPossibleSolutions        = new List <DataTable>();
                var       allPossibleSolutionsCleaned = new List <DataTable>();
                string    logicalOperator             = (string)logicalOperatorSpec.Examples[logicalOperatorSpec.ProvidedInputs.First()];
                switch (logicalOperator)
                {
                case "AND":
                    allPossibleSolutions = GetAndInputTable2(inputTable, inputTable1, outputTable);
                    break;

                case "OR":
                    var transformedOutputTable = Utils.Utils.CreateOutputTableFromEnumerable(inputTable.AsEnumerable().Except(outputTable.AsEnumerable(), new DataTableCustomComparator()));
                    var transformInputTable1   = Utils.Utils.CreateOutputTableFromEnumerable(inputTable.AsEnumerable().Except(inputTable1.AsEnumerable(), new DataTableCustomComparator()));

                    allPossibleSolutions = GetAndInputTable2(inputTable, transformInputTable1, transformedOutputTable);
                    for (int i = 0; i < allPossibleSolutions.Count; i++)
                    {
                        allPossibleSolutions[i] = Utils.Utils.CreateOutputTableFromEnumerable(inputTable.AsEnumerable().Except(allPossibleSolutions[i].AsEnumerable(), new DataTableCustomComparator()));
                    }
                    break;
                }
                for (int i = 0; i < allPossibleSolutions.Count; i++)
                {
                    if (allPossibleSolutions[i] != null && allPossibleSolutions[i].Rows.Count != 0)
                    {
                        allPossibleSolutionsCleaned.Add(allPossibleSolutions[i]);
                    }
                }
                ppExamples[input] = allPossibleSolutionsCleaned;
            }
            return(DisjunctiveExamplesSpec.From(ppExamples));
        }
Beispiel #24
0
        DisjunctiveExamplesSpec WitnessSearchCompareFun(SearchCompareFun fun, GrammarRule rule, ExampleSpec spec)
        {
            var nExamples = new Dictionary <State, IEnumerable <object> >();

            foreach (State st in spec.ProvidedInputs)
            {
                var input   = (Tuple <string, string>)st[rule.Body[0]];
                var output  = (Order)spec.Examples[st];
                var needles = new List <string>();
                foreach (var needle in FindCharacters(input.Item1 + input.Item2))
                {
                    if (fun(input, needle) == output)
                    {
                        needles.Add(needle);
                        //Console.WriteLine("possible needle: " + needle);
                    }
                }
                nExamples[st] = needles;
            }
            return(DisjunctiveExamplesSpec.From(nExamples));
        }
Beispiel #25
0
        public static DisjunctiveExamplesSpec WitnessMoveK(GrammarRule rule, int parameter, ExampleSpec spec)
        {
            var contextExamples = new Dictionary <State, IEnumerable <object> >();

            foreach (State input in spec.ProvidedInputs)
            {
                var operation = spec.Examples[input] as Move;
                if (operation == null)
                {
                    return(null);
                }
                var positions = new List <int>();
                positions.Add(operation.Index);
                if (operation.Index == operation.TargetNode.Children.Count)
                {
                    positions.Add(-1);
                }
                contextExamples[input] = positions.Cast <object>();
            }
            return(DisjunctiveExamplesSpec.From(contextExamples));
        }
        internal DisjunctiveExamplesSpec WitnessConcatTree1(GrammarRule rule, DisjunctiveExamplesSpec spec)
        {
            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> >();
                int count = 0;
                foreach (IReadOnlyList <Node> output in example.Value)
                {
                    for (var i = 0; i < output.Count - 1; i++)
                    {
                        List <Node> temp = new List <Node>();
                        if (count == 0)
                        {
                            temp.Add(output[i]);
                        }
                        else
                        {
                            IReadOnlyList <Node> previous = possibleCombinations[count - 1];
                            foreach (Node prev in previous)
                            {
                                temp.Add(prev);
                            }

                            temp.Add(output[i]);
                        }

                        possibleCombinations.Add(temp);
                        count++;
                    }
                }

                result[inputState] = possibleCombinations;
            }

            return(DisjunctiveExamplesSpec.From(result));
        }
Beispiel #27
0
        public DisjunctiveExamplesSpec WitnessK(GrammarRule rule, DisjunctiveExamplesSpec spec)
        {
            var kExamples = new Dictionary <State, IEnumerable <object> >();

            foreach (KeyValuePair <State, IEnumerable <object> > example in spec.DisjunctiveExamples)
            {
                State inputState = example.Key;
                var   v          = inputState[rule.Body[0]] as string;

                var positions = new List <int>();
                foreach (int pos in example.Value)
                {
                    positions.Add(pos + 1);
                }
                if (positions.Count == 0)
                {
                    return(null);
                }
                kExamples[inputState] = positions.Cast <object>();
            }
            return(DisjunctiveExamplesSpec.From(kExamples));
        }
Beispiel #28
0
        DisjunctiveExamplesSpec WitnessFirstLetterOfWord(GrammarRule rule, ExampleSpec spec)
        {
            var nExamples = new Dictionary <State, IEnumerable <object> >();

            foreach (State st in spec.ProvidedInputs)
            {
                var input     = (Tuple <string, string>)st[rule.Body[0]];
                var output    = (Order)spec.Examples[st];
                var positions = new List <object>();
                var words1    = Regex.Split(input.Item1, @"\s+").Length;
                var words2    = Regex.Split(input.Item2, @"\s+").Length;
                foreach (var n in Enumerable.Range(0, Math.Max(words1, words2)))
                {
                    if (Semantics.Semantics.FirstLetterOfWord(input, n) == output)
                    {
                        positions.Add(n);
                    }
                }
                nExamples[st] = positions;
            }
            return(DisjunctiveExamplesSpec.From(nExamples));
        }
        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));
        }
        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));
        }