Search in sources :

Example 1 with JavaMethodWithParamsSharedPureFunction

use of org.finos.legend.pure.runtime.java.compiled.metadata.JavaMethodWithParamsSharedPureFunction in project legend-pure by finos.

the class CompiledSupport method executeFunction.

public static Object executeFunction(String uniqueFunctionId, ConcreteFunctionDefinition<?> functionDefinition, Class<?>[] paramClasses, Object[] params, ExecutionSupport es) {
    SharedPureFunction<?> spf = ((CompiledExecutionSupport) es).getFunctionCache().getIfAbsentPutJavaFunctionForPureFunction(functionDefinition, () -> {
        try {
            Class<?> clazz = ((CompiledExecutionSupport) es).getClassLoader().loadClass(JavaPackageAndImportBuilder.rootPackage() + '.' + uniqueFunctionId);
            String functionName = FunctionProcessor.functionNameToJava(functionDefinition);
            Method method = getFunctionMethod(clazz, functionName, functionDefinition, paramClasses, params, es);
            return new JavaMethodWithParamsSharedPureFunction(method, paramClasses, functionDefinition.getSourceInformation());
        } catch (ClassNotFoundException e) {
            throw new PureExecutionException("Unable to execute " + uniqueFunctionId, e);
        }
    });
    return spf.execute(Lists.fixedSize.of(params).with(es), es);
}
Also used : JavaMethodWithParamsSharedPureFunction(org.finos.legend.pure.runtime.java.compiled.metadata.JavaMethodWithParamsSharedPureFunction) PureExecutionException(org.finos.legend.pure.m3.exception.PureExecutionException) Method(java.lang.reflect.Method) HttpMethod(org.finos.legend.pure.runtime.java.shared.http.HttpMethod)

Example 2 with JavaMethodWithParamsSharedPureFunction

use of org.finos.legend.pure.runtime.java.compiled.metadata.JavaMethodWithParamsSharedPureFunction in project legend-pure by finos.

the class Pure method findSharedPureFunction.

