001    /*
002    // $Id: //open/mondrian/src/main/mondrian/olap/fun/StdevPFunDef.java#5 $
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-2008 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.olap.Exp;
016    import mondrian.calc.Calc;
017    import mondrian.calc.ExpCompiler;
018    import mondrian.calc.ListCalc;
019    import mondrian.calc.impl.ValueCalc;
020    import mondrian.calc.impl.AbstractDoubleCalc;
021    import mondrian.mdx.ResolvedFunCall;
022    
023    import java.util.List;
024    
025    /**
026     * Definition of the <code>StdevP</code> builtin MDX function, and its alias
027     * <code>StddevP</code>.
028     *
029     * @author jhyde
030     * @version $Id: //open/mondrian/src/main/mondrian/olap/fun/StdevPFunDef.java#5 $
031     * @since Mar 23, 2006
032     */
033    class StdevPFunDef extends AbstractAggregateFunDef {
034    
035        static final Resolver StdevpResolver = new ReflectiveMultiResolver(
036                "StdevP",
037                "StdevP(<Set>[, <Numeric Expression>])",
038                "Returns the standard deviation of a numeric expression evaluated over a set (biased).",
039                new String[]{"fnx", "fnxn"},
040                StdevPFunDef.class);
041    
042        static final Resolver StddevpResolver = new ReflectiveMultiResolver(
043                "StddevP",
044                "StddevP(<Set>[, <Numeric Expression>])",
045                "Alias for StdevP.",
046                new String[]{"fnx", "fnxn"},
047                StdevPFunDef.class);
048    
049        public StdevPFunDef(FunDef dummyFunDef) {
050            super(dummyFunDef);
051        }
052    
053        public Calc compileCall(ResolvedFunCall call, ExpCompiler compiler) {
054            final ListCalc listCalc =
055                    compiler.compileList(call.getArg(0));
056            final Calc calc = call.getArgCount() > 1 ?
057                    compiler.compileScalar(call.getArg(1), true) :
058                    new ValueCalc(call);
059            return new AbstractDoubleCalc(call, new Calc[] {listCalc, calc}) {
060                public double evaluateDouble(Evaluator evaluator) {
061                    List memberList = evaluateCurrentList(listCalc, evaluator);
062                    return (Double)stdev(evaluator.push(), memberList, calc, true);
063                }
064    
065                public boolean dependsOn(Dimension dimension) {
066                    return anyDependsButFirst(getCalcs(), dimension);
067                }
068            };
069        }
070    }
071    
072    // End StdevPFunDef.java