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);
}
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;
}
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 + ")";
}
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");
}
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);
}
}
Aggregations