public static SharedPureFunction<?> findSharedPureFunction(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.Function<?> func, Bridge bridge, ExecutionSupport es) {
    if (func instanceof Property) {
        Type srcType = func._classifierGenericType()._typeArguments().getFirst()._rawType();
        return ((CompiledExecutionSupport) es).getFunctionCache().getIfAbsentPutFunctionForClassProperty(srcType, func, ((CompiledExecutionSupport) es).getClassLoader());
    }
    if (func instanceof Path) {
        return new PureFunction1<Object, Object>() {

            @Override
            public Object execute(ListIterable vars, ExecutionSupport es) {
                return value(vars.getFirst(), es);
            }

            @Override
            public Object value(Object o, ExecutionSupport es) {
                RichIterable<?> result = ((Path<?, ?>) func)._path().injectInto(CompiledSupport.toPureCollection(o), (mutableList, path) -> {
                    if (!(path instanceof PropertyPathElement)) {
                        throw new PureExecutionException("Only PropertyPathElement is supported yet!");
                    }
                    return mutableList.flatCollect(instance -> {
                        MutableList<Object> parameters = ((PropertyPathElement) path)._parameters().collect(o1 -> o1 instanceof InstanceValue ? ((InstanceValue) o1)._values() : null, Lists.mutable.with(instance));
                        return CompiledSupport.toPureCollection(evaluate(es, ((PropertyPathElement) path)._property(), bridge, parameters.toArray()));
                    });
                });
                Multiplicity mult = func._classifierGenericType()._multiplicityArguments().getFirst();
                return bridge.hasToOneUpperBound().apply(mult, es) ? result.getFirst() : result;
            }
        };
    }
    if (func instanceof LambdaCompiledExtended) {
        return ((LambdaCompiledExtended) func).pureFunction();
    }
    if (func instanceof LambdaFunction) {
        LambdaFunction<?> lambda = (LambdaFunction<?>) func;
        if (canFindNativeOrLambdaFunction(es, func)) {
            return getNativeOrLambdaFunction(es, func);
        }
        if (Reactivator.canReactivateWithoutJavaCompilation(lambda, es, getOpenVariables(func, bridge), bridge)) {
            PureMap openVariablesMap = getOpenVariables(func, bridge);
            return DynamicPureLambdaFunctionImpl.createPureLambdaFunction(lambda, openVariablesMap.getMap(), bridge);
        }
        return ((LambdaCompiledExtended) CompiledSupport.dynamicallyBuildLambdaFunction(func, es)).pureFunction();
    }
    if (func instanceof ConcreteFunctionDefinition) {
        return ((CompiledExecutionSupport) es).getFunctionCache().getIfAbsentPutJavaFunctionForPureFunction(func, () -> {
            try {
                RichIterable<? extends VariableExpression> params = ((FunctionType) func._classifierGenericType()._typeArguments().getFirst()._rawType())._parameters();
                Class<?>[] paramClasses = new Class[params.size() + 1];
                int index = 0;
                for (VariableExpression o : params) {
                    paramClasses[index] = pureTypeToJavaClassForExecution(o, bridge, es);
                    index++;
                }
                paramClasses[params.size()] = ExecutionSupport.class;
                Method method = ((CompiledExecutionSupport) es).getClassLoader().loadClass(JavaPackageAndImportBuilder.rootPackage() + "." + IdBuilder.sourceToId(func.getSourceInformation())).getMethod(FunctionProcessor.functionNameToJava(func), paramClasses);
                return new JavaMethodWithParamsSharedPureFunction(method, paramClasses, func.getSourceInformation());
            } catch (RuntimeException e) {
                throw e;
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        });
    }
    MutableMap<String, SharedPureFunction<?>> functions;
    try {
        Class<?> myClass = ((CompiledExecutionSupport) es).getClassLoader().loadClass(JavaPackageAndImportBuilder.rootPackage() + "." + IdBuilder.sourceToId(func.getSourceInformation()));
        functions = (MutableMap<String, SharedPureFunction<?>>) myClass.getDeclaredField("__functions").get(null);
    } catch (RuntimeException e) {
        throw e;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return functions.get(func.getName());
}
Also used : ConcreteFunctionDefinition(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.ConcreteFunctionDefinition) JavaMethodWithParamsSharedPureFunction(org.finos.legend.pure.runtime.java.compiled.metadata.JavaMethodWithParamsSharedPureFunction) SharedPureFunction(org.finos.legend.pure.runtime.java.compiled.generation.processors.support.function.SharedPureFunction) JavaMethodWithParamsSharedPureFunction(org.finos.legend.pure.runtime.java.compiled.metadata.JavaMethodWithParamsSharedPureFunction) InstanceValue(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.InstanceValue) Multiplicity(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.multiplicity.Multiplicity) CompiledExecutionSupport(org.finos.legend.pure.runtime.java.compiled.execution.CompiledExecutionSupport) ExecutionSupport(org.finos.legend.pure.m3.execution.ExecutionSupport) 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) PropertyPathElement(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.path.PropertyPathElement) Path(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.path.Path) PureFunction1(org.finos.legend.pure.runtime.java.compiled.generation.processors.support.function.PureFunction1) PureExecutionException(org.finos.legend.pure.m3.exception.PureExecutionException) FunctionType(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.FunctionType) 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) VariableExpression(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.VariableExpression) PureMap(org.finos.legend.pure.runtime.java.compiled.generation.processors.support.map.PureMap) Method(java.lang.reflect.Method) PureDynamicReactivateException(org.finos.legend.pure.runtime.java.compiled.compiler.PureDynamicReactivateException) TimeoutException(java.util.concurrent.TimeoutException) PureExecutionException(org.finos.legend.pure.m3.exception.PureExecutionException) ListIterable(org.eclipse.collections.api.list.ListIterable) PrimitiveType(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.PrimitiveType) GenericType(org.finos.legend.pure.m3.navigation.generictype.GenericType) FunctionType(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.FunctionType) HashType(org.finos.legend.pure.runtime.java.shared.hash.HashType) Type(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.Type) CompiledExecutionSupport(org.finos.legend.pure.runtime.java.compiled.execution.CompiledExecutionSupport) JSONObject(org.json.simple.JSONObject)

