Search in sources :

Example 1 with KeyExpression

use of org.finos.legend.pure.m3.coreinstance.meta.pure.functions.lang.KeyExpression in project legend-pure by finos.

the class FunctionExpressionProcessor method addTraceForKeyExpressions.

private void addTraceForKeyExpressions(FunctionExpression functionExpression, ProcessorSupport processorSupport) {
    ListIterable<? extends ValueSpecification> params = functionExpression._parametersValues().toList();
    if (2 < params.size()) {
        int z = 0;
        for (CoreInstance keyValue : ImportStub.withImportStubByPasses(((InstanceValue) params.get(2))._valuesCoreInstance().toList(), processorSupport)) {
            if (keyValue instanceof KeyExpression) {
                KeyValueValueSpecificationContext usageContext = (KeyValueValueSpecificationContext) processorSupport.newAnonymousCoreInstance(null, M3Paths.KeyValueValueSpecificationContext);
                usageContext._offset(z);
                usageContext._functionExpression(functionExpression);
                ValueSpecification expression = ((KeyExpression) keyValue)._expression();
                if (null != expression._usageContext()) {
                    expression._usageContextRemove();
                }
                expression._usageContext(usageContext);
            }
            z++;
        }
    }
}
Also used : KeyValueValueSpecificationContext(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.KeyValueValueSpecificationContext) CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance) KeyExpression(org.finos.legend.pure.m3.coreinstance.meta.pure.functions.lang.KeyExpression) ValueSpecification(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.ValueSpecification) InstanceValue(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.InstanceValue)

Example 2 with KeyExpression

use of org.finos.legend.pure.m3.coreinstance.meta.pure.functions.lang.KeyExpression in project legend-pure by finos.

the class InstanceValueUnbind method run.

@Override
public void run(InstanceValue instanceValue, MatcherState state, Matcher matcher, ModelRepository modelRepository, Context context) throws PureCompilationException {
    ProcessorSupport processorSupport = state.getProcessorSupport();
    for (CoreInstance value : instanceValue._valuesCoreInstance()) {
        if (!(value instanceof Property)) {
            ((UnbindState) state).freeProcessedAndValidated(value);
        }
        Shared.cleanUpReferenceUsage(value, instanceValue, processorSupport);
        // TODO replace call to Instance.instanceOf once we resolve issue with Path not being available in this module - circular dependency
        if (value instanceof LambdaFunction || value instanceof SimpleFunctionExpression || Instance.instanceOf(value, M3Paths.Path, processorSupport) || value instanceof RootRouteNode) {
            matcher.fullMatch(value, state);
        } else if (Instance.instanceOf(value, M3Paths.RootGraphFetchTree, processorSupport)) {
            matcher.fullMatch(value, state);
        } else if (value instanceof KeyExpression) {
            ((UnbindState) state).freeProcessedAndValidated(modelRepository.newBooleanCoreInstance(((KeyExpression) value)._add()));
            ((UnbindState) state).freeProcessedAndValidated(((KeyExpression) value)._key());
            matcher.fullMatch(((KeyExpression) value)._expression(), state);
        } else {
            Shared.cleanImportStub(value, processorSupport);
        }
        if (value instanceof ValueSpecification) {
            matcher.fullMatch(value, state);
            ((ValueSpecification) value)._usageContextRemove();
        }
    }
}
Also used : ProcessorSupport(org.finos.legend.pure.m3.navigation.ProcessorSupport) RootRouteNode(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.treepath.RootRouteNode) SimpleFunctionExpression(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.SimpleFunctionExpression) CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance) KeyExpression(org.finos.legend.pure.m3.coreinstance.meta.pure.functions.lang.KeyExpression) ValueSpecification(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.ValueSpecification) LambdaFunction(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.LambdaFunction) Property(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.property.Property)

Example 3 with KeyExpression

use of org.finos.legend.pure.m3.coreinstance.meta.pure.functions.lang.KeyExpression in project legend-pure by finos.

the class LambdaFunctionProcessor method processValue.

