Commit 04906a91 authored by Sascha Niklas Schneiders's avatar Sascha Niklas Schneiders
Browse files

added more tests fixed bug when constant matrix is used as parameter

parent cd7011cf
......@@ -29,6 +29,7 @@ public class Variable {
Optional<String> constantValue = Optional.empty();
List<String> additionalInformation = new ArrayList<>();
List<String> dimensionalInformation = new ArrayList<>();
Optional<String> customTypeName = Optional.empty();
public Variable() {
......@@ -40,7 +41,7 @@ public class Variable {
}
public Variable(Variable variable) {
this.type = variable.getVariableType();
this.type.set(variable.getVariableType());
this.name = variable.getName();
this.inputVariable = variable.isInputVariable();
this.constantVariable = variable.constantVariable;
......@@ -73,7 +74,8 @@ public class Variable {
}
public void setVariableType(VariableType variableType) {
this.type = variableType;
this.type.set(variableType);
}
public VariableType getVariableType() {
......@@ -192,8 +194,8 @@ public class Variable {
public void setDimensionalInformation(String dimension1, String dimension2) {
this.dimensionalInformation.clear();
this.dimensionalInformation.add(dimension1);
this.dimensionalInformation.add(dimension2);
addDimensionalInformation(dimension1);
addDimensionalInformation(dimension2);
}
public int howManyDimensions() {
......
package de.monticore.lang.monticar.generator;
import de.monticore.lang.monticar.generator.cpp.converter.ComponentConverter;
import de.monticore.lang.monticar.generator.cpp.converter.MathConverter;
import de.monticore.lang.monticar.generator.optimization.MathInformationRegister;
/**
* @author Sascha Schneiders
*/
......@@ -45,4 +49,10 @@ public class VariableType {
public boolean hasInclude() {
return !includeName.equals("");
}
public void set(VariableType variableType) {
this.typeNameMontiCar = variableType.typeNameMontiCar;
this.typeNameTargetLanguage = variableType.typeNameTargetLanguage;
this.includeName = variableType.includeName;
}
}
......@@ -66,6 +66,7 @@ public class ComponentConverter {
public static void addVariables(ExpandedComponentInstanceSymbol componentSymbol, BluePrintCPP bluePrint) {
//add parameters as variables
for (EMAVariable variable : componentSymbol.getParameters()) {
System.out.println("EMAVAR: " + variable.getName() + " " + variable.getType().toString());
Variable var = new Variable();
var.setName(variable.getName());
var.setTypeNameMontiCar(variable.getType());
......@@ -81,44 +82,14 @@ public class ComponentConverter {
public static void generateInitMethod(ExpandedComponentInstanceSymbol componentSymbol, BluePrintCPP bluePrint, GeneratorCPP generatorCPP, List<String> includeStrings) {
Method method = new Method("init", "void");
bluePrint.addMethod(method);
for (Variable v : bluePrint.getMathInformationRegister().getVariables()) {
Log.info(v.getName(), "Inspecting Variable:");
if (v.isStaticVariable()) {
//TODO add static variable filter function before generate init method
//extract the static variable and their possible assignments
//generate the static variable and their assignment if present
//check if value is constant matrix/vector definition
//fix this too
VariableStatic variableStatic = (VariableStatic) v;
if (!variableStatic.getAssignmentSymbol().isPresent()) {
String instructionString = "";
if (variableStatic.getVariableType().getTypeNameTargetLanguage().equals("Matrix")) {
instructionString = MathInformationRegister.getVariableInitName(v, bluePrint) + "=Matrix(" + v.getDimensionalInformation().get(0) + "," + v.getDimensionalInformation().get(1) + ");\n";
} else if (variableStatic.getVariableType().getTypeNameTargetLanguage().equals("double")) {
instructionString = MathInformationRegister.getVariableInitName(v, bluePrint) + "=0;\n";
} else {
//RowVector and ColumnVector generation not handled here
}
method.addInstruction(new TargetCodeInstruction(instructionString));
} else {
String instructionString = "";
instructionString += variableStatic.getNameTargetLanguageFormat();
instructionString += "=" + MathConverter.getConstantConversion(variableStatic.getAssignmentSymbol().get());
instructionString += ";\n";
method.addInstruction(new TargetCodeInstruction(instructionString));
}
} else if (v.getVariableType().getTypeNameTargetLanguage().equals("Matrix")) {
Log.info(v.getName(), "Matrix found:");
if (v.isParameterVariable()) {
method.addInstruction(new TargetCodeInstruction("this->" + MathInformationRegister.getVariableInitName(v, bluePrint) + "=" + MathInformationRegister.getVariableInitName(v, bluePrint) + ";\n"));
method.addParameter(v);
} else
method.addInstruction(new TargetCodeInstruction(MathInformationRegister.getVariableInitName(v, bluePrint) + "=Matrix(" + v.getDimensionalInformation().get(0) + "," + v.getDimensionalInformation().get(1) + ");\n"));
generateInitStaticVariablePart(method, v, bluePrint);
} else {
generateInitNonStaticVariable(method, v, bluePrint);
}
}
for (Variable v : bluePrint.getVariables()) {
Log.info("Variable: " + v.getName(), "initBluePrintCreate:");
......@@ -130,25 +101,93 @@ public class ComponentConverter {
Instruction instruction = new ConstantConnectInstructionCPP(v, v);
method.addInstruction(instruction);
}
}
for (ExpandedComponentInstanceSymbol subComponent : componentSymbol.getSubComponents()) {
String parameterString = "";
for (ASTExpression var : subComponent.getArguments()) {
if (var.getSymbol().isPresent()) {
parameterString += ((MathExpressionSymbol) var.getSymbol().get()).getTextualRepresentation();
} else {
parameterString += var.toString();
}
System.out.println(var.toString());
parameterString += getExpressionParameterConversion(var);
}
String result = GeneralHelperMethods.getTargetLanguageVariableInstanceName(subComponent.getName()) + ".init(" + parameterString + ");\n";
String result = "";
result += GeneralHelperMethods.getTargetLanguageVariableInstanceName(subComponent.getName()) + ".init(" + parameterString + ");\n";
TargetCodeInstruction instruction = new TargetCodeInstruction(result);
method.addInstruction(instruction);
}
bluePrint.addMethod(method);
}
public static void generateInitStaticVariablePart(Method method, Variable v, BluePrintCPP bluePrint) {
//TODO add static variable filter function before generate init method
//extract the static variable and their possible assignments
//generate the static variable and their assignment if present
//check if value is constant matrix/vector definition
//fix this too
VariableStatic variableStatic = (VariableStatic) v;
if (!variableStatic.getAssignmentSymbol().isPresent()) {
String instructionString = "";
if (variableStatic.getVariableType().getTypeNameTargetLanguage().equals("Matrix")) {
instructionString = MathConverter.getMatrixInitLine(v, bluePrint);
} else if (variableStatic.getVariableType().getTypeNameTargetLanguage().equals("double")) {
instructionString = MathInformationRegister.getVariableInitName(v, bluePrint) + "=0;\n";
} else if (variableStatic.getVariableType().getTypeNameTargetLanguage().equals("RowVector")) {
instructionString = MathConverter.getRowVectorInitLine(v, bluePrint);
} else if (variableStatic.getVariableType().getTypeNameTargetLanguage().equals("ColumnVector")) {
instructionString = MathConverter.getColumnVectorInitLine(v, bluePrint);
}
method.addInstruction(new TargetCodeInstruction(instructionString));
} else {
String instructionString = "";
instructionString += variableStatic.getNameTargetLanguageFormat();
instructionString += "=" + MathConverter.getConstantConversion(variableStatic.getAssignmentSymbol().get());
instructionString += ";\n";
method.addInstruction(new TargetCodeInstruction(instructionString));
}
}
public static void generateInitNonStaticVariable(Method method, Variable v, BluePrintCPP bluePrint) {
if (v.getVariableType().getTypeNameTargetLanguage().equals("Matrix")) {
if (v.isParameterVariable()) {
method.addInstruction(new TargetCodeInstruction("this->" + MathInformationRegister.getVariableInitName(v, bluePrint) + "=" + MathInformationRegister.getVariableInitName(v, bluePrint) + ";\n"));
method.addParameter(v);
} else
method.addInstruction(new TargetCodeInstruction(MathConverter.getMatrixInitLine(v, bluePrint)));
} else if (v.getVariableType().getTypeNameTargetLanguage().equals("RowVector")) {
if (v.isParameterVariable()) {
method.addInstruction(new TargetCodeInstruction("this->" + MathInformationRegister.getVariableInitName(v, bluePrint) + "=" + MathInformationRegister.getVariableInitName(v, bluePrint) + ";\n"));
method.addParameter(v);
} else
method.addInstruction(new TargetCodeInstruction(MathConverter.getRowVectorInitLine(v, bluePrint)));
} else if (v.getVariableType().getTypeNameTargetLanguage().equals("ColumnVector")) {
if (v.isParameterVariable()) {
method.addInstruction(new TargetCodeInstruction("this->" + MathInformationRegister.getVariableInitName(v, bluePrint) + "=" + MathInformationRegister.getVariableInitName(v, bluePrint) + ";\n"));
method.addParameter(v);
} else
method.addInstruction(new TargetCodeInstruction(MathConverter.getColumnVectorInitLine(v, bluePrint)));
}
}
public static String getExpressionParameterConversion(ASTExpression var) {
String parameterString = "";
if (var.getSymbol().isPresent()) {
boolean handled = false;
MathExpressionSymbol symbol = (MathExpressionSymbol) var.getSymbol().get();
if (symbol.isMatrixExpression()) {
MathMatrixExpressionSymbol mathMatrixExpressionSymbol = (MathMatrixExpressionSymbol) symbol;
if (mathMatrixExpressionSymbol.isValueExpression()) {
parameterString = MathConverter.getConstantConversion((MathMatrixArithmeticValueSymbol) mathMatrixExpressionSymbol);
handled = true;
}
}
if (!handled)
parameterString += symbol.getTextualRepresentation();
} else {
parameterString += var.toString();
}
return parameterString;
}
public static BluePrint convertComponentSymbolToBluePrint(ExpandedComponentInstanceSymbol componentSymbol, List<String> includeStrings, GeneratorCPP generatorCPP) {
......
......@@ -9,6 +9,7 @@ import de.monticore.lang.monticar.generator.Variable;
import de.monticore.lang.monticar.generator.cpp.BluePrintCPP;
import de.monticore.lang.monticar.generator.cpp.symbols.MathChainedExpression;
import de.monticore.lang.monticar.generator.cpp.symbols.MathStringExpression;
import de.monticore.lang.monticar.generator.optimization.MathInformationRegister;
import de.se_rwth.commons.logging.Log;
import java.lang.reflect.Type;
......@@ -76,7 +77,7 @@ public class MathConverter {
}
++column;
}
String firstPart = matrixName+" = " + typeName;
String firstPart = matrixName + " = " + typeName;
if (typeName.equals("RowVector")) {
firstPart += "(" + mathExpressionSymbol.getVectors().get(0).getMathMatrixAccessSymbols().size() + ");\n";
} else if (typeName.equals("ColumnVector")) {
......@@ -98,4 +99,17 @@ public class MathConverter {
public static int getNextConstantConstantVectorID() {
return CONSTANTCONSTANTVECTORID++;
}
public static String getMatrixInitLine(Variable v, BluePrintCPP bluePrint) {
return MathInformationRegister.getVariableInitName(v, bluePrint) + "=Matrix(" + v.getDimensionalInformation().get(0) + "," + v.getDimensionalInformation().get(1) + ");\n";
}
public static String getRowVectorInitLine(Variable v, BluePrintCPP bluePrint) {
return MathInformationRegister.getVariableInitName(v, bluePrint) + "= RowVector(" + v.getDimensionalInformation().get(1) + ");\n";
}
public static String getColumnVectorInitLine(Variable v, BluePrintCPP bluePrint) {
return MathInformationRegister.getVariableInitName(v, bluePrint) + "= ColumnVector(" + v.getDimensionalInformation().get(0) + ");\n";
}
}
......@@ -83,6 +83,7 @@ public class TypeConverter {
if (typeNameMontiCar.equals("CommonMatrixType")) {
ASTCommonMatrixType astCommonMatrixType = (ASTCommonMatrixType) astType;
handleCommonMatrixType(variable, astCommonMatrixType);
variableType = getRealVariableType(astCommonMatrixType);
} else if (typeNameMontiCar.equals("AssignmentType")) {//TODO Add MatrixProperties to MathInformation
ASTAssignmentType astAssignmentType = (ASTAssignmentType) astType;
handleAssignmentType(variable, astAssignmentType);
......@@ -95,6 +96,29 @@ public class TypeConverter {
return Optional.empty();
}
public static VariableType getRealVariableType(ASTCommonMatrixType astCommonMatrixType) {
VariableType variableType;
List<ASTCommonDimensionElement> dimensionElements = astCommonMatrixType.getCommonDimension().getCommonDimensionElements();
if (dimensionElements.size() == 2) {
if (isVectorDimension(dimensionElements.get(0)))
variableType = new VariableType("CommonRowVectorType", "RowVector", "octave/oct");
else if (isVectorDimension(dimensionElements.get(1))) {
variableType = new VariableType("CommonColumnVectorType", "ColumnVector", "octave/oct");;
}else{
variableType = new VariableType("CommonMatrixType", "Matrix", "octave/oct");
}
}else{
variableType = new VariableType("CommonMatrixType", "Matrix", "octave/oct");
}
return variableType;
}
public static boolean isVectorDimension(ASTCommonDimensionElement astCommonDimensionElement) {
return astCommonDimensionElement.getUnitNumber().get().getNumber().get().intValue() == 1;
}
public static Optional<VariableType> getVariableTypeForMontiCarTypeName(String typeNameMontiCar, Variable variable, PortSymbol portSymbol) {
for (VariableType variableType : nonPrimitiveVariableTypes) {
if (variableType.getTypeNameMontiCar().equals(typeNameMontiCar)) {
......
......@@ -9,7 +9,10 @@ import de.monticore.ModelingLanguageFamily;
import de.monticore.io.paths.ModelPath;
import de.monticore.java.lang.JavaDSLLanguage;
import de.monticore.lang.embeddedmontiarc.LogConfig;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.ConstantPortSymbol;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarcmath._symboltable.EmbeddedMontiArcMathLanguage;
import de.monticore.lang.monticar.generator.cpp.converter.MathConverter;
import de.monticore.lang.monticar.generator.optimization.ThreadingOptimizer;
import de.monticore.lang.monticar.generator.order.nfp.TagBreakpointsTagSchema.TagBreakpointsTagSchema;
import de.monticore.lang.monticar.generator.order.nfp.TagDelayTagSchema.TagDelayTagSchema;
import de.monticore.lang.monticar.generator.order.nfp.TagExecutionOrderTagSchema.TagExecutionOrderTagSchema;
......@@ -31,35 +34,38 @@ import java.nio.file.Paths;
* @author Robert Heim
*/
public class AbstractSymtab {
protected static Scope createSymTab(String... modelPath) {
ModelingLanguageFamily fam = new ModelingLanguageFamily();
EmbeddedMontiArcMathLanguage montiArcLanguage = new EmbeddedMontiArcMathLanguage();
protected static Scope createSymTab(String... modelPath) {
ConstantPortSymbol.resetLastID();
MathConverter.resetIDs();
ThreadingOptimizer.resetID();
ModelingLanguageFamily fam = new ModelingLanguageFamily();
EmbeddedMontiArcMathLanguage montiArcLanguage = new EmbeddedMontiArcMathLanguage();
TagMinMaxTagSchema.registerTagTypes(montiArcLanguage);
TagTableTagSchema.registerTagTypes(montiArcLanguage);
TagBreakpointsTagSchema.registerTagTypes(montiArcLanguage);
TagExecutionOrderTagSchema.registerTagTypes(montiArcLanguage);
TagInitTagSchema.registerTagTypes(montiArcLanguage);
TagThresholdTagSchema.registerTagTypes(montiArcLanguage);
TagDelayTagSchema.registerTagTypes(montiArcLanguage);
TagMinMaxTagSchema.registerTagTypes(montiArcLanguage);
TagTableTagSchema.registerTagTypes(montiArcLanguage);
TagBreakpointsTagSchema.registerTagTypes(montiArcLanguage);
TagExecutionOrderTagSchema.registerTagTypes(montiArcLanguage);
TagInitTagSchema.registerTagTypes(montiArcLanguage);
TagThresholdTagSchema.registerTagTypes(montiArcLanguage);
TagDelayTagSchema.registerTagTypes(montiArcLanguage);
fam.addModelingLanguage(montiArcLanguage);
fam.addModelingLanguage(new StreamUnitsLanguage());
// TODO should we use JavaDSLLanguage or add the resolvers in MALang?
fam.addModelingLanguage(new JavaDSLLanguage());
// TODO how to add java default types?
Path argument;
if(AbstractSymtab.class.getClassLoader().getResource("").getPath().contains(":")) {
argument = Paths.get(AbstractSymtab.class.getClassLoader().getResource("").getPath().substring(1));
} else {
argument = Paths.get(AbstractSymtab.class.getClassLoader().getResource("").getPath());
fam.addModelingLanguage(montiArcLanguage);
fam.addModelingLanguage(new StreamUnitsLanguage());
// TODO should we use JavaDSLLanguage or add the resolvers in MALang?
fam.addModelingLanguage(new JavaDSLLanguage());
// TODO how to add java default types?
Path argument;
if (AbstractSymtab.class.getClassLoader().getResource("").getPath().contains(":")) {
argument = Paths.get(AbstractSymtab.class.getClassLoader().getResource("").getPath().substring(1));
} else {
argument = Paths.get(AbstractSymtab.class.getClassLoader().getResource("").getPath());
}
final ModelPath mp = new ModelPath(Paths.get(argument + "/../../src/main/resources/defaultTypes"));
for (String m : modelPath) {
mp.addEntry(Paths.get(m));
}
LogConfig.init();//TODO comment for debug output
GlobalScope scope = new GlobalScope(mp, fam);
return scope;
}
final ModelPath mp = new ModelPath(Paths.get(argument + "/../../src/main/resources/defaultTypes"));
for (String m : modelPath) {
mp.addEntry(Paths.get(m));
}
LogConfig.init();//TODO comment for debug output
GlobalScope scope = new GlobalScope(mp, fam);
return scope;
}
}
package de.monticore.lang.monticar.generator.cpp;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.ConstantPortSymbol;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.ExpandedComponentInstanceSymbol;
import de.monticore.lang.monticar.generator.AbstractSymtabTest;
import de.monticore.symboltable.Scope;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
import java.util.List;
import static org.junit.Assert.assertNotNull;
/**
* @author Sascha Schneiders
*/
public class BasicGenerationTest extends AbstractSymtabTest {
@Test
public void testBasicLookUpInstanceGeneration() throws IOException {
Scope symtab = createSymTab("src/test/resources");
ExpandedComponentInstanceSymbol componentSymbol = symtab.<ExpandedComponentInstanceSymbol>resolve("testing.basicLookUpInstance", ExpandedComponentInstanceSymbol.KIND).orElse(null);
assertNotNull(componentSymbol);
GeneratorCPP generatorCPP = new GeneratorCPP();
generatorCPP.setGenerationTargetPath("./target/generated-sources-cpp/testing/l0");
List<File> files = generatorCPP.generateFiles(componentSymbol, symtab);
String restPath = "testing/l0/";
testFilesAreEqual(files, restPath);
}
}
......@@ -20,8 +20,6 @@ import static org.junit.Assert.assertNotNull;
public class ThreadingOptimizerTest extends AbstractSymtabTest {
@Test
public void testMathUnitThreading() throws IOException {
MathConverter.resetIDs();
ThreadingOptimizer.resetID();
Scope symtab = createSymTab("src/test/resources");
ExpandedComponentInstanceSymbol componentSymbol = symtab.<ExpandedComponentInstanceSymbol>resolve("paper.mathUnit", ExpandedComponentInstanceSymbol.KIND).orElse(null);
......
package fas.basicLibrary;
component LookUp<T> (Q^{1,n} lookuptable) {
component LookUp<T, N1 n = 1> (Q^{1,n} lookuptable) {
ports
in T in1,
out T out1;
......
......@@ -17,7 +17,7 @@ component VelocityControl {
out (-oo:d:oo) BrakeForce_pc;
instance Saturation<(-oo:d:oo)> sat1, sat2;
instance LookUp<(-oo:d:oo)> look1,look2;
instance LookUp<(-oo:d:oo),1> look1,look2;//TODO add constant value
instance Max<(-oo:d:oo),2> max1;
instance Max<(-oo:d:oo),5> max2;
instance SwitchB<(-oo:d:oo)> switch1;
......
......@@ -15,7 +15,7 @@ component Distronic_Enabled {
instance SwitchB<(-oo:d:oo)> switch2;
instance Multiplication<(-oo:d:oo)> mult1, mult2, mult3;
instance PlusMinusPlus<(-oo:d:oo)> pmp1;
instance LookUp<(-oo:d:oo)> look1, look2, look3, look4, look5;
instance LookUp<(-oo:d:oo),1> look1, look2, look3, look4, look5;//TODO add constant value
instance Smaller<(-oo:d:oo)> smaller1;
connect Distance_stat -> switch1.in1;
......
......@@ -11,7 +11,7 @@ component Distronic_FTS_Enabled {
out (-oo:d:oo) Decelerator_pc;
instance PlusMinus<(-oo:d:oo)> plusMinus1;
instance LookUp<(-oo:d:oo)> look1, look2;
instance LookUp<(-oo:d:oo),1> look1, look2;//TODO add constant value
instance Smaller<(-oo:d:oo)> smaller1;
instance Multiplication<(-oo:d:oo)> mult1;
instance SwitchB<(-oo:d:oo)> switch1;
......
#ifndef HELPER_H
#define HELPER_H
#define _GLIBCXX_USE_CXX11_ABI 0
#include <iostream>
#include <octave/oct.h>
#include <octave/octave.h>
#include <octave/parse.h>
#include <octave/interpreter.h>
#include <stdarg.h>
#include <initializer_list>
class Helper
{
public:
static void init()
{
string_vector argv(2);
argv(0) = "embedded";
argv(1) = "-q";
octave_main(2, argv.c_str_vec(), 1);
//octave_debug=1;
//feval ("pkg", ovl ("load", "all"), 0);
}
static octave_value_list convertToOctaveValueList(double a)
{
octave_value_list in;
in(0) = a;
return in;
}
static octave_value_list convertToOctaveValueList(Matrix a)
{
octave_value_list in;
in(0) = a;
return in;
}
static octave_value_list convertToOctaveValueList(RowVector a)
{
octave_value_list in;
in(0) = a;
return in;
}
static octave_value_list convertToOctaveValueList(ColumnVector a)
{
octave_value_list in;
in(0) = a;
return in;
}
static octave_value_list convertToOctaveValueList(double a, double b)
{
octave_value_list in;
in(0) = a;
in(1) = b;
return in;
}
static octave_value_list convertToOctaveValueList(std::initializer_list<double> args)
{
octave_value_list in;
int counter = 0;
for(double element : args) {
in(counter) = octave_value(element);
++counter;
}
return in;
}
static octave_value_list convertToOctaveValueList(Matrix a, double b)
{
octave_value_list in;
in(0) = a;
in(1) = b;
return in;
}
static octave_value_list convertToOctaveValueList(RowVector a, double b)
{
octave_value_list in;
in(0) = a;
in(1) = b;
return in;
}
static octave_value_list convertToOctaveValueList(ColumnVector a, double b)
{
octave_value_list in;
in(0) = a;
in(1) = b;
return in;
}
static octave_value_list callOctaveFunction(octave_value_list in, std::string functionName,int argsOut)