Example 3 with JavaMethodWithParamsSharedPureFunction

use of org.finos.legend.pure.runtime.java.compiled.metadata.JavaMethodWithParamsSharedPureFunction in project legend-pure by finos.

the class Pure method evaluate.

public static Object evaluate(ExecutionSupport es, org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.Function<?> func, Bridge bridge, Object... instances) {
    if (func instanceof Property) {
        return getSharedPureFunction(func, bridge, es).execute(FastList.newListWith(instances[0]), es);
    }
    if (func instanceof Path) {
        return getSharedPureFunction(func, bridge, es).execute(FastList.newListWith(instances[0]), es);
    }
    if (func instanceof LambdaCompiledExtended) {
        return getSharedPureFunction(func, bridge, es).execute(Lists.fixedSize.with(instances), es);
    }
    if (func instanceof ConcreteFunctionDefinition) {
        JavaMethodWithParamsSharedPureFunction p = (JavaMethodWithParamsSharedPureFunction) getSharedPureFunction(func, bridge, es);
        Class<?>[] paramClasses = p.getParametersTypes();
        int l = paramClasses.length;
        MutableList<Object> paramInstances = FastList.newList();
        for (int i = 0; i < (l - 1); i++) {
            if (instances[i] instanceof RichIterable && paramClasses[i] != RichIterable.class) {
                paramInstances.add(CompiledSupport.toOne(((RichIterable<?>) instances[i]), null));
            } else {
                paramInstances.add(instances[i]);
            }
        }
        paramInstances.add(es);
        return p.execute(paramInstances, es);
    }
    if (func instanceof LambdaFunction) {
        return getSharedPureFunction(func, bridge, es).execute(Lists.fixedSize.with(instances), es);
    }
    SharedPureFunction<?> reflectiveNative = getSharedPureFunction(func, bridge, es);
    return reflectiveNative.execute(instances == null ? Lists.mutable.empty() : FastList.newListWith(instances), es);
}
Also used : Path(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.path.Path) ConcreteFunctionDefinition(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.ConcreteFunctionDefinition) JavaMethodWithParamsSharedPureFunction(org.finos.legend.pure.runtime.java.compiled.metadata.JavaMethodWithParamsSharedPureFunction) 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) 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)

Aggregations

JavaMethodWithParamsSharedPureFunction (org.finos.legend.pure.runtime.java.compiled.metadata.JavaMethodWithParamsSharedPureFunction)3 Method (java.lang.reflect.Method)2 ConcreteFunctionDefinition (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.ConcreteFunctionDefinition)2 LambdaFunction (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.LambdaFunction)2 Property (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.property.Property)2 QualifiedProperty (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.property.QualifiedProperty)2 Path (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.path.Path)2 PureExecutionException (org.finos.legend.pure.m3.exception.PureExecutionException)2 PureLambdaFunction (org.finos.legend.pure.runtime.java.compiled.generation.processors.support.function.PureLambdaFunction)2 JSONObject (org.json.simple.JSONObject)2 TimeoutException (java.util.concurrent.TimeoutException)1 RichIterable (org.eclipse.collections.api.RichIterable)1 ListIterable (org.eclipse.collections.api.list.ListIterable)1 Multiplicity (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.multiplicity.Multiplicity)1 PropertyPathElement (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.path.PropertyPathElement)1 FunctionType (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.FunctionType)1 PrimitiveType (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.PrimitiveType)1 Type (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.Type)1 InstanceValue (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.InstanceValue)1 VariableExpression (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.VariableExpression)1