default <T> List<DataMapper<T>> getDataMappers(String spec) { List<ResolvedFunction> resolvedFunctions1 = this.resolveFunctions(spec); return resolvedFunctions1.stream().map( r -> DataMapperFunctionMapper.<T>map(r.getFunctionObject())).collect(Collectors.toList()); }
/** * Provide a way to promote a long function into a data mapper. * * @param spec a binding spec * @return An optional data mapper */ default <T> Optional<DataMapper<T>> getDataMapper(String spec) { List<ResolvedFunction> resolvedFunctions = this.resolveFunctions(spec); switch (resolvedFunctions.size()) { case 0: return Optional.empty(); case 1: return Optional.of(DataMapperFunctionMapper.<T>map(resolvedFunctions.get(0).getFunctionObject())); default: throw new RuntimeException( "Found " + resolvedFunctions.size() + " data mapping functions, expected exactly one for library-level function lookups." + " This may require both an input and an output type qualifier like 'int -> f() -> int'." + " \nFound: [<library name>::] input->class->output [initializer type->parameter type,...]: \n" + resolvedFunctions.stream().map(String::valueOf).collect(Collectors.joining("\n"))); } }
@SuppressWarnings("unchecked") default <T> DataMapper<T> getDataMapper(String spec, Class<? extends T> clazz) { if (!canParseSpec(spec)) { return null; } Optional<ResolvedFunction> resolvedFunction = resolveFunction(spec); if (!resolvedFunction.isPresent()) { return null; } ResolvedFunction rf = resolvedFunction.get(); DataMapper<Object> dm = DataMapperFunctionMapper.map(rf.getFunctionObject()); return (DataMapper<T>) dm; }
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()); }
for (ResolvedFunction resolvedFunction : flattenedFuncs) { try { Object functionObject = resolvedFunction.getFunctionObject(); assembly.andThen(functionObject); if (!resolvedFunction.isThreadSafe()) {
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; }
boolean isThreadSafe = true; for (ResolvedFunction resolvedFunction : flattenedFuncs) { assembly.andThen(resolvedFunction.getFunctionObject()); if (!resolvedFunction.isThreadSafe()) { isThreadSafe = false;