Search in sources :

Example 1 with NativeFunction

use of org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.NativeFunction in project legend-pure by finos.

the class AntlrContextToM3CoreInstance method nativeFunction.

private void nativeFunction(NativeFunctionContext ctx, ImportGroup importId, String space, MutableList<CoreInstance> coreInstancesResult) {
    this.functionCounter++;
    NativeFunctionInstance function = NativeFunctionInstance.createPersistent(this.repository, ctx.qualifiedName().identifier().getText() + this.functionCounter, this.sourceInformation.getPureSourceInformation(ctx.NATIVE().getSymbol(), ctx.qualifiedName().identifier().getStart(), ctx.END_LINE().getSymbol()));
    MutableList<String> typeParametersNames = Lists.mutable.empty();
    MutableList<String> multiplicityParametersNames = Lists.mutable.empty();
    if (ctx.typeAndMultiplicityParameters() != null) {
        this.typeParametersAndMultiplicityParameters(ctx.typeAndMultiplicityParameters(), typeParametersNames, multiplicityParametersNames);
    }
    FunctionType signature = functionTypeSignature(ctx.functionTypeSignature(), function, typeParametersNames, multiplicityParametersNames, importId, spacePlusTabs(space, 1));
    function._functionName(ctx.qualifiedName().identifier().getText());
    PackageInstance packageInstance = this.buildPackage(ctx.qualifiedName().packagePath());
    function._package(packageInstance);
    packageInstance._childrenAdd(function);
    GenericTypeInstance genericTypeInstance = GenericTypeInstance.createPersistent(this.repository);
    Type type = (Type) this.processorSupport.package_getByUserPath(M3Paths.NativeFunction);
    genericTypeInstance._rawTypeCoreInstance(type);
    GenericTypeInstance genericTypeInstanceTa = GenericTypeInstance.createPersistent(this.repository);
    genericTypeInstanceTa._rawTypeCoreInstance(signature);
    genericTypeInstance._typeArguments(Lists.mutable.<GenericType>of(genericTypeInstanceTa));
    function._classifierGenericType(genericTypeInstance);
    coreInstancesResult.add(function);
}
Also used : PackageInstance(org.finos.legend.pure.m3.coreinstance.PackageInstance) GenericTypeInstance(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.generics.GenericTypeInstance) FunctionType(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.FunctionType) 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) FunctionType(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.FunctionType) NativeFunctionInstance(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.NativeFunctionInstance)

Example 2 with NativeFunction

use of org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.NativeFunction in project legend-pure by finos.

the class DynamicPureLambdaFunctionImpl method execute.

@Override
public T execute(ListIterable vars, ExecutionSupport es) {
    Objects.requireNonNull(vars, "vars");
    Objects.requireNonNull(es, "es");
    PureMap runningOpenVariablesMap = new PureMap(UnifiedMap.newMap());
    for (Pair entry : openVariables.keyValuesView()) {
        runningOpenVariablesMap.getMap().put(entry.getOne(), createList()._valuesAddAll(CompiledSupport.toPureCollection(entry.getTwo())));
    }
    FunctionType ft = (FunctionType) func._classifierGenericType()._typeArguments().getFirst()._rawType();
    ImmutableList<? extends VariableExpression> parameters = Lists.immutable.withAll(ft._parameters());
    for (int i = 0; i < parameters.size(); i++) {
        runningOpenVariablesMap.getMap().put(parameters.get(i)._name(), createList()._valuesAddAll(CompiledSupport.toPureCollection(vars.get(i))));
    }
    Object finalResult = null;
    for (Object expressionSequenceItem : func._expressionSequence()) {
        ValueSpecification vs = (ValueSpecification) expressionSequenceItem;
        Object result = Reactivator.reactivateWithoutJavaCompilation(bridge, vs, runningOpenVariablesMap, es);
        if (vs instanceof SimpleFunctionExpression) {
            SimpleFunctionExpression sfe = (SimpleFunctionExpression) vs;
            if (sfe._func() instanceof NativeFunction && sfe._func()._name().equals("letFunction_String_1__T_m__T_m_")) {
                String varName = (String) ((InstanceValue) sfe._parametersValues().getFirst())._values().getFirst();
                runningOpenVariablesMap.getMap().put(varName, createList()._valuesAddAll(CompiledSupport.toPureCollection(result)));
            }
        }
        finalResult = result;
    }
    return (T) finalResult;
}
Also used : SimpleFunctionExpression(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.SimpleFunctionExpression) FunctionType(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.FunctionType) NativeFunction(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.NativeFunction) ValueSpecification(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.ValueSpecification) PureMap(org.finos.legend.pure.runtime.java.compiled.generation.processors.support.map.PureMap) InstanceValue(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.InstanceValue) Pair(org.eclipse.collections.api.tuple.Pair)

