Commit 3303b8bb authored by Sascha Niklas Schneiders's avatar Sascha Niklas Schneiders
Browse files

fixed #43

parent b3df03ac
......@@ -8,7 +8,7 @@
<groupId>de.monticore.lang.monticar</groupId>
<artifactId>embedded-montiarc-math-generator</artifactId>
<version>0.0.7-SNAPSHOT</version>
<version>0.0.8-SNAPSHOT</version>
<!-- == PROJECT DEPENDENCIES ============================================= -->
......
......@@ -9,6 +9,8 @@ import de.monticore.lang.math.math._symboltable.matrix.MathMatrixArithmeticExpre
public interface MathBackend {
String getMatrixTypeName();
String getCubeTypeName();
String getMatrixInitString(int sizeN, int sizeM);
String getRowVectorTypeName();
......
......@@ -27,6 +27,7 @@ public class Variable {
boolean constantVariable = false;
boolean parameterVariable = false;
int arraySize = 1;
boolean isArray = false;
Optional<String> constantValue = Optional.empty();
List<String> additionalInformation = new ArrayList<>();
List<String> dimensionalInformation = new ArrayList<>();
......@@ -40,6 +41,7 @@ public class Variable {
public Variable(String name, String additionalInformation) {
this.name = name;
this.additionalInformation.add(additionalInformation);
updateArrayStatus();
}
public Variable(Variable variable) {
......@@ -49,6 +51,7 @@ public class Variable {
this.constantVariable = variable.constantVariable;
this.arraySize = variable.arraySize;
this.additionalInformation = variable.additionalInformation;
updateArrayStatus();
}
public List<String> getAdditionalInformation() {
......@@ -104,6 +107,7 @@ public class Variable {
public void setName(String name) {
this.name = name;
updateArrayStatus();
}
public void setTypeNameTargetLanguage(String typeName) {
......@@ -139,6 +143,7 @@ public class Variable {
public void setArraySize(int size) {
this.arraySize = size;
updateArrayStatus();
}
public int getArraySize() {
......@@ -146,7 +151,7 @@ public class Variable {
}
public boolean isArray() {
return arraySize > 1;
return isArray;
}
public boolean isStaticVariable() {
......@@ -230,4 +235,13 @@ public class Variable {
public static String convertToVariableName(String fullName) {
return fullName.replaceAll("\\.", "_");
}
private void updateArrayStatus() {
if ((this.name.contains("[") && this.name.contains("]")) || arraySize > 1) {
isArray = true;
Log.debug("set isArray to true of v: " + getName(), "VARIABLE");
} else {
isArray = false;
}
}
}
......@@ -18,6 +18,10 @@ public class ArmadilloBackend implements MathBackend {
return "mat";
}
@Override
public String getCubeTypeName() {
return "cube";
}
@Override
public String getMatrixInitString(int sizeN, int sizeM) {
......
......@@ -2,6 +2,7 @@ package de.monticore.lang.monticar.generator.cpp;
import de.monticore.lang.monticar.generator.BluePrint;
import de.monticore.lang.monticar.generator.Variable;
import de.se_rwth.commons.logging.Log;
import java.util.ArrayList;
import java.util.List;
......@@ -11,6 +12,7 @@ import java.util.List;
*/
public class BluePrintFixer {
public static void fixBluePrintVariableArrays(BluePrint bluePrint) {
List<Variable> newVars = new ArrayList<>();
for (Variable variable : bluePrint.getVariables()) {
String currentArrayName = variable.getNameWithoutArrayNamePart();
......@@ -25,6 +27,10 @@ public class BluePrintFixer {
if (add)
newVars.add(variable);
}
for (Variable v : newVars) {
Log.info("v: " + v.getName() + " isArray: " + v.isArray() + " size: " + v.getArraySize() + " ", "NEW VAR:");
}
bluePrint.setVariables(newVars);
}
}
......@@ -34,12 +34,12 @@ public class GeneralHelperMethods {
* @return
*/
public static String getTargetLanguageVariableInstanceName(String name) {
String nameChanged = ""; int indexSecond = 0;
String nameChanged = "";
int indexSecond = 0;
while (true) {
int indexFirst = name.indexOf("[", indexSecond);
if (indexFirst != -1)
nameChanged += name.substring(0, indexFirst);
if (indexFirst != -1) {
nameChanged += name.substring(0, indexFirst);
indexSecond = name.indexOf("]", indexFirst + 1);
if (indexSecond != -1) {
String subString = name.substring(indexFirst + 1, indexSecond++);
......@@ -53,7 +53,7 @@ public class GeneralHelperMethods {
} else
break;
}
if (indexSecond != -1 && name.length() > indexSecond)
if (indexSecond != -1 && name.length() > nameChanged.length())
nameChanged += name.substring(indexSecond);
if (nameChanged.equals(""))
return name;
......
......@@ -52,6 +52,7 @@ public class GeneratorCPP implements Generator {
TypeConverter.clearTypeSymbols();
}
public void useArmadilloBackend() {
MathConverter.curBackend = new ArmadilloBackend();
}
......
......@@ -166,7 +166,7 @@ public class LanguageUnitCPP extends LanguageUnit {
for (Instruction instruction : method.getInstructions()) {
if (instruction instanceof ConnectInstructionCPP) {
ConnectInstructionCPP connectInstructionCPP = (ConnectInstructionCPP) instruction;
Log.info("v1:" + connectInstructionCPP.getVariable1().getName() + "v2:" + connectInstructionCPP.getVariable2().getName(), "Instruction:");
Log.info("v1: " + connectInstructionCPP.getVariable1().getName() + "v2: " + connectInstructionCPP.getVariable2().getName(), "Instruction:");
} else if (instruction instanceof ExecuteInstruction) {
ExecuteInstruction executeInstruction = (ExecuteInstruction) instruction;
......
......@@ -14,6 +14,11 @@ public class LinalgBackend implements MathBackend {
return null;
}
@Override
public String getCubeTypeName() {
return null;
}
@Override
public String getMatrixInitString(int sizeN, int sizeM) {
return "(" + sizeN + "," + sizeM + ");\n";
......
......@@ -10,12 +10,19 @@ import de.se_rwth.commons.logging.Log;
* @author Sascha Schneiders
*/
public class OctaveBackend implements MathBackend {
public static final String NAME="OctaveBackend";
public static final String NAME = "OctaveBackend";
@Override
public String getMatrixTypeName() {
return "Matrix";
}
@Override
public String getCubeTypeName() {
Log.info("Cube Type not supported by currentBackend. ", getBackendName());
return null;
}
@Override
public String getMatrixInitString(int sizeN, int sizeM) {
return "(" + sizeN + "," + sizeM + ");\n";
......
......@@ -21,7 +21,7 @@ import java.util.List;
/**
* @author Sascha Schneiders
* Handles code generation for a component and its "subsymbols"
* Handles code generation for a component and its "subsymbols"
*/
public class ComponentConverter {
......@@ -84,11 +84,19 @@ public class ComponentConverter {
Method method = new Method("init", "void");
bluePrint.addMethod(method);
for (Variable v : bluePrint.getMathInformationRegister().getVariables()) {
String oldName = v.getName();
if (v.isArray()) {
if (!v.getName().contains("[")) {
v.setName(v.getName() + "[1]");
}
}
if (v.isStaticVariable()) {
generateInitStaticVariablePart(method, v, bluePrint);
} else {
generateInitNonStaticVariable(method, v, bluePrint);
}
if (v.isArray())
v.setName(oldName);
}
for (Variable v : bluePrint.getVariables()) {
Log.info("Variable: " + v.getName(), "initBluePrintCreate:");
......@@ -107,8 +115,8 @@ public class ComponentConverter {
String parameterString = "";
int i = 0;
for (ASTExpression var : subComponent.getArguments()) {
Log.debug(var.toString(),"ComponentConverter");
if(i > 0)
Log.debug(var.toString(), "ComponentConverter");
if (i > 0)
parameterString += ", ";
i++;
parameterString += getExpressionParameterConversion(var);
......@@ -152,6 +160,7 @@ public class ComponentConverter {
}
public static void generateInitNonStaticVariable(Method method, Variable v, BluePrintCPP bluePrint) {
Log.info("v: " + v.getName(), "generateInitNonStaticVariable");
if (v.getVariableType().getTypeNameTargetLanguage().equals(MathConverter.curBackend.getMatrixTypeName())) {
if (v.isParameterVariable()) {
method.addInstruction(new TargetCodeInstruction("this->" + MathInformationRegister.getVariableInitName(v, bluePrint) + "=" + MathInformationRegister.getVariableInitName(v, bluePrint) + ";\n"));
......@@ -170,12 +179,19 @@ public class ComponentConverter {
method.addParameter(v);
} else
method.addInstruction(new TargetCodeInstruction(MathConverter.getColumnVectorInitLine(v, bluePrint)));
}else if(v.getVariableType().getTypeNameTargetLanguage().equals("double")){
} else if (v.getVariableType().getTypeNameTargetLanguage().equals("double")) {
//TODO: check backend for typeNameTargetLanguage? and handle additional types here
if (v.isParameterVariable()){
method.addInstruction(new TargetCodeInstruction("this->" + v.getNameTargetLanguageFormat() + " = " + v.getNameTargetLanguageFormat() +";\n"));
if (v.isParameterVariable()) {
method.addInstruction(new TargetCodeInstruction("this->" + v.getNameTargetLanguageFormat() + " = " + v.getNameTargetLanguageFormat() + ";\n"));
method.addParameter(v);
}
} else if (v.getVariableType().getTypeNameTargetLanguage().equals(MathConverter.curBackend.getCubeTypeName())) {
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.getCubeTypeInitLine(v, bluePrint)));
}
}
......@@ -200,7 +216,8 @@ public class ComponentConverter {
return parameterString;
}
public static BluePrint convertComponentSymbolToBluePrint(ExpandedComponentInstanceSymbol componentSymbol, List<String> includeStrings, GeneratorCPP generatorCPP) {
public static BluePrint convertComponentSymbolToBluePrint(ExpandedComponentInstanceSymbol
componentSymbol, List<String> includeStrings, GeneratorCPP generatorCPP) {
return convertComponentSymbolToBluePrint(componentSymbol, null, includeStrings, generatorCPP);
}
......
......@@ -34,7 +34,8 @@ public class ComponentConverterMethodGeneration {
Log.info("source:" + connector.getSource() + " target:" + connector.getTarget(), "Port info:");
Variable v1 = PortConverter.getVariableForPortSymbol(connector, connector.getSource(), bluePrint);
Variable v2 = PortConverter.getVariableForPortSymbol(connector, connector.getTarget(), bluePrint);
Log.info("v1: "+v1.getName()+" v2: "+v2.getName(),"Variable Info:");
Log.info("v1: "+v1.getNameTargetLanguageFormat()+" v2: "+v2.getNameTargetLanguageFormat(),"Variable Info:");
Instruction instruction = new ConnectInstructionCPP(v2, v1);
method.addInstruction(instruction);
......
......@@ -123,4 +123,10 @@ public class MathConverter {
return "" + unitNumber.getNumber().get().doubleValue();
}
}
public static String getCubeTypeInitLine(Variable variable, BluePrintCPP bluePrintCPP) {
//TODO implement me
return null;
}
}
......@@ -10,6 +10,7 @@ import de.monticore.lang.monticar.ts.MCTypeSymbol;
import de.monticore.lang.monticar.ts.references.MCASTTypeSymbolReference;
import de.monticore.lang.monticar.ts.references.MCTypeReference;
import de.monticore.lang.monticar.types2._ast.ASTType;
import de.se_rwth.commons.logging.Log;
/**
* @author Sascha Schneiders
......@@ -60,6 +61,7 @@ public class PortConverter {
variable.setVariableType(TypeConverter.getVariableTypeForMontiCarTypeName(typeNameMontiCar, variable, portSymbol).get());
variable.addAdditionalInformation(Variable.ORIGINPORT);
bluePrint.getMathInformationRegister().addVariable(variable);
Log.debug("EMAVAR: " + variable.getName() + " targetType:" + variable.getVariableType().getTypeNameTargetLanguage()+" isArray:"+variable.isArray(), "PortConverter");
return variable;
}
......
......@@ -17,7 +17,8 @@ public class ConnectInstructionCPP extends ConnectInstruction {
@Override
public String getTargetLanguageInstruction() {
String resultString = "";
if (getVariable1().isArray()) {
if (getVariable1().isArray()&&false) {
Log.info("Size: "+getVariable1().getArraySize(),"Array True Method:");
for (int i = 0; i < getVariable1().getArraySize(); ++i) {
if (isUseThis1())
......
......@@ -139,10 +139,17 @@ public class MathInformationRegister {
}
public void addVariable(Variable variable) {
this.variables.add(variable);
if (this.variables.contains(variable)) {
} else {
Log.info("v: " + variable.getName(), "addVariable");
this.variables.add(variable);
}
}
public void addVariable(MathValueSymbol mathValueSymbol) {
Log.info("mathValueSymbol: " + mathValueSymbol.getTextualRepresentation(), "addVariable");
Variable var = new Variable(mathValueSymbol.getName(), Variable.VARIABLE);
var.setTypeNameTargetLanguage(TypeConverter.getVariableTypeNameForMathLanguageTypeName(mathValueSymbol.getType()));
for (MathExpressionSymbol dimension : mathValueSymbol.getType().getDimensions())
......@@ -162,13 +169,13 @@ public class MathInformationRegister {
}
public static String getVariableInitName(Variable v, BluePrintCPP bluePrint) {
for (Variable v2 : bluePrint.getVariables()) {
/*for (Variable v2 : bluePrint.getVariables()) {
if (v2.isArray() && PortConverter.getPortNameWithoutArrayBracketPart(v2.getName()).equals(v.getName())) {
if (!v.getName().endsWith("]")) {
return v.getName() + "[0]";
}
}
}
}*/
return v.getNameTargetLanguageFormat();
}
}
......@@ -66,7 +66,7 @@ public class AbstractSymtab {
for (String m : modelPath) {
mp.addEntry(Paths.get(m));
}
LogConfig.init();//TODO comment for debug output
//LogConfig.init();//TODO comment for debug output
GlobalScope scope = new GlobalScope(mp, fam);
de.monticore.lang.monticar.Utils.addBuiltInTypes(scope);
return scope;
......
......@@ -95,4 +95,16 @@ public class BasicGenerationTest extends AbstractSymtabTest {
generatorCPP.setGenerationTargetPath(firstPartOfPath + additionalPath);
generatorCPP.generateFiles(componentSymbol, symtab);
}
@Test
public void testArrayPortsComp() throws IOException{
TaggingResolver symtab = createSymTabAndTaggingResolver("src/test/resources");
ExpandedComponentInstanceSymbol componentSymbol = symtab.<ExpandedComponentInstanceSymbol>resolve("testing.arrayPortsComp", ExpandedComponentInstanceSymbol.KIND).orElse(null);
assertNotNull(componentSymbol);
GeneratorCPP generatorCPP = new GeneratorCPP();
generatorCPP.useArmadilloBackend();
generatorCPP.setGenerationTargetPath("./target/generated-sources-cpp/armadillo/testing");
generatorCPP.generateFiles(componentSymbol,symtab);
}
}
#ifndef HELPERA_H
#define HELPERA_H
#define _GLIBCXX_USE_CXX11_ABI 0
#include <iostream>
#include "armadillo.h"
#include <stdarg.h>
#include <initializer_list>
using namespace arma;
class HelperA{
public:
static mat getEigenVectors(mat A){
vec eigenValues;
mat eigenVectors;
eig_sym(eigenValues,eigenVectors,A);
return eigenVectors;
}
static vec getEigenValues(mat A){
vec eigenValues;
mat eigenVectors;
eig_sym(eigenValues,eigenVectors,A);
return eigenValues;
}
static mat getKMeansClusters(mat A, int k){
mat clusters;
kmeans(clusters,A.t(),k,random_subset,20,true);
/*printf("cluster centroid calculation done\n");
std::ofstream myfile;
myfile.open("data after cluster.txt");
myfile << A;
myfile.close();
std::ofstream myfile2;
myfile2.open("cluster centroids.txt");
myfile2 << clusters;
myfile2.close();*/
mat indexedData=getKMeansClustersIndexData(A.t(), clusters);
/*std::ofstream myfile3;
myfile3.open("data after index.txt");
myfile3 << indexedData;
myfile3.close();
*/
return indexedData;
}
static mat getKMeansClustersIndexData(mat A, mat centroids){
mat result=mat(A.n_cols, 1);
for(int i=0;i<A.n_cols;++i){
result(i, 0) = getIndexForClusterCentroids(A, i, centroids);
}
return result;
}
static int getIndexForClusterCentroids(mat A, int colIndex, mat centroids){
int index=1;
double lowestDistance=getEuclideanDistance(A, colIndex, centroids, 0);
for(int i=1;i<centroids.n_cols;++i){
double curDistance=getEuclideanDistance(A, colIndex, centroids, i);
if(curDistance<lowestDistance){
lowestDistance=curDistance;
index=i+1;
}
}
return index;
}
static double getEuclideanDistance(mat A, int colIndexA, mat B, int colIndexB){
double distance=0;
for(int i=0;i<A.n_rows;++i){
double elementA=A(i,colIndexA);
double elementB=B(i,colIndexB);
double diff=elementA-elementB;
distance+=diff*diff;
}
return sqrt(distance);
}
static mat getSqrtMat(mat A){
for(int i=0;i<A.n_rows;++i){
double curVal = A(i,i);
A(i,i) = sqrt(curVal);
}
return A;
}
static mat getSqrtMatDiag(mat A){
for(int i=0;i<A.n_rows;++i){
double curVal = A(i,i);
A(i,i) = sqrt(curVal);
}
return A;
}
static mat invertDiagMatrix(mat A){
for(int i=0;i<A.n_rows;++i){
double curVal = A(i,i);
A(i,i) = 1/curVal;
}
return A;
}
};
#endif
#ifndef TESTING_ARRAYPORTSCOMP
#define TESTING_ARRAYPORTSCOMP
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#include "armadillo.h"
using namespace arma;
class testing_arrayPortsComp{
public:
double arrayPort1[1];
double arrayPort2[2];
void init()
{
}
void execute()
{
}
};
#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