src/edu/stanford/nlp/sempre/interactive/VegaEngine.java
public VegaResponse compileVegaLite(JsonNode vlSpec, VegaResponse contextResponse) {
    String vlSpecStr = Json.writeValueAsStringHard(vlSpec);
    String cacheKey = vlSpecStr + "|||" + contextResponse;  // Okay this is a silly hack
    if (opts.useCache) {
      if (cache.containsKey(cacheKey)) {
        return cache.get(cacheKey);
      }
    }
    Invocable inv = (Invocable) scriptEngine;
    ScriptContext sc = scriptEngine.getContext();
    Bindings bindings = sc.getBindings(ScriptContext.ENGINE_SCOPE);
    bindings.put("vl_str", Json.writeValueAsStringHard(vlSpec));
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    Writer oldWriter = sc.getWriter();
    sc.setWriter(pw);
    VegaResponse vr = null;
    JsonNode vegaSpec = null;
    String svg = null;
    String background = null;
    long t0, t1;
    try {
      scriptEngine.eval(COMPILE_SCRIPT);
      String vegaStr = (String) scriptEngine.get("vega_spec_str");
      vegaSpec = Json.readValueHard(vegaStr, JsonNode.class);
      String message = sw.toString();
      vr = new VegaResponse(vegaSpec, null, message);
      if (vr.isGoodChange(contextResponse) && opts.compileToSVG) {
        // Abort if we already know this is not a good change
        // i.e. compiler complained or vegaSpec is identical
        scriptEngine.eval(SVG_SCRIPT);
        String msg2 = sw.toString();
        if (opts.verbose >= 2) {
          LogInfo.logs("SVG Rendering Logs: " + msg2);
        }
        svg = (String) scriptEngine.get("svg");
        background = (String) scriptEngine.get("background");
        vr = new VegaResponse(vegaSpec, new VegaImage(svg, background) , message);
      }
    } catch (ScriptException e) {
      if (opts.verbose >= 2) {
        e.printStackTrace();
      }
      vr = new VegaResponse(vegaSpec, new VegaImage(svg, background), e.getMessage());
    } finally {
      sc.setWriter(oldWriter);
      // Just to be safe, remove bindings
      bindings.remove("vl_spec");
      bindings.remove("vega_spec");
      bindings.remove("vega_spec_str");
      bindings.remove("view");
      bindings.remove("svg");
      bindings.remove("background");
    }
    if (opts.useCache) {
      cache.put(cacheKey, vr);
    }
    return vr;
  }
/**
   * Compile a Vega-lite spec into Vega, then into SVG if compilation worked.
   */
Compile a Vega-lite spec into Vega, then into SVG if compilation worked.
Compile a Vega - lite spec into Vega then into SVG if compilation worked .
public VegaResponse compileVegaLite(JsonNode vlSpec, VegaResponse contextResponse) {
    String vlSpecStr = Json.writeValueAsStringHard(vlSpec);
    String cacheKey = vlSpecStr + "|||" + contextResponse;  
    if (opts.useCache) {
      if (cache.containsKey(cacheKey)) {
        return cache.get(cacheKey);
      }
    }
    Invocable inv = (Invocable) scriptEngine;
    ScriptContext sc = scriptEngine.getContext();
    Bindings bindings = sc.getBindings(ScriptContext.ENGINE_SCOPE);
    bindings.put("vl_str", Json.writeValueAsStringHard(vlSpec));
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    Writer oldWriter = sc.getWriter();
    sc.setWriter(pw);
    VegaResponse vr = null;
    JsonNode vegaSpec = null;
    String svg = null;
    String background = null;
    long t0, t1;
    try {
      scriptEngine.eval(COMPILE_SCRIPT);
      String vegaStr = (String) scriptEngine.get("vega_spec_str");
      vegaSpec = Json.readValueHard(vegaStr, JsonNode.class);
      String message = sw.toString();
      vr = new VegaResponse(vegaSpec, null, message);
      if (vr.isGoodChange(contextResponse) && opts.compileToSVG) {
        scriptEngine.eval(SVG_SCRIPT);
        String msg2 = sw.toString();
        if (opts.verbose >= 2) {
          LogInfo.logs("SVG Rendering Logs: " + msg2);
        }
        svg = (String) scriptEngine.get("svg");
        background = (String) scriptEngine.get("background");
        vr = new VegaResponse(vegaSpec, new VegaImage(svg, background) , message);
      }
    } catch (ScriptException e) {
      if (opts.verbose >= 2) {
        e.printStackTrace();
      }
      vr = new VegaResponse(vegaSpec, new VegaImage(svg, background), e.getMessage());
    } finally {
      sc.setWriter(oldWriter);
      bindings.remove("vl_spec");
      bindings.remove("vega_spec");
      bindings.remove("vega_spec_str");
      bindings.remove("view");
      bindings.remove("svg");
      bindings.remove("background");
    }
    if (opts.useCache) {
      cache.put(cacheKey, vr);
    }
    return vr;
  }