Example 3 with NativeFunction

use of org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.NativeFunction in project legend-pure by finos.

the class AbstractNativeFunctionGeneric method build.

@Override
public String build(CoreInstance topLevelElement, CoreInstance functionExpression, ListIterable<String> transformedParams, ProcessorContext processorContext) {
    SourceInformation sourceInformation = functionExpression.getSourceInformation();
    String cast = "";
    if (this.castReturnValue) {
        ProcessorSupport processorSupport = processorContext.getSupport();
        CoreInstance nativeFunction = Instance.getValueForMetaPropertyToOneResolved(functionExpression, M3Properties.func, processorSupport);
        CoreInstance functionType = processorSupport.function_getFunctionType(nativeFunction);
        String returnType = TypeProcessor.typeToJavaPrimitiveSingle(Instance.getValueForMetaPropertyToOneResolved(functionType, M3Properties.returnType, processorSupport), processorSupport);
        cast = "(" + returnType + ")";
    }
    String sourceInformationStr = this.hasSrcInformation ? buildSourceInformationParameterValues(sourceInformation, transformedParams.size()) : "";
    String es = this.hasExecutionSupport ? buildEs(transformedParams.size()) : "";
    return cast + this.methodName + "(" + StringUtils.join(transformedParams, ", ") + sourceInformationStr + es + ")";
}
Also used : ProcessorSupport(org.finos.legend.pure.m3.navigation.ProcessorSupport) CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance) SourceInformation(org.finos.legend.pure.m4.coreinstance.SourceInformation)

Example 4 with NativeFunction

use of org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.NativeFunction in project legend-pure by finos.

the class NativeFunctionProcessor method processNativeFunction.

public String processNativeFunction(CoreInstance topLevelElement, CoreInstance functionExpression, ProcessorContext processorContext) {
    ProcessorSupport processorSupport = processorContext.getSupport();
    CoreInstance nativeFunction = Instance.getValueForMetaPropertyToOneResolved(functionExpression, M3Properties.func, processorSupport);
    ListIterable<? extends CoreInstance> parametersValues = Instance.getValueForMetaPropertyToManyResolved(functionExpression, M3Properties.parametersValues, processorSupport);
    Native nat = this.natives.get(nativeFunction.getName());
    if (nat != null) {
        ListIterable<String> transformedParams = nat.transformParameterValues(parametersValues, topLevelElement, processorSupport, processorContext);
        return nat.build(topLevelElement, functionExpression, transformedParams, processorContext);
    }
    throw new RuntimeException(nativeFunction.getName() + " Not supported yet");
}
Also used : Native(org.finos.legend.pure.runtime.java.compiled.generation.processors.natives.Native) ProcessorSupport(org.finos.legend.pure.m3.navigation.ProcessorSupport) CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance)

Example 5 with NativeFunction

use of org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.NativeFunction in project legend-pure by finos.

the class Pure method _evaluateToMany.

