Commit 238086d3 authored by Wei Lu's avatar Wei Lu

Dynamic Interface Autopilot Generation

parent 3aa54423
Pipeline #198342 passed with stages
in 32 minutes and 46 seconds
/* (c) https://github.com/MontiCore/monticore */ /* (c) https://github.com/MontiCore/monticore */
package de.monticore.lang.monticar.generator.cpp.viewmodel; package de.monticore.lang.monticar.generator.cpp.viewmodel;
import java.util.regex.*;
public final class AutopilotAdapterDataModel extends ViewModelBase { public final class AutopilotAdapterDataModel extends ViewModelBase {
...@@ -12,6 +13,13 @@ public final class AutopilotAdapterDataModel extends ViewModelBase { ...@@ -12,6 +13,13 @@ public final class AutopilotAdapterDataModel extends ViewModelBase {
private String outputTypes = ""; private String outputTypes = "";
private String functionDeclarations = ""; private String functionDeclarations = "";
private String functionImplementations = ""; private String functionImplementations = "";
private String structCountFuncDeclarations = "";
private String structNameFuncDeclarations = "";
private String structTypeFuncDeclarations = "";
private String structCountFuncImplementations = "";
private String structNameFuncImplementations = "";
private String structTypeFuncImplementations = "";
public String getMainModelName() { public String getMainModelName() {
return mainModelName; return mainModelName;
...@@ -40,6 +48,25 @@ public final class AutopilotAdapterDataModel extends ViewModelBase { ...@@ -40,6 +48,25 @@ public final class AutopilotAdapterDataModel extends ViewModelBase {
public String getFunctionImplementations() { public String getFunctionImplementations() {
return functionImplementations; return functionImplementations;
} }
public String getStructCountFuncDeclarations() {
return structCountFuncDeclarations;
}
public String getStructCountFuncImplementations() {
return structCountFuncImplementations;
}
public String getStructNameFuncDeclarations() {
return structNameFuncDeclarations;
}
public String getStructNameFuncImplementations() {
return structNameFuncImplementations;
}
public String getStructTypeFuncDeclarations() {
return structTypeFuncDeclarations;
}
public String getStructTypeFuncImplementations() {
return structTypeFuncImplementations;
}
public void setMainModelName(String mainModelName) { public void setMainModelName(String mainModelName) {
this.mainModelName = mainModelName; this.mainModelName = mainModelName;
...@@ -55,28 +82,52 @@ public final class AutopilotAdapterDataModel extends ViewModelBase { ...@@ -55,28 +82,52 @@ public final class AutopilotAdapterDataModel extends ViewModelBase {
public void addInput(String name, String type){ public void addInput(String name, String type){
//Add lines to name and type array //Add lines to name and type array
this.inputNames += "\n \"" + name + "\","; this.inputNames += "\n \"" + name + "\",";
this.inputTypes += "\n \"" + type + "\","; if (type.equals("Q")|type.equals("Z")|type.equals("CommonMatrixType")) {
this.inputTypes += "\n \"" + type + "\",";
} else {
this.inputTypes += "\n \"struct__" + type + "\",";
}
//this.inputTypes += "\n \"" + type + "\",";
//Add line to function declarations and implementations //Add line to function declarations and implementations
String functionHeader = "EXPORT void set_input_" + name; String functionHeader = "EXPORT void set_input_" + name;
if (type.equals("Q")){ if (type.equals("Q")){
functionHeader += "(double v)"; functionHeader += "(double v)";
this.functionImplementations += functionHeader + " { AUTOPILOT_INSTANCE." + name + " = v; }\n"; this.functionImplementations += functionHeader + " { AUTOPILOT_INSTANCE." + name + " = v; }\n";
this.functionDeclarations += functionHeader + ";\n";
} else if (type.equals("Z")) { } else if (type.equals("Z")) {
functionHeader += "(int v)"; functionHeader += "(int v)";
this.functionImplementations += functionHeader + " { AUTOPILOT_INSTANCE." + name + " = v; }\n"; this.functionImplementations += functionHeader + " { AUTOPILOT_INSTANCE." + name + " = v; }\n";
this.functionDeclarations += functionHeader + ";\n";
} else if (type.equals("CommonMatrixType")) { } else if (type.equals("CommonMatrixType")) {
functionHeader += "(double *data, int size)"; functionHeader += "(double *data, int size)";
this.functionImplementations += functionHeader + " { copy_double_array_to_mat( data, size, AUTOPILOT_INSTANCE." + name + "); }\n"; this.functionImplementations += functionHeader + " { copy_double_array_to_mat( data, size, AUTOPILOT_INSTANCE." + name + "); }\n";
} this.functionDeclarations += functionHeader + ";\n";
this.functionDeclarations += functionHeader + ";\n"; } else {
String pattern = ".*" + type + ".*\n.*";
boolean isMatch = Pattern.matches(pattern, this.structCountFuncDeclarations);
if (!isMatch) {
this.structCountFuncImplementations += "EXPORT int get_struct_count__" + type + "(){\n return struct_count;\n}\n";
this.structNameFuncImplementations += "EXPORT const char *get_struct_name__" + type + "(int index){\n return struct_names__" + type + ";\n}\n";
this.structTypeFuncImplementations += "EXPORT const char *get_struct_type__" + type + "(int index){\n return struct_types__" + type + ";\n}\n";
this.structCountFuncDeclarations += "EXPORT int get_struct_count__" + type + "();\n";
this.structNameFuncDeclarations += "EXPORT const char *get_struct_name__" + type + "(int index);\n";
this.structTypeFuncDeclarations += "EXPORT const char *get_struct_type__" + type + "(int index);\n";
}
}
} }
public void addOutput(String name, String type){ public void addOutput(String name, String type){
this.outputNames += "\n \"" + name + "\","; this.outputNames += "\n \"" + name + "\",";
this.outputTypes += "\n \"" + type + "\","; //this.outputTypes += "\n \"" + type + "\",";
if (type.equals("Q")|type.equals("Z")|type.equals("CommonMatrixType")) {
this.outputTypes += "\n \"" + type + "\",";
} else {
this.outputTypes += "\n \"struct__" + type + "\",";
}
String functionHeader = ""; String functionHeader = "";
if (type.equals("Q")){ if (type.equals("Q")){
functionHeader = "EXPORT double get_output_" + name + "()"; functionHeader = "EXPORT double get_output_" + name + "()";
......
...@@ -32,24 +32,24 @@ const char *output_types[] = { ${viewModel.outputTypes} ...@@ -32,24 +32,24 @@ const char *output_types[] = { ${viewModel.outputTypes}
extern "C" { extern "C" {
EXPORT int get_input_count(){ EXPORT int get_count_input(){
return input_count; return input_count;
} }
EXPORT const char *get_input_name(int id){ EXPORT const char *get_name_input(int index){
return input_names[id]; return input_names[index];
} }
EXPORT const char *get_input_type(int id){ EXPORT const char *get_type_input(int index){
return input_types[id]; return input_types[index];
} }
EXPORT int get_output_count(){ EXPORT int get_count_output(){
return output_count; return output_count;
} }
EXPORT const char *get_output_name(int id){ EXPORT const char *get_name_output(int index){
return output_names[id]; return output_names[index];
} }
EXPORT const char *get_output_type(int id){ EXPORT const char *get_type_output(int index){
return output_types[id]; return output_types[index];
} }
EXPORT void init(){ EXPORT void init(){
...@@ -59,8 +59,8 @@ EXPORT void init(){ ...@@ -59,8 +59,8 @@ EXPORT void init(){
EXPORT void execute(){ EXPORT void execute(){
AUTOPILOT_INSTANCE.execute(); AUTOPILOT_INSTANCE.execute();
} }
${viewModel.functionImplementations} ${viewModel.functionImplementations}
${viewModel.structCountFuncImplementations}
${viewModel.structNameFuncImplementations}
${viewModel.structTypeFuncImplementations}
} }
...@@ -18,17 +18,20 @@ ...@@ -18,17 +18,20 @@
extern "C" { extern "C" {
EXPORT int get_input_count(); EXPORT int get_count_input();
EXPORT const char *get_input_name(int id); EXPORT const char *get_name_input(int index);
EXPORT const char *get_input_type(int id); EXPORT const char *get_type_input(int index);
EXPORT int get_output_count(); EXPORT int get_count_output();
EXPORT const char *get_output_name(int id); EXPORT const char *get_name_output(int index);
EXPORT const char *get_output_type(int id); EXPORT const char *get_type_output(int index);
EXPORT void init(); EXPORT void init();
EXPORT void execute(); EXPORT void execute();
${viewModel.functionDeclarations} ${viewModel.functionDeclarations}
${viewModel.structCountFuncDeclarations}
${viewModel.structNameFuncDeclarations}
${viewModel.structTypeFuncDeclarations}
} }
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