Class FWSimComponentCodeGen

java.lang.Object
net.simplace.sim.model.FWSimComponentCodeGen

public class FWSimComponentCodeGen extends Object
Code generator for SimComponents: creates the Java source of a FWSimComponent from the template codegen/FWSimComponent.javatemplate (class path) and a component definition. Supported definitions are a tab separated CSV file (generateCodeCSV(Vector)), a simple XML file (generateCodeXML(Element)), an AMEI model unit (generateCodeAmeiXML(Element, File)) and the first FWSimpleSimComponent of a solution (
invalid reference
#generateCodeSimpleSimComponent(File)
).

The template contains placeholders (${classname}, ${package}, ${author}, ${javadoc_class}, ${process}) and lines starting with FOR(constant), FOR(input), FOR(out), FOR(state) or FOR(rate), which are repeated for every variable of that content type. The process code of the definition is rewritten to access the generated fields (assignments become setValue calls, variable reads getValue() calls).

An instance works on a single copy of the template, so it can generate only one class. The result is written with writeCodeToFile(String).

Author:
Andreas Enders
  • Constructor Details

    • FWSimComponentCodeGen

      public FWSimComponentCodeGen(File aJavaSourcePath) throws IOException
      Creates the generator and loads the Java template.
      Parameters:
      aJavaSourcePath - source folder the generated class is written to (the package path is appended)
      Throws:
      IOException - if the source folder does not exist or the template cannot be read
  • Method Details

    • generateCodeCSV

      public String generateCodeCSV(Vector<String> aConfigStrings)
      Generates the class from a tab separated definition. The lines are grouped in sections, each introduced by a line starting with # and ended by an empty line:
      • #classname: full class name including package
      • #author: author text
      • #javadoc_class: lines of the class JavaDoc
      • #process: lines of the process code
      • a header line containing contenttype (e.g. #id contenttype datatype ...): the column names, followed by one line per variable. The column names are used as placeholders (${id}, ${unit}, ...) in the FOR(...) lines of the template.
      Parameters:
      aConfigStrings - lines of the definition file
      Returns:
      the content of the generated class
    • generateCodeSimpleSimComponent

      public String generateCodeSimpleSimComponent(File aSolutionFile, String aComponentId) throws IOException
      Generates a class from a FWSimpleSimComponent of a solution, chosen by its id, with all its rules translated to Java (see FWSimpleSimComponentCodeGen, package "net.simplace.usermodules"). The simcomponent element replacing the simple component is logged.
      Parameters:
      aSolutionFile - solution file (.sol.xml)
      aComponentId - id of the simple component
      Returns:
      the content of the generated class
      Throws:
      IOException - if the solution cannot be loaded
    • generateCodeAmeiXML

      public String generateCodeAmeiXML(org.jdom2.Element aConfigElement, File aSourcePath) throws IOException
      Generates the class from an AMEI model unit. The class name is taken from attribute name, the package from attribute modelid (without its last part), author and class JavaDoc from the Description child (Authors, Institution, Abstract, Reference). The children of Inputs and Outputs become the variables (see createSimVariable(Element)). The process code is taken from the Algorithm element with language="java", either from its text or, if empty, from the file given in its attribute filename.
      Parameters:
      aConfigElement - the model unit element
      aSourcePath - directory the algorithm file name is resolved against
      Returns:
      the content of the generated class
      Throws:
      IOException - if the algorithm file cannot be read
    • generateCodeXML

      public String generateCodeXML(org.jdom2.Element aConfigElement)
      Generates the class from a simple XML definition: attribute class holds the full class name, the optional children author, process and javadoc-class the respective texts, and the child variables the variable elements (see createSimVariable(Element)).
      Parameters:
      aConfigElement - root element of the definition
      Returns:
      the content of the generated class
    • createSimVariable

      public static FWSimVariable<?> createSimVariable(org.jdom2.Element aVarElement)
      Creates a typed FWSimVariable from an AMEI-style variable element.

      Evaluated attributes: name, description, unit, format, datatype (a FWSimVariable.DATA_TYPE name or one of the AMEI names STRING, DOUBLELIST, INTLIST, STRINGLIST, DATELIST), min, max, default and mode (only for output-like content types). The value is taken from the element text or, for array types, from value children.

      The content type is out for elements named Output, otherwise input. It is refined by

      • inputtype="variable" with variablecategory state or rate (auxiliary keeps the default)
      • inputtype="parameter": privat for parametercategory="private", otherwise constant (constant, species, genotypic, soil)
      If the data type is missing or a number cannot be parsed, the data type is guessed from the element text and the error is logged.
      Parameters:
      aVarElement - the variable element
      Returns:
      the new variable
    • writeCodeToFile

      public String writeCodeToFile(String aContentOfFile)
      Writes the generated code to the Java file determined by the last generateCode... call. Errors are logged.
      Parameters:
      aContentOfFile - the Java code
      Returns:
      the full class name
    • main

      public static void main(String[] aStringArray)
      Entry point: generates the class into simplace_modules/src from a definition file. The generator is chosen by the file name: .sol.xml (simple component of a solution), .unit.xml (AMEI model unit), other .xml (simple XML definition), otherwise CSV.
      Parameters:
      aStringArray - optional: path of the definition file; without argument a file dialog asks for it