public static Object _evaluateToMany(ExecutionSupport es, Bridge bridge, org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.Function<?> func, ListIterable<?> paramInputs) {
    if (func instanceof LambdaCompiledExtended) {
        return ((LambdaCompiledExtended) func).pureFunction().execute(paramInputs, es);
    }
    if (func instanceof Property) {
        try {
            Object o = ((RichIterable<?>) paramInputs.getFirst()).getFirst();
            return o.getClass().getMethod("_" + func.getName()).invoke(o);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    RichIterable<? extends VariableExpression> params = ((FunctionType) func._classifierGenericType()._typeArguments().getFirst()._rawType())._parameters();
    Class<?>[] paramClasses = new Class<?>[params.size()];
    int index = 0;
    for (VariableExpression o : params) {
        paramClasses[index] = pureTypeToJavaClassForExecution(o, bridge, es);
        index++;
    }
    Object[] paramInstances = new Object[params.size()];
    Iterator<?> iterator = paramInputs.iterator();
    for (int i = 0; i < params.size(); i++) {
        paramInstances[i] = (paramClasses[i] == RichIterable.class) ? iterator.next() : ((RichIterable<?>) iterator.next()).getFirst();
    }
    try {
        if (func instanceof QualifiedProperty) {
            Object o = ((RichIterable<?>) paramInputs.getFirst()).getFirst();
            return CompiledSupport.executeMethod(o.getClass(), func._functionName(), func, Arrays.copyOfRange(paramClasses, 1, paramClasses.length), o, Arrays.copyOfRange(paramInstances, 1, paramInstances.length), es);
        }
        if (func instanceof ConcreteFunctionDefinition) {
            return CompiledSupport.executeFunction(func, paramClasses, paramInstances, es);
        }
        if (func instanceof NativeFunction || func instanceof LambdaFunction) {
            SharedPureFunction<?> foundFunc = getNativeOrLambdaFunction(es, func);
            if (foundFunc == null) {
                StringBuilder builder = new StringBuilder("Can't execute ").append(func).append(" | name: ");
                String name = func._name();
                if (name == null) {
                    builder.append("null");
                } else {
                    builder.append("'").append(name).append("'");
                }
                builder.append(" id: '").append(func.getName()).append("' yet");
                throw new PureExecutionException(builder.toString());
            }
            return foundFunc.execute(Lists.mutable.with(paramInstances), es);
        }
        throw new PureExecutionException("Unknown function type:" + func.getClass().getName());
    } catch (RuntimeException e) {
        throw e;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : ConcreteFunctionDefinition(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.ConcreteFunctionDefinition) FunctionType(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.FunctionType) NativeFunction(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.NativeFunction) PureExecutionException(org.finos.legend.pure.m3.exception.PureExecutionException) VariableExpression(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.VariableExpression) PureLambdaFunction(org.finos.legend.pure.runtime.java.compiled.generation.processors.support.function.PureLambdaFunction) LambdaFunction(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.LambdaFunction) PureDynamicReactivateException(org.finos.legend.pure.runtime.java.compiled.compiler.PureDynamicReactivateException) TimeoutException(java.util.concurrent.TimeoutException) PureExecutionException(org.finos.legend.pure.m3.exception.PureExecutionException) RichIterable(org.eclipse.collections.api.RichIterable) JSONObject(org.json.simple.JSONObject) Property(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.property.Property) QualifiedProperty(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.property.QualifiedProperty) QualifiedProperty(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.property.QualifiedProperty)

Aggregations

CoreInstance (org.finos.legend.pure.m4.coreinstance.CoreInstance)4 FunctionType (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.FunctionType)3 RichIterable (org.eclipse.collections.api.RichIterable)2 NativeFunction (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.NativeFunction)2 PureExecutionException (org.finos.legend.pure.m3.exception.PureExecutionException)2 ProcessorSupport (org.finos.legend.pure.m3.navigation.ProcessorSupport)2 IOException (java.io.IOException)1 TimeoutException (java.util.concurrent.TimeoutException)1 ListIterable (org.eclipse.collections.api.list.ListIterable)1 MutableList (org.eclipse.collections.api.list.MutableList)1 MutableMap (org.eclipse.collections.api.map.MutableMap)1 Pair (org.eclipse.collections.api.tuple.Pair)1 CheckedFunction2 (org.eclipse.collections.impl.block.function.checked.CheckedFunction2)1 PackageInstance (org.finos.legend.pure.m3.coreinstance.PackageInstance)1 org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function)1 ConcreteFunctionDefinition (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.ConcreteFunctionDefinition)1 FunctionDefinition (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.FunctionDefinition)1 LambdaFunction (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.LambdaFunction)1 NativeFunctionInstance (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.NativeFunctionInstance)1 Property (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.property.Property)1