Search in sources :

Example 1 with RoutedValueSpecification

use of org.finos.legend.pure.m3.coreinstance.meta.pure.router.RoutedValueSpecification in project legend-pure by finos.

the class Reactivator method reactivateWithoutJavaCompilationImpl.

private static Object reactivateWithoutJavaCompilationImpl(final ValueSpecification valueSpecification, final PureMap lambdaOpenVariablesMap, final ExecutionSupport es, final boolean atRoot, Bridge bridge) {
    if (valueSpecification instanceof RoutedValueSpecification) {
        return reactivateWithoutJavaCompilationImpl(((RoutedValueSpecification) valueSpecification)._value(), lambdaOpenVariablesMap, es, atRoot, bridge);
    } else if (valueSpecification instanceof InstanceValue) {
        InstanceValue iv = (InstanceValue) valueSpecification;
        RichIterable<?> result = iv._values().flatCollect(new org.eclipse.collections.api.block.function.Function<Object, RichIterable<Object>>() {

            @Override
            public RichIterable<Object> valueOf(Object value) {
                Object result;
                if (value instanceof ValueSpecification) {
                    result = reactivateWithoutJavaCompilationImpl((ValueSpecification) value, lambdaOpenVariablesMap, es, false, bridge);
                } else if ((!lambdaOpenVariablesMap.getMap().isEmpty()) && value instanceof KeyExpression) {
                    KeyExpression ke = (KeyExpression) value;
                    CompiledProcessorSupport processorSupport = ((CompiledExecutionSupport) es).getProcessorSupport();
                    Object key = reactivateWithoutJavaCompilationImpl(ke._key(), lambdaOpenVariablesMap, es, false, bridge);
                    InstanceValue rKey = (InstanceValue) ((CompiledExecutionSupport) es).getProcessorSupport().newCoreInstance("key", M3Paths.InstanceValue, null);
                    rKey._values(key instanceof RichIterable ? (RichIterable) key : Lists.immutable.with(key));
                    Object expression = reactivateWithoutJavaCompilationImpl(ke._expression(), lambdaOpenVariablesMap, es, false, bridge);
                    InstanceValue rExpression = (InstanceValue) ((CompiledExecutionSupport) es).getProcessorSupport().newCoreInstance("key", M3Paths.InstanceValue, null);
                    rExpression._values(expression instanceof RichIterable ? (RichIterable) expression : Lists.immutable.with(expression));
                    KeyExpression rKe = ke.copy();
                    rKe._key(rKey);
                    rKe._expression(rExpression);
                    result = rKe;
                } else if (value instanceof LambdaFunction) {
                    LambdaFunction lambdaFunction = (LambdaFunction) value;
                    UnifiedMap openVariables = UnifiedMap.newMap();
                    for (Object entry : lambdaOpenVariablesMap.getMap().keyValuesView()) {
                        Pair<String, org.finos.legend.pure.m3.coreinstance.meta.pure.functions.collection.List> pair = (Pair<String, org.finos.legend.pure.m3.coreinstance.meta.pure.functions.collection.List>) entry;
                        openVariables.put(pair.getOne(), pair.getTwo()._values());
                    }
                    for (Object entry : Pure.getOpenVariables(lambdaFunction, bridge).getMap().keyValuesView()) {
                        Pair<String, org.finos.legend.pure.m3.coreinstance.meta.pure.functions.collection.List> pair = (Pair<String, org.finos.legend.pure.m3.coreinstance.meta.pure.functions.collection.List>) entry;
                        openVariables.put(pair.getOne(), pair.getTwo()._values());
                    }
                    result = bridge.lambdaBuilder().value().lambdaFunction(lambdaFunction).pureFunction(DynamicPureLambdaFunctionImpl.createPureLambdaFunction(lambdaFunction, openVariables, bridge));
                } else {
                    result = value;
                }
                return CompiledSupport.toPureCollection(result);
            }
        });
        return (result != null) && (result.size() == 1) ? result.getFirst() : result;
    } else if (valueSpecification instanceof SimpleFunctionExpression) {
        SimpleFunctionExpression sfe = (SimpleFunctionExpression) valueSpecification;
        Function func = (Function) sfe._func();
        if (!canReactivateWithoutJavaCompilationImpl(func, es, atRoot, lambdaOpenVariablesMap, bridge)) {
            throw new PureDynamicReactivateException(valueSpecification.getSourceInformation(), "Can not reactivate function, unexpected:" + func._name());
        }
        RichIterable<?> paramValues = sfe._parametersValues().collect(new org.eclipse.collections.api.block.function.Function<Object, Object>() {

            @Override
            public Object valueOf(Object value) {
                if (value instanceof ValueSpecification) {
                    Object newValue = reactivateWithoutJavaCompilationImpl((ValueSpecification) value, lambdaOpenVariablesMap, es, false, bridge);
                    if (newValue instanceof RichIterable) {
                        return newValue;
                    } else {
                        return Lists.fixedSize.of(newValue);
                    }
                } else {
                    return value;
                }
            }
        });
        MutableList<Object> vars = Lists.mutable.withAll(paramValues);
        if (sfe._func()._name().equals("new_Class_1__String_1__KeyExpression_MANY__T_1_")) {
            // Have to get the first param from the generic type
            vars.set(0, Lists.fixedSize.of(sfe._genericType()._rawType()));
        } else if (sfe._func()._name().equals("cast_Any_m__T_1__T_m_")) {
            // Have to get the second param from the generic type
            vars.set(1, Lists.fixedSize.of(sfe._genericType()));
        }
        return Pure._evaluateToMany(es, bridge, func, vars);
    } else if (valueSpecification instanceof VariableExpression) {
        String varName = ((VariableExpression) valueSpecification)._name();
        if (!lambdaOpenVariablesMap.getMap().containsKey(varName)) {
            throw new PureDynamicReactivateException("Attempt to use out of scope variable: " + varName);
        }
        Object result = lambdaOpenVariablesMap.getMap().get(varName);
        return ((org.finos.legend.pure.m3.coreinstance.meta.pure.functions.collection.List<Object>) result)._values();
    } else {
        throw new PureDynamicReactivateException(valueSpecification.getSourceInformation(), "Unexpected type to dynamically reactivate: " + valueSpecification.getClass().getName());
    }
}
Also used : UnifiedMap(org.eclipse.collections.impl.map.mutable.UnifiedMap) RoutedValueSpecification(org.finos.legend.pure.m3.coreinstance.meta.pure.router.RoutedValueSpecification) ValueSpecification(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.ValueSpecification) InstanceValue(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.InstanceValue) CompiledProcessorSupport(org.finos.legend.pure.runtime.java.compiled.execution.CompiledProcessorSupport) NativeFunction(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.NativeFunction) Function(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.Function) LambdaFunction(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.LambdaFunction) SimpleFunctionExpression(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.SimpleFunctionExpression) MutableList(org.eclipse.collections.api.list.MutableList) List(java.util.List) RoutedValueSpecification(org.finos.legend.pure.m3.coreinstance.meta.pure.router.RoutedValueSpecification) Pair(org.eclipse.collections.api.tuple.Pair) KeyExpression(org.finos.legend.pure.m3.coreinstance.meta.pure.functions.lang.KeyExpression) LambdaFunction(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.LambdaFunction) VariableExpression(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.VariableExpression) PureDynamicReactivateException(org.finos.legend.pure.runtime.java.compiled.compiler.PureDynamicReactivateException) RichIterable(org.eclipse.collections.api.RichIterable)

Aggregations

List (java.util.List)1 RichIterable (org.eclipse.collections.api.RichIterable)1 MutableList (org.eclipse.collections.api.list.MutableList)1 Pair (org.eclipse.collections.api.tuple.Pair)1 UnifiedMap (org.eclipse.collections.impl.map.mutable.UnifiedMap)1 KeyExpression (org.finos.legend.pure.m3.coreinstance.meta.pure.functions.lang.KeyExpression)1 Function (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.Function)1 LambdaFunction (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.LambdaFunction)1 NativeFunction (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.NativeFunction)1 InstanceValue (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.InstanceValue)1 SimpleFunctionExpression (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.SimpleFunctionExpression)1 ValueSpecification (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.ValueSpecification)1 VariableExpression (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.VariableExpression)1 RoutedValueSpecification (org.finos.legend.pure.m3.coreinstance.meta.pure.router.RoutedValueSpecification)1 PureDynamicReactivateException (org.finos.legend.pure.runtime.java.compiled.compiler.PureDynamicReactivateException)1 CompiledProcessorSupport (org.finos.legend.pure.runtime.java.compiled.execution.CompiledProcessorSupport)1