V1_0.java [src/java/m/crp/smweps] Revision: 2e8ad8e9186e3a42c999b6c68fc5b7ea319868de  Date: Mon Dec 09 10:05:34 MST 2019
/*
 * $Id$
 *
 * This file is part of the Cloud Services Integration Platform (CSIP),
 * a Model-as-a-Service framework, API, and application suite.
 *
 * 2012-2019, OMSLab, Colorado State University.
 *
 * OMSLab licenses this file to you under the MIT license.
 * See the LICENSE file in the project root for more information.
 */
package m.crp.smweps;

import csip.ModelDataService;
import csip.ServiceException;
import csip.annotations.Description;
import csip.annotations.Name;
import csip.annotations.Resource;
import csip.annotations.ResourceType;
import csip.utils.SimpleCache;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import javax.ws.rs.Path;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.WildcardFileFilter;
import org.apache.commons.lang.SerializationUtils;
import org.encog.ml.data.MLData;
import org.encog.neural.neat.NEATNetwork;

/**
 *
 * @author <a href="mailto:shaun.case@colostate.edu">Shaun Case</a>
 */
@Name("WEPS SM")
@Description("WEPS surrogate model")
@Path("m/smweps/1.0")
@Resource(file = "/data/ens1.zip", id = "ens1", type = ResourceType.ARCHIVE)
public class V1_0 extends ModelDataService {

  static SimpleCache<String, List<NEATNetwork>> anns = new SimpleCache<>();


  List<NEATNetwork> getANNs(String name) {
    try {
      File ens = resources().getFile(name);
      if (ens.isDirectory()) {
        File[] files = ens.listFiles((FilenameFilter) new WildcardFileFilter("*.ann"));
        List<NEATNetwork> l = new ArrayList<>();
        for (File file : files) {
          byte[] ann = FileUtils.readFileToByteArray(file);
          l.add((NEATNetwork) SerializationUtils.deserialize(ann));
        }
        if (l.isEmpty()) {
          throw new IllegalArgumentException("No anns found for ensemble: " + name);
        }
        return l;
      }
    } catch (ServiceException | IOException ex) {
      throw new IllegalArgumentException("Failed to load ensemble:: " + name, ex);
    }
    throw new IllegalArgumentException("Not an ann ensemble resource: " + name);
  }


  /**
   * Computes the output from all ANNs
   *
   * @param nn
   * @param input
   * @return
   */
  List<double[]> compute(List<NEATNetwork> nn, MLData input) {
    return nn.parallelStream()
        .map(n -> n.compute(input).getData())
        .collect(Collectors.toList());
  }


  @Override
  public void doProcess() throws Exception {
    String annName = parameter().getString("ens_name");
    
    // read inputs ...
    // ....
    List<NEATNetwork> nn = anns.get(annName, () -> getANNs(annName));

    List<double[]> l = compute(nn, null);
    
    // todo create outputs from l
    // ...
  }

}