Search in sources :

Example 1 with Calc

use of mondrian.calc.Calc in project mondrian by pentaho.

the class CaseMatchFunDef method compileCall.

public Calc compileCall(ResolvedFunCall call, ExpCompiler compiler) {
    final Exp[] args = call.getArgs();
    final List<Calc> calcList = new ArrayList<Calc>();
    final Calc valueCalc = compiler.compileScalar(args[0], true);
    calcList.add(valueCalc);
    final int matchCount = (args.length - 1) / 2;
    final Calc[] matchCalcs = new Calc[matchCount];
    final Calc[] exprCalcs = new Calc[matchCount];
    for (int i = 0, j = 1; i < exprCalcs.length; i++) {
        matchCalcs[i] = compiler.compileScalar(args[j++], true);
        calcList.add(matchCalcs[i]);
        exprCalcs[i] = compiler.compile(args[j++]);
        calcList.add(exprCalcs[i]);
    }
    final Calc defaultCalc = args.length % 2 == 0 ? compiler.compile(args[args.length - 1]) : ConstantCalc.constantNull(call.getType());
    calcList.add(defaultCalc);
    final Calc[] calcs = calcList.toArray(new Calc[calcList.size()]);
    return new GenericCalc(call) {

        public Object evaluate(Evaluator evaluator) {
            Object value = valueCalc.evaluate(evaluator);
            for (int i = 0; i < matchCalcs.length; i++) {
                Object match = matchCalcs[i].evaluate(evaluator);
                if (match.equals(value)) {
                    return exprCalcs[i].evaluate(evaluator);
                }
            }
            return defaultCalc.evaluate(evaluator);
        }

        public Calc[] getCalcs() {
            return calcs;
        }
    };
}
Also used : GenericCalc(mondrian.calc.impl.GenericCalc) ArrayList(java.util.ArrayList) ConstantCalc(mondrian.calc.impl.ConstantCalc) Calc(mondrian.calc.Calc) GenericCalc(mondrian.calc.impl.GenericCalc)

Example 2 with Calc

use of mondrian.calc.Calc in project mondrian by pentaho.

the class CoalesceEmptyFunDef method compileCall.

public Calc compileCall(ResolvedFunCall call, ExpCompiler compiler) {
    final Exp[] args = call.getArgs();
    final Calc[] calcs = new Calc[args.length];
    for (int i = 0; i < args.length; i++) {
        calcs[i] = compiler.compileScalar(args[i], true);
    }
    return new GenericCalc(call) {

        public Object evaluate(Evaluator evaluator) {
            for (Calc calc : calcs) {
                final Object o = calc.evaluate(evaluator);
                if (o != null) {
                    return o;
                }
            }
            return null;
        }

        public Calc[] getCalcs() {
            return calcs;
        }
    };
}
Also used : GenericCalc(mondrian.calc.impl.GenericCalc) Calc(mondrian.calc.Calc) GenericCalc(mondrian.calc.impl.GenericCalc)

Example 3 with Calc

use of mondrian.calc.Calc in project mondrian by pentaho.

the class RolapEvaluator method evaluateCurrent.

public final Object evaluateCurrent() {
    // Get the member in the current context which is (a) calculated, and
    // (b) has the highest solve order. If there are no calculated members,
    // go ahead and compute the cell.
    RolapCalculation maxSolveMember;
    switch(calculationCount) {
        case 0:
            final Object o = cellReader.get(this);
            if (o == Util.nullValue) {
                return null;
            }
            return o;
        case 1:
            maxSolveMember = calculations[0];
            break;
        default:
            switch(root.solveOrderMode) {
                case ABSOLUTE:
                    maxSolveMember = getAbsoluteMaxSolveOrder();
                    break;
                case SCOPED:
                    maxSolveMember = getScopedMaxSolveOrder();
                    break;
                default:
                    throw Util.unexpected(root.solveOrderMode);
            }
    }
    final int savepoint = savepoint();
    maxSolveMember.setContextIn(this);
    final Calc calc = maxSolveMember.getCompiledExpression(root);
    final Object o;
    try {
        o = calc.evaluate(this);
    } finally {
        restore(savepoint);
    }
    if (o == Util.nullValue) {
        return null;
    }
    return o;
}
Also used : Calc(mondrian.calc.Calc)

Example 4 with Calc

use of mondrian.calc.Calc in project mondrian by pentaho.

the class ScenarioImpl method forMember.

/**
 * Returns the scenario inside a calculated member in the scenario
 * dimension. For example, applied to [Scenario].[1], returns the Scenario
 * object representing scenario #1.
 *
 * @param member Wrapper member
 * @return Wrapped scenario
 */
static Scenario forMember(final RolapMember member) {
    if (isScenario(member.getHierarchy())) {
        final Formula formula = ((RolapCalculatedMember) member).getFormula();
        final ResolvedFunCall resolvedFunCall = (ResolvedFunCall) formula.getExpression();
        final Calc calc = resolvedFunCall.getFunDef().compileCall(null, null);
        return ((ScenarioCalc) calc).getScenario();
    } else {
        return null;
    }
}
Also used : ResolvedFunCall(mondrian.mdx.ResolvedFunCall) Calc(mondrian.calc.Calc) GenericCalc(mondrian.calc.impl.GenericCalc)

Example 5 with Calc

use of mondrian.calc.Calc in project mondrian by pentaho.

the class IifFunDefTest method testGetResultType.

public void testGetResultType() {
    ResultStyle actualResStyle = null;
    ResultStyle expectedResStyle = setListCalc.getResultStyle();
    // Compile calculation for IIf function for (<Logical Expression>, <SetType>, <SetType>) params
    Calc calc = IifFunDef.SET_INSTANCE.compileCall(call, compilerMock);
    try {
        actualResStyle = calc.getResultStyle();
    } catch (Exception e) {
        fail("Should not have thrown any exception.");
    }
    assertNotNull(actualResStyle);
    assertEquals(expectedResStyle, actualResStyle);
}
Also used : ResultStyle(mondrian.calc.ResultStyle) SetListCalc(mondrian.olap.fun.SetFunDef.SetListCalc) Calc(mondrian.calc.Calc)

Aggregations

Calc (mondrian.calc.Calc)8 GenericCalc (mondrian.calc.impl.GenericCalc)4 ArrayList (java.util.ArrayList)2 ResolvedFunCall (mondrian.mdx.ResolvedFunCall)2 ResultStyle (mondrian.calc.ResultStyle)1 TupleList (mondrian.calc.TupleList)1 ArrayTupleList (mondrian.calc.impl.ArrayTupleList)1 ConstantCalc (mondrian.calc.impl.ConstantCalc)1 UnaryTupleList (mondrian.calc.impl.UnaryTupleList)1 Exp (mondrian.olap.Exp)1 FunDef (mondrian.olap.FunDef)1 Member (mondrian.olap.Member)1 SetListCalc (mondrian.olap.fun.SetFunDef.SetListCalc)1 SetType (mondrian.olap.type.SetType)1 Type (mondrian.olap.type.Type)1