<commit_before>/*
 *  eXist Open Source Native XML Database
 *  Copyright (C) 2001-06 <NAME>
 *  <EMAIL>
 *  http://exist.sourceforge.net
 *  
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or (at your option) any later version.
 *  
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *  
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *  
 *  $Id$
 */
package org.exist.xquery;

import org.exist.dom.DocumentSet;
import org.exist.xquery.util.ExpressionDumper;
import org.exist.xquery.value.Item;
import org.exist.xquery.value.Sequence;
import org.exist.xquery.value.Type;

/**
 * XQuery if ... then ... else expression.
 * 
 * @author wolf
 */
public class ConditionalExpression extends AbstractExpression implements RewritableExpression {

    private Expression testExpr;
    private Expression thenExpr;
    private Expression elseExpr;

    public ConditionalExpression(XQueryContext context, Expression testExpr,
            Expression thenExpr, Expression elseExpr) {
        super(context);
        this.testExpr = testExpr.simplify();
        this.thenExpr = thenExpr.simplify();
        this.elseExpr = elseExpr.simplify();
    }

    /* (non-Javadoc)
     * @see org.exist.xquery.AbstractExpression#getDependencies()
     */
    public int getDependencies() {
        return Dependency.CONTEXT_SET | Dependency.CONTEXT_ITEM;
    }

    public Expression getTestExpr() {
        return testExpr;
    }

    public Expression getThenExpr() {
        return thenExpr;
    }

    public Expression getElseExpr() {
        return elseExpr;
    }

    /* (non-Javadoc)
     * @see org.exist.xquery.AbstractExpression#getCardinality()
     */
    public int getCardinality() {
        return thenExpr.getCardinality() | elseExpr.getCardinality();
    }

    /* (non-Javadoc)
     * @see org.exist.xquery.Expression#analyze(org.exist.xquery.Expression)
     */
    public void analyze(AnalyzeContextInfo contextInfo) throws XPathException {
        contextInfo.setFlags(contextInfo.getFlags() & (~IN_PREDICATE));
        contextInfo.setParent(this);
        testExpr.analyze(contextInfo);
        thenExpr.analyze(contextInfo);
        elseExpr.analyze(contextInfo);
    }

    /* (non-Javadoc)
     * @see org.exist.xquery.Expression#eval(org.exist.dom.DocumentSet, 
     * org.exist.xquery.value.Sequence, org.exist.xquery.value.Item)
     */
    public Sequence eval(Sequence contextSequence, Item contextItem) throws XPathException {
        context.expressionStart(this);
        final Sequence testSeq = testExpr.eval(contextSequence, contextItem);
        try {
            if (testSeq.effectiveBooleanValue()) {
                return thenExpr.eval(contextSequence, contextItem);
            } else {
                return elseExpr.eval(contextSequence, contextItem);
            }
        } catch (final XPathException e) {
            if (e.getLine() == 0)
                {e.setLocation(line, column);}
            throw e;
        } finally {
            context.expressionEnd(this);
        }
    }

    /* (non-Javadoc)
     * @see org.exist.xquery.Expression#preselect(org.exist.dom.DocumentSet)
     */
    public DocumentSet preselect(DocumentSet in_docs) throws XPathException {
        return in_docs;<commit_msg>[bugfix] Optimizer failed to properly rewrite if-then-else and showed warning. Fixed ConditionalExpression.analyze.<commit_after>/*
 *  eXist Open Source Native XML Database
 *  Copyright (C) 2001-06 <NAME>
 *  <EMAIL>-db.org
 *  http://exist.sourceforge.net
 *  
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or (at your option) any later version.
 *  
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *  
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *  
 *  $Id$
 */
package org.exist.xquery;

import org.exist.dom.DocumentSet;
import org.exist.xquery.util.ExpressionDumper;
import org.exist.xquery.value.Item;
import org.exist.xquery.value.Sequence;
import org.exist.xquery.value.Type;

/**
 * XQuery if ... then ... else expression.
 * 
 * @author wolf
 */
public class ConditionalExpression extends AbstractExpression implements RewritableExpression {

    private Expression testExpr;
    private Expression thenExpr;
    private Expression elseExpr;

    public ConditionalExpression(XQueryContext context, Expression testExpr,
            Expression thenExpr, Expression elseExpr) {
        super(context);
        this.testExpr = testExpr.simplify();
        this.thenExpr = thenExpr.simplify();
        this.elseExpr = elseExpr.simplify();
    }

    /* (non-Javadoc)
     * @see org.exist.xquery.AbstractExpression#getDependencies()
     */
    public int getDependencies() {
        return Dependency.CONTEXT_SET | Dependency.CONTEXT_ITEM;
    }

    public Expression getTestExpr() {
        return testExpr;
    }

    public Expression getThenExpr() {
        return thenExpr;
    }

    public Expression getElseExpr() {
        return elseExpr;
    }

    /* (non-Javadoc)
     * @see org.exist.xquery.AbstractExpression#getCardinality()
     */
    public int getCardinality() {
        return thenExpr.getCardinality() | elseExpr.getCardinality();
    }

    /* (non-Javadoc)
     * @see org.exist.xquery.Expression#analyze(org.exist.xquery.Expression)
     */
    public void analyze(AnalyzeContextInfo contextInfo) throws XPathException {
        AnalyzeContextInfo myContextInfo = new AnalyzeContextInfo(contextInfo);
        myContextInfo.setFlags(myContextInfo.getFlags() & (~IN_PREDICATE));
        myContextInfo.setParent(this);
        testExpr.analyze(myContextInfo);
        // parent may have been modified by testExpr: set it again
        myContextInfo.setParent(this);
        thenExpr.analyze(myContextInfo);
        myContextInfo.setParent(this);
        elseExpr.analyze(myContextInfo);
    }

    /* (non-Javadoc)
     * @see org.exist.xquery.Expression#eval(org.exist.dom.DocumentSet, 
     * org.exist.xquery.value.Sequence, org.exist.xquery.value.Item)
     */
    public Sequence eval(Sequence contextSequence, Item contextItem) throws XPathException {
        context.expressionStart(this);
        final Sequence testSeq = testExpr.eval(contextSequence, contextItem);
        try {
            if (testSeq.effectiveBooleanValue()) {
                return thenExpr.eval(contextSequence, contextItem);
            } else {
                return elseExpr.eval(contextSequence, contextItem);
            }
        } catch (final XPathException e) {
            if (e.getLine() == 0)
                {e.setLocation(line, column);}
            throw e;
        } finally {
            context.expressionEnd(this);
        }
    }

    /* (non-Javadoc)
     * @see org.exist.xquery.Expression#preselect(org.exist.dom.DocumentSet)
     */
    public DocumentSet preselect(DocumentSet in_docs) throws XPathException {
        return in_docs;
{'fraction_non_alphanumeric': 0.09468354430379747, 'fraction_numerical': 0.004050632911392405, 'mean_word_length': 2.8410306271268837, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '363596', 'n_tokens_mistral': 2274, 'n_tokens_neox': 2182, 'n_words': 586}