public void generateMainScript(String exampleFilename)
      throws LEMSException, GenerationException, IOException, ModelFeatureSupportException,
          NeuroMLException {

    Lems lems = AppTest.readLemsFileFromExamples(exampleFilename);

    System.out.println("Loaded: " + exampleFilename);

    CWriter cw = new CWriter(lems);

    String code = cw.getMainScript();

    System.out.println(code);

    File cFile = new File(AppTest.getTempDir(), exampleFilename.replaceAll(".xml", ".c"));
    System.out.println("Writing to: " + cFile.getAbsolutePath());

    FileUtil.writeStringToFile(code, cFile);

    assertTrue(cFile.exists());

    File mFile = new File(AppTest.getTempDir(), "Makefile");
    String makefile = cw.getMakefile();
    FileUtil.writeStringToFile(makefile, mFile);
  }
  private void runDiscreteUpdateComponent(File f1)
      throws ContentError, IOException, ParseError, ConnectionError, RuntimeError {

    String stxt = FileUtil.readStringFromFile(f1);

    XMLElementReader exmlr = new XMLElementReader(stxt + "    ");

    XMLElement xel = exmlr.getRootElement();

    LemsLiteFactory lf = new LemsLiteFactory();
    LemsLite lemsLite = lf.buildLemsFromXMLElement(xel);

    E.info("lemsLite model read: " + lemsLite.getSummary());

    // XMLSerializer xs = new XMLSerializer();
    // String sx = xs.serialize(lemsLite);

    File fdir = f1.getParentFile();

    LemsLiteSimulation lls = new LemsLiteSimulation(lemsLite);

    FileDataSource fds = new FileDataSource(fdir);

    lls.run(fds);
  }
  @Override
  public List<File> convert() throws GenerationException, IOException {
    String code = this.getMainScript();

    File outputFile = new File(this.getOutputFolder(), this.getOutputFileName());
    FileUtil.writeStringToFile(code, outputFile);
    outputFiles.add(outputFile);

    return this.outputFiles;
  }
  public void generateMainScript(File localFile)
      throws LEMSException, IOException, GenerationException, ModelFeatureSupportException,
          NeuroMLException {

    Lems lems = Utils.readLemsNeuroMLFile(FileUtil.readStringFromFile(localFile)).getLems();

    System.out.println("Loaded from: " + localFile);

    PyNNWriter mw = new PyNNWriter(lems);

    String mod = mw.generateMainScriptAndCellFiles(AppTest.getTempDir());

    for (File genFile : mw.allGeneratedFiles) {
      assertTrue(genFile.exists());
      System.out.println(
          "------------------"
              + genFile.getAbsolutePath()
              + "------------------------------------");
      System.out.println(FileUtil.readStringFromFile(genFile));
    }
  }
  public void generateMainScript(File localFile)
      throws LEMSException, GenerationException, IOException, ModelFeatureSupportException,
          NeuroMLException {

    Lems lems = Utils.readLemsNeuroMLFile(FileUtil.readStringFromFile(localFile)).getLems();

    System.out.println("Loaded from: " + localFile);

    CWriter cw = new CWriter(lems);

    String code = cw.getMainScript();

    System.out.println(code);

    File cFile = new File(AppTest.getTempDir(), localFile.getName().replaceAll(".xml", ".c"));

    FileUtil.writeStringToFile(code, cFile);

    assertTrue(cFile.exists());

    File mFile = new File(AppTest.getTempDir(), "Makefile");
    String makefile = cw.getMakefile();
    FileUtil.writeStringToFile(makefile, mFile);
  }
  public String getMainScript() throws GenerationException, IOException {
    StringBuilder mainRunScript = new StringBuilder();

    addComment(
        mainRunScript,
        format + " simulator compliant export for:\n\n" + lems.textSummary(false, false));

    try {
      List<File> files = dlemsw.convert();

      for (File file : files) {

        E.info("Writing " + format + " files to: " + this.getOutputFolder());
        if (file.getName().equals(mainDlemsFile)) {

          VelocityUtils.initializeVelocity();
          VelocityContext context = new VelocityContext();
          VelocityEngine ve = VelocityUtils.getVelocityEngine();
          String dlems = FileUtil.readStringFromFile(file);
          DLemsWriter.putIntoVelocityContext(dlems, context);

          addComment(
              mainRunScript,
              "Using the following distilled version of the LEMS model description for the script below:\n\n"
                  + dlems);

          StringWriter sw1 = new StringWriter();
          boolean generationStatus =
              ve.evaluate(
                  context,
                  sw1,
                  "LOG",
                  VelocityUtils.getTemplateAsReader(VelocityUtils.vertexRunTemplateFile));
          mainRunScript.append(sw1);
        } else if (file.getName().indexOf(".synapse.") > 0) {

          StringBuilder synapseScript = new StringBuilder();
          addComment(
              synapseScript,
              format + " simulator compliant export for:\n\n" + lems.textSummary(false, false));
          E.info(" ====  Handling DLEMS file: " + file.getAbsolutePath());

          VelocityUtils.initializeVelocity();
          VelocityContext context = new VelocityContext();
          VelocityEngine ve = VelocityUtils.getVelocityEngine();
          String dlems = FileUtil.readStringFromFile(file);
          DLemsWriter.putIntoVelocityContext(dlems, context);

          StringWriter sw2 = new StringWriter();
          boolean generationStatus2 =
              ve.evaluate(
                  context,
                  sw2,
                  "LOG",
                  VelocityUtils.getTemplateAsReader(VelocityUtils.vertexSynapseTemplateFile));

          addComment(
              synapseScript,
              "Using the following distilled version of the LEMS model description for the script below:\n\n"
                  + dlems);
          synapseScript.append(sw2);

          String name = (String) context.internalGet(DLemsKeywords.NAME.get());
          E.info("Name: " + name);

          File synapseScriptFile =
              new File(this.getOutputFolder(), "SynapseModel_" + name.toLowerCase() + ".m");

          FileUtil.writeStringToFile(synapseScript.toString(), synapseScriptFile);
          outputFiles.add(synapseScriptFile);

        } else {

          StringBuilder cellScript = new StringBuilder();
          addComment(
              cellScript,
              format + " simulator compliant export for:\n\n" + lems.textSummary(false, false));
          E.info(" ====  Handling DLEMS file: " + file.getAbsolutePath());

          VelocityUtils.initializeVelocity();
          VelocityContext context = new VelocityContext();
          VelocityEngine ve = VelocityUtils.getVelocityEngine();
          String dlems = FileUtil.readStringFromFile(file);
          DLemsWriter.putIntoVelocityContext(dlems, context);

          StringWriter sw2 = new StringWriter();
          boolean generationStatus2 =
              ve.evaluate(
                  context,
                  sw2,
                  "LOG",
                  VelocityUtils.getTemplateAsReader(VelocityUtils.vertexCellTemplateFile));

          addComment(
              cellScript,
              "Using the following distilled version of the LEMS model description for the script below:\n\n"
                  + dlems);
          cellScript.append(sw2);

          String name = (String) context.internalGet(DLemsKeywords.NAME.get());
          E.info("Name: " + name);

          File cellScriptFile =
              new File(this.getOutputFolder(), "PointNeuronModel_" + name.toLowerCase() + ".m");

          FileUtil.writeStringToFile(cellScript.toString(), cellScriptFile);
          outputFiles.add(cellScriptFile);
        }
      }

    } catch (IOException e1) {
      throw new GenerationException("Problem converting LEMS to dLEMS", e1);
    } catch (VelocityException e) {
      throw new GenerationException("Problem using Velocity template", e);
    }

    return mainRunScript.toString();
  }