V1_0.java [src/java/m/oms/ages] Revision: 6a33f9b51e92c88b42b5cbf1a44f4f5bab4d7dd8  Date: Mon Feb 17 10:22:28 MST 2020
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package m.oms.ages;

import csip.Executable;
import csip.ModelDataService;
import csip.ServiceException;
import csip.annotations.*;
import static csip.annotations.ResourceType.*;
import csip.utils.Binaries;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import javax.ws.rs.Path;
import static m.oms.ages.V1_0.ID_AGES_JAR;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOCase;
import org.apache.commons.io.filefilter.WildcardFileFilter;
import org.apache.commons.lang.ArrayUtils;

/**
 * Ages service.
 *
 * @author od
 */
@Name("AGES model execution")
@Description("OMS based AGES model service")
@VersionInfo("1.0")
@Path("m/ages/1.0")
@Resource(file = "/bin/ages/ages.jar", type = JAR, id = ID_AGES_JAR)
@Resource(file = "/bin/ages/ages-lib.zip", type = ARCHIVE)
@Resource(file = "/bin/ages/ages-static.zip", type = ARCHIVE)
@Resource(file = "java-*-std*.txt output/csip_run/out/*.csv", type = OUTPUT)
@Resource(file = "${csip.dir}/bin/ages/simulation/ages.sim", type = REFERENCE, id = "ages.sim")
public class V1_0 extends ModelDataService {

  // parameter keys
  static final String KEY_SCRIPT = "ages.sim";
  static final String KEY_LOGLEVEL = "loglevel";
  static final String KEY_OPTIONS = "java.options";

  static final String ID_AGES_JAR = "ages.jar";
  static final String RUN_INC = "run.inc";


  @Override
  public void doProcess() throws Exception {
    String dsl = parameter().getString(KEY_SCRIPT, resources().getFile(KEY_SCRIPT).toString());

    // pass request param to model runtime parameter -> run.inc
    Map<String, String> runParam = new HashMap<>();
    passReqQuotedParam(runParam, "startTime");
    passReqQuotedParam(runParam, "endTime");

    passOptParam(runParam, "flowRouteTA");
    passOptParam(runParam, "SoilOutLPS");

    createParamInclude(runParam);

    File d = new File(dsl);
    if (!(d.isAbsolute() && d.exists())) {
      d = getWorkspaceFile(dsl);
    }
    runAges(d);
//    results().put(getWorkspaceFile("output"));
  }


  /**
   * pass a required parameter, quoted (string).
   */
  private void passReqQuotedParam(Map<String, String> p, String name) throws ServiceException {
    p.put(name, "\"" + parameter().getString(name) + "\"");
  }


  /**
   * pass optional parameter, no quotes.
   */
  private void passOptParam(Map<String, String> p, String name) throws ServiceException {
    if (parameter().has(name)) {
      p.put(name, parameter().getString(name));
    }
  }


  /**
   * create a 'sim' include file for the run part.
   */
  private void createParamInclude(Map<String, String> p) throws IOException {
    StringBuilder b = new StringBuilder();
    b.append("parameter {\n");
    p.keySet().forEach((name) -> {
      b.append("  ").append(name).append(" ").append(p.get(name)).append("\n");
    });
    b.append("}\n");
    FileUtils.writeStringToFile(getWorkspaceFile(RUN_INC), b.toString());
  }


  /**
   * Run Ages
   *
   * @param dsl
   * @param options
   * @throws Exception
   */
  private void runAges(File dsl) throws Exception {

    getWorkspaceFile("output").mkdirs();
    getWorkspaceFile("logs").mkdirs();

    // Create/execute a Ages.
    Executable p = createProcess(dsl);
    int result = p.exec();
    if (result != 0) {
      FilenameFilter ff = new WildcardFileFilter("java*stderr.txt", IOCase.INSENSITIVE);
      File[] f = getWorkspaceDir().listFiles(ff);
      if (f != null && f.length > 0) {
        String err = FileUtils.readFileToString(f[0]);
        LOG.info("Ages execution error. " + f[0] + ":\n" + err);
        throw new ServiceException("Ages execution error. " + f[0] + ":\n" + err);
      }
      throw new ServiceException("Ages execution error." + result);
    }
  }


  /**
   * Create the external Ages process.
   */
  private Executable createProcess(File dsl) throws Exception {

    Map<String, String> sysprops = new HashMap();
    sysprops.put("oms_prj", getWorkspaceDir().toString());
    sysprops.put("csip_ages", resources().getFile(ID_AGES_JAR).getParent());

    String[] jvmOptions = Binaries.asSysProps(sysprops);
    String options = parameter().getString(KEY_OPTIONS, "");
    if (options != null && !options.isEmpty()) {
      jvmOptions = (String[]) ArrayUtils.addAll(jvmOptions, options.split("\\s+"));
    }

    // java -Doms_prj=. -cp "dist/AgES.jar" oms3.CLI -l OFF -r "projects/sfir30/simulation/sfir30.sim"
    return Binaries.getResourceOMSDSL(
        dsl, // the dsl file to run
        jvmOptions, // jvm options
        getWorkspaceDir(), // workspace dir
        Arrays.asList(resources().getFile(ID_AGES_JAR)), // the ages jar file 
        parameter().getString(KEY_LOGLEVEL, "INFO"), // The log level
        LOG); // This session logger
  }
}