Commit bec47b36 authored by PONCELET's avatar PONCELET

Update java globe code generator to declare variable on use only

parent d206488c
......@@ -10,7 +10,7 @@ public class SCDimensionAdapter {
* */
public static List<String> getSourceCode(Dimension dimension) {
return Arrays.asList(
String.format("\t\t%s = addDimension(%s, %s); //%s", getDimVariableName(dimension), getDimNameDeclaration(dimension), "100", dimension.comment==null ? "" : dimension.comment));
String.format("\t\t%s = declareDimension(%s, %s); //%s", getDimVariableName(dimension), getDimNameDeclaration(dimension), "100", dimension.comment==null ? "" : dimension.comment));
}
public static List<String> getSourceAccessor(Dimension dim) {
......
......@@ -10,6 +10,7 @@ import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.text.WordUtils;
public class SCGroupAdapter {
private static boolean forceVariableDeclaration=false;
/** @return class name **/
public static String getClassName(Group group) {
String className = StringUtils.deleteWhitespace(WordUtils.capitalizeFully(group.name.replace("_", " ")));
......@@ -97,25 +98,42 @@ public class SCGroupAdapter {
// Variables declaration
if (!group.variables.isEmpty()) {
sourceCode.add("\n\t\t/** Variables **/");
group.variables.forEach(v -> {
try {
sourceCode.addAll(SCVariableAdapter.getSourceCode(v));
} catch (Exception e) {
throw new RuntimeException(e);
}
});
if(forceVariableDeclaration)
{
sourceCode.add("\n\t\t/** Variables **/");
group.variables.forEach(v -> {
try {
sourceCode.add("\t\t"+SCVariableAdapter.getDeclaratorName(v)+";\n");
} catch (Exception e) {
throw new RuntimeException(e);
}
});
}
}
// End constructor
sourceCode.add("\t}");
// Start utility function
if(!group.dims.isEmpty())
{
sourceCode.add("\t/**\n"
+ "\t* declare a dimension is its lenght is strictly positive\n"
+ "\t**/\n"
+ "\tprivate NCDimension declareDimension(String dimName, long len) throws NCException {\n"
+ "\t\t if(len <= 0) return null; //dimension is considered as not defined (optional) in this file\n"
+ "\t\t return addDimension(dimName, len);\n"
+ "\t}\n");
}
// Variables Accessors
if (!group.variables.isEmpty()) {
// sourceCode.add("\n\t/** Accessors**/\n");
group.variables.forEach(v -> {
try {
sourceCode.addAll(SCVariableAdapter.getSourceAccessor(v));
sourceCode.addAll(SCVariableAdapter.declareVariableSourceCode(v));
} catch (Exception e) {
throw new RuntimeException(e);
}
......@@ -143,13 +161,13 @@ public class SCGroupAdapter {
private static String pythonCommentMarker="\"\"\"\n";
public static List<String> getPythonSourceCode(Group group, String ident, String path) {
// Build file contents
List<String> sourceCode = new ArrayList<>();
path=path+group.name+"/";
//reject if empty group
// if(group.dims.isEmpty() && group.variables.isEmpty() && group.subGroups.isEmpty())
// return sourceCode;
// if(group.dims.isEmpty() && group.variables.isEmpty() && group.subGroups.isEmpty())
// return sourceCode;
// This group declaration
if(ident.isEmpty())
......@@ -157,25 +175,25 @@ public class SCGroupAdapter {
sourceCode.add(String.format(pythonCommentMarker+"Class generated for XSF see %s\n"+pythonCommentMarker, "https://gitlab.ifremer.fr/fleet/XSF"));
}
sourceCode.add(String.format("\n"+ident+"class %s:", getClassName(group)));
String localIdent=ident+pythonIndent;
sourceCode.add(String.format("%sGROUP_PATH = \"%s\"", localIdent,path));
// Dimension name code generation
if(!group.dims.isEmpty())
{
sourceCode.add(localIdent+"# dimensions names declaration");
group.dims.forEach(d->
sourceCode.add(localIdent+String.format("%s = \"%s\"", SCDimensionAdapter.getDimNameDeclaration(d),SCDimensionAdapter.getDimVariableName(d))));
sourceCode.add(localIdent+String.format("%s = \"%s\"", SCDimensionAdapter.getDimNameDeclaration(d),SCDimensionAdapter.getDimVariableName(d))));
}
// Variables declaration
if (!group.variables.isEmpty()) {
sourceCode.add(localIdent+"# Variables names");
String localPath=path;
group.variables.forEach(v -> {
sourceCode.add(String.format(localIdent+"%s = \"%s%s\"", v.name.toUpperCase(),localPath,v.name));
sourceCode.add(String.format(localIdent+"%s = \"%s%s\"", v.name.toUpperCase(),localPath,v.name));
});
}
// Sub groups
......
......@@ -16,9 +16,10 @@ public class SCVariableAdapter {
return Arrays.asList(String.format("\t/**\n"
+ "\t * @return the %s\n"
+ "\t */\n"
+ "\tpublic NCVariable get%s() {\n"
+ "\tpublic NCVariable get%s() throws NCException {\n"
+ "\t\tif (%s == null) %s;\n"
+ "\t\treturn %s;"
+ "\n\t}\n", variable.name,CamelCase(variable.name),variable.name));
+ "\n\t}\n", variable.name,CamelCase(variable.name),variable.name,getDeclaratorName(variable),variable.name));
}
private static String CamelCase(String name)
......@@ -26,12 +27,18 @@ public class SCVariableAdapter {
return name.substring(0, 1).toUpperCase()+name.substring(1);
}
public static List<String> getSourceCode(Variable variable) throws Exception {
public static String getDeclaratorName(Variable variable)
{
return String.format("declare_%s()", CamelCase(variable.name));
}
public static List<String> declareVariableSourceCode(Variable variable) throws Exception {
List<String> codeLines = new ArrayList<>();
codeLines.add(String.format("\n\t\t/** Variable: %s **/", variable.name));
codeLines.add(String.format("\n\t/** Variable: %s declarator**/", variable.name));
codeLines.add(String.format("\n\tprivate void %s throws NCException {\n\t", getDeclaratorName(variable)));
StringBuilder dimString ;
if(!variable.dims.isEmpty())
{
......@@ -54,7 +61,13 @@ public class SCVariableAdapter {
// "comment" attribute
if (variable.comment!=null && !variable.comment.trim().isEmpty())
codeLines.add(String.format("\t\t%s.addAttribute(\"comment\", \"%s\");", variable.name, variable.comment));
//for test purpose add an attribute containing the variable obligation value
if(variable.obligation!=null && !variable.obligation.isEmpty())
{
codeLines.add(String.format("\t\t%s.addAttribute(\"obligation\", \"%s\");", variable.name, variable.obligation));
}
codeLines.add("\t}");
return codeLines;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment