Пример #1
0
        private void AddAnyOptions(IteratorExp node)
        {
            if (node.Iterator.Count == 1)
            {
                TranslationOption optionFor = new TranslationOption();
                optionFor.FormatString = string.Format("(for ${0} in {{0}} return if ({{1}}) then ${0} else ())[1]",
                                                       node.Iterator[0].Name);
                optionFor.ParenthesisWhenNotTopLevel = true;
                SubexpressionTranslations.AddTranslationOption(node, optionFor);

                bool canRewrite = true;
                SubexpressionCollector collector = new SubexpressionCollector();
                collector.Visit(node);
                List <LoopExp> loopSubExps = collector.Expressions.OfType <LoopExp>().ToList();
                foreach (LoopExp nestedLoopExp in loopSubExps)
                {
                    collector.Clear();
                    nestedLoopExp.Accept(collector);
                    if (collector.ReferredVariables.Contains(node.Iterator[0]))
                    {
                        canRewrite = false;
                        break;
                    }
                }

                if (canRewrite)
                {
                    TranslationOption optionFilter = new TranslationOption();
                    optionFilter.ContextVariableForSubExpressions = node.Iterator[0];
                    optionFilter.FormatString = string.Format("({{0}}[{{1}}])[1]");
                    optionFilter.ParenthesisWhenNotTopLevel = true;
                    SubexpressionTranslations.AddTranslationOption(node, optionFilter);
                }
            }
        }
Пример #2
0
        private void AddSelectRejectOptions(IteratorExp node, bool select)
        {
            // select and reject differ only in not() applied in filter
            if (node.Iterator.Count == 1)
            {
                TranslationOption optionFor = new TranslationOption();
                optionFor.ParenthesisWhenNotTopLevel = true;
                if (select)
                {
                    optionFor.FormatString = string.Format("for ${0} in {{0}} return if ({{1}}) then ${0} else ()",
                                                           node.Iterator[0].Name);
                }
                else
                {
                    optionFor.FormatString = string.Format(
                        "for ${0} in {{0}} return if (not({{1}})) then ${0} else ()", node.Iterator[0].Name);
                }
                SubexpressionTranslations.AddTranslationOption(node, optionFor);

                /*
                 * this option can be used only when there is no iterator in body, which references the current iterator variable,
                 * because there is no XPath variable corresponding to the iterator variable (context is used instead).
                 */
                {
                    bool canRewrite = true;
                    SubexpressionCollector collector = new SubexpressionCollector();
                    collector.Visit(node);
                    List <LoopExp> loopSubExps = collector.Expressions.OfType <LoopExp>().ToList();
                    foreach (LoopExp nestedLoopExp in loopSubExps)
                    {
                        collector.Clear();
                        nestedLoopExp.Accept(collector);
                        if (collector.ReferredVariables.Contains(node.Iterator[0]))
                        {
                            canRewrite = false;
                            break;
                        }
                    }

                    if (canRewrite)
                    {
                        TranslationOption optionFilter = new TranslationOption();
                        optionFilter.ContextVariableForSubExpressions = node.Iterator[0];
                        if (select)
                        {
                            optionFilter.FormatString = string.Format("{{0}}[{{1}}]");
                        }
                        else
                        {
                            optionFilter.FormatString = string.Format("{{0}}[not({{1}})]");
                        }
                        SubexpressionTranslations.AddTranslationOption(node, optionFilter);
                    }
                    else // translation with let
                    {
                        TranslationOption optionFilterLet = new TranslationOption();
                        optionFilterLet.ContextVariableForSubExpressions = node.Iterator[0];
                        if (select)
                        {
                            optionFilterLet.FormatString = string.Format("{{0}}[let ${0} := . return {{1}}]",
                                                                         node.Iterator[0].Name);
                        }
                        else
                        {
                            optionFilterLet.FormatString = string.Format("{{0}}[let ${0} := . return not({{1}})]",
                                                                         node.Iterator[0].Name);
                        }
                        SubexpressionTranslations.AddTranslationOption(node, optionFilterLet);
                    }
                }
            }
        }