public VegaResponse compileVegaLite ( JsonNode vlSpec , VegaResponse contextResponse ) { String vlSpecStr = Json . writeValueAsStringHard ( vlSpec ) ; String cacheKey = vlSpecStr + "|||" + contextResponse ; if ( opts . useCache ) { if ( cache . containsKey ( cacheKey ) ) { return cache . get ( cacheKey ) ; } } Invocable inv = ( Invocable ) scriptEngine ; ScriptContext sc = scriptEngine . getContext ( ) ; Bindings bindings = sc . getBindings ( ScriptContext . ENGINE_SCOPE ) ; bindings . put ( "vl_str" , Json . writeValueAsStringHard ( vlSpec ) ) ; StringWriter sw = new StringWriter ( ) ; PrintWriter pw = new PrintWriter ( sw ) ; Writer oldWriter = sc . getWriter ( ) ; sc . setWriter ( pw ) ; VegaResponse vr = null ; JsonNode vegaSpec = null ; String svg = null ; String background = null ; long t0 , t1 ; try { scriptEngine . eval ( COMPILE_SCRIPT ) ; String vegaStr = ( String ) scriptEngine . get ( "vega_spec_str" ) ; vegaSpec = Json . readValueHard ( vegaStr , JsonNode . class ) ; String message = sw . toString ( ) ; vr = new VegaResponse ( vegaSpec , null , message ) ; if ( vr . isGoodChange ( contextResponse ) && opts . compileToSVG ) { scriptEngine . eval ( SVG_SCRIPT ) ; String msg2 = sw . toString ( ) ; if ( opts . verbose >= 2 ) { LogInfo . logs ( "SVG Rendering Logs: " + msg2 ) ; } svg = ( String ) scriptEngine . get ( "svg" ) ; background = ( String ) scriptEngine . get ( "background" ) ; vr = new VegaResponse ( vegaSpec , new VegaImage ( svg , background ) , message ) ; } } catch ( ScriptException e ) { if ( opts . verbose >= 2 ) { e . printStackTrace ( ) ; } vr = new VegaResponse ( vegaSpec , new VegaImage ( svg , background ) , e . getMessage ( ) ) ; } finally { sc . setWriter ( oldWriter ) ; bindings . remove ( "vl_spec" ) ; bindings . remove ( "vega_spec" ) ; bindings . remove ( "vega_spec_str" ) ; bindings . remove ( "view" ) ; bindings . remove ( "svg" ) ; bindings . remove ( "background" ) ; } if ( opts . useCache ) { cache . put ( cacheKey , vr ) ; } return vr ; }
Compile a Vega-lite spec into Vega, then into SVG if compilation worked.
Compile a Vega - lite spec into Vega then into SVG if compilation worked .
// Okay this is a silly hack // Abort if we already know this is not a good change // i.e. compiler complained or vegaSpec is identical // Just to be safe, remove bindings