Esempio n. 1
0
        protected override object OnVariableRule(ASTNode node)
        {
            var name       = VisitChild <Name>(node, 0);
            var expression = VisitChild <IExpression>(node, 1);

            return(NonterminalRule.From(name.Text, false, expression));
        }
Esempio n. 2
0
        protected override object OnRule(INonterminalToken token)
        {
            if (token == null)
            {
                throw new ArgumentNullException(nameof(token));
            }

            return(NonterminalRule.From(token.Terminal(0).Value, false, Visit <IExpression>(token[1])));
        }
Esempio n. 3
0
 protected override object OnRule(INonterminalToken token)
 {
     return(NonterminalRule.From(token.Terminal(0).Value, false, Visit <IExpression>(token[1])));
 }
        internal Optional <ProgramSet> LearnDupLet(SynthesisEngine engine, LetRule rule,
                                                   LearningTask <DisjunctiveExamplesSpec> task,
                                                   CancellationToken cancel)
        {
            var             examples = task.Spec;
            List <string[]> pathsArr = new List <string[]>();

            foreach (KeyValuePair <State, IEnumerable <object> > example in examples.DisjunctiveExamples)
            {
                State         inputState = example.Key;
                var           input      = example.Key[Grammar.InputSymbol] as MergeConflict;
                List <string> idx        = new List <string>();
                foreach (IReadOnlyList <Node> output in example.Value)
                {
                    foreach (Node n in Semantics.Concat(input.Upstream, input.Downstream))
                    {
                        bool flag = false;
                        n.Attributes.TryGetValue(Path, out string inPath);
                        foreach (Node node in output)
                        {
                            node.Attributes.TryGetValue(Path, out string outputPath);
                            if (inPath == outputPath)
                            {
                                flag = true;
                            }
                        }
                        if (!flag)
                        {
                            idx.Add(inPath);
                        }
                    }
                }

                pathsArr.Add(idx.ToArray());
            }

            pathsArr.Add(new string[1] {
                string.Empty
            });
            List <ProgramSet> programSetList = new List <ProgramSet>();

            foreach (string[] path in pathsArr)
            {
                NonterminalRule findMatchRule = Grammar.Rule(nameof(Semantics.FindMatch)) as NonterminalRule;
                ProgramSet      letValueSet   =
                    ProgramSet.List(
                        Grammar.Symbol("find"),
                        new NonterminalNode(
                            findMatchRule,
                            new VariableNode(Grammar.InputSymbol),
                            new LiteralNode(Grammar.Symbol("paths"), path)));

                var bodySpec = new Dictionary <State, IEnumerable <object> >();
                foreach (KeyValuePair <State, IEnumerable <object> > kvp in task.Spec.DisjunctiveExamples)
                {
                    State         input = kvp.Key;
                    MergeConflict x     = (MergeConflict)input[Grammar.InputSymbol];
                    List <IReadOnlyList <Node> > dupValue = Semantics.FindMatch(x, path);

                    State newState = input.Bind(rule.Variable, dupValue);
                    bodySpec[newState] = kvp.Value;
                }

                LearningTask bodyTask       = task.Clone(rule.LetBody, new DisjunctiveExamplesSpec(bodySpec));
                ProgramSet   bodyProgramSet = engine.Learn(bodyTask, cancel);

                var dupLetProgramSet = ProgramSet.Join(rule, letValueSet, bodyProgramSet);
                programSetList.Add(dupLetProgramSet);
            }

            ProgramSet ps = new UnionProgramSet(rule.Head, programSetList.ToArray());

            return(ps.Some());
        }