private static void processValue(CoreInstance valueSpecification, Consumer<String> declaredVarConsumer, Consumer<String> otherVarConsumer, ProcessorSupport processorSupport) {
    if (valueSpecification instanceof FunctionExpression) {
        FunctionExpression functionExpression = (FunctionExpression) valueSpecification;
        if ("letFunction".equals(functionExpression._functionName())) {
            InstanceValue firstParamValue = (InstanceValue) ListHelper.wrapListIterable(functionExpression._parametersValues()).get(0);
            String letVarName = PrimitiveUtilities.getStringValue(ListHelper.wrapListIterable(firstParamValue._valuesCoreInstance()).get(0));
            declaredVarConsumer.accept(letVarName);
        }
        functionExpression._parametersValues().forEach(p -> processValue(p, declaredVarConsumer, otherVarConsumer, processorSupport));
    } else if (valueSpecification instanceof InstanceValue) {
        ((InstanceValue) valueSpecification)._valuesCoreInstance().forEach(v -> processValue(v, declaredVarConsumer, otherVarConsumer, processorSupport));
    } else if (valueSpecification instanceof KeyExpression) {
        processValue(((KeyExpression) valueSpecification)._expression(), declaredVarConsumer, otherVarConsumer, processorSupport);
    } else if (valueSpecification instanceof LambdaFunction) {
        processLambda((LambdaFunction<?>) valueSpecification, processorSupport).forEach(otherVarConsumer);
    } else if (valueSpecification instanceof VariableExpression) {
        otherVarConsumer.accept(((VariableExpression) valueSpecification)._name());
    }
}
Also used : VariableExpression(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.VariableExpression) ModelRepository(org.finos.legend.pure.m4.ModelRepository) TypeInference(org.finos.legend.pure.m3.compiler.postprocessing.inference.TypeInference) ListHelper(org.finos.legend.pure.m3.tools.ListHelper) CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance) MutableList(org.eclipse.collections.api.list.MutableList) Matcher(org.finos.legend.pure.m3.tools.matcher.Matcher) ProcessorState(org.finos.legend.pure.m3.compiler.postprocessing.ProcessorState) M3Paths(org.finos.legend.pure.m3.navigation.M3Paths) ProcessorSupport(org.finos.legend.pure.m3.navigation.ProcessorSupport) Context(org.finos.legend.pure.m3.compiler.Context) LambdaFunction(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.LambdaFunction) Consumer(java.util.function.Consumer) MutableSet(org.eclipse.collections.api.set.MutableSet) InstanceValue(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.InstanceValue) FunctionType(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.FunctionType) RichIterable(org.eclipse.collections.api.RichIterable) FunctionExpression(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.FunctionExpression) PrimitiveUtilities(org.finos.legend.pure.m3.navigation.PrimitiveUtilities) KeyExpression(org.finos.legend.pure.m3.coreinstance.meta.pure.functions.lang.KeyExpression) Sets(org.eclipse.collections.api.factory.Sets) FunctionExpression(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.FunctionExpression) 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) InstanceValue(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.InstanceValue)

Example 4 with KeyExpression

use of org.finos.legend.pure.m3.coreinstance.meta.pure.functions.lang.KeyExpression 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)

Example 5 with KeyExpression

use of org.finos.legend.pure.m3.coreinstance.meta.pure.functions.lang.KeyExpression in project legend-pure by finos.

the class CopyValidator method validateProperties.

private static void validateProperties(Matcher matcher, ValidatorState state, FunctionExpression expression, GenericType genericType, ProcessorSupport processorSupport) {
    Type classifier = (Type) ImportStub.withImportStubByPass(genericType._rawTypeCoreInstance(), processorSupport);
    ListIterable<? extends ValueSpecification> parametersValues = expression._parametersValues().toList();
    if (parametersValues.size() > 2) {
        if (parametersValues.get(2) instanceof InstanceValue) {
            for (CoreInstance keyValue : ((InstanceValue) parametersValues.get(2))._valuesCoreInstance()) {
                if (keyValue instanceof KeyExpression) {
                    // Validate key is an actual property
                    GenericType previousGenericType = genericType;
                    GenericType currentGenericType = null;
                    Type currentClassifier = classifier;
                    AbstractProperty property = null;
                    RichIterable<? extends CoreInstance> keys = ((KeyExpression) keyValue)._key()._valuesCoreInstance();
                    for (CoreInstance key : keys) {
                        property = (AbstractProperty) processorSupport.class_findPropertyUsingGeneralization(currentClassifier, key.getName());
                        if (property == null) {
                            throw new PureCompilationException(((KeyExpression) keyValue)._key().getSourceInformation(), "The property '" + key.getName() + "' can't be found in the type '" + classifier.getName() + "' or in its hierarchy.");
                        }
                        if (currentGenericType != null) {
                            previousGenericType = currentGenericType;
                        }
                        currentGenericType = property._genericType();
                        currentClassifier = (Type) ImportStub.withImportStubByPass(currentGenericType._rawTypeCoreInstance(), processorSupport);
                    }
                    NewValidator.validatePropertyValue(matcher, state, expression, (KeyExpression) keyValue, previousGenericType, property, processorSupport);
                }
            }
        }
    }
}
Also used : Type(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.Type) GenericType(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.generics.GenericType) GenericType(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.generics.GenericType) CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance) KeyExpression(org.finos.legend.pure.m3.coreinstance.meta.pure.functions.lang.KeyExpression) AbstractProperty(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.property.AbstractProperty) InstanceValue(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.InstanceValue) PureCompilationException(org.finos.legend.pure.m4.exception.PureCompilationException)

Aggregations

KeyExpression (org.finos.legend.pure.m3.coreinstance.meta.pure.functions.lang.KeyExpression)6 CoreInstance (org.finos.legend.pure.m4.coreinstance.CoreInstance)6 InstanceValue (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.InstanceValue)5 ValueSpecification (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.ValueSpecification)5 RichIterable (org.eclipse.collections.api.RichIterable)4 GenericType (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.generics.GenericType)4 MutableList (org.eclipse.collections.api.list.MutableList)3 LambdaFunction (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.LambdaFunction)3 PureCompilationException (org.finos.legend.pure.m4.exception.PureCompilationException)3 List (java.util.List)2 Pair (org.eclipse.collections.api.tuple.Pair)2 UnifiedMap (org.eclipse.collections.impl.map.mutable.UnifiedMap)2 Type (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.Type)2 SimpleFunctionExpression (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.SimpleFunctionExpression)2 VariableExpression (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.VariableExpression)2 ProcessorSupport (org.finos.legend.pure.m3.navigation.ProcessorSupport)2 Method (java.lang.reflect.Method)1 Arrays (java.util.Arrays)1 Map (java.util.Map)1 Objects (java.util.Objects)1