001    /*
002    // $Id: //open/mondrian/src/main/mondrian/olap/fun/CorrelationFunDef.java#3 $
003    // This software is subject to the terms of the Common Public License
004    // Agreement, available at the following URL:
005    // http://www.opensource.org/licenses/cpl.html.
006    // Copyright (C) 2006-2007 Julian Hyde
007    // All Rights Reserved.
008    // You must accept the terms of that agreement to use this software.
009    */
010    package mondrian.olap.fun;
011    
012    import mondrian.olap.FunDef;
013    import mondrian.olap.Evaluator;
014    import mondrian.olap.Dimension;
015    import mondrian.calc.Calc;
016    import mondrian.calc.ExpCompiler;
017    import mondrian.calc.ListCalc;
018    import mondrian.calc.impl.ValueCalc;
019    import mondrian.calc.impl.AbstractDoubleCalc;
020    import mondrian.mdx.ResolvedFunCall;
021    
022    import java.util.List;
023    
024    /**
025     * Definition of the <code>Correlation</code> MDX function.
026     *
027     * @author jhyde
028     * @version $Id: //open/mondrian/src/main/mondrian/olap/fun/CorrelationFunDef.java#3 $
029     * @since Mar 23, 2006
030     */
031    class CorrelationFunDef extends AbstractAggregateFunDef {
032        static final ReflectiveMultiResolver Resolver = new ReflectiveMultiResolver(
033                "Correlation",
034                "Correlation(<Set>, <Numeric Expression>[, <Numeric Expression>])",
035                "Returns the correlation of two series evaluated over a set.",
036                new String[]{"fnxn","fnxnn"},
037                CorrelationFunDef.class);
038    
039        public CorrelationFunDef(FunDef dummyFunDef) {
040            super(dummyFunDef);
041        }
042    
043        public Calc compileCall(ResolvedFunCall call, ExpCompiler compiler) {
044            final ListCalc listCalc =
045                    compiler.compileList(call.getArg(0));
046            final Calc calc1 =
047                    compiler.compileScalar(call.getArg(1), true);
048            final Calc calc2 = call.getArgCount() > 2 ?
049                    compiler.compileScalar(call.getArg(2), true) :
050                    new ValueCalc(call);
051            return new AbstractDoubleCalc(call, new Calc[] {listCalc, calc1, calc2}) {
052                public double evaluateDouble(Evaluator evaluator) {
053                    List memberList = evaluateCurrentList(listCalc, evaluator);
054                    return correlation(evaluator.push(),
055                            memberList, calc1, calc2);
056                }
057    
058                public boolean dependsOn(Dimension dimension) {
059                    return anyDependsButFirst(getCalcs(), dimension);
060                }
061            };
062        }
063    }
064    
065    // End CorrelationFunDef.java