default List<ResolvedFunction> resolveFunctions(String spec) { List<ResolvedFunction> resolvedFunctions = new ArrayList<>(); VirtDataDSL.ParseResult parseResult = VirtDataDSL.parse(spec); if (parseResult.throwable!=null) { throw new RuntimeException(parseResult.throwable); } List<Expression> expressions = parseResult.flow.getExpressions(); if (expressions.size() > 1) { throw new RuntimeException("Unable to promote a lambda flow to a data mapper here."); } FunctionCall call = expressions.get(0).getCall(); List<ResolvedFunction> found = resolveFunctions( Optional.ofNullable(call.getOutputType()).map(ValueType::valueOfClassName).map(ValueType::getValueClass).orElse(null), Optional.ofNullable(call.getInputType()).map(ValueType::valueOfClassName).map(ValueType::getValueClass).orElse(null), call.getFunctionName(), call.getArguments()); resolvedFunctions.addAll(found); return resolvedFunctions; }
public static LongUnaryOperator forSpec(String spec) { LambdasDSL.ParseResult parseResult = LambdasDSL.parse(spec); if (parseResult.throwable!=null) { throw new RuntimeException(parseResult.throwable); } MetagenFlow flow = parseResult.flow; if (flow.getExpressions().size()>1) { throw new RuntimeException("Unable to parse flows in " + IntegerDistributions.class); } FunctionCall call = flow.getLastExpression().getCall(); Class<?> inType = Optional.ofNullable(call.getInputType()).map(ValueType::valueOfClassName).map(ValueType::getValueClass).orElse(null); Class<?> outType = Optional.ofNullable(call.getOutputType()).map(ValueType::valueOfClassName).map(ValueType::getValueClass).orElse(null); inType = (inType==null ? long.class : inType); outType = (outType==null ? long.class : outType); List<ResolvedFunction> resolvedFunctions = new IntegerDistributions().resolveFunctions( outType, inType, call.getFunctionName(), call.getArguments() ); if (resolvedFunctions.size()>1) { throw new RuntimeException("Found " + resolvedFunctions.size() + " implementations, be more specific with" + "input or output qualifiers as in int -> or -> long"); } return (LongUnaryOperator) resolvedFunctions.get(0).getFunctionObject(); }
public static LongToDoubleFunction forSpec(String spec) { LambdasDSL.ParseResult parseResult = LambdasDSL.parse(spec); if (parseResult.throwable!=null) { throw new RuntimeException(parseResult.throwable); } MetagenFlow flow = parseResult.flow; if (flow.getExpressions().size()>1) { throw new RuntimeException("Unable to parse flows in " + IntegerDistributions.class); } FunctionCall call = flow.getLastExpression().getCall(); Class<?> inType = Optional.ofNullable(call.getInputType()).map(ValueType::valueOfClassName).map(ValueType::getValueClass).orElse(null); Class<?> outType = Optional.ofNullable(call.getOutputType()).map(ValueType::valueOfClassName).map(ValueType::getValueClass).orElse(null); if (inType!=null && inType!=long.class) { throw new RuntimeException("This only supports long for input."); } if (outType!=null && outType!=double.class) { throw new RuntimeException("This only supports double fo routput."); } inType = (inType==null ? long.class : inType); outType = (outType==null ? int.class : outType); List<ResolvedFunction> resolvedFunctions = new RealDistributions().resolveFunctions( outType, inType, call.getFunctionName(), call.getArguments() ); if (resolvedFunctions.size()>1) { throw new RuntimeException("Found " + resolvedFunctions.size() + " implementations, be more specific with" + "input or output qualifiers as in int -> or -> long"); } return ((LongToDoubleFunction) resolvedFunctions.get(0).getFunctionObject()); }
Class<?> inputType = ValueType.classOfType(call.getInputType()); Class<?> outputType = ValueType.classOfType(call.getOutputType()); Object[] args = call.getArguments(); args = populateFunctions(args);
private Object[] populateFunctions(Object[] args) { for (int i = 0; i < args.length; i++) { Object o = args[i]; if (o instanceof FunctionCall) { FunctionCall call = (FunctionCall) o; String funcName = call.getFunctionName(); Class<?> inputType = ValueType.classOfType(call.getInputType()); Class<?> outputType = ValueType.classOfType(call.getOutputType()); Object[] fargs = call.getArguments(); fargs = populateFunctions(fargs); List<ResolvedFunction> resolved = functionLibrary.resolveFunctions(outputType, inputType, funcName, fargs); if (resolved.size() == 0) { throw new RuntimeException("Unable to find even one function for " + call); } args[i] = resolved.get(0).getFunctionObject(); } } return args; }