Commit 4cb22c6f authored by Alexander Ryndin's avatar Alexander Ryndin
Browse files

add AutopilotAdapter generation

parent f1e641ae
package de.monticore.lang.monticar.generator.cpp;
import de.monticore.lang.monticar.generator.FileContent;
import java.io.InputStream;
import java.util.Scanner;
public final class FileUtil {
private FileUtil() {
}
public static FileContent getResourceAsFile(String resourcePath, String destinationFilePath) {
InputStream resource = FileUtil.class.getResourceAsStream(resourcePath);
String body = new Scanner(resource, "UTF-8").useDelimiter("\\A").next();
return new FileContent(body, destinationFilePath);
}
}
......@@ -9,13 +9,17 @@ import de.monticore.lang.monticar.generator.Helper;
import de.monticore.lang.monticar.generator.MathCommandRegister;
import de.monticore.lang.monticar.generator.cpp.converter.MathConverter;
import de.monticore.lang.monticar.generator.cpp.converter.TypeConverter;
import de.monticore.lang.monticar.generator.cpp.template.AllTemplates;
import de.monticore.lang.monticar.generator.cpp.viewmodel.AutopilotAdapterViewModel;
import de.monticore.lang.monticar.ts.MCTypeSymbol;
import de.monticore.lang.tagging._symboltable.TaggingResolver;
import de.monticore.symboltable.Scope;
import de.se_rwth.commons.logging.Log;
import java.io.*;
import java.nio.file.Files;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
......@@ -29,6 +33,7 @@ public class GeneratorCPP implements Generator {
private Path modelsDirPath;
private boolean isGenerateTests = false;
private boolean isGenerateAutopilotAdapter = false;
private final List<BluePrintCPP> bluePrints = new ArrayList<>();
protected String generationTargetPath = "./target/generated-sources-cpp/";
......@@ -145,6 +150,9 @@ public class GeneratorCPP implements Generator {
TestsGeneratorCPP g = new TestsGeneratorCPP(this);
fileContents.addAll(g.generateStreamTests(symtab));
}
if (isGenerateAutopilotAdapter()) {
fileContents.addAll(getAutopilotAdapterFiles(componentSymbol));
}
//System.out.println(fileContents);
if (getGenerationTargetPath().charAt(getGenerationTargetPath().length() - 1) != '/') {
setGenerationTargetPath(getGenerationTargetPath() + "/");
......@@ -275,6 +283,14 @@ public class GeneratorCPP implements Generator {
isGenerateTests = generateTests;
}
public boolean isGenerateAutopilotAdapter() {
return isGenerateAutopilotAdapter;
}
public void setGenerateAutopilotAdapter(boolean generateAutopilotAdapter) {
isGenerateAutopilotAdapter = generateAutopilotAdapter;
}
public List<BluePrintCPP> getBluePrints() {
return Collections.unmodifiableList(bluePrints);
}
......@@ -283,4 +299,18 @@ public class GeneratorCPP implements Generator {
TypesGeneratorCPP tg = new TypesGeneratorCPP();
return tg.generateTypes(typeSymbols);
}
private static List<FileContent> getAutopilotAdapterFiles(ExpandedComponentInstanceSymbol componentSymbol) {
List<FileContent> result = new ArrayList<>();
result.add(FileUtil.getResourceAsFile("/template/autopilotadapter/AutopilotAdapter.h", "AutopilotAdapter.h"));
result.add(generateAutopilotAdapter(componentSymbol));
return result;
}
private static FileContent generateAutopilotAdapter(ExpandedComponentInstanceSymbol componentSymbol) {
AutopilotAdapterViewModel vm = new AutopilotAdapterViewModel();
vm.setMainModelName(GeneralHelperMethods.getTargetLanguageComponentName(componentSymbol.getFullName()));
String fileContents = AllTemplates.generateAutopilotAdapter(vm);
return new FileContent(fileContents, "AutopilotAdapter.cpp");
}
}
......@@ -63,6 +63,13 @@ public final class GeneratorCppCli {
.required(false)
.build();
public static final Option OPTION_FLAG_AUTOPILOT_ADAPTER = Option.builder()
.longOpt("flag-generate-autopilot-adapter")
.desc("optional flag indicating if autopilot adapter should be generated")
.hasArg(false)
.required(false)
.build();
private GeneratorCppCli() {
}
......@@ -82,6 +89,7 @@ public final class GeneratorCppCli {
options.addOption(OPTION_OUTPUT_PATH);
options.addOption(OPTION_FLAG_TESTS);
options.addOption(OPTION_FLAG_ARMADILLO);
options.addOption(OPTION_FLAG_AUTOPILOT_ADAPTER);
return options;
}
......@@ -112,6 +120,7 @@ public final class GeneratorCppCli {
if (cliArgs.hasOption(OPTION_FLAG_ARMADILLO.getOpt())) {
g.useArmadilloBackend();
}
g.setGenerateAutopilotAdapter(cliArgs.hasOption(OPTION_FLAG_AUTOPILOT_ADAPTER.getLongOpt()));
try {
g.generateFiles(componentSymbol, symTab);
} catch (IOException e) {
......
......@@ -26,14 +26,12 @@ import de.monticore.lang.numberunit._ast.ASTUnitNumber;
import de.monticore.symboltable.Scope;
import de.se_rwth.commons.logging.Log;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
public final class TestsGeneratorCPP {
......@@ -195,9 +193,10 @@ public final class TestsGeneratorCPP {
}
private static FileContent getCatchLib() {
InputStream resource = TestsGeneratorCPP.class.getResourceAsStream("/vendor/catch.hpp");
String body = new Scanner(resource, "UTF-8").useDelimiter("\\A").next();
return new FileContent(body, TESTS_DIRECTORY_NAME + "/catch.hpp");
return FileUtil.getResourceAsFile(
"/vendor/catch.hpp",
TESTS_DIRECTORY_NAME + "/catch.hpp"
);
}
private static String getFileName(ComponentStreamTestViewModel viewModel) {
......
package de.monticore.lang.monticar.generator.cpp.template;
import de.monticore.lang.monticar.generator.cpp.viewmodel.AutopilotAdapterViewModel;
import de.monticore.lang.monticar.generator.cpp.viewmodel.ComponentStreamTestViewModel;
import de.monticore.lang.monticar.generator.cpp.viewmodel.EnumViewModel;
import de.monticore.lang.monticar.generator.cpp.viewmodel.StructViewModel;
......@@ -20,6 +21,7 @@ public final class AllTemplates {
private static final Template TESTS_MAIN_ENTRY;
private static final Template STRUCT;
private static final Template ENUM;
private static final Template AUTOPILOT_ADAPTER;
static {
Configuration conf = new Configuration(Configuration.VERSION_2_3_23);
......@@ -32,6 +34,7 @@ public final class AllTemplates {
TESTS_MAIN_ENTRY = conf.getTemplate("/test/TestsMainEntry.ftl");
STRUCT = conf.getTemplate("/type/Struct.ftl");
ENUM = conf.getTemplate("/type/Enum.ftl");
AUTOPILOT_ADAPTER = conf.getTemplate("/autopilotadapter/AutopilotAdapter.ftl");
} catch (IOException e) {
String msg = "could not load templates";
Log.error(msg, e);
......@@ -58,6 +61,10 @@ public final class AllTemplates {
return generate(ENUM, viewModel);
}
public static String generateAutopilotAdapter(AutopilotAdapterViewModel viewModel) {
return generate(AUTOPILOT_ADAPTER, viewModel);
}
private static String generate(Template template, ViewModelBase viewModelBase) {
return generate(template, TemplateHelper.getDataForTemplate(viewModelBase));
}
......
package de.monticore.lang.monticar.generator.cpp.viewmodel;
public final class AutopilotAdapterViewModel extends ViewModelBase {
private String mainModelName;
public String getMainModelName() {
return mainModelName;
}
public void setMainModelName(String mainModelName) {
this.mainModelName = mainModelName;
}
}
<#include "/Common.ftl">
#ifndef AUTOPILOT_ADAPTER
#define AUTOPILOT_ADAPTER
#include "AutopilotAdapter.h"
#define _GLIBCXX_USE_CXX11_ABI 0
#include "./${viewModel.mainModelName}.h"
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include "./Helper.h"
${viewModel.mainModelName} AUTOPILOT_INSTANCE;
void copyJLongArrayToMatrix(JNIEnv *jenv, jlongArray &source, Matrix &dest) {
jsize len = jenv -> GetArrayLength(source);
if (len <= 0) {
return;
}
jlong *body = jenv -> GetLongArrayElements(source, 0);
for (int i=0; i<len; i++) {
dest(0,i) = body[i];
}
jenv -> ReleaseLongArrayElements(source, body, 0);
}
void copyJDoubleArrayToMatrix(JNIEnv *jenv, jdoubleArray &source, Matrix &dest) {
jsize len = jenv -> GetArrayLength(source);
if (len <= 0) {
return;
}
jdouble *body = jenv -> GetDoubleArrayElements(source, 0);
for (int i=0; i<len; i++) {
dest(0,i) = body[i];
}
jenv -> ReleaseDoubleArrayElements(source, body, 0);
}
/*
* Class: simulator_integration_AutopilotAdapter
* Method: set_timeIncrement
* Signature: (D)V
*/
JNIEXPORT void JNICALL Java_simulator_integration_AutopilotAdapter_set_1timeIncrement
(JNIEnv *jenv, jobject jobj, jdouble v) { AUTOPILOT_INSTANCE.timeIncrement = v; }
/*
* Class: simulator_integration_AutopilotAdapter
* Method: set_currentVelocity
* Signature: (D)V
*/
JNIEXPORT void JNICALL Java_simulator_integration_AutopilotAdapter_set_1currentVelocity
(JNIEnv *jenv, jobject jobj, jdouble v) { AUTOPILOT_INSTANCE.currentVelocity = v; }
/*
* Class: simulator_integration_AutopilotAdapter
* Method: set_currentGpsLat
* Signature: (D)V
*/
JNIEXPORT void JNICALL Java_simulator_integration_AutopilotAdapter_set_1x
(JNIEnv *jenv, jobject jobj, jdouble v) { AUTOPILOT_INSTANCE.x = v; }
/*
* Class: simulator_integration_AutopilotAdapter
* Method: set_currentGpsLon
* Signature: (D)V
*/
JNIEXPORT void JNICALL Java_simulator_integration_AutopilotAdapter_set_1y
(JNIEnv *jenv, jobject jobj, jdouble v) { AUTOPILOT_INSTANCE.y = v; }
/*
* Class: simulator_integration_AutopilotAdapter
* Method: set_compass
* Signature: (D)V
*/
JNIEXPORT void JNICALL Java_simulator_integration_AutopilotAdapter_set_1compass
(JNIEnv *jenv, jobject jobj, jdouble v) { AUTOPILOT_INSTANCE.compass = v; }
/*
* Class: simulator_integration_AutopilotAdapter
* Method: set_currentEngine
* Signature: (D)V
*/
JNIEXPORT void JNICALL Java_simulator_integration_AutopilotAdapter_set_1currentEngine
(JNIEnv *jenv, jobject jobj, jdouble v) { AUTOPILOT_INSTANCE.currentEngine = v; }
/*
* Class: simulator_integration_AutopilotAdapter
* Method: set_currentSteering
* Signature: (D)V
*/
JNIEXPORT void JNICALL Java_simulator_integration_AutopilotAdapter_set_1currentSteering
(JNIEnv *jenv, jobject jobj, jdouble v) { AUTOPILOT_INSTANCE.currentSteering = v; }
/*
* Class: simulator_integration_AutopilotAdapter
* Method: set_currentBrakes
* Signature: (D)V
*/
JNIEXPORT void JNICALL Java_simulator_integration_AutopilotAdapter_set_1currentBrakes
(JNIEnv *jenv, jobject jobj, jdouble v) { AUTOPILOT_INSTANCE.currentBrakes = v; }
/*
* Class: simulator_integration_AutopilotAdapter
* Method: set_addNodes_length
* Signature: (I)V
*/
JNIEXPORT void JNICALL Java_simulator_integration_AutopilotAdapter_set_1trajectory_1length
(JNIEnv *jenv, jobject jobj, jint v) { AUTOPILOT_INSTANCE.trajectory_length = v; }
/*
* Class: simulator_integration_AutopilotAdapter
* Method: set_addNodes_gpsLat
* Signature: ([D)V
*/
JNIEXPORT void JNICALL Java_simulator_integration_AutopilotAdapter_set_1trajectory_1x
(JNIEnv *jenv, jobject jobj, jdoubleArray v) { copyJDoubleArrayToMatrix(jenv, v, AUTOPILOT_INSTANCE.trajectory_x); }
/*
* Class: simulator_integration_AutopilotAdapter
* Method: set_addNodes_gpsLon
* Signature: ([D)V
*/
JNIEXPORT void JNICALL Java_simulator_integration_AutopilotAdapter_set_1trajectory_1y
(JNIEnv *jenv, jobject jobj, jdoubleArray v) { copyJDoubleArrayToMatrix(jenv, v, AUTOPILOT_INSTANCE.trajectory_y); }
/*
* Class: simulator_integration_AutopilotAdapter
* Method: get_engine
* Signature: ()D
*/
JNIEXPORT jdouble JNICALL Java_simulator_integration_AutopilotAdapter_get_1engine
(JNIEnv *jenv, jobject jobj) { return AUTOPILOT_INSTANCE.engine; }
/*
* Class: simulator_integration_AutopilotAdapter
* Method: get_steering
* Signature: ()D
*/
JNIEXPORT jdouble JNICALL Java_simulator_integration_AutopilotAdapter_get_1steering
(JNIEnv *jenv, jobject jobj) { return AUTOPILOT_INSTANCE.steering; }
/*
* Class: simulator_integration_AutopilotAdapter
* Method: get_brakes
* Signature: ()D
*/
JNIEXPORT jdouble JNICALL Java_simulator_integration_AutopilotAdapter_get_1brakes
(JNIEnv *jenv, jobject jobj) { return AUTOPILOT_INSTANCE.brakes; }
/*
* Class: simulator_integration_AutopilotAdapter
* Method: exec
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_simulator_integration_AutopilotAdapter_exec
(JNIEnv *jenv, jobject jobj) { AUTOPILOT_INSTANCE.execute(); }
/*
* Class: simulator_integration_AutopilotAdapter
* Method: init
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_simulator_integration_AutopilotAdapter_init
(JNIEnv *jenv, jobject jobj) {
srand(time(NULL));
Helper::init();
AUTOPILOT_INSTANCE.init();
}
#endif
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class simulator_integration_AutopilotAdapter */
#ifndef _Included_simulator_integration_AutopilotAdapter
#define _Included_simulator_integration_AutopilotAdapter
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: simulator_integration_AutopilotAdapter
* Method: set_timeIncrement
* Signature: (D)V
*/
JNIEXPORT void JNICALL Java_simulator_integration_AutopilotAdapter_set_1timeIncrement
(JNIEnv *, jobject, jdouble);
/*
* Class: simulator_integration_AutopilotAdapter
* Method: set_currentVelocity
* Signature: (D)V
*/
JNIEXPORT void JNICALL Java_simulator_integration_AutopilotAdapter_set_1currentVelocity
(JNIEnv *, jobject, jdouble);
/*
* Class: simulator_integration_AutopilotAdapter
* Method: set_x
* Signature: (D)V
*/
JNIEXPORT void JNICALL Java_simulator_integration_AutopilotAdapter_set_1x
(JNIEnv *, jobject, jdouble);
/*
* Class: simulator_integration_AutopilotAdapter
* Method: set_y
* Signature: (D)V
*/
JNIEXPORT void JNICALL Java_simulator_integration_AutopilotAdapter_set_1y
(JNIEnv *, jobject, jdouble);
/*
* Class: simulator_integration_AutopilotAdapter
* Method: set_compass
* Signature: (D)V
*/
JNIEXPORT void JNICALL Java_simulator_integration_AutopilotAdapter_set_1compass
(JNIEnv *, jobject, jdouble);
/*
* Class: simulator_integration_AutopilotAdapter
* Method: set_currentEngine
* Signature: (D)V
*/
JNIEXPORT void JNICALL Java_simulator_integration_AutopilotAdapter_set_1currentEngine
(JNIEnv *, jobject, jdouble);
/*
* Class: simulator_integration_AutopilotAdapter
* Method: set_currentSteering
* Signature: (D)V
*/
JNIEXPORT void JNICALL Java_simulator_integration_AutopilotAdapter_set_1currentSteering
(JNIEnv *, jobject, jdouble);
/*
* Class: simulator_integration_AutopilotAdapter
* Method: set_currentBrakes
* Signature: (D)V
*/
JNIEXPORT void JNICALL Java_simulator_integration_AutopilotAdapter_set_1currentBrakes
(JNIEnv *, jobject, jdouble);
/*
* Class: simulator_integration_AutopilotAdapter
* Method: set_trajectory_length
* Signature: (I)V
*/
JNIEXPORT void JNICALL Java_simulator_integration_AutopilotAdapter_set_1trajectory_1length
(JNIEnv *, jobject, jint);
/*
* Class: simulator_integration_AutopilotAdapter
* Method: set_trajectory_x
* Signature: ([D)V
*/
JNIEXPORT void JNICALL Java_simulator_integration_AutopilotAdapter_set_1trajectory_1x
(JNIEnv *, jobject, jdoubleArray);
/*
* Class: simulator_integration_AutopilotAdapter
* Method: set_trajectory_y
* Signature: ([D)V
*/
JNIEXPORT void JNICALL Java_simulator_integration_AutopilotAdapter_set_1trajectory_1y
(JNIEnv *, jobject, jdoubleArray);
/*
* Class: simulator_integration_AutopilotAdapter
* Method: get_engine
* Signature: ()D
*/
JNIEXPORT jdouble JNICALL Java_simulator_integration_AutopilotAdapter_get_1engine
(JNIEnv *, jobject);
/*
* Class: simulator_integration_AutopilotAdapter
* Method: get_steering
* Signature: ()D
*/
JNIEXPORT jdouble JNICALL Java_simulator_integration_AutopilotAdapter_get_1steering
(JNIEnv *, jobject);
/*
* Class: simulator_integration_AutopilotAdapter
* Method: get_brakes
* Signature: ()D
*/
JNIEXPORT jdouble JNICALL Java_simulator_integration_AutopilotAdapter_get_1brakes
(JNIEnv *, jobject);
/*
* Class: simulator_integration_AutopilotAdapter
* Method: exec
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_simulator_integration_AutopilotAdapter_exec
(JNIEnv *, jobject);
/*
* Class: simulator_integration_AutopilotAdapter
* Method: init
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_simulator_integration_AutopilotAdapter_init
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif
Supports Markdown
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