@Override public void enterVirtdataCall(VirtDataParser.VirtdataCallContext ctx) { callContexts.push(ctx); calls.push(new FunctionCall()); }
@Override public void exitVirtdataCall(VirtDataParser.VirtdataCallContext ctx) { FunctionCall topFunctionCall = calls.pop(); if (calls.empty()) { flows.peek().getLastExpression().setCall(topFunctionCall); } else { calls.peek().addFunctionArg(topFunctionCall); } callContexts.pop(); }
@Override public void exitFuncName(MetagenParser.FuncNameContext ctx) { calls.peek().setFuncName(ctx.getText()); }
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; }
String specifiedInputClassName = flow.getFirstExpression().getCall().getInputType(); Class<?> specifiedInputClass = ValueType.classOfType(specifiedInputClassName); if (specifiedInputClass!=null) { flow.getFirstExpression().getCall().setInputType(requiredInputType.getCanonicalName()); String specifiedOutputClassName = flow.getLastExpression().getCall().getOutputType(); Class<?> specifiedOutputClass = ValueType.classOfType(specifiedOutputClassName); if (specifiedOutputClass != null) { flow.getLastExpression().getCall().setOutputType(requiredOutputType.getCanonicalName());
String outputType = flow.getLastExpression().getCall().getOutputType(); flow.getLastExpression().getCall().setOutputType(clazz.getCanonicalName());
@Override public void exitInputType(MetagenParser.InputTypeContext ctx) { calls.peek().setInputType(ctx.getText()); }
@Override public void exitOutputType(MetagenParser.OutputTypeContext ctx) { calls.peek().setOutputType(ctx.getText()); }
public Object[] getArguments() { List<Object> args = new ArrayList<>(); for (ArgType argType : getArgs()) { ArgType.TypeName typeName = ArgType.TypeName.valueOf(argType); switch (typeName) { case RefArg: args.add(new VariableRef(((RefArg) argType).getRefName())); break; case FunctionCall: args.add(argType); // TODO: revisit this break; case StringArg: args.add(((StringArg) argType).getStringValue()); break; case FloatArg: args.add(((FloatArg) argType).getFloatValue()); break; case IntegerArg: args.add(((IntegerArg) argType).getIntValue()); break; default: throw new RuntimeException("Could not map type into argument object: " + typeName); } } return args.toArray(); }
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; }
@Override public void exitInputType(VirtDataParser.InputTypeContext ctx) { calls.peek().setInputType(ctx.getText()); }
@Override public void exitOutputType(VirtDataParser.OutputTypeContext ctx) { calls.peek().setOutputType(ctx.getText()); }
public Object[] getArguments() { List<Object> args = new ArrayList<>(); for (ArgType argType : getArgs()) { ArgType.TypeName typeName = ArgType.TypeName.valueOf(argType); switch (typeName) {
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(); }
@Override public void exitVirtdataCall(VirtDataParser.VirtdataCallContext ctx) { FunctionCall topFunctionCall = calls.pop(); if (calls.empty()) { flows.peek().getLastExpression().setCall(topFunctionCall); } else { calls.peek().addFunctionArg(topFunctionCall); } callContexts.pop(); }
@Override public void enterVirtdataCall(VirtDataParser.VirtdataCallContext ctx) { callContexts.push(ctx); calls.push(new FunctionCall()); }
@Override public void exitFuncName(LambdasParser.FuncNameContext ctx) { calls.peek().setFuncName(ctx.getText()); }
@Override public void exitInputType(LambdasParser.InputTypeContext ctx) { calls.peek().setInputType(ctx.getText()); }
@Override public void exitOutputType(VirtDataParser.OutputTypeContext ctx) { calls.peek().setOutputType(ctx.getText()); }
public Object[] getArguments() { List<Object> args = new ArrayList<>(); for (ArgType argType : getArgs()) { ArgType.TypeName typeName = ArgType.TypeName.valueOf(argType); switch (typeName) {