001    /*
002     * Cumulus4j - Securing your data in the cloud - http://cumulus4j.org
003     * Copyright (C) 2011 NightLabs Consulting GmbH
004     *
005     * This program is free software: you can redistribute it and/or modify
006     * it under the terms of the GNU Affero General Public License as
007     * published by the Free Software Foundation, either version 3 of the
008     * License, or (at your option) any later version.
009     *
010     * This program is distributed in the hope that it will be useful,
011     * but WITHOUT ANY WARRANTY; without even the implied warranty of
012     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013     * GNU Affero General Public License for more details.
014     *
015     * You should have received a copy of the GNU Affero General Public License
016     * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017     */
018    package org.cumulus4j.store.query.eval;
019    
020    import java.util.Set;
021    
022    import org.cumulus4j.store.query.QueryEvaluator;
023    import org.datanucleus.query.expression.DyadicExpression;
024    
025    /**
026     * <p>
027     * Evaluator handling "!" (negation).
028     * </p>
029     * <p>
030     * It is quite expensive to evaluate a negation (JDOQL "!") by first querying the normal (non-negated)
031     * result and then negating it by querying ALL candidates and finally filtering the normal result
032     * out. Therefore, we instead push the negation down the expression-evaluator-tree into the leafs.
033     * Thus <code>NotExpressionEvaluator</code> simply calls {@link ResultDescriptor#negate()} and passes the negated
034     * <code>ResultDescriptor</code> down the evaluator-tree. All nodes in the tree therefore have to take this
035     * negation-flag into account.
036     * </p>
037     * <p>
038     * Example 1: Instead of querying the expensive expression "!( a > 5 &amp;&amp; b <= 12 )", the cheaper
039     * equivalent "a <= 5 || b > 12" is used.
040     * </p>
041     * <p>
042     * Example 2: Instead of "!( !( a > 5 &amp;&amp; b <= 12 ) || c > 3 )" the equivalent "( a <= 5 || b > 12 ) &amp;&amp; c <= 3"
043     * is executed.
044     * </p>
045     * <p>
046     * See <a target="_blank" href="http://en.wikipedia.org/wiki/De_Morgan%27s_laws">De Morgan's laws</a> in wikipedia for details.
047     * </p>
048     *
049     * @author Marco หงุ่ยตระกูล-Schulze - marco at nightlabs dot de
050     */
051    public class NotExpressionEvaluator extends AbstractExpressionEvaluator<DyadicExpression>
052    {
053            public NotExpressionEvaluator(QueryEvaluator queryEvaluator, AbstractExpressionEvaluator<?> parent, DyadicExpression expression)
054            {
055                    super(queryEvaluator, parent, expression);
056            }
057    
058            @Override
059            protected Set<Long> _queryResultDataEntryIDs(ResultDescriptor resultDescriptor)
060            {
061                    if (getLeft() != null && getRight() != null)
062                            throw new UnsupportedOperationException("Both left and right are assigned - one of them must be null!");
063    
064                    if (getLeft() != null)
065                            return getLeft().queryResultDataEntryIDs(resultDescriptor.negate());
066    
067                    if (getRight() != null)
068                            return getRight().queryResultDataEntryIDs(resultDescriptor.negate());
069    
070                    throw new UnsupportedOperationException("Both left and right are null - one of them must be assigned!");
071            }
072    }