/*
 * Copyright 2009-2010 by The Regents of the University of California
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * you may obtain a copy of the License from
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.vxquery.compiler.rewriter.rules;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang3.mutable.Mutable;

import edu.uci.ics.hyracks.algebricks.core.algebra.base.ILogicalExpression;
import edu.uci.ics.hyracks.algebricks.core.algebra.base.ILogicalOperator;
import edu.uci.ics.hyracks.algebricks.core.algebra.base.ILogicalPlan;
import edu.uci.ics.hyracks.algebricks.core.algebra.base.LogicalExpressionTag;
import edu.uci.ics.hyracks.algebricks.core.algebra.base.LogicalOperatorTag;
import edu.uci.ics.hyracks.algebricks.core.algebra.functions.FunctionIdentifier;
import edu.uci.ics.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator;
import edu.uci.ics.hyracks.algebricks.core.algebra.operators.logical.SubplanOperator;
import edu.uci.ics.hyracks.algebricks.rewriter.rules.InlineVariablesRule;

/**
 * The inline reference variable rule searches for assignments that only create
 * a new variable ID and then update all reference for that variable to be the
 * original variable. This rule is immediately followed by a rule to remove
 * unused assign variables.
 * 
 * <pre>
 * Before
 * 
 *  plan__parent
 *  ASSIGN( $v : reference_variable($vr) )
 *  plan__child
 *  
 *  Where $vr is a reference variable defined in plan__child.
 *  
 * After
 * 
 *  plan__parent_new
 *  ASSIGN( $v : reference_variable($vr) )
 *  plan__child
 *  
 *  such that plan__parent_new = {for each $v in plan__parent is replaced with $vr}
 * </pre>
 */
public class InlineReferenceVariablePolicy implements InlineVariablesRule.IInlineVariablePolicy {

    @Override
    public boolean addExpressionToInlineMap(ILogicalExpression expr, Set<FunctionIdentifier> doNotInlineFuncs) {
        if (expr.getExpressionTag() == LogicalExpressionTag.VARIABLE) {
            return true;
        }
        return false;
    }

    @Override
    public List<Mutable<ILogicalOperator>> descendIntoNextOperator(AbstractLogicalOperator op) {
        List<Mutable<ILogicalOperator>> descendOp = new ArrayList<Mutable<ILogicalOperator>>();
        // Descend into nested plans removing projects on the way.
        if (op.getOperatorTag() == LogicalOperatorTag.SUBPLAN) {
            SubplanOperator subplan = (SubplanOperator) op;
            for (ILogicalPlan nestedOpRef : subplan.getNestedPlans()) {
                for (Mutable<ILogicalOperator> rootOpRef : nestedOpRef.getRoots()) {
                    descendOp.add(rootOpRef);
                }
            }
        }
        // Descend into children removing projects on the way.
        for (Mutable<ILogicalOperator> inputOpRef : op.getInputs()) {
            descendOp.add(inputOpRef);
        }
        return descendOp;
    }

    @Override
    public boolean transformOperator(AbstractLogicalOperator op) {
        return true;
    }

}

@@ -736,30 +736,8 @@
 ist;
-%0Aimport java.util.Set;
 %0A%0Aim
@@ -1162,89 +1162,8 @@
 ag;%0A
-import edu.uci.ics.hyracks.algebricks.core.algebra.functions.FunctionIdentifier;%0A
 impo
@@ -2187,32 +2187,30 @@
 ean 
-addExpressionTo
+isCandidateFor
 Inlin
-eMap
+ing
 (ILo
@@ -2233,50 +2233,8 @@
 expr
-, Set%3CFunctionIdentifier%3E doNotInlineFuncs
 ) %7B%0A
@@ -3265,25 +3265,30 @@
 ean 
-transformOperator
+isCanidateInlineTarget
 (Abs

Update the interface based on reviewer's feedback in Hyracks.